-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
181 lines (160 loc) · 5.01 KB
/
index.js
File metadata and controls
181 lines (160 loc) · 5.01 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
const randomBytes = require('crypto').randomBytes;
const stripe = require('stripe')('sk_test_51JjocdDQkELcVjKd6ERbHJLHnnLnZe6eKSFJYovfJF7UpSzRybYkIPrTv22EG4byEBZAhkLTteqLhqURGLb8wGv000rb3kE2AK');
exports.handler = async (event, context, callback) => {
// const orderId = toUrlString(randomBytes(16));
console.log('Received event: ', event);
// The body field of the event in a proxy integration is a raw string.
// In order to extract meaningful values, we need to first parse this string
// into an object. A more robust implementation might inspect the Content-Type
// header first and use a different parsing strategy based on that value.
const requestBody = JSON.parse(event.body);
if (requestBody.checkoutCategory == "CheckoutSession") {
// Stripe checkout session
const session = await stripe.checkout.sessions.create({
payment_method_types: [
'card',
'ideal',
'giropay',
'bancontact'
],
line_items: [
{
price_data: {
currency: 'eur',
product_data: {
name: 'Unicorn ride',
},
unit_amount: 100,
},
quantity: 1,
},
],
mode: 'payment',
success_url: 'https://www.dogfoodidea.com/success',
cancel_url: 'https://www.dogfoodidea.com/',
});
console.log(session);
callback(null, {
statusCode: 201,
body: JSON.stringify(session),
headers: {
'Access-Control-Allow-Origin': '*',
},
});
} else if (requestBody.checkoutCategory == "CheckoutSessionSubscription") {
// const portalSession = await stripe.billingPortal.sessions.create({
// customer: customerId,
// return_url: 'https://www.dogfoodidea.com/',
// });
const prices = await stripe.prices.list({
lookup_keys: ["WildRydesPremium"],
expand: ['data.product'],
});
const session = await stripe.checkout.sessions.create({
payment_method_types: [
'card',
],
billing_address_collection: 'auto',
line_items: [
{
price: prices.data[0].id,
// For metered billing, do not pass quantity
quantity: 1,
},
],
mode: 'subscription',
success_url: 'https://www.dogfoodidea.com/success',
cancel_url: 'https://www.dogfoodidea.com/',
});
console.log(session);
callback(null, {
statusCode: 201,
body: JSON.stringify(session),
headers: {
'Access-Control-Allow-Origin': '*',
},
});
} else if (requestBody.checkoutCategory == "PaymentElement") {
const paymentIntent = await stripe.paymentIntents.create({
amount: 100,
currency: "eur",
payment_method_types: [
"giropay",
"eps",
"p24",
"sofort",
"sepa_debit",
"card",
"bancontact",
"ideal",
],
});
callback(null, {
statusCode: 201,
// body: JSON.stringify(session),
body: JSON.stringify({
clientSecret: paymentIntent.client_secret,
}),
headers: {
'Access-Control-Allow-Origin': '*',
},
});
} else if (requestBody.checkoutCategory == "PaymentElementSubscription") {
// Create a new customer object
const customer = await stripe.customers.create({
email: "diptarko.b9@gmail.com",
});
console.log(customer);
const prices = await stripe.prices.list({
lookup_keys: ["WildRydesPremium"],
expand: ['data.product'],
});
let output;
try {
// Create the subscription. Note we're expanding the Subscription's
// latest invoice and that invoice's payment_intent
// so we can pass it to the front end to confirm the payment
const subscription = await stripe.subscriptions.create({
customer: customer.id,
items: [{
price: prices.data[0].id,
}],
payment_behavior: 'default_incomplete',
expand: ['latest_invoice.payment_intent'],
});
output = {
subscriptionId: subscription.id,
clientSecret: subscription.latest_invoice.payment_intent.client_secret,
};
} catch (error) {
output = { error: { message: error.message } };
}
callback(null, {
statusCode: 201,
body: JSON.stringify(output),
headers: {
'Access-Control-Allow-Origin': '*',
},
});
}
};
function toUrlString(buffer) {
return buffer.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
function errorResponse(errorMessage, requestId, callback) {
callback(null, {
statusCode: 500,
body: JSON.stringify({
Error: errorMessage,
Reference: requestId,
}),
headers: {
'Access-Control-Allow-Origin': '*',
},
});
}