我正在使用 paypal sdk 将 paypal 付款集成到我的应用程序中,createOrder 请求应该由 spring boot 应用程序处理,但是现在当用户单击 paypal 按钮时,插入用户电子邮件的弹出窗口不会显示,而是我得到错误:
Uncaught Error: Expected an order id to be passed
at https://www.sandbox.paypal.com/smart/buttons?
这是 JavaScript 代码:
createOrder(data, actions, cartInfo) {
console.log('CREATE ORDER DATA: ', data);
console.log('CREATE ORDER ACTIONS: ', actions);
console.log('CREATE ORDER CARTINFO: ', cartInfo);
const totalAmount = cartInfo.total;
// Order is created on the server and the order id is returned
return fetch("http://127.0.0.1:8102/paypal/create-order", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
// use the "body" param to optionally pass additional order information
// like product skus and quantities
body: JSON.stringify({
intent: "SALE",
purchase_units: [
{
items: cartInfo.items,
amount: {
currency_code: 'CAD',
value: {totalAmount},
},
},
],
}),
})
.then((response) => response.json()).catch(error => {
console.log('ERROR: ', error);
})
.then((order) => order.id);
}
JAVA代码:
@PostMapping(value = "/create-order", produces = MediaType.APPLICATION_JSON_VALUE)
public String createOrder(HttpServletRequest request, @RequestBody String order) {
STRING orderId = "1";
return orderId;
}
我不知道createOrder java方法中该怎么做才能向字体端返回满意的响应。 任何帮助或文档将不胜感激。
使用 PayPal REST API 首先获取 access_token,然后用它调用 v2/checkout/orders 以按需创建订单并返回响应。 标准集成指南中有一个node.js示例,您可以简单地使用Java而不是node重新实现该后端。
以下是 API 响应示例:
{
"id": "4UC20475T8165880J",
"status": "CREATED",
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/4UC20475T8165880J",
"rel": "self",
"method": "GET"
},
{
"href": "https://www.sandbox.paypal.com/checkoutnow?token=4UC20475T8165880J",
"rel": "approve",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/4UC20475T8165880J",
"rel": "update",
"method": "PATCH"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/4UC20475T8165880J/capture",
"rel": "capture",
"method": "POST"
}
]
}
但是对于创建订单来说,唯一重要的是新创建的用于结帐的
id
,因此从技术上讲,您转发的响应部分可以短至:
{
"id": "4UC20475T8165880J"
}