我在PHP中实现了快速结账集成(使用PayPal智能按钮),但当我尝试支付时,我得到了一个错误。
该错误是在以下情况下触发的 createOrder
函数被调用。我相信这个错误是在服务器端,因为这个 fetch
正在成功执行,服务器生成了一个ORDER ID,但是它没有正确地将ORDER ID传递给客户端,我最终得到了以下错误。
错误: 在JSON中0号位置出现了意外的标记S。
我不知道哪里出了问题,因为我使用的是PayPal提供的SDK。
我的客户
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return fetch('*http://localhost/test/createorder.php', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(data) {
return data.orderID;
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return fetch('https://api.sandbox.paypal.com/v2/checkout/orders/' + data.orderID + '/capture/', {
method: 'post'
}).then(function(res) {
return res.json();
}).then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onError: function(err){
alert(err)
}
}).render('#paypal-button-container');
</script>
我的服务器
<?php
namespace Sample\CaptureIntentExamples;
require __DIR__ . '/vendor/autoload.php';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
class CreateOrder
{
// 2. Set up your server to receive a call from the client
/**
*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();
// 3. 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);
}
// 4. Return a successful response to the client.
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(
'return_url' => 'https://example.com/return',
'cancel_url' => 'https://example.com/cancel'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'USD',
'value' => '220.00'
)
)
)
);
}
}
/**
*This is the driver function that invokes the createOrder function to create
*a sample order.
*/
if (!count(debug_backtrace()))
{
CreateOrder::createOrder(true);
}
?>
我从PayPal文档中得到了代码。
更新
当我更换 return $response
用这样的代码。
$orderID=['orderID'=>$response->result->id];
echo json_encode($orderID, true);
并删除这部分代码
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);
}
部分就能用了 PayPal灯箱打开后,生成的令牌就会出现,但随后就关闭了。当我尝试使用URL直接访问它时,它显示 "Something went wrong"。
我终于找到了一个解决方案,在后端和前端做了一些修改。
我得到了这个工作通过
在这部分代码中添加注释
if ($debug=true)
{
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
与
$json_obj= array('id'=>$response->result->id);
$jsonstring = json_encode($json_obj);
echo $jsonstring;
并在前端也调整货币。
错误的货币选项引发异常,导致PayPal灯箱关闭(以及信用卡选项)。