Search code examples
node.jspaypal-sandbox

How to add many items in paypal checkout Using Nodejs


I'm working with "paypal-rest-sdk" module in Nodejs i want to add more items in cart not only one item like in my code the problem was when i add a other item this error is showing "Error: Response Status : 400".

I don't how to make this and i'm really tired to understand the problem can any one help me to solve this please !

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);
                }
            }
        }
    });

});

Solution

  • That SDK is deprecated and uses the deprecated v1/payments API.

    Use the current v2/checkout/orders API instead; here is the guide. There is a Checkout-NodeJS-SDK available, aka @paypal/checkout-server-sdk in npm

    The syntax for adding items is best documented by the "items" array example in Set up standard payments, within 'Add and modify the code' section that explains a server integration. That will get you a working example of the necessary JSON, and you can read the v2 orders API reference for full details.

    When a capture response is successful, you probably want to store its resulting payment details in your database (particularly purchase_units[0].payments.captures[0].id, the PayPal transaction ID) and perform any necessary business logic (such as sending confirmation emails or reserving product) before sending your return JSON to the caller.

    Once you have two routes on your server (one for creating an order and one for capturing) that call the PayPal API and propagate a JSON response to the caller, pair your two routes with this approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server