php - Paypal 订阅 IPN 返回无效

标签 php paypal paypal-ipn subscription

如标题所述,我在使用 Paypal 和 IPN 返回验证订阅付款方式时遇到问题。对于我到目前为止所做的每个订阅 IPN,它都返回了 INVALID 和数据。问题是,对于相同的代码,例如将按钮从 _xclick-subscriptions 更改为 _donations 会给我一个已验证的结果。

我一直在寻找解决我问题的方法,但一直找不到。这是正在发生的事情——我创建了一个订阅按钮,该按钮将用户引导至 paypal(在本例中为沙盒),然后他们在那里付款,付款后他们可以选择返回网站。这是按钮的代码。

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but20.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
<input type="hidden" name="cmd" value="_xclick-subscriptions">
<input type="hidden" name="business" value="mail@url.com"> 
<input type="hidden" name="item_name" value="Monthly Sub">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="a3" value="10.00"> 
<input type="hidden" name="p3" value="1"> 
<input type="hidden" name="t3" value="M"> 
<input type="hidden" name="src" value="1"> 
<input type="hidden" name="sra" value="1">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="return" value="http://url.com/thanks.php">
</form>

因为这对我来说是一个不错的问题,所以我将按钮分解为具有几乎所有准系统所需的值,直到我能够真正让它返回已验证而不是无效。 以下是我作为 IPN 监听器的代码。

define("DEBUG", 1);
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);
}
// Set TCP timeout to 30 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
$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);
            // Split response headers and payload
            list($headers, $res) = explode("\r\n\r\n", $res, 2);
        }
        curl_close($ch);
}
// Inspect IPN validation result and act accordingly
if (strcmp ($res, "VERIFIED") == 0) {
    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) {
    if(DEBUG == true) {
        error_log(date('[Y-m-d H:i e] '). "Invalid IPN: $req" . PHP_EOL, 3, LOG_FILE);
    }
}

正如我之前所说,_donations 的 cmd 值返回 VERIFIED,但 _xclick-subscriptions 返回 INVALID 和数据。我有一个返回数据的例子。

cmd=_notify-validate&txn_type=subscr_signup&subscr_id=I-SFYTVAKSSYGK&last_name=lastname&residence_country=US&mc_currency=USD&item_name=Monthly+Sub&business=mail%40url.com&amount3=10.00&recurring=1&address_street=1+Main+St&payer_status=verified&payer_email=name%40email.com&address_status=confirmed&first_name=firstname&receiver_email=mail%40url.com&address_country_code=US&payer_id=SYPW3V3BVWTJW&address_city=San+Jose&reattempt=1&address_state=CA&subscr_date=07%3A33%3A13+Aug+27%2C+2014+PDT&address_zip=95131&charset=windows-1252&period3=1+M&address_country=United+States&mc_amount3=10.00&address_name=firstname+lastname&auth=AwIHoldf-BK2GZqtPhPo0O2g3go74cV9ZOLRYhHTJdKDM5EP0YHuqHLo23RYPfQs-3YDnvhjVf.J3AtydGfvDfA&form_charset=UTF-8

老实说,我不知道从现在开始到哪里去,所以非常感谢任何和所有的帮助!

最佳答案

经过又一整天的测试,我最终找出了导致我的特定问题的原因。事实证明,Paypal 喜欢使用多个 IP 将沙箱中的信息发布到您的服务器。我正在使用 htaccess 来阻止除我的 ip 地址之外的所有用户查看该网站(有一个服务器,但我真的快要上线了,这是阻止我打开网站的最后一件事)。在捕获我认为是所有 Paypal 沙盒 IP 的东西后,我在 htaccess 中打开了它们。事实证明,与一次性付款方式相比,Paypal 使用不同的 IP 从订阅方式发布数据。一旦我发现了这一点并将 IP 添加到 htaccess,我的问题就消失了。

关于php - Paypal 订阅 IPN 返回无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533058/

相关文章:

php - 使用 php 和 mySqli 建表

php - 圆形函数 - PHP

php - 在基于 PHP 网络的游戏中集成 JavaScript 聊天

php - 为什么在响应成功时不通过 Paypal MassApi 转账

url - php 将用户名发送到 paypal 捐赠页面

Paypal IPN 回调在沙盒环境中停止工作

asp.net - 如何配置 IIS 以便我在同一台机器上运行的 ASP.NET Web 应用程序可以相互 POST,但公众不能 POST?

php - Paypal IPN 连续访问我的 IPN 页面并进行相同的交易

php - 更改表排序规则 LIKE 子句不会返回任何内容

Perl,LWP "certificate verify failed"与 paypal.com