-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (60 loc) · 1.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const stripe = require('stripe')('REPLACE_WITH_YOUR_TOKEN');
// helpers
const createUser = async () => {
try {
const customer = await stripe.customers.create({
email: 'jdoe@example.com',
});
console.log(customer);
return customer;
} catch (error) {
console.error(error);
}
};
const addCreditCard = async (user, card) => {
try {
const paymentMethod = await stripe.paymentMethods.create({
type: 'card',
card,
});
console.log(paymentMethod);
const attached = await stripe.paymentMethods.attach(paymentMethod.id, {
customer: user.id,
});
console.log(attached);
return paymentMethod;
} catch (error) {
console.error(error);
}
};
const processPayment = async (user, card) => {
try {
const paymentIntent = await stripe.paymentIntents.create({
amount: 1250,
customer: user.id,
currency: 'usd',
payment_method: card.id,
});
console.log(paymentIntent);
} catch (error) {
console.error(error);
}
};
(async () => {
try {
console.log('Stripe demo');
// create user
const user = await createUser();
// add credit card to user
const creditCard = await addCreditCard(user, {
number: '4242424242424242',
exp_month: 9,
exp_year: 2021,
cvc: '314',
});
// create payment intent for user
await processPayment(user, creditCard);
} catch (e) {
console.error(e);
}
})();