-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (130 loc) · 5.13 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
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
const fs = require('fs');
const path = require('path');
const Stripe = require('stripe');
let stripe = null;
if (process.env.STRIPE_MOCK_TEST_MODE === 'true') {
stripe = new Stripe('sk_test_xyz', {
host: '127.0.0.1',
protocol: 'http',
port: 12111
});
} else {
stripe = Stripe(process.env.STRIPE_KEY);
}
const bodyParser = require('body-parser');
const stripeWebhookEndpoint = '/api/v1/stripe-checkout/webhook';
module.exports = {
options: {
alias: 'stripeCheckout',
i18n: {
ns: 'stripeCheckout',
browser: true
},
csrfExceptions: [ stripeWebhookEndpoint ]
},
bundle: {
directory: 'modules',
modules: getBundleModuleNames()
},
init(self) {
const groupName = 'stripe';
const itemsToAdd = [ 'stripe-checkout/session' ];
const existingStripeGroup = self.apos.adminBar.groups.find(
group => group.name === groupName
);
const newStripeGroup = {
name: groupName,
label: 'Stripe',
items: itemsToAdd
};
if (existingStripeGroup) {
existingStripeGroup.items = existingStripeGroup.items.concat(itemsToAdd);
} else {
self.apos.adminBar.groups.push(newStripeGroup);
}
self.apos.app.use(stripeWebhookEndpoint, bodyParser.raw({ type: '*/*' }));
},
routes(self) {
return {
post: {
[stripeWebhookEndpoint]: async function (req, res, next) {
async function handleCheckoutSession(req, res) {
try {
if (!req.headers['stripe-signature']) {
throw new Error('Stripe signature was not provided in the header.');
}
const event = await constructStripeEvent(req);
if (event.type === 'checkout.session.completed') {
const checkoutSession = await stripe.checkout.sessions.retrieve(event.data.object.id);
const checkoutSessionInstance = createCheckoutSessionInstance(checkoutSession);
await insertCheckoutSessionInstance(checkoutSessionInstance);
}
return res.status(200).end();
} catch (error) {
console.error('Error handling checkout session:', error);
return res.status(500).send(error.message);
}
}
async function constructStripeEvent(req) {
if (process.env.STRIPE_MOCK_TEST_MODE === 'true') {
return {
type: 'checkout.session.completed',
data: { object: { id: 'cs_xyz' } }
};
} else {
return stripe.webhooks.constructEvent(req.body, req.headers['stripe-signature'], process.env.STRIPE_WEBHOOK_ENDPOINT_SECRET);
}
}
function createCheckoutSessionInstance(checkoutSession) {
const checkoutSessionInstance = self.apos.stripeCheckoutSession.newInstance();
checkoutSessionInstance.slug = checkoutSession.id;
checkoutSessionInstance.stripeCheckoutSessionObject = checkoutSession;
checkoutSessionInstance.stripeCheckoutSessionObject.created_timestamp = new Date(checkoutSession.created * 1000).toISOString();
checkoutSessionInstance.stripeCheckoutSessionObject.amount_subtotal = (checkoutSession.amount_subtotal / 100).toFixed(2);
checkoutSessionInstance.stripeCheckoutSessionObject.amount_total = (checkoutSession.amount_total / 100).toFixed(2);
return checkoutSessionInstance;
}
async function insertCheckoutSessionInstance(checkoutSessionInstance) {
const lineItems = await stripe.checkout.sessions.listLineItems(checkoutSessionInstance.slug, { limit: 99 });
checkoutSessionInstance.stripeCheckoutSessionObject.line_items_quantity_total = lineItems.data.reduce((sum, item) => sum + item.quantity, 0);
lineItems.data.forEach(item => {
[ 'amount_total', 'amount_subtotal', 'amount_discount', 'amount_tax' ].forEach(field => {
item[field] = (item[field] / 100).toFixed(2);
});
item.price.unit_amount = (item.price.unit_amount / 100).toFixed(2);
});
checkoutSessionInstance.stripeCheckoutSessionLineItemsObject = lineItems;
await self.apos.stripeCheckoutSession.insert(req, checkoutSessionInstance, { permissions: false });
}
await handleCheckoutSession(req, res);
}
}
};
},
apiRoutes(self) {
return {
post: {
'/api/v1/stripe-checkout/sessions/create': async function (req) {
try {
return await stripe.checkout.sessions.create(req.body);
} catch (error) {
req.res.status(500).send(error);
}
}
}
};
}
};
function getBundleModuleNames() {
return fs.readdirSync(path.resolve(__dirname, 'modules')).reduce((result, dir) => {
if (dir.includes('stripe') && !(/(^|\/)\.[^/.]/g).test(dir)) {
const subdirectories = fs.readdirSync(path.resolve(__dirname, `modules/${dir}`));
subdirectories.forEach((subdir) => {
if (!(/(^|\/)\.[^/.]/g).test(subdir)) {
result.push(`${dir}/${subdir}`);
}
});
}
return result;
}, []);
}