我正在 PHP 中实现快速结账集成(使用 PayPal 智能按钮),但在尝试付款时出现错误。
调用createOrder
函数时会触发该错误。我相信错误在于服务器端,因为 fetch
已成功执行并且服务器生成了一个订单 ID,但是它没有正确地将订单 ID 传递给客户端,我最终得到了以下错误:
Error: Unexpected token S in JSON at position 0
我不知道会出现什么问题,因为我使用的是 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 灯箱会打开并显示生成的 token ,但随后就会关闭。当我尝试使用 URL 直接访问它时,它显示“出现问题”。
最佳答案
我终于找到了解决方案,在后端和前端做了一些修改。
我通过以下方式让这个工作
注释这部分代码
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 灯箱关闭(以及信用卡选项)。
关于json - PayPal 智能按钮向我返回 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61410133/