PayPal IPN 在沙箱中返回无效

Posted

技术标签:

【中文标题】PayPal IPN 在沙箱中返回无效【英文标题】:PayPal IPN returns invalid in sandbox 【发布时间】:2016-01-24 05:29:56 【问题描述】:

我正在使用 PayPal IPN 创建支付网关。提交付款后,我检索了 PayPal 的回复。这是我收到的(php var_dump):

    array(45) 
    ["mc_gross"]                    => string(4)  "1.00"
    ["protection_eligibility"]      => string(10) "Ineligible"
    ["address_status"]              => string(9)  "confirmed"
    ["item_number1"]                => string(1)  "1"
    ["payer_id"]                    => string(13) "KUN39T5E6UA3W"
    ["tax"]                         => string(4)  "0.00"
    ["address_street"]              => string(14) "1 Main Terrace"
    ["payment_date"]                => string(25) "14:01:20 Oct 24, 2015 PDT"
    ["payment_status"]              => string(7)  "Pending"
    ["charset"]                     => string(12) "Windows-1252"
    ["mc_tax1"]                     => string(4)  "0.00"
    ["address_zip"]                 => string(7)  "W12 4LQ"
    ["mc_shipping"]                 => string(4)  "0.00"
    ["mc_handling"]                 => string(4)  "0.00"
    ["first_name"]                  => string(4)  "test"
    ["address_country_code"]        => string(2)  "GB"
    ["address_name"]                => string(10) "test buyer"
    ["notify_version"]              => string(3)  "3.8"
    ["custom"]                      => string(13) "562bf19083b11"
    ["payer_status"]                => string(8)  "verified"
    ["address_country"]             => string(14) "United Kingdom"
    ["num_cart_items"]              => string(1)  "1"
    ["mc_handling1"]                => string(4)  "0.00"
    ["address_city"]                => string(13) "Wolverhampton"
    ["payer_email"]                 => string(44) "[myemail]"
    ["verify_sign"]                 => string(56) "AiPC9BjkCyDFQXbSkoZcgqH3hpacAymQrx4nOnSeR2hKuwHDAUFmNy.-"
    ["mc_shipping1"]                => string(4)  "0.00"
    ["tax1"]                        => string(4)  "0.00"
    ["txn_id"]                      => string(17) "9UV91932BX9643323"
    ["payment_type"]                => string(7)  "instant"
    ["last_name"]                   => string(5)  "buyer"
    ["item_name1"]                  => string(5)  "test1"
    ["address_state"]               => string(13) "West Midlands"
    ["receiver_email"]              => string(38) "[myemail]"
    ["quantity1"]                   => string(1)  "1"
    ["pending_reason"]              => string(10) "unilateral"
    ["txn_type"]                    => string(4)  "cart"
    ["mc_gross_1"]                  => string(4)  "1.00"
    ["mc_currency"]                 => string(3)  "GBP"
    ["residence_country"]           => string(2)  "GB"
    ["test_ipn"]                    => string(1)  "1"
    ["transaction_subject"]         => string(13) "562bf19083b11"
    ["payment_gross"]               => string(0)  ""
    ["merchant_return_link"]        => string(10) "click here"
    ["auth"]                        => string(87) "AkxCT8gKLRplBETrTgj4KrgsPA.1rCwR.lM1EpzTQoW9QR-M0l3-kiwjq0hgNoZUQuJaGRdDHsGnjZ9NsbsRamg"

然后我执行以下操作:

$responseURL = "https://sandbox.paypal.com/cgi-bin/webscr?";
$params = "cmd=_notify-validate&" .http_build_query($_REQUEST);

var_dump(file_get_contents($responseURL.$params));

返回:无效

有什么建议吗?测试可能无法在 Sandbox 中运行?

【问题讨论】:

我可能错了,但我认为你应该发回POST 请求。 【参考方案1】:

您需要发布数据;这是我使用的工作代码:

$req = 'cmd=_notify-validate';

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


$res = '';
$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'));   
$res = curl_exec($ch);
curl_close($ch);

if(strcmp($res, "VERIFIED") == 0) 

    //VALID

【讨论】:

【参考方案2】:
$params = ['http' => [
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => http_build_query(array_merge(['cmd' => '_notify-validate'], $_REQUEST))
]];

$result = file_get_contents('https://sandbox.paypal.com/cgi-bin/webscr', false, stream_context_create($params));

【讨论】:

以上是关于PayPal IPN 在沙箱中返回无效的主要内容,如果未能解决你的问题,请参考以下文章

无论我做啥,PayPal IPN 都会不断返回“无效”代码

Paypal 沙盒 IPN 返回无效

Paypal IPN 无效返回,不知道为啥

PayPal沙箱IPN处理器拒绝所有消息?

nodejs - 无法让 paypal-ipn 工作。 Paypal 不断返回“无效”

IPN 验证返回 INVALID,因为 PayPal 在 POST 中向 IPN 侦听器发送了不正确的字符集