-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (57 loc) · 1.7 KB
/
server.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
import dotenv from 'dotenv';
import express from 'express';
import stripe from 'stripe';
// Load variables
dotenv.config();
// Start Server
const app = express();
app.use(express.static('public'));
app.use(express.json());
// Home Route
app.get('/', (req, res) => {
res.sendFile('register_form.php', {root: 'public'});
});
// Success
app.get('/success', (req, res) => {
res.sendFile('success.html', {root: 'public'});
});
// Cancel
app.get('/cancel', (req, res) => {
res.sendFile('cancel.html', {root: 'public'});
});
//Stripe
let stripeGateway = stripe(process.env.stripe_api);
let DOMAIN = process.env.DOMAIN;
app.post('/stripe-checkout', async (req, res) => {
const lineItems = req.body.items.map((item) => {
const unitAmount = parseInt(item.price.replace(/[^0-9.-]+/g, '') * 100);
console.log('item-price:', item.price);
console.log('unitAmount:', unitAmount);
return {
price_data: {
currency: 'usd',
product_data: {
name: item.title,
images: [item.productImg],
},
unit_amount: unitAmount,
},
quantity: item.quantity,
};
});
console.log('lineItems:', lineItems);
// Create Checkout Session
const session = await stripeGateway.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'payment',
success_url: `${DOMAIN}/success`,
cancel_url: `${DOMAIN}/cancel`,
line_items: lineItems,
// Asking Address In Stripe Checkout Page
billing_address_collection: 'required',
});
res.json(session.url);
});
app.listen(3000, () => {
console.log('listening on port 3000;');
});