Ad

I Can't Create Stripe Token Use 'Stripe.tokens.create'

- 1 answer

I creating Stripe Backend use Nodejs.

I want to create customer credit card use API

I enter all the card information necessary for registration in the parameter and access the API http://localhost:5000/stripe-create-card, but the return token is null.

I wrote the code according to the reference, so I think there is no mistake.

How can I get a token?

const express = require('express');
const stripe = require('stripe')('MYKEY');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

app.set('port', (process.env.PORT || 5000));
//app.listen(3000);

app.post('/create_customer',(req,res) => {
    const email = req.body.email;
    console.log(email);
    stripe.customers.create({
        email : email
   },function(err, customer) {
        // asynchronously called
        res.send(customer);
        console.log(customer);
    })
});

app.post('/body',(req,res) =>{
   //res.setHeader('Content-Type', 'text/plain');
    console.log(req.body);
    console.log(req.body.email);
});

app.post('/stripe-create-card', (req, res)=>  {
    const customer_id = req.body.customer_id;
    const card_num = req.body.card_num;
    const card_month = req.body.card_month;
    const card_year = req.body.card_year;
    const card_cvc = req.body.card_cvc;

    console.log(customer_id,card_num,card_month,card_year,card_cvc);

    stripe.tokens.create({
        card: {
            "number": '4242424242424242',
            "exp_month": 12,
            "exp_year": 2020,
            "cvc": "123"
        }
    }, function(err, token) {
        // asynchronously called
        console.log(token);
        const params = {
            source: token.id
        };
        stripe.customers.createSource(customer_id, params, function(err, card) {
            console.log(card);
        });
    });
});


app.listen(app.get('port'), function () {
    console.log('Node app is running on port', app.get('port'))
});
Ad

Answer

I got a token, but this was from the client side by using the following code:

<form action="/orders/payment/confirmed" method="POST">
        <div class="formError"></div>
        <div class="formSuccess">Your payment has been successfully submitted, please check your email</div>
        <div>
            <script src="https://checkout.stripe.com/checkout.js"></script>
            <button id="paymentData" class="cta blue" >Purchase</button>            
        </div>
    </form>

After that configure the values:

    const handler = StripeCheckout.configure({
    key: 'your public key',
      image: 'your image path',
      locale: 'auto',
      token: token => {
      //Here you receive your token id as token.id, so you can make a request 
      //to your server with this key
    })

After that you can use the token id to process the payment.

Good luck.

Ad
source: stackoverflow.com
Ad