Search code examples
stripe-payments

Stripe: "Cannot read properties of undefined (reading 'price')"


i can't create a payment link for a product of stripe. Am i missing something in the code or is this something inside the API going on? Also just in case how do i check my currently installed stripe version?

NodeJS code:

async createPaymentlink(data) {
        try {
          const params = new URLSearchParams();

          params.append('line_items[0][price]', data.line_items[0].price);
          params.append('line_items[0][quantity]', data.line_items[0].quantity);
          params.append('after_completion[1][confirmation]', data.after_completion[0].confirmation);

            const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
                    'Content-Type': 'application/x-www-form-urlencoded'
                },

            body: params});
            return { data: response };
            } catch (err) {
            return { error: err.message };
            }
        }

JSON body:

{
    "line_items": {
        "price": "price_1PtUNVA15zK6SDK7f0llFJ3v",
        "quantity": 1
    },
    "after_completion": {
        "type": "confirmation"

    }
}

Solution

  • The createPaymentlink function code assumes that data.line_items is an array, as signified by the bracket notation (e.g. data.line_items[0]).

    The JSON you've shared, which I assume is what is passed to the data parameter, does not have an array shape. Instead, you should update your code to be:

    const params = new URLSearchParams();
    
    params.append('line_items[0][price]', data.line_items.price);
    params.append('line_items[0][quantity]', data.line_items.quantity);
    params.append('after_completion[1][confirmation]', data.after_completion.confirmation);