简体   繁体   English

贝宝IPN PHP脚本

[英]paypal IPN php script

I just realized that my paypal ipn handler in php doesn't work anymore (and I change nothing), I use the sample php script provided by paypal. 我只是意识到我的php中的paypal ipn处理程序不再起作用(并且我什么也没改变),我使用了paypal提供的示例php脚本。

I tried to isolate the problem by making several test, and at this time I can say that the problem is not the "VERIFIED" or the "INVALID" thing but comes from these lines: 我试图通过进行几次测试来隔离问题,这时我可以说问题不是“ VERIFIED”或“ INVALID”,而是来自以下几行:

[...] [...]

fputs ($fp, $header . $req);
    while (!feof($fp))
    {
        $res = fgets ($fp, 1024);
        if (strcmp ($res, "VERIFIED") == 0)
        {
        // check the payment_status is Completed

[...] [...]

Anyone know if paypal change something or why it doesn't work? 任何人都知道贝宝是否更改了某些内容或为什么它不起作用?

thanks' 谢谢'

ps: if I put my code before all the paypal tests (before the line "if (!$fp)") it just works fine ps:如果我将代码放在所有的贝宝测试之前(在“ if(!$ fp)”行之前),就可以正常工作

This is my code, I comment where the "database process" works and where it doesn't. 这是我的代码,我评论“数据库进程”在哪里起作用,在哪里不起作用。

<?php

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value)
{
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

// assign posted variables to local variables
$payment_status = $_POST['payment_status'];


// DATABASE PROCESS WORKS (BEFORE THE PAYPAL TESTS)



if (!$fp)
{
    // HTTP ERROR
}
else
   {

// DATABASE PROCESS WORKS

    fputs ($fp, $header . $req);
    while (!feof($fp))
    {
        $res = fgets ($fp, 1024);
        if (strcmp ($res, "VERIFIED") == 0)
        {
            // check 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 payment


// DATABASE PROCESS NOT WORKING

        }
        else if (strcmp ($res, "INVALID") == 0)
        {
            // log for manual investigation

// DATABASE PROCESS NOT WORKING
        }
    }
fclose ($fp);
}

?>

try using the updated script on x.xom https://www.x.com/instant-payment-notification-4 尝试在x.xom https://www.x.com/instant-payment-notification-4上使用更新的脚本

it uses CURL instead of fsock 它使用CURL而不是fsock

This error occurred when server not configured properly for socket or openSSL module not configured. 如果未针对套接字正确配置服务器或未配置openSSL模块,则会发生此错误。

OR 要么

You can try with below code sample of using file_get_contents instead of $fp code 您可以尝试以下使用file_get_contents而不是$ fp代码的代码示例

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// for live mode
$url="https://www.paypal.com/cgi-bin/webscr";
// for developement mode
$url="https://www.sandbox.paypal.com/cgi-bin/webscr";

// instead of use of fopen you can use
$res=file_get_contents($url"?".$req);
// compare response
if (strcmp (trim($res), "VERIFIED") == 0) {
// check 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 payment
}
else if (strcmp (trim($res), "INVALID") == 0) {
// log for manual investigation
}

?>

if file_get_contents not working then you can use CURL for getting response... 如果file_get_contents无法正常工作,则可以使用CURL获取响应...

Let me know if have any question? 让我知道是否有任何疑问?

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM