PayPal IPN - 测试 - 在沙盒中不起作用

Posted

技术标签:

【中文标题】PayPal IPN - 测试 - 在沙盒中不起作用【英文标题】:PayPal IPN - Testing - Does Not Work in Sandbox 【发布时间】:2015-11-25 18:59:47 【问题描述】:

我正在尝试编写一个简单的 PayPal 购买页面,其中包含我网站上的选项。但是,我在测试它时遇到了麻烦。出于某种奇怪的原因,当我使用“http://paypal.com/cgi-bin/webscr”作为表单 URL 时,它似乎可以工作,但是我无法在没有人购买的情况下测试 IPN。如果我将其保留为“http://sandbox.paypal.com/cgi-bin/webscr”,它将在 PayPal 沙盒网站上弹出,

由于卖家网站出现问题,PayPal 无法处理此交易。请直接联系卖家解决此问题。

下面是我的表格:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" value="YDPZBQ2LRBCSE">
    <table>
    <tr><td><input type="hidden" name="on0" value="Packages">Packages</td></tr><tr><td><select name="os0">
    <option value="Bronze">Bronze $10.00 USD</option>
    <option value="Silver">Silver $25.00 USD</option>
    <option value="Gold">Gold $50.00 USD</option>
    </select> </td></tr>
    <tr><td><input type="hidden" name="on1" value="Wubbo Username">Wubbo Username</td></tr><tr><td><input type="text" name="os1" maxlength="200"></td></tr>
    </table>
    <input type="hidden" name="currency_code" value="USD">
    <input type="image" src="http://wubbo.me/paypal/buynow.png" border="0" name="submit" >
    <img  border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif"  >
</form>

这是我的 IPN 代码:

<?php
mysql_connect("localhost", "root", "PASSWORD") or die(mysql_error());
mysql_select_db("DATABASE") or die(mysql_error());

// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream. 
$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 IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$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";


// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
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);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set 
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) ) 
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;

curl_close($ch);

// inspect IPN validation result and act accordingly
if (strcmp ($res, "VERIFIED") == 0) 
    // The IPN is verified, process it:
    // check whether the payment_status is Completed
    // check that txn_id has not been previously processed
    // check that receiver_email is your Primary PayPal email
    // check that payment_amount/payment_currency are correct
    // process the notification
    // assign posted variables to local variables
    $package = $_POST['os0'];
    $username = $_POST['os1'];

    if ($package == 'Bronze') 
        mysql_query("UPDATE `users` SET `vip_points` = (`vip_points` + 10) WHERE username = '" . $username . "'");
     else if ($package == 'Silver') 
        mysql_query("UPDATE `users` SET `vip_points` = (`vip_points` + 30) WHERE username = '" . $username . "'");
     else if ($package == 'Gold') 
        mysql_query("UPDATE `users` SET `vip_points` = (`vip_points` + 100) WHERE username = '" . $username . "'");
    



 else if (strcmp ($res, "INVALID") == 0) 
// IPN invalid, log for manual investigation
echo "The response from IPN was: <b>" .$res ."</b>";

?>

任何帮助将不胜感激!

【问题讨论】:

【参考方案1】:

当您使用托管按钮时,您不能将其从 paypal.com 更改为 sandbox.paypal.com。您必须在您的沙盒卖家账户中创建一个单独的托管按钮。

然后您将使用沙盒买家帐户通过该按钮进行购买,如果在卖家帐户中设置了 IPN,它将相应地触发。

【讨论】:

如果我尝试在沙盒上创建按钮,它会将我重定向到常规的贝宝网站。 另外,我之前测试过这样的IPN,所以我认为你说得不对。

以上是关于PayPal IPN - 测试 - 在沙盒中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Paypal IPN 在沙盒中有效 直播时无效

PayPal IPN _notify-validate 在沙盒中引发错误 302

测试 IPN 不应该从我的 Paypal 沙盒帐户中取钱吗?

应用内购买在 Apple Store 中不起作用,但在沙盒中起作用

在 PayPal 沙盒中使用 IPN 测试拒付/争议

自适应链式支付:在沙盒中测试的必要凭证是啥?