问题描述
我正在尝试使用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',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','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:JSON输入意外结束
我认为这是因为它是一个空页面,因为 createOrder 脚本返回了而不是输出了数据?但这就是贝宝所说的。
所以我尝试将其更改为:echo json_encode($response);
,然后又遇到另一个错误:
预期要传递的订单ID
在JS中,我最初是这样的:
}).then(function(data) {
return data.orderID;
});
...但是后来意识到从 createOrder 脚本返回的数组引用了id
而不是orderID
,所以我将其更改为return data.id;
,但是没有没有帮助。
我在做什么错了?
解决方法
只需确保您要回显/返回整个响应,或者至少使用{"id":"......"}
您可以在浏览器的“开发工具网络”标签中查看正在发生的事情。
因此从PHP进行:
echo json_encode($response->result,JSON_PRETTY_PRINT);
..应该没问题,但是在$ debug = true if块的下面/外面,并且在上面或周围没有任何其他打印或回显线,因为您必须输出有效的JSON,并且仅输出有效的JSON(对于JS,将createOrder提取到您的服务器以进行解析)