我正在尝试使用 REST checkout SDK 实现 PayPal 的智能按钮并使用服务器端调用 API。
但是我在 createOrder
部分遇到了一些问题。
JS代码:
paypal.Buttons({
style: {
size: 'responsive',
layout: 'vertical'
},
createOrder: function() {
return fetch(blm_custom_vars.wp_home + 'classes/paypal/paypal-create-order.php', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.id;
});
},
onApprove: function(data) {
return fetch(blm_custom_vars.wp_home + 'classes/paypal/paypal-capture-order.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.id
})
}).then(function(res) {
return res.json();
}).then(function(details) {
alert('Transaction funds captured from ' + details.payer_given_name);
})
},
}).render('.purchase-modal');
来自 paypal-create-order.php 的 PHP 代码:
namespace Sample\CaptureIntentExamples;
require(__DIR__ . '/paypal-client.php');
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use Sample\PayPalClient;
class CreateOrder {
/**
* This is the sample function to create an order. It uses the
* JSON body returned by buildRequestBody() to create an order.
*/
public static function createOrder($debug = false) {
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = self::buildRequestBody();
// Call PayPal to set up a transaction
$client = PayPalClient::client();
$response = $client->execute($request);
if ($debug) {
print "Status Code: {$response->statusCode}\n";
print "Status: {$response->result->status}\n";
print "Order ID: {$response->result->id}\n";
print "Intent: {$response->result->intent}\n";
print "Links:\n";
foreach ($response->result->links as $link) {
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n";
}
// To print the whole response body, uncomment the following line
echo json_encode($response->result, JSON_PRETTY_PRINT);
}
return $response;
}
/**
* Setting up the JSON request body for creating the order with minimum request body. The intent in the
* request body should be "AUTHORIZE" for authorize intent flow.
*
*/
private static function buildRequestBody() {
return array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'shipping_preference' => 'NO_SHIPPING',
'user_action' => 'PAY_NOW',
'payment_method' => array(
'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED',
),
//'custom_id' => '',
'return_url' => 'https://example.com/return',
'cancel_url' => 'https://example.com/cancel'
),
'purchase_units' => array(
0 => array(
'amount' => array(
'currency_code' => 'AUD',
'value' => '70.00',
'breakdown' => array(
'item_total' => array(
'currency_code' => 'AUD',
'value' => '75.00',
),
'discount' => array(
'currency_code' => 'AUD',
'value' => '5.00',
),
),
),
'description' => 'something',
'items' => array(
0 => array(
'name' => 'something',
'unit_amount' => array(
'currency_code' => 'AUD',
'value' => '75.00',
),
'quantity' => 1,
'category' => 'DIGITAL_GOODS',
),
),
)
)
);
}
}
/**
* This is the driver function that invokes the createOrder function to create
* a sample order.
*/
$debug = (ACTIVE_SERVER == 'dev') ? true : false;
if (!count(debug_backtrace())) {
CreateOrder::createOrder($debug);
}
现在,当我有 $debug = true
时,服务器输出似乎是正确的响应。那里看起来什么都没有。尽管我必须关闭调试以避免其他 JSON 错误。
所以一旦我将其关闭,我就会收到错误消息:
SyntaxError: Unexpected end of JSON input
我认为这是因为它是一个空页面,因为 createOrder 脚本返回数据而不是输出数据?但这就是 PayPal 声明的方式。
所以我尝试将其更改为:echo json_encode($response);
然后我得到另一个错误:
Expected an order id to be passed
在 JS 中我最初有这个:
}).then(function(data) {
return data.orderID;
});
...但随后意识到 createOrder 脚本返回的数组引用了 id
而不是 orderID
所以我将其更改为 return data.id;
但它没有帮助。
我在这里做错了什么?
最佳答案
只需确保您回显/返回整个响应,或者至少包含 {"id":"......"}
您可以在浏览器的 Dev Tools Network 选项卡中查看发生了什么。
所以从 PHP 开始,做:
echo json_encode($response->result, JSON_PRETTY_PRINT);
..应该没问题,但在 $debug=true if block 下方/之外,并且在其上方或周围没有任何其他打印或回显行,因为您必须输出有效的 JSON,并且只输出有效的 JSON(对于 JS 做createOrder 提取到您的服务器以便能够解析它)
关于javascript - PayPal 智能按钮和 REST Checkout SDK - SyntaxError : Unexpected end of JSON input OR Expected an order id to be passed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64345667/