-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.ts
150 lines (123 loc) · 4.23 KB
/
app.ts
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
import path from 'path';
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import Stripe from 'stripe';
import dotenv from 'dotenv';
import Checkout from '../lib';
dotenv.config({ path: path.join(__dirname, '.env') });
const STRIPE_PUBLIC_KEY = process.env.STRIPE_PUBLIC_KEY;
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
const PLAN_ID = process.env.PLAN_ID;
const PORT = process.env.PORT || 3000;
const TAX_RATES = { eu: 'txr_1HHlhiATZ14HzUt2zBY1XkBb' };
const TAX_ORIGIN = 'SE';
if (!STRIPE_PUBLIC_KEY || !STRIPE_SECRET_KEY) {
throw new Error('You must add your stripe public and secret key.');
}
if (!PLAN_ID) {
throw new Error('You must define a plan id for testing purposes.');
}
const checkout = Checkout(new Stripe(STRIPE_SECRET_KEY, { apiVersion: '2020-08-27' }));
// Setup express
const app = express();
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.set('views', __dirname);
app.set('view engine', 'pug');
app.use('/js/checkout.js', express.static(path.join(__dirname, '../dist/client/checkout.js')));
// Implement endpoint for VAT number validation
app.get('/validateVatNumber', (req, res) => {
checkout.validateVatNumber(req.query.q as string).then(valid => {
res.status(valid ? 200 : 400).json({ valid });
});
});
// Implement endpoint for coupon validation
app.get('/validateCoupon', (req, res) => {
checkout.validateCoupon(req.query.q as string).then(valid => {
res.status(valid ? 200 : 400).json({ valid });
});
});
// Landing page showing current subscription status and receipts.
app.get('/', async (req, res) => {
res.render('index', {
customer: req.query.customer || '',
sub: await checkout.getSubscription(req.query.customer as string),
receipts: await checkout.getReceipts(req.query.customer as string),
stripePublicKey: STRIPE_PUBLIC_KEY,
});
});
// Update payment information for an existing subscription
app.get('/card', async (req, res) => {
const sub = await checkout.getSubscription(req.query.customer as string);
res.render('checkout', {
checkout: {
stripePublicKey: STRIPE_PUBLIC_KEY,
clientSecret: await checkout.getClientSecret(),
titleText: 'Update card details',
actionText: 'Save',
prefill: sub,
showCountry: true,
}
});
});
app.post('/card', async (req, res) => {
try {
await checkout.manageSubscription(req.query.customer as string, {
email: req.body.email,
name: req.body.name,
country: req.body.country,
postcode: req.body.postcode,
paymentMethod: req.body.paymentMethod,
taxRates: TAX_RATES,
taxOrigin: TAX_ORIGIN,
});
res.redirect('/?customer=' + req.query.customer);
} catch (err) {
res.json({ error: err.message });
}
});
// Upgrade subscription for new customers
app.get('/upgrade', async (req, res) => {
const sub = await checkout.getSubscription(req.query.customer as string);
res.render('checkout', {
checkout: {
stripePublicKey: STRIPE_PUBLIC_KEY,
clientSecret: await checkout.getClientSecret(),
headerText: 'Upgrade to Gold',
titleText: '$10.00 per month',
showCoupon: true,
actionText: 'Upgrade',
couponValidationUrl: '/validateCoupon',
prefill: sub,
showCountry: true,
}
});
});
app.post('/upgrade', async (req, res) => {
try {
const stripeCustomerId = await checkout.manageSubscription(req.query.customer as string, {
plan: PLAN_ID,
email: req.body.email,
name: req.body.name,
country: req.body.country,
postcode: req.body.postcode,
coupon: req.body.coupon,
paymentMethod: req.body.paymentMethod,
taxRates: TAX_RATES,
taxOrigin: TAX_ORIGIN,
});
res.redirect('/?customer=' + stripeCustomerId);
} catch (err) {
res.json({ error: err.message });
}
});
app.get('/cancel', async (req, res) => {
await checkout.cancelSubscription(req.query.customer as string);
res.redirect('/?customer=' + req.query.customer);
});
app.get('/reactivate', async (req, res) => {
await checkout.reactivateSubscription(req.query.customer as string);
res.redirect('/?customer=' + req.query.customer);
});
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}!`));