saksaokp
saksaokp

Reputation: 3

stripe.tokens.create returns nothing

I have the following code

import { STRIPE_SK } from '../../constants';
const secretKey = STRIPE_SK 
const stripe = require('stripe')(secretKey);

export async function createToken(){
  try{
    console.log("Start process")
    const token = await stripe.tokens.create({
      card: {
        number: '4242424242424242',
        exp_month: 12,
        exp_year: 2022,
        cvc: '314',
      },
    });
    console.log("Token - ", token)
    return token
  }catch(err){
    console.log(err)
    throw err
  }
}

i want to get a token but i am not getting anything. There is no mistake and no success

the result of the function in the console

Upvotes: 0

Views: 452

Answers (1)

soma
soma

Reputation: 2184

I just copy-pasted the content of your function and it worked: I saw a card token in my logs. When running this code, you need to make sure that:

  • You are actually calling your function somewhere in your backend code.
  • You are looking at logs in the right place. Your screenshot looks like a browser console where you’ll only see frontend logs. For backend logs, you should be looking at the terminal where you started your node.js server.
  • And that you are using your test API key (and not the live one) since you are currently not processing real cards.

That being said, I would not recommend using this code because sending raw card numbers has PCI compliance issues. Instead you should follow this Stripe guide to accept payments.

Upvotes: 1

Related Questions