gpt4 book ai didi

php - 无法使用 Paypal API 退款订单

转载 作者:行者123 更新时间:2023-12-03 20:55:15 32 4
gpt4 key购买 nike

我正在使用 PHP 和 cURL 集成 PayPal,并设法通过 https://api.sandbox.paypal.com/v2/checkout/orders 创建订单并获取付款。和https://api.sandbox.paypal.com/v2/checkout/orders/<order_id>/capture端点

我尝试退款的订单已成功捕获,并查看显示其状态为“已完成”且 final_capture 字段为 true 的详细信息,因此已下订单,我可以在商家帐户中看到交易成功结束

现在我正在尝试使用从捕获调用中获得的捕获 ID 测试退款,但它总是失败并出现以下响应正文的错误
{
"error":"invalid_subject",
"error_description":"Subject Authentication failed"
}

我找到了主题身份验证失败问题背后的问题,即错误的 Paypal-Auth-Assertion header ,我已经打印并仔细检查了多次,这似乎是正确的。这是我用来调用电话的代码

// Codeigniter function to read from $_POST
$capture_id = $this->input->post('paypal_capture_id');

$auth_1 = base64_encode("{\"alg\":\"none\"}");
$auth_2 = base64_encode("{\"payer_id\":<payer_id>,\"iss\":<client_id>}");
$auth_assertion_header = $auth_1 . "." . $auth_2 . ".";


$curlSES=curl_init();
curl_setopt($curlSES,CURLOPT_URL,"https://api.sandbox.paypal.com/v2/payments/captures/$capture_id/refund");
curl_setopt($curlSES, CURLOPT_POST, true);
curl_setopt($curlSES, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '. $access_token,
'PayPal-Auth-Assertion:' . $auth_assertion_header
));
curl_setopt($curlSES, CURLOPT_POSTFIELDS, '{}'); // empty payload means full refund
curl_setopt($curlSES, CURLOPT_RETURNTRANSFER, true);

$result=curl_exec($curlSES);

其中 payer_id 和 client_id 使用用于沙盒环境的商家 id 帐户填充,client_id 是 Paypal 提供的应用程序的 secret id,$access_token 之前是从我在应用程序的其他部分使用的函数生成的,它工作正常。

此外,如果我尝试使用 Postman(以及 PayPal API explorer)进行相同的调用,则会产生不同的错误,即
{
"error": "invalid_request",
"error_description": "No permissions to set target_client_id"
}

没有针对此错误的搜索结果真的很有帮助,所以我迷失在那里我做错了什么,尽管它似乎与 Paypal-Auth-Assertion 无关,因为如果我提供,它会退回到主题身份验证失败错误故意的错误值。

最佳答案

在尝试为合作伙伴帐户退还捕获的订单时,我遇到了同样的问题。在我看来,您正在使用示例中的字符串转义 json。
尝试使用 json_encode 函数,如下所示:

$header = base64_encode(json_encode(['alg' => 'none']));
$body = base64_encode(json_encode(['payer_id' => $merchantId, 'iss' => $clientId]));
$authHeader = $header . "." . $body . ".";
我能够使用此 $authHeader 退还捕获的订单。
问候

关于php - 无法使用 Paypal API 退款订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61143763/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com