· 5 years ago · Jul 06, 2020, 11:20 AM
1// Set your secret key. Remember to switch to your live secret key in production!
2// See your keys here: https://dashboard.stripe.com/account/apikeys
3
4const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
5
6//consider this a api which get userId and productId and quantity in params, below are dummy values
7let userId = "xyz";
8let quantity = 1;
9let productDetail = getProductFromDbById("xyz"); //productId comes from param GET or POST
10
11function getProductFromDbById(id){
12 return {
13 id: 111,
14 price: 100,
15 name: "T-Shirt",
16 };
17}
18
19
20const session = await stripe.checkout.sessions.create({
21 payment_method_types: ['card'],
22 line_items: [{
23 price_data: {
24 currency: 'usd',
25 product_data: {
26 name: productDetail.name,
27 },
28 unit_amount: productDetail.price * 100,
29 },
30 quantity: quantity,
31 }],
32 mode: 'payment',
33 success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}&userId='+userId+"&productId="+productDetail.id+"&quantity="+quantity,
34 cancel_url: 'https://example.com/cancel',
35});
36
37
38//now you have to just return the id from session in response
39//like
40
41response.send({"id": session.id})