我尝试通过前端的Vue应用程序和运行Express的Node.js后端实现Paypal Checkout。我正在尝试运行默认设置,并且一切正常,直到第6步:(https://developer.paypal.com/docs/checkout/integrate/#6-verify-the-transaction)
在这里,我必须从前端发送请求(文章中为6.1)
// Call your server to save the transaction
return fetch('/paypal-transaction-complete', { ...
我将URL更改为自己的URL,所以http://localhost:3000/paypal-transaction-complete'
我的服务器端代码与本教程中的代码完全相同,但包含在明确的路线中:
app.post('/paypal-transaction-complete', function (req, res) {
// 1. Set up your server to make calls to PayPal
// 1a. Import the SDK package
const checkoutNodeJssdk = require('@paypal/checkout-server-sdk');
// 1b. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
/**
*
* PayPal HTTP client dependency
*/
const payPalClient = require('./paypalClient');
// 2. Set up your server to receive a call from the client
module.exports = async function handleRequest(req, res) {
// 2a. Get the order ID from the request body
const orderID = req.body.orderID;
// 3. Call PayPal to get the transaction details
let request = new checkoutNodeJssdk.orders.OrdersGetRequest(orderID);
let order;
try {
order = await payPalClient.client().execute(request);
} catch (err) {
// 4. Handle any errors from the call
console.error(err);
return res.send(500);
}
// 5. Validate the transaction details are as expected
// if (order.result.purchase_units[0].amount.value !== '220.00') {
// return res.send(400);
// }
// 6. Save the transaction in your database
// await database.saveTransaction(orderID);
// 7. Return a successful response to the client
console.log('payment successful');
return res.send(200);
};
});
不幸的是,服务器从未停止运行,过了一会儿,我在前端控制台中收到一个奇怪的typeError:
未捕获的TypeError:无法获取
Error: Failed to fetch
at or.<computed> (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:50112)
at Object.<anonymous> (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:56841)
at JSON.parse (<anonymous>)
at o (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:56700)
at gr (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:56853)
at Nr.l.on (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:61935)
at Nr (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:62124)
at https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:66869
at https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:66915
Error: Failed to fetch
at or.<computed> (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:50112)
at Array.<anonymous> (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:56841)
at JSON.parse (<anonymous>)
at o (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:56700)
at gr (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:56853)
at Nr.l.on (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:61935)
at Nr (https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:62124)
at https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:66869
at https://www.paypal.com/sdk/js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1:66915
or.<computed> @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
o @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
gr @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
Nr.l.on @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
Nr @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
setTimeout (async)
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
n.dispatch @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
n.then @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
e.try @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
l @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
trigger @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
e.try @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
n.onError @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
t @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
n.dispatch @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
n.then @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
e.try @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
Tr.postrobot_message_request @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
Nr @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
(anonymous) @ js?client-id=AWPUth_gVe12aLPuvNT7Yp4YZbtn_sDZb7AHYoevpU5e6gH2zBy1rIdtlx7ao8fIYHA_JRI2wvKwmCrz:1
我想我在express中包装该代码有些不对。 Express的实际工作效果如何?
非常感谢!