Paypal IPN,在账户中更改ipn url后,未能获取所有交易响应

17

我正在我的项目中实现ipnlistner。我已经在我的paypal账户中设置了ipn url,但我没有收到所有交易的ipn响应。但是,当我检查我的账户中的ipn历史记录时,它显示所有的ipn都已发送。例如,昨天显示总共发送了112个ipn,但是我只在我的数据库中得到了7个。这是我的ipn listner代码。我仅将获取的所有数据插入到第一行。

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;

class PaypalIPN extends Controller {

private $use_sandbox = null;

const VALID = 'VERIFIED';

const INVALID = 'INVALID';

public function useSandbox() {
    $this->use_sandbox = env( 'USE_SANDBOX' );
}

public function getPaypalUri() {
    if ( $this->use_sandbox ) {
        return env( 'SANDBOX_VERIFY_URI' );
    } else {
        return env( 'VERIFY_URI' );
    }
}

public function verifyIPN() {
    try {
        DB::table( 'ipn_response' )->insert( [ 'data' => json_encode( $_POST, true ) ] );
        if ( ! count( $_POST ) ) {
            throw new \Exception( "Missing POST Data" );
        }
        $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 ) {
                // Since we do not want the plus in the datetime string to be encoded to a space, we manually encode it.
                if ( $keyval[0] === 'payment_date' ) {
                    if ( substr_count( $keyval[1], '+' ) === 1 ) {
                        $keyval[1] = str_replace( '+', '%2B', $keyval[1] );
                    }
                }
                $myPost[ $keyval[0] ] = urldecode( $keyval[1] );
            }
        }
        // Build the body of the verification post request, adding the _notify-validate command.
        $req                     = 'cmd=_notify-validate';
        $get_magic_quotes_exists = false;
        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";
        }

        // Use the sandbox endpoint during testing.
        $this->useSandbox();

        // Post the data back to PayPal, using curl. Throw exceptions if errors occur.
        $ch = curl_init( $this->getPaypalUri() );
        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_SSLVERSION, 6 );
        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_CONNECTTIMEOUT, 30 );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Connection: Close' ) );
        $res = curl_exec( $ch );
        if ( ! ( $res ) ) {
            $errno  = curl_errno( $ch );
            $errstr = curl_error( $ch );
            curl_close( $ch );
            throw new \Exception( "cURL error: [$errno] $errstr" );
        }
        $info      = curl_getinfo( $ch );
        $http_code = $info['http_code'];
        if ( $http_code != 200 ) {
            throw new \Exception( "PayPal responded with http code $http_code" );
        }
        curl_close( $ch );

        // Check if PayPal verifies the IPN data, and if so, return true.
        if ( $res == self::VALID ) {
            DB::table( 'ipn_response' )->insert( [ 'data' => json_encode( $res, true ) ] );
        } else {
            DB::table( 'ipn_response' )->insert( [ 'data' => json_encode( $res, true ) ] );
        }

        // Reply with an empty 200 response to indicate to paypal the IPN was received correctly.
        header( "HTTP/1.1 200 OK" );
    }catch (\Exception $e){
        DB::table( 'ipn_response' )->insert( [ 'data' => json_encode( ["Exception"=>$e->getMessage()]) ] );
    }
}
}

我正在此网址验证IPN

https://ipnpb.paypal.com/cgi-bin/webscr

我的 IPN 网址是

https://www.myproject.com/api/verify-ipn

注:之前我在这个帐户上创建了一些 PayPal 按钮,但没有收到按钮付款的 IPN 响应。

请帮忙或指导该怎么做...


你在什么时候向数据库写入数据?-- 展示那段代码。 - Martin
1
你的PHP错误日志显示了什么? - Martin
数据库写入是通过DB类完成的。不确定它是哪个库,但很可能是问题的根源。 - smcjones
@smcjones,由于我们看不到它,我们无法知道它是否是问题所在。 - Martin
@smcjones 在 Laravel 中,我们有 DB 门面来编写查询。所以这不是问题。 - RAUSHAN KUMAR
显示剩余3条评论
3个回答

4
请在您的控制器中创建以下功能,并在您的Paypal帐户中添加到IPN URL并检查每个IPN的命中率。
<?php
 function paymentIpnlistener(){
    $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";
    // If testing on Sandbox use: 
    $header .= "Host: www.sandbox.paypal.com:443\r\n";
    //$header .= "Host: ipnpb.paypal.com:443\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
    if (strpos('ssl://www.sandbox.paypal.com', 'sandbox') !== FALSE && function_exists('openssl_open')) {
    $fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
  }
    else{
    // The old "normal" way of validating an IPN.
     $fp = fsockopen('ssl://www.sandbox.paypal.com', 80, $errno, $errstr, 30);
    }
    // If testing on Sandbox use:
    //$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
    //$fp = fsockopen ('ssl://ipnpb.paypal.com', 443, $errno, $errstr, 30);
    // 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 (!$fp) {
     // HTTP ERROR
    } else {
        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

                // Add your live email address    
                $mail_From = "From: me@mybiz.com";
                // Add your live email address 
                $mail_To = "raghbendra.nayak@systematixindia.com";
                $mail_Subject = "VERIFIED IPN";
                $mail_Body = $req;
                foreach ($_POST as $key => $value){
                    $emailtext .= $key . " = " .$value ."\n\n";
                }

                mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From);

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

                $mail_From = "From: me@mybiz.com";
                $mail_To = "raghbendra.nayak@systematixindia.com";
                $mail_Subject = "INVALID IPN";
                $mail_Body = $req;

                foreach ($_POST as $key => $value){
                    $emailtext .= $key . " = " .$value ."\n\n";
                }

                mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From);

            }
        }   // while end
     fclose ($fp);
    }

    }
?>

上述函数将在IPN监听器触发时向您发送电子邮件。如果它正常工作,那么您可以根据自己的需求进行管理。请尝试并告诉我。

只是在确认一下 - 这与原帖的问题有什么关系吗?这对我来说看起来不像是一个答案。 - Don't Panic
如果他创建函数并放置此代码,则他将获得IPN响应,无论是失败还是成功,发送至提供的电子邮件......然后他可以根据自己的需求使用相同的功能...我之前也遇到过这个问题,并找到了解决方案... 就像我分享的一样。 - Raghbendra Nayak

3
似乎你的代码中可能存在错误。需要进一步调试。当我遇到这种情况时,通常会检查错误日志以了解情况。
PayPal将继续尝试发送请求,直到收到没有内容的200 OK HTTP状态响应为止。如果PayPal显示终端成功接收,则数据库未能输入数据的位置可能就在调用您的header函数之前。
我的下一步调试动作是尝试弄清楚是否由于某种数据完整性错误/警告而导致我的数据库插入失败。
捕获DB错误并触发某种非传递性响应可能会有所帮助,以便PayPal重新发送,直到您解决了脚本问题为止。
此外:
  • 尝试使用像Monolog或甚至只是error_log这样的东西添加额外的日志记录,以了解您的脚本何时终止或什么不按预期工作。
值得注意的是,我正在实现PayPal IPN,而这个库很难使用。我使用订阅者模式自己编写了一个库。它还没有准备好公开,但主要是为了将逻辑与验证分离,以便进行更好的测试和可读性。

OP正在使用沙箱,关于实时版本的观点在这里是不需要的。 - Martin
已删除有关沙盒的注释。 - smcjones
@smcjones,我刚刚得知,对于同一个账户,我们之前创建了一些用于定期付款的PayPal按钮。现在我们已经在账户设置中更改了IPN URL。因此,通过该按钮进行的付款未将IPN响应发送到我的新URL。有什么办法可以克服这种情况吗? - RAUSHAN KUMAR
对于新的付款,我正在收到IPN响应。 - RAUSHAN KUMAR

1
首先,作为Web服务的第一行,请编写日志,以便您知道何时收到请求。
然后,有时您没有设置所有请求参数,因此当未找到参数时,可能会出现SQL错误。
例如,在我的PayPal表单中,我发送了invoice_number。然后,当我从IPN收到请求时,我将检查invoice_number以将付款与发票链接起来。 但是我还从其他来源在同一个PayPal帐户上收到付款,所以另一种情况下invoice_number没有设置。
您可以编写包含IPN调用中找到的所有参数的日志,以便您可以检查缺少什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接