json - PayPal 智能按钮返回 JSON 错误
问题描述
我正在 PHP 中实现 Express Checkout 集成(使用 PayPal 智能按钮),但是当我尝试付款时出现错误。
createOrder
调用函数时会触发错误。我相信错误在于服务器端,因为fetch
正在成功执行并且服务器生成了一个 ORDER ID,但是它没有正确地将 ORDER ID 传递给客户端,我最终得到以下错误:
错误:位置 0 处 JSON 中的意外标记 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 直接访问它时,它显示“出了点问题”。
解决方案
我终于找到了一个解决方案,在后端和前端进行了一些修改。
我得到了这个工作
注释这部分代码
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 灯箱关闭(以及信用卡选项)。
推荐阅读
- javascript - 使用带有 for..of 循环的 async/await
- reactjs - 如何在功能组件 React 中使用函数
- c++ - 向量
>::size_type 或向量 ::size_type 处理向量时 >的大小()? - python - 将 xy 转换为 x*y?
- bevy - 如何在 Bevy 中围绕本地原点旋转 3d 网格
- postgresql - Amazon RDS 上的 PostgresSQL
- python - mysql没有插入正确的值
- python - 根据条件创建修改后的熊猫行副本的更好方法
- c# - 使用网络服务获取随机视频并播放。C# WPF
- arrays - 使用strtok在C中为这个结构分配空间的正确方法是什么