paypal-checkout-sdk Capture函数只捕获一次,如何在发生第二次时修复致命错误



我使用paypal-checkout-sdk进行服务器端配置。我已经使用自定义按钮。提交后,我正在创建秩序代码

$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => [[
"reference_id" => "test_ref_id1",
"amount" => [
"value" => "100.00",
"currency_code" => "USD"
]
]],
"application_context" => [
"cancel_url" => "https://example.com/cancel",
"return_url" => "https://example.com/return"
]
];
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
echo "<pre>";
print_r($response);
echo "</pre>";
//exit();
// If call returns body in response, you can get the deserialized version from the result attribute of the response
if(isset($response->result->links[1]->href) && !empty($response->result->links[1]->href)){
//echo $response->result->links[1]->href;
//header('Location:'.$response->result->links[1]->href);
//die();
}
}catch (HttpException $ex) {
//echo $ex->statusCode;
//print_r($ex->getMessage());
}

然后重定向到paypal与批准的url。从那以后,如果我使用捕获类来捕获数据。我收到了正确的回复。

$request = new OrdersCaptureRequest("6UD13042B02825225");
$request->prefer('return=representation');
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
print_r($response);
// If call returns body in response, you can get the deserialized version from the result attribute of the response
if ($response->statusCode == 201)
{
print_r($response);
}
}catch (HttpException $ex) {
$message = json_decode($ex->getMessage(), true);
//echo json_encode($response->result, JSON_PRETTY_PRINT), "n";
print_r($message);
print "Status Code: {$ex->statusCode}n";
}
Response sample.
PayPalHttpHttpResponse Object ( [statusCode] => 201 [result] => stdClass Object ( [id] => 6UD13042B02825225 [intent] => CAPTURE [status] => COMPLETED [purchase_units] => Array ( [0] => stdClass Object ( [reference_id] => test_ref_id1 [amount] => stdClass Object ( [currency_code] => USD [value] => 100.00 ) [

如果我试图为相同的订单运行上面的OrderCapture代码,它会显示如下错误

Fatal error: Uncaught PayPalHttpHttpException: {"name":"UNPROCESSABLE_ENTITY","details":[{"issue":"ORDER_ALREADY_CAPTURED","description":"Order already captured.If 'intent=CAPTURE' only one capture per order is allowed."}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"cfdc8ac1d115b","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_ALREADY_CAPTURED","rel":"information_link","method":"GET"}]} in

我不知道如何修复这个错误。我想多次捕捉它

当使用intent:capture时,一个订单只能捕获一次,并且是全部金额。这是付款人通过结账审批流程时所批准的内容。

在这种情况下,您不能捕获多次,因为这没有得到付款人的批准。

您需要一个新的订单(并获得批准)来进行新的捕获。