forked from RevenueCat-Samples/stripe-no-website-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (93 loc) · 3.08 KB
/
app.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
// --- Set these values in your environment variables
const PORT = process.env.PORT;
const TEST_MODE = process.env.TEST_MODE == 'true';
const STRIPE_KEY = process.env.STRIPE_KEY;
const STRIPE_KEY_TEST = process.env.STRIPE_KEY_TEST;
const RC_API_KEY = process.env.RC_API_KEY;
const SUCCESS_URL = process.env.SUCCESS_URL;
const CANCEL_URL = process.env.CANCEL_URL;
// -------------------
// --- Imports
const express = require('express');
const createError = require('http-errors');
const path = require('path');
// -------------------
// --- Axios request library
const axios = require('axios').create({
baseURL: 'https://api.revenuecat.com/v1',
headers: { 'X-Platform': 'stripe', 'Authorization': `Bearer ${RC_API_KEY}` }
});
// -------------------
// --- Express app setup
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// -------------------
// --- Landing Page
app.get('/', function (req, res, next) {
res.render('landing', { test_mode: TEST_MODE });
});
// -------------------
// --- Endpoints
app.get('/purchase/:userId/:productId', async function (req, res, next) {
// - Render our redirection page with context
res.render('redirectToCheckout', {
user_id: req.params.userId,
key: (TEST_MODE == true ? STRIPE_KEY_TEST : STRIPE_KEY),
success_url: SUCCESS_URL,
cancel_url: CANCEL_URL,
product_id: req.params.productId
});
});
app.post('/webhooks/stripe', async function (req, res, next) {
// - Ensure there is a purchase object
let purchaseObject = req.body?.data?.object
if (purchaseObject) {
// - Get the associated app user ID and token
let userId = purchaseObject.client_reference_id;
let token = purchaseObject.subscription;
// - Print to log
console.log(userId);
console.log(token);
// - Post receipt data to RevenueCat
axios.post('/receipts', {
app_user_id: userId,
fetch_token: token,
attributes: { "stripe_customer_id": { value: purchaseObject.customer } }
})
.then(function (response) {
// TODO: ensure a successful response from RevenueCat, retry if necessary
})
.catch(function (error) {
// TODO: error- retry if necessary
});
// - Respond to Stripe to let them know we got the webhook
res.status(200).json();
} else {
// - No purchase object found
console.log("No purchase found in webhook body:")
console.log(req.body);
res.status(400).json();
}
});
// -------------------
// --- Catch generic errors
app.use(function (req, res, next) {
next(createError(404));
});
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = TEST_MODE ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// -------------------
// --- Start listening
app.listen(PORT, function () {
console.log(`App is listening on port ${PORT} - Test Mode: ${TEST_MODE}`);
});
// -------------------