-
Notifications
You must be signed in to change notification settings - Fork 3
/
createAndPayStripeOrder.js
59 lines (57 loc) · 1.54 KB
/
createAndPayStripeOrder.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
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
export function handler(event, context, callback) {
const { item, description, currency, token } = JSON.parse(event.body);
Promise.resolve()
.then(() => {
return stripe.orders.create({
email: token.email,
currency: currency,
items: [
item,
{
type: "sku",
parent: "sku_DjLy6j1CO2JWST",
quantity: 1
}
],
shipping: {
address: {
city: token.card.address_city,
country: token.card.address_country,
line1: token.card.address_line1,
line2: token.card.address_line2,
postal_code: token.card.address_zip,
state: token.card.address_state
},
name: token.card.name
}
});
})
.then(order => {
console.log("order created", order.id);
return stripe.orders.pay(order.id, {
source: token.id
});
})
.then(payment => {
console.log("payment created", payment.id);
return stripe.charges.update(payment.charge, {
receipt_email: token.email,
description: description
});
})
.then(charge => {
console.log("charge updated", charge.id);
callback(null, {
statusCode: 200,
body: JSON.stringify(charge)
});
})
.catch(error => {
console.log("Error", error.message);
callback(null, {
statusCode: 500,
body: JSON.stringify(error)
});
});
}