Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Stripe Payment Method using node.js

I am implementing stripe payment method using node and express it runs successfully and returns success message but customer is not adding in stripe customers and also my node code crashed. I am new here so learning help will be appreciated.

Here is my code:

const express = require('express')
const app = express()
const { stripecard } = require('../../schemas')
var stripe = require("stripe")("sk_test_51LM4hdClyoITdq3ZfTfLdVZjmLKskcWAV17Yef5fGAjKFBReC82bstJOP7VyuauMiHFVGvHgyfQdSLsfcQHTzb9w00s65S9CT6")

const CreateCard = app.post('/payment', function(req, res) {
    const {
        id,
        duration,
        cardNumber,
        expMM,
        expYY,
        cvv,
        email,
        name
    } = req.body;

    const createdUser =  stripe.customers.create({
        email: email || 'testUser@gmail.com',
        name: name || "123"
    })

    //console.log("createdUser", createdUser)
    if (createdUser) {
        try {
            const token =  stripe.tokens.create({
                card: {
                    number: cardNumber,
                    exp_month: expMM,
                    exp_year: expYY,
                    cvc: cvv
                }
            })
            //console.log("token : ", token)
            const AddingCardToUser =  stripe.customers.createSource(createdUser.id, {
                source: token.id
            })

            return res.status(201).json({
                success: true,
                AmountCharged: req.body.charge,
                message: "Payment Charged Successfully and also a mail has been sent to User as well as Admin."
            });
        } catch (error) {
            return res.status(501).json({
                success: false,
                message: `Error in ${error.type} and error is :  ${error.message}`
            });
        }
    }

})
module.exports = CreateCard

Here is the output in postman:

{
    "success": true,
    "AmountCharged": "1200",
    "message": "Payment Charged Successfully and also a mail has been sent to User as well as Admin."
}
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You might want to add await keyword in front of each API call so that it returns the customer object instead of Promise.

I'd also like to highlight a few more things.

  1. Your secret key in leaked in this post, you should roll it as soon as possible.
  2. You are passing credit card number and details to your application directly. Unless you plan to handle the PCI compliance by yourself, I'd highly recommend you to tokenize the card details at the frontend, and pass the token ID to backend.
  3. Sources and Tokens are Stripe's old APIs. If you are not maintaining a legacy project, you should use the new Payment Intents and Setup Intents APIs.
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!