贝宝:创建和捕获订单

时间:2019-10-14 10:03:52

标签: asp.net-mvc razor paypal paypal-rest-sdk

我正在跟踪此示例(https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/#on-the-server),以使PayPal在服务器上工作。

我已经完成了订单的创建,但是不能同时捕获订单。是否可以同时创建和捕获订单?

var request = new OrdersCreateRequest();
request.Prefer("return=representation");
request.RequestBody(BuildRequestBody());

var response = await PayPalClient.client().Execute(request);  //this will only create the order. How to capture it at the same time?

当我尝试捕获订单时:

//continue from above
var result = response.Result<Order>();

var requestCapture = new OrdersCaptureRequest(result.Id);
requestCapture.Prefer("return=representation");
requestCapture.RequestBody(new OrderActionRequest());

response = await PayPalClient.Client().Execute(requestCapture);

我得到了错误:

"name":"UNPROCESSABLE_ENTITY",
"details":  [{
                "issue":"ORDER_NOT_APPROVED",
                "description":"Payer has not yet approved the Order for payment. Please redirect the payer to the 'rel':'approve' url returned as part of the HATEOAS links within the Create Order call or provide a valid payment_source in the request."
            }],
"message": "The requested action could not be performed, semantically incorrect, or failed business validation.",
"links":    [{
                "href": "https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_NOT_APPROVED",
                "rel": "information_link",
                "method": "GET"
            }]

我的问题是,是否可以在 相同 时间创建订单,授权和捕获?

谢谢

2 个答案:

答案 0 :(得分:0)

在使用{'a': 'needs repair', 'b': 'needs repair', 'c': 'ok'} 之前,您必须重定向到[rel] =>批准来自OrdersCaptureRequest的响应的链接:

OrdersCreateRequest

然后然后使用 $client = PayPalClient::client(); $response = $client->execute($request); $res_links = $response->result->links; $approve_index = array_search('approve',array_column($res_links,'rel')); redirect($res_links[$approve_index]->href,'location',302);`

答案 1 :(得分:0)

我不认为有一种方法可以在 PayPal 中同时创建、授权和捕获订单,即使在沙盒环境中也是如此。您必须分别执行每个步骤。 关于 ORDER_NOT_APPROVED 错误,您可以像@raj-kamal 所说的那样获得用户批准的订单,或者您可以制作一个 Paypal-Auth-Assertion 并将其发送到标题中。此标头是 API 客户端提供的 JSON 网络令牌 (JWT) 断言,用于标识商家。

参考:https://developer.paypal.com/docs/api/reference/api-requests/#paypal-auth-assertion

注意:

<块引用>

要使用此标头,您必须获得代表商家的同意。