laravel - How to fix Paypal Checkout Order Creation Error
问题描述
I am using Laravel 8 framework for PHP and I am trying to integrate paypal into my the local web. However I am stuck on `create_order_error` even though I have strictly followed some sample snippets provided by paypal I still encounter this pro
References:
- https://developer.paypal.com/demo/checkout/#/pattern/server
- https://github.com/paypal/Checkout-PHP-SDK#code
- https://developer.paypal.com/docs/checkout/reference/server-integration/
Error:
SyntaxError: Unexpected token < in JSON at positio…1kLoyti46gxJY-Rl1PH23n49yWhf¤cy=PHP:2:79380"
Code:
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
style: {
shape: 'pill',
layout: 'horizontal',
color: 'blue',
height: 35
},
// Call your server to set up the transaction
createOrder: function(data, actions) {
return fetch('/billing/createOrder', {
method: 'post',
headers: {
'content-type': 'application/json'
}
}).then(function(res) {
return res.json();
}).then(function(orderData) {
return orderData.id;
});
},
}).render('#paypal-button-container');
</script>
Note: I have removed the onApprove function since I'm stuck on createOrder
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use PayPalHttp\HttpException;
class PaypalCheckoutController extends Controller
{
private $environment;
private $client;
public function __construct()
{
$this->environment = new SandboxEnvironment(config('paypal.client_id'), config('paypal.secret'));
$this->client = new PayPalHttpClient($this->environment);
}
public function index(Request $request)
{
return view('payment.checkout');
}
public function createOrder(Request $request)
{
$order = new OrdersCreateRequest();
$order->prefer('return=representation');
$order->body = array(
'intent' => 'CAPTURE',
'application_context' =>
array(
'return_url' => 'http://dummyweb.test/billing/checkout',
'cancel_url' => 'http://dummyweb.test/billing/checkout'
),
'purchase_units' =>
array(
0 =>
array(
'amount' =>
array(
'currency_code' => 'PHP',
'value' => '420.00'
)
)
)
);
try {
$result = $this->client->execute($order);
return $result;
}
catch(HttpException $ex) {
print_r($ex->getMessage());
}
}
}
解决方案
SyntaxError: Unexpected token < in JSON at position...</p>
当浏览器调用 /billing/createOrder 时,您返回的不是 JSON。您只能返回 JSON。
使用浏览器开发工具中的网络选项卡,或在新选项卡中加载路径,以检查您实际返回的响应正文。
它显然不是 JSON。根据该错误消息,它将以一些 HTML(<
字符)开头
只返回 JSON。您需要能够将整个响应正文复制到 JSON 验证器中并使其正常。
推荐阅读
- c++ - BFS 对 Leetcode 的叶子相似树问题的错误回答
- wordpress - 如何在 woocommerce 上创建“有货”自定义状态,如果再次将其更改为“处理中”,如何减少库存?
- javascript - 没有在 zillow 网站(和其他)上的 devtools 中获取所有 html 数据
- extract - 使用 Google 地球引擎中的 ImageCollection 按多边形提取像素值
- asp.net-web-api - 请求有关从 azure 中继(混合)连接 webapi 的帮助
- zsh - zsh:如何在 vim 模式下为 history-beginning-search-forward 创建键绑定(使用 oh-my-zsh 的 vi-mode 插件)?
- php - 为什么不在 PHP 中的数据库中保存 order 和 order_detail
- r - 在 RStudio 4.1.0 中安装 Lubridate 1.3.3 的问题
- android - setContent 块是可组合的吗?
- java - 如何在两个数组中找到最接近的匹配对以在最后一个数组中查找结果