首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试整合贝宝ipn到我的网站。我一直收到以下错误。我在这里束手无策。我遗漏了什么?

我正在尝试整合贝宝ipn到我的网站。我一直收到以下错误。我在这里束手无策。我遗漏了什么?
EN

Stack Overflow用户
提问于 2015-12-03 08:48:16
回答 1查看 124关注 0票数 0

我正在尝试整合贝宝ipn到我的网站。我一直收到以下错误。我在这里束手无策。我错过了什么?我猜我只是不理解为什么ipn.php页面看不到贝宝发送给POST变量的内容,或者这是问题所在吗?我已经做了一整天了,而且我对这个过程还很陌生,所以任何帮助都会很好!

代码语言:javascript
复制
INVALID
[2015-12-02 17:39 America/Denver] Invalid IPN: cmd=_notify-validate

下面是我的代码ipn.php

代码语言:javascript
复制
<?php
// CONFIG: Enable debug mode. This means we'll log requests into 'ipn.log' in the same directory.
// Especially useful if you encounter network errors or other intermittent problems with IPN (validation).
// Set this to 0 once you go live or don't require logging.
define("DEBUG", 1);
// Set to 0 once you're ready to go live
define("USE_SANDBOX", 1);
define("LOG_FILE", "./ipn.log");
// Read POST data
// reading posted data directly from $_POST causes serialization
// issues with array data in POST. Reading raw POST data from input stream instead.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) {
    $keyval = explode ('=', $keyval);
    if (count($keyval) == 2)
        $myPost[$keyval[0]] = urldecode($keyval[1]);
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) {
    $get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value) {
    if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1) {
        $value = urlencode(stripslashes($value));
    } else {
        $value = urlencode($value);
    }
    $req .= "&$key=$value";
}
// Post IPN data back to PayPal to validate the IPN data is genuine
// Without this step anyone can fake IPN data
if(USE_SANDBOX == true) {
    $paypal_url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
} else {
    $paypal_url = "https://www.paypal.com/cgi-bin/webscr";
}
$ch = curl_init($paypal_url);
if ($ch == FALSE) {
    return FALSE;
}
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
if(DEBUG == true) {
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
}
// CONFIG: Optional proxy configuration
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
//curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
// Set TCP timeout to 30 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// CONFIG: Please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set the directory path
// of the certificate as shown below. Ensure the file is readable by the webserver.
// This is mandatory for some environments.
//$cert = __DIR__ . "./cacert.pem";
//curl_setopt($ch, CURLOPT_CAINFO, $cert);
$res = curl_exec($ch);
if (curl_errno($ch) != 0) // cURL error
    {
    if(DEBUG == true) { 
        error_log(date('[Y-m-d H:i e] '). "Can't connect to PayPal to validate IPN message: " . curl_error($ch) . PHP_EOL, 3, LOG_FILE);
    }
    curl_close($ch);
    exit;
} else {
        // Log the entire HTTP response if debug is switched on.
        if(DEBUG == true) {
            error_log(date('[Y-m-d H:i e] '). "HTTP request of validation request:". curl_getinfo($ch, CURLINFO_HEADER_OUT) ." for IPN payload: $req" . PHP_EOL, 3, LOG_FILE);
            error_log(date('[Y-m-d H:i e] '). "HTTP response of validation request: $res" . PHP_EOL, 3, LOG_FILE);
        }
        curl_close($ch);
}
// Inspect IPN validation result and act accordingly
// Split response headers and payload, a better way for strcmp
$tokens = explode("\r\n\r\n", trim($res));
$res = trim(end($tokens));
if (strcmp ($res, "VERIFIED") == 0) {
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your PayPal email
    // check that payment_amount/payment_currency are correct
    // process payment and mark item as paid.
    // assign posted variables to local variables
    $item_name = $_POST['item_name'];
    $item_number = $_POST['item_number'];
    $payment_status = $_POST['payment_status'];
    $payment_amount = $_POST['mc_gross'];
    $payment_currency = $_POST['mc_currency'];
    $txn_id = $_POST['txn_id'];
    $receiver_email = $_POST['receiver_email'];
    $payer_email = $_POST['payer_email'];

    if(DEBUG == true) {
        error_log(date('[Y-m-d H:i e] '). "Verified IPN: $req ". PHP_EOL, 3, LOG_FILE);
    }
} else if (strcmp ($res, "INVALID") == 0) {
    // log for manual investigation
    // Add business logic here which deals with invalid IPN messages
    if(DEBUG == true) {
        error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
    }
}
?>

orderdetails.php

代码语言:javascript
复制
<?php
require 'ipn.php';

?>

<form target="_new" method="post" action="https://www.kathyhaggerty.info/Final/ipn.php">
<input type="hidden" name="SomePayPalVar" value="SomeValue1"/>
<input type="hidden" name="SomeOtherPPVar" value="SomeValue2"/>

<!-- code for other variables to be tested ... -->

<input type="submit"/>
</form>
<br />
Thank you for your payment. Your transaction has been completed, and a receipt for your purchase has been emailed to you. You may log into your account at <a href="http://www.sandbox.paypal.com/ie">www.sandbox.paypal.com/ie</a> to view details of this transaction.

index.php

代码语言:javascript
复制
<!-- INFO: The post URL "checkout.php" is invoked when clicked on "Pay with PayPal" button.-->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
    <form action='checkout.php' METHOD='POST'>
    <input type='image' name='paypal_submit' id='paypal_submit'  src='https://www.paypal.com/en_US/i/btn/btn_dg_pay_w_paypal.gif' border='0' align='top' alt='Pay with PayPal'/>
</form>
</body>

<!-- Add Digital goods in-context experience. Ensure that this script is added before the closing of html body tag -->

<script src='https://www.paypalobjects.com/js/external/dg.js' type='text/javascript'></script>


<script>

    var dg = new PAYPAL.apps.DGFlow(
    {
        trigger: 'paypal_submit',
        expType: 'instant'
         //PayPal will decide the experience type for the buyer based on his/her 'Remember me on your computer' option.
    });

</script>

</html>

我不知所措!

EN

回答 1

Stack Overflow用户

发布于 2015-12-03 10:27:09

这是一种不正确的IPN实现方式。

首先,由于您使用的是ExpressCheckout,您做过DoExpressCheckoutPayment接口调用吗?这是因为我在你的问题中看不到。

我假设orderdetails.php是您设置的返回URL,因为下面有一条谢谢您的消息。但是,我没有看到任何DoExpressCheckoutPayments被调用。

如果没有DoEC接口调用,交易将不会完成,您的IPN将永远无效。

如果您正在通过集成向导,您应该能够到达步骤4:支付,它给您的整个代码作为orderconfirm.php,这是完整的代码,您应该如何实现DoEC应用编程接口调用。

这是必须完成并确保上述的,然后是你需要做的事情,让IPN工作:

关于IPN,可以在DoEC接口调用中设置URL。

在orderconfirm.php中,找到代码块并插入$ipnUrl,如下所示:

代码语言:javascript
复制
    //Format the  parameters that were stored or received from GetExperessCheckout call.
    $token              = $_REQUEST['token'];
    $payerID            = $_REQUEST['PayerID'];
    $paymentType        = 'Sale';
    $currencyCodeType   = $res['CURRENCYCODE'];
    $ipnUrl             = 'https://www.kathyhaggerty.info/Final/ipn.php'; 
    $items = array();
    $i = 0;
    // adding item details those set in setExpressCheckout
    while(isset($res["L_PAYMENTREQUEST_0_NAME$i"]))
    {
        $items[] = array('name' => $res["L_PAYMENTREQUEST_0_NAME$i"], 'amt' => $res["L_PAYMENTREQUEST_0_AMT$i"], 'qty' => $res["L_PAYMENTREQUEST_0_QTY$i"]);
        $i++;
    }

    $resArray = ConfirmPayment ( $token, $paymentType, $currencyCodeType, $payerID, $finalPaymentAmount, $items, $ipnUrl );
    $ack = strtoupper($resArray["ACK"]);

之后,转到您的paypalfunctions.php,找到函数ConfirmPayment,然后编辑下面的代码(或者您可以用我的函数替换整个ConfirmPayment函数):

代码语言:javascript
复制
function ConfirmPayment( $token, $paymentType, $currencyCodeType, $payerID, $FinalPaymentAmt, $items, $ipnUrl )
    {
        /* Gather the information to make the final call to
           finalize the PayPal payment.  The variable nvpstr
           holds the name value pairs
           */
        $token              = urlencode($token);
        $paymentType        = urlencode($paymentType);
        $currencyCodeType   = urlencode($currencyCodeType);
        $payerID            = urlencode($payerID);
        $serverName         = urlencode($_SERVER['SERVER_NAME']);
        $ipnUrl         = urlencode($ipnUrl);

    $nvpstr  = '&TOKEN=' . $token . '&PAYERID=' . $payerID . '&PAYMENTREQUEST_0_PAYMENTACTION=' . $paymentType . '&PAYMENTREQUEST_0_AMT=' . $FinalPaymentAmt . '&PAYMENTREQUEST_0_NOTIFYURL=' . $ipnUrl;
        $nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE=' . $currencyCodeType . '&IPADDRESS=' . $serverName; 

        foreach($items as $index => $item) {

            $nvpstr .= "&L_PAYMENTREQUEST_0_NAME" . $index . "=" . urlencode($item["name"]);
            $nvpstr .= "&L_PAYMENTREQUEST_0_AMT" . $index . "=" . urlencode($item["amt"]);
            $nvpstr .= "&L_PAYMENTREQUEST_0_QTY" . $index . "=" . urlencode($item["qty"]);
            $nvpstr .= "&L_PAYMENTREQUEST_0_ITEMCATEGORY" . $index . "=Digital";
        }
         /* Make the call to PayPal to finalize payment
            If an error occured, show the resulting errors
            */
        $resArray=hash_call("DoExpressCheckoutPayment",$nvpstr);

        /* Display the API response back to the browser.
           If the response from PayPal was a success, display the response parameters'
           If the response was an error, display the errors received using APIError.php.
           */
        $ack = strtoupper($resArray["ACK"]);

        return $resArray;
    }

完成后,将每个文件放在一起,如果一切正常,IPN应该会在完成支付时自动触发(在orderconfirm.php中调用DoExpressCheckoutPayment接口后)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34055912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档