首页 > 解决方案 > 无法使用 Paypal API 退款订单

问题描述

我正在使用 PHP 和 cURL 集成 PayPal,并设法通过https://api.sandbox.paypal.com/v2/checkout/ordershttps://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 标头,我已经多次打印并仔细检查了它,这似乎是正确的。这是我用来拨打电话的代码

// 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 提供的应用程序的秘密 id,$access_token 之前是从我在应用程序的其他部分使用的函数生成的,它工作正常。

此外,如果我尝试使用 Postman(以及PayPal API 资源管理器)进行相同的调用,则会产生不同的错误,即

{
  "error": "invalid_request",
  "error_description": "No permissions to set target_client_id"
}

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

标签: phpcurlpaypalpaypal-sandbox

解决方案


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

$header = base64_encode(json_encode(['alg' => 'none']));
$body = base64_encode(json_encode(['payer_id' => $merchantId, 'iss' => $clientId]));
$authHeader = $header . "." . $body . ".";

我能够使用此 $authHeader 退还捕获的订单。

问候


推荐阅读