访问时得到Http响应代码400https://api.paypal.com/v1/payments/payment/.



我正试图使用PayPal PHP SDK将PayPal与自定义的WordPress电子商务主题集成,但我收到了一个错误:

访问时得到Http响应代码400https://api.paypal.com/v1/payments/payment/...

此错误仅在我居住的时候出现,它在沙箱模式下运行完全正常。

在客户端,我使用checkout.js并将其重定向到PHP文件,在那里我试图使用PayPal PHP SDK执行支付,但我得到了错误代码:

$result = $payment->execute($execution, $apiContext);

下面是示例代码我正在使用

JavaScript

paypal.Button.render({
env: 'production',
client: {
sandbox: 'sandbox_id',
production: 'production_id'
},
locale: 'en_US',
style: {
color: 'blue',
size: 'responsive',
},
commit: true,
payment: function (data, actions) {
return actions.payment.create({
payment: {
redirect_urls: {
return_url: site_url + '/execute-payment'
},
transactions: [{
amount: {
total: '0.01',
currency: 'NZD',
details: {
subtotal: '0.01',
shipping: '0.00',
tax: '0.00',
insurance: '0.00'
}
},
description: "TEST",
invoice_number: json.invoice_number ? json.invoice_number : "",
item_list: {
items: items_ ? items_ : "",
shipping_address: {
"recipient_name": json.recipient_name ? json.recipient_name : "",
"line1": json.line1 ? json.line1 : "",
"line2": json.line2 ? json.line2 : "",
"city": json.city ? json.city : "",
"country_code": json.country_code ? json.country_code : "",
"postal_code": json.postal_code ? json.postal_code : "",
"phone": json.phone ? json.phone : "",
"state": json.state ? json.state : ""
}
}
}],
"note_to_payer": "Contact us for any questions on your order.",
}
});
},
onAuthorize: function (data, actions) {
console.log('::Authorized: ');
return actions.redirect();
},
onCancel: function (data, actions) {

console.log('::Canceled: ');
/* window.location = "/paypal/cancel/"; */
},
onError: function (error) {
console.log('::Error: ', error)
}
}, '#paypal_container');

PHP

# live client id
$live_cid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
# live secret id
$live_sid = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$apiContext = new PayPalRestApiContext(
new PayPalAuthOAuthTokenCredential(
$live_cid,
$live_sid
)
);
$apiContext->setConfig(
array(
'log.LogEnabled' => true,
'log.FileName' => 'PayPal.log',
'log.LogLevel' => 'DEBUG',
'mode' => 'live'
)
);
# die(var_dump($apiContext));
# redirect here via link: https://example.com/execute-payment/?paymentId=xxxxxxxxxxxxxxxxxxxxxxxxxxxx&token=xxxxxxxxxxxxxxxxxx&PayerID=xxxxxxxxxxxxxx
# die(var_dump("<br><br>" . "paymentId: " . $_GET['paymentId'] . "<br>" . "PayerID: " . $_GET['PayerID']));
$paymentId = $_GET['paymentId'];
$payment = Payment::get($paymentId, $apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($_GET['PayerID']);
$transaction = new Transaction();
$amount = new Amount();
$details = new Details();
$details->setShipping(0.00)
->setTax(0.00)
->setSubtotal(0.01);
$amount->setCurrency('NZD');
$amount->setTotal(0.01);
$amount->setDetails($details);
$transaction->setAmount($amount);
$execution->addTransaction($transaction);
$result = $payment->execute($execution, $apiContext);
# I get the error after trying to execute the payment
$paymentId = $result->id;
$PayerID = $result->payer->payer_info->payer_id;
die(var_dump($result));

错误

致命错误:未捕获PayPal\Exception\PayPalConnectionException:访问时获得Http响应代码400https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxx/execute.在/mnt/stor10-wc1-ord1/825767/984521/www.example.com/web/content/newww/wp-content/themes/example/framework/paypal/vendor/paypal/rest-api-sdk-php/lib/paypal/Core/PayPalHttpConnection.php中:207堆栈跟踪:#0/mnt/stort10-wc1-order1/825767/984521/www.example.com/web/content/newww/wp-content/themes/enxample/fframework/paypl/vendor/payPaypal/rest-api-sdk-php/lib/paypal/Transport/PayPalRestCall.php(78(:PayPal\Core\PayPalHttpConnection->execute("{quot;payer_id":"66…"(#1/mnt/stor10-wc1-ord1/825767/984521/www.example.com/web/content/newww/wp-content/themes/example/framework/paypal/vendor/paypal/rest-api-sdkphp/lib/paypal/Common/PayPalResourceModel.php',数组(#2/mnt/stor10-wc1-ord1/825767/984521/www.example.com在/mnt/stor10-wc1-order1/825767/984521/www.example.com/web/content/newww/wp-cont/themes/example/framework/paypal/vendor/paypal/rest-api-sdk-php/lib/paypal/Core/PayPalHttpConnection.php中

上面的代码是网站上旧代码的一部分,我只是想让它发挥作用。在我们更改客户和机密ID之前,它一直工作得很好。付款创建得很好,但问题出现在我执行付款时

PayPal.log

[05-08-2020 08:51:12] PayPalCorePayPalHttpConnection : INFO: POST https://api.paypal.com/v1/oauth2/token
[05-08-2020 08:51:13] PayPalCorePayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:51:13] PayPalCorePayPalHttpConnection : INFO: GET https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[05-08-2020 08:51:14] PayPalCorePayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:51:14] PayPalCorePayPalHttpConnection : INFO: POST https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute
[05-08-2020 08:51:14] PayPalCorePayPalHttpConnection : INFO: Response Status  : 404
[05-08-2020 08:51:14] PayPalCorePayPalHttpConnection : ERROR: Got Http response code 404 when accessing https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute. {"name":"INVALID_RESOURCE_ID","message":"Requested resource ID was not found.","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"acba76cd232a5"}

[05-08-2020 08:55:31] PayPalCorePayPalHttpConnection : INFO: POST https://api.paypal.com/v1/oauth2/token
[05-08-2020 08:55:33] PayPalCorePayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:55:34] PayPalCorePayPalHttpConnection : INFO: GET https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[05-08-2020 08:55:36] PayPalCorePayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:55:40] PayPalCorePayPalHttpConnection : INFO: POST https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute
[05-08-2020 08:55:42] PayPalCorePayPalHttpConnection : INFO: Response Status  : 404
[05-08-2020 08:55:42] PayPalCorePayPalHttpConnection : ERROR: Got Http response code 404 when accessing https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute. {"name":"INVALID_RESOURCE_ID","message":"Requested resource ID was not found.","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"3c22c723fa27a"}

我肯定做错了什么。

如果没有看到完整的API响应主体和头,包括PayPal-Debug-ID ,很难猜测执行调用到底出了什么问题

400个错误通常是错误的凭据问题,或者凭据与错误的环境配对,但如果您的支付创建和oauth2调用正常,就不太可能出现这种情况

更重要的问题是,您为什么要使用已弃用的V1 SDK,而该SDK已不再维护?您应该将集成完全更改为当前的V2 PHP SDK,文档如下:https://developer.paypal.com/docs/api/rest-sdks/


当前最佳的前端体验是https://developer.paypal.com/demo/checkout/#/pattern/server

最新更新