PayPal智能按钮返回我JSON错误
我正在用PHP实现快速结帐集成(使用PayPal智能按钮),但我在尝试付款时出错 调用PayPal智能按钮返回我JSON错误,json,paypal,client,response,integration,Json,Paypal,Client,Response,Integration,我正在用PHP实现快速结帐集成(使用PayPal智能按钮),但我在尝试付款时出错 调用createOrder函数时会触发该错误。我认为错误在于服务器端,因为获取正在成功执行,并且服务器生成了订单ID,但是它没有正确地将订单ID传递给客户端,我最终出现以下错误: 错误:JSON中位于位置0的意外标记 我不知道会出什么问题,因为我使用的是贝宝提供的SDK 我的客户 <script> // Render the PayPal button into #paypal-but
createOrder
函数时会触发该错误。我认为错误在于服务器端,因为获取
正在成功执行,并且服务器生成了订单ID,但是它没有正确地将订单ID传递给客户端,我最终出现以下错误:
错误:JSON中位于位置0的意外标记
我不知道会出什么问题,因为我使用的是贝宝提供的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>
并删除这部分代码:
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 lightbox打开时会显示生成的令牌,但随后立即关闭。当我试图直接使用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);
}
将返回$response
替换为
$json_obj= array('id'=>$response->result->id);
$jsonstring = json_encode($json_obj);
echo $jsonstring;
以及在前端调整货币
错误的货币选项引发了异常,导致PayPal lightbox关闭(以及信用卡选项)。将调试添加到服务器端创建订单中,以便查看发生了什么。这取决于客户端希望收到什么。如果您的客户端解析json并读取result.id,这很好。需要理解的关键是,您正在设计客户端和服务器之间的中间件接口。贝宝没有设计这个界面。因此,规范由您决定,绝对没有理由需要与PayPal服务器与PayPal通信的PayPal API规范相同。重要的是,您至少要以某种方式正确地传输OrderID。使用
控制台.log
,控制台.warn
而不是alert会让您的运气更好,但由于它未定义,您可能需要更深入地查看获取它的位置出现了什么问题。您可以登录的事实很有趣,如果它真的利用了这个id,我会从PayPal自己的控制台日志中寻找调试信息。寻找这个答案已经很久了!我不敢相信像贝宝这样大的公司在其主要目的上会有如此错误和不完整的文档。。。接受网上付款!Ronald,不幸的是,正如我所注意到的,当涉及到这种集成时,不仅PayPal有如此糟糕的文档。他们提供了大量的文档,但没有做基本的工作:举例说明如何实现他们的API。我在他们的页面上留下了反馈。
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);
}
$json_obj= array('id'=>$response->result->id);
$jsonstring = json_encode($json_obj);
echo $jsonstring;