forked from kimanikevin254/rapyd-checkout-invoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (231 loc) · 7.33 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
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
// import the required dependencies
require('dotenv').config({path: './.env'});
const express = require('express')
const storage = require('node-persist');
var mailgun = require('mailgun-js')({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN});
// import the utility file for making requests to the Rapyd API
const makeRequest = require('./utils').makeRequest;
storage.init()
const app = express()
// set the port
const port = 3000
// variables
const planId = "<your-plan-id>"
//set the render engine
app.set("view engine", "ejs")
// parse application/json
app.use(express.json())
//Add routes here
// Route to query the plan and return plan+product details
app.get('/', async (req, res) => {
try {
const { body: { data } } = await makeRequest('GET', `/v1/plans/${planId}`);
await storage.setItem('productPrice', data.amount)
res.render('product', {
title: data.product.name,
price: data.amount,
description: data.product.description,
image: data.product.images[0]
})
} catch (error) {
console.log(error)
}
})
// Route to receive client details and create a customer on Rapyd
app.post('/', async (req, res) => {
//create a customer using the payment details from the request body
try {
const body = {
name: req.body.customerDetails.name,
email: req.body.customerDetails.email,
phone_number: req.body.customerDetails.phone,
payment_method: {
type: 'us_debit_visa_card',
fields: {
number: req.body.customerDetails.cardNo,
expiration_month: req.body.customerDetails.cardExpMonth,
expiration_year: req.body.customerDetails.cardExpYear,
cvv: req.body.customerDetails.cardCVV
}
}
}
const { body: { data } } = await makeRequest('POST', '/v1/customers', body)
await storage.setItem('customerId', data.id)
await storage.setItem('defaultPaymentMethod', data.default_payment_method)
await storage.setItem('customerEmail', data.email)
res.status(200).json({ success: true })
} catch (error) {
console.log(error)
}
})
// Route to create a checkout page and return the checkout ID to the client
app.get('/checkout', async (req, res) => {
let customerId = await storage.getItem('customerId')
let productPrice = await storage.getItem('productPrice')
try {
const body = {
amount: productPrice,
country: 'US',
currency: 'USD',
customer: customerId,
language: 'en',
expiration: 1704019927,
};
const { body: { data }} = await makeRequest('POST', '/v1/checkout', body);
res.render('checkout', {
checkoutId: data.id
})
} catch (error) {
console.error('Error completing request', error);
}
})
app.get('/verification', async (req, res) => {
let customerId = await storage.getItem('customerId')
let defaultPaymentMethod = await storage.getItem('defaultPaymentMethod')
// Create subscription
try {
const subscriptionBody = {
customer: customerId,
billing: 'pay_automatically',
billing_cycle_anchor: '',
cancel_at_period_end: true,
coupon: '',
days_until_due: null,
payment_method: defaultPaymentMethod,
subscription_items: [
{
plan: planId,
quantity: 1
}
],
tax_percent: 10.5,
plan_token: ''
};
const { body: { data }} = await makeRequest('POST', '/v1/payments/subscriptions', subscriptionBody);
await storage.setItem('subscriptionId', data.id)
// create invoice
try {
let subscriptionId = await storage.getItem('subscriptionId')
const invoiceBody = {
customer: customerId,
billing: 'pay_automatically',
days_until_due: null,
description: '',
due_date: 0,
metadata: {
merchant_defined: true
},
statement_descriptor: '',
subscription: subscriptionId,
tax_percent: '',
currency: 'USD'
};
const { body: { data }} = await makeRequest('POST', '/v1/invoices', invoiceBody);
await storage.setItem('invoiceId', data.id)
console.log(data);
// create invoice items
try {
let customerId = await storage.getItem('customerId')
let invoiceId = await storage.getItem('invoiceId')
let subscriptionId = await storage.getItem('subscriptionId')
const invoiceItemBody = {
currency: 'USD',
customer: customerId,
invoice: invoiceId,
plan: planId,
metadata: {
merchant_defined: true
},
amount: 1150
};
const { body: { data }} = await makeRequest('POST', '/v1/invoice_items', invoiceItemBody);
console.log(data);
} catch (error) {
console.error('Error completing request', error);
}
} catch (error) {
console.error('Error completing request', error);
}
} catch (error) {
console.error('Error completing request', error);
}
// finalize invoice
let invoiceId = await storage.getItem('invoiceId')
try {
const { body: { data }} = await makeRequest(
'POST',
`/v1/invoices/${invoiceId}/finalize`
);
console.log(data);
res.render('verification', {
authLink: data.payment.redirect_url
})
} catch (error) {
console.error('Error completing request', error);
}
})
// Retrieve invoice, line items => return line items to client
app.get('/invoice', async (req, res) => {
let custEmail = await storage.getItem('customerEmail')
let item
let quantity
let subTotal
let tax
let total
let status
// retrieve invoice
try {
let invoiceId = await storage.getItem('invoiceId')
console.log(invoiceId)
const { body: { data }} = await makeRequest(
'GET',
`/v1/invoices/${invoiceId}`
);
console.log(data)
quantity = data.lines.length
subTotal = data.subtotal
tax = data.tax
total = data.total
status = data.status
// retrieve subscription
try {
const subscriptionId = await storage.getItem('subscriptionId')
const { body: { data }} = await makeRequest(
'GET',
`/v1/payments/subscriptions/${subscriptionId}`
);
item = data.subscription_items.data[0].plan.product.name;
// console.log(data.subscription_items.data[0].plan.product)
} catch (error) {
console.error('Error completing request', error);
}
} catch (error) {
console.error('Error completing request', error);
}
// send email
let invoiceId = await storage.getItem('invoiceId')
var data = {
from: `Monthly Booking Services <${process.env.MAILGUN_SENDER_EMAIL}>`,
to: custEmail,
subject: 'Monthly Booking Services Invoice',
html: `<h1>Monthly Booking Services</h1><h3>Your Invoice ID: ${invoiceId}</h3><p>Item: ${item}</p><p>Quantity: ${quantity}</p><p>Tax: ${tax}</p><p>Total: ${total}</p><p>Invoice status: ${status}</p>`
};
mailgun.messages().send(data, function (error, body) {
if(error){
console.log(error)
}
console.log(body);
});
res.render('invoice', {
item,
quantity,
subTotal,
tax,
total,
status,
custEmail
})
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})