如何使用 Nodejs 在 paypal 结帐中添加多个项目

How to add many items in paypal checkout Using Nodejs

我在 Nodejs 中使用“paypal-rest-sdk”模块我想在购物车中添加更多项目,而不仅仅是像我的代码中的一个项目问题是当我添加另一个项目时这个错误显示“错误:响应状态:400”。

我不知道怎么做,我真的厌倦了理解这个问题 谁能帮我解决这个问题!

router.post('/payment/paypal/:id', (req, res) => {

    let obj = JSON.stringify(req.body)
    let stringify = JSON.parse(obj);
    //console.log(req.body)

    // Get List of levels 
    var LevelList = JSON.stringify(req.body.level_id);
    var List = JSON.parse(LevelList);


    // Get Course ID
    var course_id = (req.body.course_id);
    console.log("Course ID: " + course_id)
    console.log("Course Length: " + course_id.length)



    var subscription_id = req.params.id;
    var username = req.params.username;

    console.log("subscription ID: " + subscription_id)

    // Get End Date Monthly

    var end_date_monthly = new Date();
    end_date_monthly.setMonth(end_date_monthly.getMonth() + 1);
    console.log("Date end : " + end_date_monthly.getFullYear(), end_date_monthly.getMonth() + 1, end_date_monthly.getDate());

    // Get End Date Yearly

    var end_date_yearly = new Date();
    end_date_yearly.setMonth(end_date_yearly.getMonth() + 12);
    console.log("Date end : " + end_date_yearly.getFullYear(), end_date_yearly.getMonth() + 12, end_date_yearly.getDate());

    // Get Number of users
    var get_numberofusers = JSON.stringify(req.body.numberofusers);
    var numbersofusers = JSON.parse(get_numberofusers);
    console.log("number of users: " + numbersofusers)

    // Get Total
    var get_totalPirce = JSON.stringify(req.body.total);
    var TotalPrice = JSON.parse(get_totalPirce);
    console.log("Total: " + TotalPrice)

    // Get Course Name
    var get_course_name = JSON.stringify(req.body.c_name);
    var course_name = JSON.parse(get_course_name);
    console.log("Course Name: " + course_name);

    // Get Unit Price
    var get_unit_price = JSON.stringify(req.body.price_unit);
    var UnitPrice = JSON.parse(get_unit_price);
    console.log("Unit Price: " + UnitPrice)




    var url = 'https://v1.qalamnet.com/dashboard/'


    var create_payment_paypal_json = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "redirect_urls": {
            "return_url": url + "transaction_success",
            "cancel_url": url + "transaction_canceled"
        },
        "transactions": [{
            "item_list": {
                "items": [
                    {
                        "name": course_name,
                        "sku": "C001",
                        "price": UnitPrice,
                        "currency": "USD",
                        "quantity": numbersofusers
                    },
                ]
            },
            "amount": {
                "currency": "USD",
                "total": TotalPrice
            },
            "description": "Subscription"
        }]
    };
    
    paypal.payment.create(create_payment_paypal_json, function (error, payment) {
        if (error) {
            throw error;
        } else {
            for (let i = 0; i < payment.links.length; i++) {
                if (payment.links[i].rel === 'approval_url') {
                    res.redirect(payment.links[i].href);
                }
            }
        }
    });

});

该 SDK 已弃用并使用已弃用的 v1/payments API。

改用当前的v2/checkout/ordersAPI; here is the guide. There is a Checkout-NodeJS-SDK available, aka @paypal/checkout-server-sdk in npm

添加项目的语法在 Set up standard payments 中的“项目”数组示例中得到了最好的记录,在 'Add and modify the code' 部分解释了服务器集成。这将为您提供必要 JSON 的工作示例,您可以阅读 v2 订单 API 参考以获取完整详细信息。

当捕获响应成功时,您可能希望将其生成的付款详细信息存储在您的数据库中(特别是 purchase_units[0].payments.captures[0].id,PayPal 交易 ID)并执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品),然后再将您的 return JSON 发送给来电者。

一旦您的服务器上有两条路由(一条用于创建订单,一条用于捕获)调用 PayPal API 并向调用方传播 JSON 响应,请将您的两条路由配对使用此批准流程:https://developer.paypal.com/demo/checkout/#/pattern/server