paypal checkout v2 - 捕获订单 Nodejs

问题描述

我一直在尝试在我正在开发的网站上实施 PayPal。我可以创建一个订单并将用户重定向到 PayPal 页面进行付款,但是一旦用户确认付款并重定向到成功页面,该页面就会持续加载,直到我收到内部服务器错误并且控制台中没有任何内容。不知道出了什么问题,因为我从文档中做了所有的事情。

这是我的代码

var dotenv = require('dotenv');
var express = require('express');
var router = express.Router({ mergeParams: true });
var async = require('async');


const paypal = require('@paypal/checkout-server-sdk');

// Creating an environment
let clientId = 'SECRET1';
let clientSecret =
    'SECRET2';
// This sample uses SandBoxEnvironment. In production,use LiveEnvironment
let environment = new paypal.core.SandBoxEnvironment(clientId,clientSecret);
let client = new paypal.core.PayPalHttpClient(environment);

router.post('/donate',(req,res) => {
    var selectedamount = req.body.options;

    let request = new paypal.orders.OrdersCreateRequest();
    request.requestBody({
        intent: 'CAPTURE',application_context: {
            return_url: 'https://localhost:3000/success',cancel_url: 'https://localhost:3000/cancel',brand_name: 'Example site',user_action: 'CONTINUE',},purchase_units: [
            {
                reference_id: 'ws_123',description: 'sample product',amount: {
                    currency_code: 'USD',value: selectedamount,],});

    // Call API with your client and get a response for your call
    let createOrder = async function () {
        let response = await client.execute(request);
        
        for (let i = 0; i < response.result.links.length; i++) {
            if (response.result.links[i].rel === 'approve') {
                res.redirect(response.result.links[i].href);
            }
        }
    };
    createOrder();
    console.log('Creating Order...');
    
});

router.get('/success',res) =>{
    
    let captureOrder =  async function(orderId) {
    request = new paypal.orders.OrdersCaptureRequest(orderId);
    request.requestBody({});
    // Call API with your client and get a response for your call
    let response = await client.execute(request);
    console.log(`Response: ${JSON.stringify(response)}`);
    // If call returns body in response,you can get the deserialized version from the result attribute of the response.
    console.log(`Capture: ${JSON.stringify(response.result)}`);
}
        
});

module.exports = router;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)