PayPal Checkout 与智能支付按钮的集成
Posted
技术标签:
【中文标题】PayPal Checkout 与智能支付按钮的集成【英文标题】:PayPal Checkout Integration with Smart Payment Buttons 【发布时间】:2021-01-28 00:07:36 【问题描述】:我目前正在使用 php 框架 Codeigniter 4.0.4 并尝试添加带有智能支付按钮的 PayPal Checkout 集成。
我以 PayPal API 为例,但我在尝试创建订单时总是收到错误消息。 当我点击 PayPal 按钮进行支付时,窗口会打开 1-2 秒,然后立即关闭。
控制台错误:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
Error: Unexpected token < in JSON at position 0
at $t.error (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:59754)
at Object.<anonymous> (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67521)
at JSON.parse (<anonymous>)
at o (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67380)
at cr (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67533)
at Cr.u.on (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:72204)
at Cr (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:72341)
at https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:78460
at Function.n.try (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:14069)
at https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:78257
服务器端:
$clientId = getenv('paypal.CLIENT_ID');
$clientSecret = getenv('paypal.CLIENT_SECRET');
$environment = new SandboxEnvironment($clientId, $clientSecret);
$client = new PayPalHttpClient($environment);
$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
"intent" => "CAPTURE",
"purchase_units" => [[
'reference_id' => '123',
"amount" => [
"value" => 10,
"currency_code" => "USD"
]
]],
"application_context" => [
"cancel_url" => base_url() . "/checkout",
"return_url" => base_url() . "/checkout"
]
];
try
$response = $client->execute($request);
return json_encode($response);
catch (HttpException $ex)
echo $ex->statusCode;
print_r($ex->getMessage());
客户端:
<script type="text/javascript">
paypal.Buttons(
env: 'sandbox',
style:
layout: 'vertical',
size: 'responsive',
shape: 'pill',
color: 'blue',
label: 'pay'
,
createOrder: function()
return fetch('/checkout/paypal',
method: 'post',
headers:
'content-type': 'application/json'
).then(function(response)
return response.json();
).then(function(resJson)
return resJson.result.id;
);
).render('#paypal-button-container');
</script>
【问题讨论】:
它没有返回 json。 JSON 中位置 0 的意外标记 但是为什么呢? /checkout/paypal 正在返回一个 json 你确定它的post请求没有得到吗?检查路线 获取完成加载:POST "localhost:8080/checkout/paypal". console.log(response.json());和 resJson 以确保它返回 【参考方案1】:试试
<script type="text/javascript">
paypal.Buttons(
env: 'sandbox',
style:
layout: 'vertical',
size: 'responsive',
shape: 'pill',
color: 'blue',
label: 'pay'
,
createOrder: function()
return fetch('/checkout/paypal',
method: 'post',
headers:
'content-type': 'application/json'
).then(function(response)
console.log(response);
);
).render('#paypal-button-container');
</script>
控制台会返回什么
【讨论】:
click_initiate_payment_reject err:“错误:预期在 ht...tF1Siv9WsfCp&disable-funding=credit,card:2:10596) 传递一个订单 ID,时间戳:“1602606170399”,引用者:“www .sandbox.paypal.com", sdkCorrelationID: "ffdd74d018833", sessionID: "39b2cf6396_mty6mtq6mjc", ... 啊,问题出在 php 部分,不在客户端。也检查这个类似的:***.com/questions/57197907/… 响应错误,第二个 .then 用于 orderId 但它没有进入它,因为响应给了我一个错误。我已经多次查看 PayPal 文档,但不明白为什么会发生错误...以上是关于PayPal Checkout 与智能支付按钮的集成的主要内容,如果未能解决你的问题,请参考以下文章
Paypal 与 reactjs -node.js 集成 - 实施指南