Home > Blockchain >  When implementing Stripe, how do I redirect using Express (or other method)?
When implementing Stripe, how do I redirect using Express (or other method)?

Time:09-22

I am trying to integrate Stripe using this tutorial.

My code is working, but I now want to redirect the user to the URL returned by Stripe.

This is the original unmodified node.js code provided by Stripe:

// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(SECRET_KEY_GOES_HERE);

// The price ID passed from the client
//   const {priceId} = req.body;
const priceId = '{{PRICE_ID}}';

const session = await stripe.checkout.sessions.create({
  mode: 'subscription',
  payment_method_types: ['card'],
  line_items: [
    {
      price: priceId,
      // For metered billing, do not pass quantity
      quantity: 1,
    },
  ],
  // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
  // the actual Session ID is returned in the query parameter when your customer
  // is redirected to the success page.
  success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/canceled.html',
});

// Redirect to the URL returned on the Checkout Session.
// With express, you can redirect with:
//   res.redirect(303, session.url);

At the bottom it says I can redirect to the returned URL using express.

To achieve that I have tried requiring express at the top and then uncommenting that last line. But res is not defined, so it won't work.

I'm new to node.js and express, so I'm not sure how to set this up.

Here is my modified code that does not work because res is not defined:

const app = require('express')();
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(SECRET_KEY_GOES_HERE);

// The price ID passed from the client
//   const {priceId} = req.body;
const priceId = '{{PRICE_ID}}';

const session = await stripe.checkout.sessions.create({
  mode: 'subscription',
  payment_method_types: ['card'],
  line_items: [
    {
      price: priceId,
      // For metered billing, do not pass quantity
      quantity: 1,
    },
  ],
  // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
  // the actual Session ID is returned in the query parameter when your customer
  // is redirected to the success page.
  success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/canceled.html',
});

// Redirect to the URL returned on the Checkout Session.
// With express, you can redirect with:
res.redirect(303, session.url);

How do I get this working?

The redirection doesn't have to use express if you have another method that works.

CodePudding user response:

The reason res is undefined is because it's an argument in a callback that is passed into an route.

Here's a basic example from the docs:

app.get('/', function (req, res) {
  res.send('Hello World!')
})

What's happening here is that when the client requests the home page they'll get the response 'Hello World'.

You'll need to do something like this:

const express = require('express')
// Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
const stripe = require('stripe')(SECRET_KEY_GOES_HERE);
const app = express()
const port = 3000

app.get('/<the path your client will call to run stripe>', (req, res) => {
  
// The price ID passed from the client
//   const {priceId} = req.body;
const priceId = '{{PRICE_ID}}';

const session = await stripe.checkout.sessions.create({
  mode: 'subscription',
  payment_method_types: ['card'],
  line_items: [
    {
      price: priceId,
      // For metered billing, do not pass quantity
      quantity: 1,
    },
  ],
  // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
  // the actual Session ID is returned in the query parameter when your customer
  // is redirected to the success page.
  success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/canceled.html',
});

// Redirect to the URL returned on the Checkout Session.
// With express, you can redirect with:
res.redirect(303, session.url);
})

You see the arguments req and res in the callback? One is the request and the other is the response. If you console log the request you'll see the request that was made to this path.

Also, notice that express has been called and assigned to the app variable, whereas you were directly invoking it at the top of the file.

It might be helpful for you to check out the Express documentation and go through the Getting Started guide here

  • Related