-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathServer.js
More file actions
394 lines (341 loc) · 11.3 KB
/
Copy pathServer.js
File metadata and controls
394 lines (341 loc) · 11.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import express from "express";
import { fileURLToPath } from "url";
import path from "path";
import { dirname } from "path";
import cors from "cors";
import dotenv from "dotenv";
import commerceTools from "./CommerceToolsHelper.js";
import Stripe from "stripe";
import { version } from "os";
const app = express();
app.use(express.json());
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
app.use(express.static(path.join(__dirname, "client/build")));
app.use("/confirm", express.static(path.join(__dirname, "client/build")));
app.use(cors());
dotenv.config();
// Don't put any keys in code. Use an environment variable (as shown
// here) or secrets vault to supply keys to your integration.
//
// See https://docs.stripe.com/keys-best-practices and find your
// keys at https://dashboard.stripe.com/apikeys.
const STRIPE_KEY = process.env.REACT_APP_SK;
const STRIPE_ADMIN = process.env.REACT_APP_ADMIN;
const PORT = process.env.REACT_APP_PORT;
const BASE_URL = process.env.REACT_APP_BASE_URL;
const stripe = new Stripe(STRIPE_KEY);
/* ------ SETUP WEBHOOK ON START------ */
const WEBHOOK_URL = BASE_URL + "/events";
const WEBHOOK_EVENTS = [
"payment_intent.payment_failed",
"payment_intent.succeeded",
"charge.refunded",
"charge.dispute.created",
];
const webhookEndpoints = await stripe.webhookEndpoints.list();
const existingWebhook = webhookEndpoints.data.find(
({ url }) => url === WEBHOOK_URL
);
if (existingWebhook) {
//Add any missing events if webhook created
await stripe.webhookEndpoints.update(existingWebhook.id, {
enabled_events: WEBHOOK_EVENTS,
});
console.log("Existing Webhook Found!");
} else {
try {
//Create new webhook if none found
const webhookEndpoint = await stripe.webhookEndpoints.create({
url: WEBHOOK_URL,
enabled_events: WEBHOOK_EVENTS,
});
if (webhookEndpoint) {
console.log("Created Webhook!");
}
} catch (e) {
console.log("ERROR creating webhook: " + e.message);
console.log(
'NOTE: If your REACT_APP_BASE_URL is a local address you may receive a "URL must be publicly accessible" error, this is expected. Please refer to the Testing Webhooks section in the README.md'
);
}
}
/* ------ BUSINESS MODEL ------ */
app.get("/settings", async (req, res) => {
const account = await stripe.accounts.retrieve(STRIPE_ADMIN);
let icon = false;
if (account.settings.branding.logo) {
const iconFile = await stripe.files.retrieve(
account.settings.branding.icon
);
icon = iconFile.links.data[0].url || false;
}
res.send({
shop_name: account.settings.dashboard.display_name,
icon: icon,
primary_color: account.settings.branding.primary_color,
});
});
app.get("/products/:currency", async (req, res) => {
const currency = req.params.currency;
const ctProducts = await commerceTools.getProducts();
res.send(ctProducts.results);
});
/* ------ GET CUSTOMER BY EMAIL ------ */
app.get("/customer/:email?", async (req, res) => {
const customers = await commerceTools.getCustomerByEmail(req.params.email);
if (req.params.email === undefined || customers.total === 0)
res.send({
id: "",
name: "",
address: "",
city: "",
country: "",
});
else {
res.send({
id: customers.results[0].id,
name: `${customers.results[0].firstName} ${customers.results[0].lastName}`,
addressId: customers.results[0].addresses[0].id,
address: `${customers.results[0].addresses[0].streetName}`,
city: customers.results[0].addresses[0].city,
country: customers.results[0].addresses[0].country,
});
}
});
/* ------ GET CART------ */
app.get("/cart/:cartId?", async (req, res) => {
res.send(await commerceTools.getCart(req.params.cartId));
});
/* ------ CREATE CART ------ */
app.post("/cart", async (req, res) => {
res.send(await commerceTools.createCart());
});
/* ------ ADD CART LINE ITEM ------ */
app.post("/cart/line-item", async (req, res) => {
const cartId = req.body.cartId;
const productId = req.body.productId;
const variantId = req.body.variantId;
const version = req.body.version;
res.send(
await commerceTools.cartAddLineItem(cartId, productId, variantId, version)
);
});
/* ------ ADD CUSTOMER TO CART------ */
app.post("/cart/customer", async (req, res) => {
const cartId = req.body.cartId;
const customerId = req.body.customerId;
res.send(await commerceTools.cartAddCustomer(cartId, customerId));
});
/* ------ CREATE CUSTOMER ------ */
app.post("/customer", async (req, res) => {
const payload = {
email: req.body.email,
name: req.body.name,
address: {
line1: req.body.address,
city: req.body.city,
country: req.body.country,
},
shipping: {
name: req.body.name,
address: {
line1: req.body.address,
city: req.body.city,
country: req.body.country,
},
}
};
let stripeCustomer = await stripe.customers.create(payload);
payload.cartId = req.body.cartId;
res.send(await commerceTools.createCustomer(payload, stripeCustomer.id));
});
/* ------ HOSTED CHECKOUT ------ */
app.post("/create-checkout-session", async (req, res) => {
const cart = req.body.cart;
const ctCustomerId = req.body.customer;
const ctCustomer = await commerceTools.getCustomer(ctCustomerId);
const currency = req.body.currency;
const items = [];
let summary = "";
const order = await commerceTools.createOrder(cart, "Open");
cart.lineItems.forEach((item) => {
const price = item.price.value.centAmount;
items.push({
price_data: {
currency: currency,
unit_amount: price,
product_data: {
name: item.name["en-US"],
images: [item.variant.images[0].url],
},
},
quantity: 1,
});
summary += item.id + " [" + item.name["en-US"] + "] ";
});
try {
const payload = {
mode: "payment",
customer: ctCustomer.externalId,
line_items: items,
success_url: BASE_URL + "/confirm?checkout_session={CHECKOUT_SESSION_ID}",
cancel_url: BASE_URL,
metadata: { summary: summary },
payment_intent_data:{
metadata:{
summary: summary,
commerceToolsCartId: cart.id,
commerceToolsOrderId: order.id,
}
},
shipping_address_collection: {
allowed_countries: [],
},
customer_update: {
shipping: "auto",
},
};
if (currency === "usd") {
payload.payment_method_types = ["card", "afterpay_clearpay"];
payload.shipping_address_collection.allowed_countries = ["US"];
}
if (currency === "eur") {
payload.payment_method_types = ["card", "sofort", "giropay"];
payload.shipping_address_collection.allowed_countries = [
"FR",
"NL",
"GB",
"DE",
];
}
if (currency === "gbp") {
payload.payment_method_types = ["card", "bacs_debit"];
payload.payment_intent_data.setup_future_usage = "off_session";
payload.shipping_address_collection.allowed_countries = ["GB"];
}
//need to use old api version as the new one doesn't create payment intent when creating checkout session.
//https://stripe.com/docs/upgrades#2022-08-01
const stripe2020 = new Stripe(STRIPE_KEY, {apiVersion: '2020-08-27'});
const session = await stripe2020.checkout.sessions.create(payload);
// Copy metadata to PI
const pi = await stripe.paymentIntents.retrieve(session.payment_intent)
const payment = await commerceTools.createPayment(pi, currency, pi.amount);
commerceTools.updatePaymentState("Authorization", pi);
await commerceTools.addPaymentToOrder(order.id, payment);
res.send({
sessionId: session.id,
url: session.url
});
} catch (e) {
res.status(400);
return res.send({
error: {
message: e.message,
},
});
}
});
// Retrieving a session or a PI to display details on redirect from hosted checkout or from UPE
app.get("/session/:id", async (req, res) => {
const id = req.params.id;
let pi;
if (id.indexOf("cs_") > -1) {
const session = await stripe.checkout.sessions.retrieve(id);
pi = await stripe.paymentIntents.retrieve(session.payment_intent);
} else {
pi = await stripe.paymentIntents.retrieve(id);
}
res.send({
receipt: pi.id,
//email: pi.charges.data[0].billing_details.email
});
});
/* ------ ELEMENTS AND UPE ------ */
app.post("/create-payment-intent", async (req, res) => {
const cart = req.body.cart;
const ctCustomerId = req.body.customer;
const ctCustomer = await commerceTools.getCustomer(ctCustomerId);
const currency = req.body.currency;
const order = await commerceTools.createOrder(cart, "Open");
let total = cart.totalPrice.centAmount;
let summary = "";
cart.lineItems.forEach((item) => {
const price = item.price.value.centAmount;
summary += item.id + " [" + item.name["en-US"] + "] ";
});
const payload = {
amount: total,
currency: currency,
metadata: {
summary: summary,
commerceToolsCartId: cart.id,
commerceToolsOrderId: order.id,
},
capture_method: "automatic",
customer: ctCustomer.externalId,
};
if (currency === "usd") {
payload.payment_method_types = ["card", "afterpay_clearpay"];
}
if (currency === "eur") {
payload.payment_method_types = ["card", "sofort", "giropay"];
}
if (currency === "gbp") {
payload.payment_method_types = ["card", "bacs_debit"];
}
const paymentIntent = await stripe.paymentIntents.create(payload);
const payment = await commerceTools.createPayment(
paymentIntent,
currency,
total
);
commerceTools.updatePaymentState("Authorization", paymentIntent);
await commerceTools.addPaymentToOrder(order.id, payment);
res.send({
clientSecret: paymentIntent.client_secret,
});
});
/* ------ WEBHOOK ENDPOINT ------ */
app.post("/events", async (req, res) => {
// console.log('====================================');
// console.log('webhook event : ', req.body.type);
// console.log('====================================');
if (req.body.type == "payment_intent.succeeded") {
const paymentIntent = await stripe.paymentIntents.retrieve(
req.body.data.object.id
);
const orderstatus = await commerceTools.updateOrder(
paymentIntent.metadata.commerceToolsOrderId,
"Paid",
paymentIntent
);
const paymentstatus = await commerceTools.updatePaymentState("Charge", paymentIntent);
}
if (req.body.type == "payment_intent.payment_failed") {
const paymentIntent = await stripe.paymentIntents.retrieve(
req.body.data.object.id
);
commerceTools.updateOrder(
paymentIntent.metadata.commerceToolsOrderId,
"Failed",
paymentIntent
);
commerceTools.updatePaymentState("CancelAuthorization", paymentIntent);
}
if (req.body.type == "charge.refunded") {
const paymentIntent = await stripe.paymentIntents.retrieve(
req.body.data.object.payment_intent
);
paymentIntent.amount = req.body.data.object.amount_refunded;
commerceTools.updatePaymentState("Refund", paymentIntent);
}
if (req.body.type == "charge.dispute.created") {
const paymentIntent = await stripe.paymentIntents.retrieve(
req.body.data.object.payment_intent
);
commerceTools.updatePaymentState("Chargeback", paymentIntent);
}
res.sendStatus(200);
});
app.listen(PORT);