-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (151 loc) · 5.27 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
// import the required modules
const express = require('express')
const fetch = require('node-fetch');
const sqlite3 = require('sqlite3').verbose();
var crypto = require('crypto');
// initialize DB
const db = new sqlite3.Database(':memory:');
const app = express()
// set view engine
app.set('view engine', 'ejs')
// parse application/json
app.use(express.json())
const port = 3000
const epiId = <your-epi-id>; // {String} Merchant's unique 4-part key, which is provided after boarding with the processor
const epiKey = <your-epi-key>
app.get('/', (req, res) => {
res.render('home')
})
// API routes go here
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
app.post('/signup', async (req, res) => {
const { FirstName, LastName, Phone, Email, AccountNumber, ExpirationDate, CVV, PlanType, Amount } = req.body.customerDetails
let today = new Date().getFullYear() + '-' +
String(new Date().getMonth() + 1).padStart(2, '0') + '-' +
String(new Date().getDate()).padStart(2, '0')
// the payload containing the request data
const payload = {
CustomerData: {
FirstName,
LastName,
Phone,
Email,
},
PaymentMethod: {
CreditCardData: {
AccountNumber,
ExpirationDate,
CVV,
}
},
SubscriptionData: {
Amount: 29.99,
Frequency: 'Monthly',
BillingDate: today,
Description: 'premium',
FailureOption: 'Pause'
}
}
// convert the payload to JSON
const payloadJson = JSON.stringify(payload)
// concatenate the API route and the payload
const concatData = '/subscription' + payloadJson
// generate the ePISignature following the instructions in the "How To Authenticate" section of the Recurring Billing API Integration Guide
console.log('epiSignature', ePISignature);
console.log('body:', JSON.stringify(payload));
fetch('https://billing.epxuap.com/subscription', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'EPI-Id': epiId,
'EPI-Signature': ePISignature,
},
body: JSON.stringify(payload)
})
.then(res => res.json()) // expecting a json response
.then(data => {
console.log(data)
db.serialize(() => {
db.run('CREATE TABLE IF NOT EXISTS customerDetails (firstName TEXT, lastName TEXT, phone TEXT, email TEXT, description TEXT, amount TEXT, status TEXT, subscriptionId TEXT)')
const stmt = db.prepare("INSERT INTO customerDetails VALUES (?,?,?,?,?,?,?,?)");
stmt.run(FirstName, LastName, Phone, Email, data.Description, data.Amount, data.Status, data.id)
db.each('SELECT * FROM customerDetails', (err, row) => console.log(row))
})
if(data.VerifyResult.Code === '00'){ // check if the transaction was approved. Code '00' is returned on approval
res.send({
success: true,
id: data.id
})
}
});
})
app.get('/profile', (req, res) => {
const { id } = req.query
let customer
db.serialize(() => {
db.get(`SELECT * FROM customerDetails WHERE subscriptionId = ${id}`, (err, row) => customer = row)
})
const payload = {SubscriptionID: Number(id)}
// convert the payload to JSON
const payloadJson = JSON.stringify(payload)
// concatenate the API route and the payload
const concatData = '/subscription/list' + payloadJson
// generate the ePISignature following the instructions in the "How To Authenticate" section of the Recurring Billing API Integration Guide
fetch('https://billing.epxuap.com/subscription/list', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'EPI-Id': epiId,
'EPI-Signature': ePISignature,
},
body: JSON.stringify(payload)
})
.then(res => res.json()) // expecting a json response
.then(data => {
console.log(data)
res.render('profile', {
customer,
data
})
});
})
app.post('/upgrade', (req, res) => {
const { id, Amount } = req.body
let customer
db.serialize(() => {
db.get(`SELECT * FROM customerDetails WHERE subscriptionId = ${id}`, (err, row) => customer = row)
})
const payload = {
SubscriptionID: Number(id),
SubscriptionData: {
Amount: 29.99,
Frequency: 'Monthly',
FailureOption: 'Pause',
Description: 'premium'
}
}
// convert the payload to JSON
const payloadJson = JSON.stringify(payload)
// concatenate the API route and the payload
const concatData = '/subscription' + payloadJson
// generate the ePISignature following the instructions in the "How To Authenticate" section of the Recurring Billing API Integration Guide
fetch('https://billing.epxuap.com/subscription', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'EPI-Id': epiId,
'EPI-Signature': ePISignature,
},
body: JSON.stringify(payload)
})
.then(res => res.json()) // expecting a json response
.then(data => {
console.log(data)
res.render('profile', {
data,
customer
})
});
})