onApprove 路线在成功创建路线后给出错误
onApprove route gives an error after successful CREATE route
我正在尝试通过 onApprove 路由获取订单,但出现以下错误:
Error: Cannot read properties of undefined (reading '0')
购买已成功但过程未完成,我没有转发到 URL 我在 php CREATE 方法中提到的。我的 onApprove 路线如下所示(没有把所有代码都写清楚):
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function(data, actions) {
return fetch('https://example.com/TESTS.php?create=testing', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.result.id;
});
},
// Call your server to finalize the transaction
onApprove: function(data, actions) {
return fetch('https://example.com/TESTS.php?id=' + data.orderID, {
method: 'post'
}).then(function(res) {
return res.json();
如您所见,看起来很正常,因为我可以毫无问题地创建订单。但是一旦我在弹出页面中付款,就会看到错误。以下是我的 php 文件 CREATE/CAPTURE:
<?php
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
require __DIR__ . '/Checkout-PHP-SDK-develop/vendor/autoload.php';
$clientId = "AQgUM4x3URK1A-rcNIq56covuc0CYGv3pb5sYeL6-cqsO1HYV2CV6h4ur6BCly_1YYd3-UOMTNGtwQXd";
$clientSecret = "EDm88hmcFd6arhd5vaJ6v9AWjIvCScR6E6s0eM3OKqwf1uZt0G0KlLNUXG057vesyXR4eYP3RKDLJBz8";
$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
if(isset($_GET['create']) && $_GET['create'] == 'testing'){
$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/cancelled",
"return_url" => "https://example.com/success"
]
];
try {
$response = $client->execute($request);
echo json_encode($response);
}catch (HttpException $ex) {
echo json_encode($ex->statusCode);
echo json_encode($ex->getMessage());
}
}
if(isset($_GET['id'])) {
$request = new OrdersCaptureRequest($_GET['id']);
$request->prefer('return=representation');
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
echo json_encode($response);
// return $response1;
// If call returns body in response, you can get the deserialized version from the result attribute of the response
// return $request1;
}catch (HttpException $ex) {
echo json_encode($ex->statusCode);
// print_r($ex->getMessage());
}
}
几乎尝试了所有方法,但似乎有一些我不知道的东西。也许你可以看到我哪里做错了并帮助我。
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
的输出如下:
"statusCode": 201,
"result": {
"id": "19X58570EP734922G",
"intent": "CAPTURE",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "test_ref_id1",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"payee": {
"email_address": "sb-qlquy8262061@business.example.com",
"merchant_id": "BHUEK7UQX3KFU"
},
"soft_descriptor": "PAYPAL *TEST STORE",
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Free Trade Zone",
"admin_area_2": "Bali",
"admin_area_1": "AZ_zip = 994",
"postal_code": "994",
"country_code": "AZ"
}
},
"payments": {
"captures": [
{
"id": "9796275092029330N",
"status": "PENDING",
"status_details": {
"reason": "PENDING_REVIEW"
},
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"final_capture": true,
"seller_protection": {
"status": "NOT_ELIGIBLE"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/9796275092029330N",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/9796275092029330N/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/19X58570EP734922G",
"rel": "up",
"method": "GET"
}
],
"create_time": "2022-03-25T22:28:23Z",
"update_time": "2022-03-25T22:28:23Z"
}
]
}
}
],
"payer": {
"name": {
"given_name": "John",
"surname": "Doe"
},
"email_address": "sb-qtxuk6283362@personal.example.com",
"payer_id": "J4RS8Q76ZEKZ2",
"address": {
"country_code": "AZ"
}
},
"create_time": "2022-03-25T22:28:12Z",
"update_time": "2022-03-25T22:28:23Z",
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/19X58570EP734922G",
"rel": "self",
"method": "GET"
}
]
},
"headers": {
"": "",
"Content-Type": "application/json",
"Content-Length": "1496",
"Connection": "keep-alive",
"Date": "Fri, 25 Mar 2022 22",
"Application_id": "APP-80W284485P519543T",
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
"Caller_acct_num": "BHUEK7UQX3KFU",
"Paypal-Debug-Id": "92f6c8280dfc2",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains"
}
}
return fetch('https://deals.az/TESTS.php?id=' + data.purchase_units[0].payments.captures[0].id,
这一行没有意义。你想要 + data.orderID
.purchase_units[0].payments.captures[0].id
是您稍后会从捕获 response.
中引用的内容
查看演示代码 https://developer.paypal.com/demo/checkout/#/pattern/server
我正在尝试通过 onApprove 路由获取订单,但出现以下错误:
Error: Cannot read properties of undefined (reading '0')
购买已成功但过程未完成,我没有转发到 URL 我在 php CREATE 方法中提到的。我的 onApprove 路线如下所示(没有把所有代码都写清楚):
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function(data, actions) {
return fetch('https://example.com/TESTS.php?create=testing', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.result.id;
});
},
// Call your server to finalize the transaction
onApprove: function(data, actions) {
return fetch('https://example.com/TESTS.php?id=' + data.orderID, {
method: 'post'
}).then(function(res) {
return res.json();
如您所见,看起来很正常,因为我可以毫无问题地创建订单。但是一旦我在弹出页面中付款,就会看到错误。以下是我的 php 文件 CREATE/CAPTURE:
<?php
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
require __DIR__ . '/Checkout-PHP-SDK-develop/vendor/autoload.php';
$clientId = "AQgUM4x3URK1A-rcNIq56covuc0CYGv3pb5sYeL6-cqsO1HYV2CV6h4ur6BCly_1YYd3-UOMTNGtwQXd";
$clientSecret = "EDm88hmcFd6arhd5vaJ6v9AWjIvCScR6E6s0eM3OKqwf1uZt0G0KlLNUXG057vesyXR4eYP3RKDLJBz8";
$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
if(isset($_GET['create']) && $_GET['create'] == 'testing'){
$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/cancelled",
"return_url" => "https://example.com/success"
]
];
try {
$response = $client->execute($request);
echo json_encode($response);
}catch (HttpException $ex) {
echo json_encode($ex->statusCode);
echo json_encode($ex->getMessage());
}
}
if(isset($_GET['id'])) {
$request = new OrdersCaptureRequest($_GET['id']);
$request->prefer('return=representation');
try {
// Call API with your client and get a response for your call
$response = $client->execute($request);
echo json_encode($response);
// return $response1;
// If call returns body in response, you can get the deserialized version from the result attribute of the response
// return $request1;
}catch (HttpException $ex) {
echo json_encode($ex->statusCode);
// print_r($ex->getMessage());
}
}
几乎尝试了所有方法,但似乎有一些我不知道的东西。也许你可以看到我哪里做错了并帮助我。
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
的输出如下:
"statusCode": 201,
"result": {
"id": "19X58570EP734922G",
"intent": "CAPTURE",
"status": "COMPLETED",
"purchase_units": [
{
"reference_id": "test_ref_id1",
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"payee": {
"email_address": "sb-qlquy8262061@business.example.com",
"merchant_id": "BHUEK7UQX3KFU"
},
"soft_descriptor": "PAYPAL *TEST STORE",
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Free Trade Zone",
"admin_area_2": "Bali",
"admin_area_1": "AZ_zip = 994",
"postal_code": "994",
"country_code": "AZ"
}
},
"payments": {
"captures": [
{
"id": "9796275092029330N",
"status": "PENDING",
"status_details": {
"reason": "PENDING_REVIEW"
},
"amount": {
"currency_code": "USD",
"value": "100.00"
},
"final_capture": true,
"seller_protection": {
"status": "NOT_ELIGIBLE"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/9796275092029330N",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/9796275092029330N/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/19X58570EP734922G",
"rel": "up",
"method": "GET"
}
],
"create_time": "2022-03-25T22:28:23Z",
"update_time": "2022-03-25T22:28:23Z"
}
]
}
}
],
"payer": {
"name": {
"given_name": "John",
"surname": "Doe"
},
"email_address": "sb-qtxuk6283362@personal.example.com",
"payer_id": "J4RS8Q76ZEKZ2",
"address": {
"country_code": "AZ"
}
},
"create_time": "2022-03-25T22:28:12Z",
"update_time": "2022-03-25T22:28:23Z",
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/19X58570EP734922G",
"rel": "self",
"method": "GET"
}
]
},
"headers": {
"": "",
"Content-Type": "application/json",
"Content-Length": "1496",
"Connection": "keep-alive",
"Date": "Fri, 25 Mar 2022 22",
"Application_id": "APP-80W284485P519543T",
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
"Caller_acct_num": "BHUEK7UQX3KFU",
"Paypal-Debug-Id": "92f6c8280dfc2",
"Strict-Transport-Security": "max-age=31536000; includeSubDomains"
}
}
return fetch('https://deals.az/TESTS.php?id=' + data.purchase_units[0].payments.captures[0].id,
这一行没有意义。你想要 + data.orderID
.purchase_units[0].payments.captures[0].id
是您稍后会从捕获 response.
查看演示代码 https://developer.paypal.com/demo/checkout/#/pattern/server