-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (174 loc) · 5.66 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
const h = require('vhtml')
const uuid = require('uuid/v4')
const crypto = require('crypto')
const fetch = require('node-fetch')
const html = require('htm').bind(h)
const { stringify, parse } = require('qs')
const { router, get, post } = require('microrouter')
const { send, json, text, buffer } = require('micro')
const base = 'https://kvdb.io/B3PXaY12sAj4ncmE7sjL6c'
const setKey = async (key, value) =>
await fetch(`${base}/${key}`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: value,
})
const getKey = async key =>
await fetch(`${base}/${key}`).then(res => (res.status == 200 ? res.text() : null))
const deleteKey = async key => await fetch(`${base}/${key}`, { method: 'DELETE' })
const key = 'Threeds2Test60System'
const cardstream_int_url = 'https://test.3ds-pit.com/direct/'
const cardstream_merchant_id_3ds = '100856'
const transactionUnique = uuid()
const new_test_data = (callback, userAgent, ref = null, parsedRes = null) => ({
merchantID: cardstream_merchant_id_3ds,
action: 'SALE',
type: 1,
currencyCode: 826,
countryCode: 826,
amount: '1.01',
cardNumber: '4929421234600821',
cardCVV: '356',
cardExpiryMonth: '12',
cardExpiryYear: '19',
customerName: 'Test Customer',
customerEmail: '[email protected]',
customerAddress: '16 Test Street',
customerPostCode: 'TE15 5ST',
orderRef: 'Test purchase',
threeDSRedirectURL: callback,
deviceType: 'desktop',
deviceChannel: 'browser',
deviceTimeZone: '0',
deviceCapabilities: 'javascript',
deviceScreenResolution: '1920x1080x1',
deviceOperatingSystem: 'macos',
deviceIdentity: userAgent,
deviceAcceptContent: 'application/json',
deviceAcceptEncoding: 'application/json',
deviceAcceptLanguage: 'en-GB,en-US;q=0.9,en;q=0.8',
deviceAcceptCharset: 'utf-8',
remoteAddress: '127.0.0.1',
transactionUnique,
threeDSRef: ref,
...(parsedRes && { threeDSResponse: parsedRes }),
})
// coming from https://github.com/cardstream/nodejs-direct-sample/blob/master/Cardstream.js
const generateBody = (SIGNATURE_KEY, obj) => {
var items = Object.keys(obj)
var string = ''
items.sort()
const sortedObj = items.reduce((acc, k) => ({ ...acc, [k]: obj[k] }), {})
string = stringify(sortedObj)
// ss = (parent = null) => item => {
// const t = parent ? obj[parent][item] : obj[item]
// if (t || item !== 'threeDSRef') {
// if (t && typeof t === 'object') Object.keys(t).forEach(ss(item))
// else
// string += parent
// ? `${parent}[${item}]=${encodeURIComponent(t)}&`
// : `${item}=${encodeURIComponent(t)}&`
// }
// }
// items.forEach(ss())
// string = string.slice(0, -1)
string = string.replace(/\(/g, '%28')
string = string.replace(/\)/g, '%29')
string = string.replace(/%20/g, '+')
return (
string +
'&signature=' +
crypto
.createHash('SHA512')
.update(string + SIGNATURE_KEY)
.digest('hex')
)
}
const callc = params => {
const body = generateBody(key, params)
console.log('TCL: body', body)
return fetch(cardstream_int_url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': body.length,
},
body,
})
.then(res => res.buffer())
.then(buffer => buffer.toString('utf8'))
.then(str => parse(str))
}
const ACSForm = async (res, { threeDSURL, threeDSRequest }) => {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
res.end(html`
<html>
<head>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<div>
<h1>HTM λ News</h1>
<p>We need 3D secure auth from you.</p>
<form name="acs" action="${threeDSURL}" method="POST">
${Object.entries(threeDSRequest).map(
([k, v]) =>
html`
<input
type="hidden"
name="${decodeURIComponent(k)}"
value="${decodeURIComponent(v)}"
/>
`,
)}
<button type="submit">Submit</button>
</form>
</div>
<!-- <script>
document.forms.acs.submit()
</script> -->
</body>
</html>
`)
}
const index = async (req, res) => {
const parsedRes = parse(await text(req))
console.log('TCL: index -> parsedRes', parsedRes)
const postData =
Object.keys(parsedRes).length && parsedRes.threeDSMethodData
? new Buffer(parsedRes.threeDSMethodData, 'base64').toString('utf8')
: null
const callback = `http://${req.headers.host}`
const ref = await getKey('ref')
const tt = new_test_data(
callback,
req.headers['user-agent'],
ref,
postData ? JSON.parse(postData) : null,
)
console.log('TCL: index -> url', req.url)
console.log('TCL: index -> parse(postData)', JSON.parse(postData))
console.log('TCL: index -> ref', ref)
console.log('TCL: index -> tt', tt)
await callc(tt)
.then(async data => {
console.log('TCL: index -> data', data)
console.log('TCL: index -> responseCode', data.responseCode)
console.log('TCL: index -> threeDSRef', data.threeDSRef)
if (data.responseCode === '65802') {
if (!ref) await setKey('ref', data.threeDSRef)
ACSForm(res, { threeDSURL: data.threeDSURL, threeDSRequest: data.threeDSRequest })
} else {
console.log('TCL: index -> responseMessage', data.responseMessage)
deleteKey('ref')
send(res, 200, data)
}
})
.catch(e => send(res, 500, e.message))
}
const favicon = (_, res) => send(res, 200, 'OK')
module.exports = router(post('/*', index), get('/favicon.ico', favicon), get('/*', index))