#hassan-checkout

1 messages · Page 1 of 1 (latest)

tacit turret
#

Hey there, let's use this thread.

#

Can you provide the request ID for the error?

#

You can see an example there of how line_items should be passed

frigid grotto
#

I've checked these links, I believe I have followed it correctly

frigid grotto
#

Sending a request from the front end with the line items ``` import React, { useState } from 'react';
import { useStripe } from '@stripe/react-stripe-js';
import stripeApi from '../apis/stripe-api.js';

const Checkout = () => {
const stripe = useStripe();

const [product, setProduct] = useState({
name: 'Hat',
description: 'Pug hat.',
price: 799,
currency: 'usd',
quantity: 0,
});

const handleCheckout = async (event) => {

const body = { line_items: [product] }

try {

  const { id: sessionId } = await stripeApi.post('/checkout', {
    body: JSON.stringify(body.line_items)
  });

  console.log(sessionId)
  
  await stripe.redirectToCheckout({
    sessionId
  });

} catch (err) {
  console.log(err)
}

};

return (
<div>
<button onClick={handleCheckout}>Checkout</button>
</div>
)
}

export default Checkout ```

#

Then on the node server, I create the session ``` /**

  • We are creating reusable function that allows us to use
  • a stripe checkout session from multiple api endpoints
  • */
    require('dotenv').config();

const stripe = require('stripe')(process.env.STRIPE_SECRET); // stripe test key

/**

  • Creates a Stripe Checkout session with line items
    */

// the line_items are coming from the front-end
const createStripeCheckoutSession = async (line_items) => {

const url = process.env.WEBAPP_URL;

const session = await stripe.checkout.sessions.create({
    line_items, 
    mode: 'payment',
    success_url: `${url}/success?session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${url}/failed`,
});

return session;

}

exports.createStripeCheckoutSession = createStripeCheckoutSession; ```

#

Then call it from the router ``` require('dotenv').config();

const express = require('express');
const { createStripeCheckoutSession } = require('../utils/stripe-checkout');
const router = express.Router()

/**

  • Catches async errors when awaiting promises
    */
    function runAsync(callback) {
    return (req, res, next) => {
    callback(req, res, next).catch(next);
    }
    }

router.post('/checkout', runAsync(async ({ body }, res) => {
console.log(body)
res.send(
await createStripeCheckoutSession(body.line_items)
);
}));

module.exports = router ```

tacit turret
#

When you log body in the router what do you see?

frigid grotto
#
} ```
#

it logs the line_items stored in the state

tacit turret
#

You are passing body.line_items but line_items isn't an object within body

#

Looks like you should be passing body.body based on your log

frigid grotto
#

Hmm. I changed the request to this router.post('/checkout', runAsync(async ({ body }, res) => { console.log(body.body) res.send( await createStripeCheckoutSession(body.body) ); }));

#

the console prints this -> [{"name":"Hat","description":"Pug hat.","price":799,"currency":"usd","quantity":0}]

#

Now instead of the line_items error, i get Error: Invalid array

tacit turret
#

Yep

#

So now you are getting your data correctly

#

But now you aren't passing line_items correctly in your Checkout Session

#

Take another look at the above docs

#

That have examples of what line_items should look like