如何根据物品数量更改总数

问题描述 投票:0回答:1

我在我的网站上收到了PayPal付款。看起来像这样:

app.post('/pay', (req, res) => {
    console.log(req.body);
    const create_payment_json = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "redirect_urls": {
            "return_url": "http://localhost:1234/success",
            "cancel_url": "http://localhost:1234/cancel"
        },
        "transactions": [{
            "item_list": {
                "items": [{
                    "name": req.body.user_name,
                    "sku": "001",
                    "price": "25",
                    "currency": "USD",
                    "quantity": req.body.persons_count
            }]
            },
            "amount": {
                "currency": "USD",
                "total": "25"
            },
            "description": req.body.user_name + " with email " + req.body.email + " just ordered " + req.body.persons_count + " places"
    }]
    };

    paypal.payment.create(create_payment_json, function (error, payment) {
        if (error) {
            throw error;
        } else {
            res.send('on my way');
            console.log(payment);
        }
    });
})

如果我更改金额对象中的总字段(这是我想做的),我得到400响应(错误请求)。我该如何付款:

"amount":{
"total": req.body.persons_count * 2
}

其中req.body.persons_count变量是一个变量,我从之前的一种形式的post请求中获得。

与该代码的战斗表明,pricetotal值必须相等,但我希望单个项目的价格与我想要的总量不同。非常感谢!

顺便说一下,数值必须等于1.在所有其他情况下,app crahes。

node.js paypal-rest-sdk
1个回答
0
投票

总金额应为项目金额,运费金额,税金和其他费用的总和。

在您的情况下,总计:项目价格*项目数量

样品,

 "transactions": [
        {
            "amount": {
                "currency": "USD",
                "total": "20",
                "details": {
                    "shipping": "1.20",
                    "tax": "1.30",
                    "subtotal": "17.50"
                }
            },
            "description": "Payment description",
            "invoice_number": "1231123524",
            "custom": "custom data",
            "item_list": {
                "items": [
                    {
                        "name": "Ground Coffee 40 oz",
                        "currency": "USD",
                        "quantity": 1,
                        "price": "7.50"
                    },
                    {
                        "name": "Granola bars",
                        "currency": "USD",
                        "quantity": 5,
                        "price": "2"
                    }
                ]
            }
        }
    ]

你也可以参考https://developer.paypal.com/docs/api/payments/v1/#payment_create

© www.soinside.com 2019 - 2024. All rights reserved.