Home > front end >  Can't access the stripe check-out page
Can't access the stripe check-out page

Time:12-01

I have written my server-side code by nodejs . I have implemented a route to create a stripe checkout session, which contains a bunch of data about the object that is going to be purchased.

router.get(
  '/checkout-session/:productId',
  catchAsync(async (req, res, next) => {
    //   1) Get the currently ordered product
    const product = await Product.findById(req.params.productId);

    // 2) Create checkout session
    const session = await stripe.checkout.sessions.create({
      expand: ['line_items'],
      payment_method_types: ['card'],
      success_url: `https://nasim67reja.github.io/CareoCIty-ecommerce/`,
      cancel_url: `https://nasim67reja.github.io/CareoCIty-ecommerce/#/${product.categories}`,
      customer_email: req.user.email,
      client_reference_id: req.params.productId,
      line_items: [
        {
          price_data: {
            currency: 'usd',
            unit_amount: product.price * 100,
            product_data: {
              name: `${product.name} `,
              description: product.summary,
              images: [
                `https://e-commerceapi.up.railway.app/Products/${product.categories}/${product.images[0]}`,
              ],
            },
          },
          quantity: 1,
        },
      ],
      mode: 'payment',
    });

    // 3) Create session as response
    res.status(200).json({
      status: 'success',
      session,
    });
  })
);

Then on the front-end (ReactJs), I created a function to request the checkout session from the server once the user clicks the buy button. So once I hit that endpoint that I created on the backend, that will make a session and send it back to the client. Then based on that session, the stripe will automatically create a checkout page for us. where the user can then input here is my client side code:

const buyProduct = async () => {
    try {
      const session = await axios.get(
        `${URL}/api/v1/orders//checkout-session/${product}`
      );
      window.location.replace(session.data.session.url);
    } catch (error) {
      console.log(`error: `, error.response);
    }
  };

All was okay when I tried on the local server.But when I hosted my backend on the Railway, then I got an errorenter image description here

I have also tried to put the stripe public & private key on the authorization header. but still got that error.I searched a lot but didn't find any solution. I will be very happy to get help from you

CodePudding user response:

Looks like your server on Railway doesn't have the correct secret key. It depends on how you initialize stripe in NodeJS, but normally if you read it from an environment variable, you want to make sure Railway also sets that value correctly.

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
  • Related