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

Posted

技术标签:

【中文标题】访问 https://api.paypal.com/v1/payments/payment/ 时得到 Http 响应码 400【英文标题】:Got Http response code 400 when accessing https://api.paypal.com/v1/payments/payment/ 【发布时间】:2020-11-25 02:29:50 【问题描述】:

我正在尝试使用 PayPal-php-SDK 将 PayPal 与自定义构建的 WordPress 电子商务主题集成,但出现错误:

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

此错误仅在我居住时出现,它在沙盒模式下完全正常。

在客户端,我使用 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 \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        $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:访问 https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxx/execute 时获得 Http 响应代码 400。在/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/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/Transport/PayPalRestCall.php(78): PayPal\Core\PayPalHttpConnection->execute('"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-sdk-php /lib/PayPal/Common/PayPalResourceModel.php(104): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '"payer_id":"66. ..',数组)#2 /mnt/stor10-wc1-ord1/825767/984521/www.example.com 在 /mnt/stor10-wc1-ord1/825767/984521/www.example.com/web/content/新万维/wp-content/themes/example/framework/paypal/vendor/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php在第 207 行

上面的代码是网站上旧代码的一部分,我只是想让它工作。在我们更改客户端和秘密 ID 之前,它工作正常。付款创建正常,但当我执行付款时出现问题

PayPal.log

[05-08-2020 08:51:12] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.paypal.com/v1/oauth2/token
[05-08-2020 08:51:13] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:51:13] PayPal\Core\PayPalHttpConnection : INFO: GET https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[05-08-2020 08:51:14] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:51:14] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute
[05-08-2020 08:51:14] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 404
[05-08-2020 08:51:14] PayPal\Core\PayPalHttpConnection : 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] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.paypal.com/v1/oauth2/token
[05-08-2020 08:55:33] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:55:34] PayPal\Core\PayPalHttpConnection : INFO: GET https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[05-08-2020 08:55:36] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 200
[05-08-2020 08:55:40] PayPal\Core\PayPalHttpConnection : INFO: POST https://api.paypal.com/v1/payments/payment/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/execute
[05-08-2020 08:55:42] PayPal\Core\PayPalHttpConnection : INFO: Response Status  : 404
[05-08-2020 08:55:42] PayPal\Core\PayPalHttpConnection : 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"

我肯定做错了。

【问题讨论】:

【参考方案1】:

如果不查看完整的 API 响应正文和标头(包括 PayPal-Debug-ID),很难猜测执行调用到底出了什么问题

400 错误通常是错误的凭据问题,或与错误环境配对的凭据,但如果您的付款创建和 oauth2 调用正常,则不太可能

更重要的问题是,您为什么要使用已弃用且不再维护的 V1 SDK?您应该完全更改与当前 V2 PHP SDK 的集成,此处记录:https://developer.paypal.com/docs/api/rest-sdks/


目前最适合搭配的前端体验是https://developer.paypal.com/demo/checkout/#/pattern/server

【讨论】:

感谢普雷斯顿的回复。问题是,上面的代码是网站上旧代码的一部分,我只是想让它工作。在我们更改客户端和秘密 ID 之前,它工作正常。付款创建良好,但当我执行付款时出现问题。 好吧,如果你记录下 400 响应的正文,我们也许可以知道它的原因 嗨普雷斯顿,我已经更新了上面的描述,如果你能看一下。谢谢。 您能否验证您是否在所有地方都使用相同的客户端 ID/密码?根据无效的资源 ID 错误,您可能正在使用例如JS 中创建的对与执行代码中的不同 客户端/秘密 ID 是正确的,甚至生成的 PAYID-###### 每次都不同.. 记录的其他错误之一是:[04-08-2020 13:52:10] PayPal\Core\PayPalHttpConnection : ERROR: Got Http response code 400 when accessing https://api.paypal.com/v1/payments/payment/PAYID-#####/execute. "name":"INSTRUMENT_DECLINED","message":"The instrument presented was either declined by the processor or bank, or it can't be used for this payment.","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"ae3e56b18b097"

以上是关于访问 https://api.paypal.com/v1/payments/payment/ 时得到 Http 响应码 400的主要内容,如果未能解决你的问题,请参考以下文章

Paypal Basic 访问身份验证中的 URL 无效

Paypal - 尝试实施定期付款时的付款问题

cURL PHP 在收到标头后挂起

贝宝 REST API 交易已获批准

各个访问限定符的权限范围

访问性模式