Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Bucephalus-lgtm/e7922d9eda92752878701183100b6d64 to your computer and use it in GitHub Desktop.
Save Bucephalus-lgtm/e7922d9eda92752878701183100b6d64 to your computer and use it in GitHub Desktop.

Procedures for integrating the stripe payment gateway with Node.js Express.js backend:

  • Install stripe package from npm
$ npm install stripe
  • Import the Stripe module in your backend server file
 const stripe = require('stripe')('sk_test_your_api_key_here');
  • Create an endpoint in your backend server to receive the payment details from the frontend
const express = require('express');
const app = express();

app.use(express.json());

app.post('/charge', async (req, res) => {
  const { amount, currency, source } = req.body;
  try {
    const charge = await stripe.charges.create({
      amount,
      currency,
      source
    });
    // Send success response to frontend
    res.send('Payment successful!');
  } catch (err) {
    console.log(err);
    // Send error response to frontend
    res.status(500).send('Payment failed!');
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment