-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
161 lines (118 loc) · 3.73 KB
/
server.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
// Add a bunch of packages that you need
const express = require('express');
const fetch = require("node-fetch");
const app = express();
var sha512 = require('js-sha512');
const { response } = require('express');
const { sign } = require('crypto');
// Anything in the "globalpayments" folder will be served as static content in localhost
app.use(express.static('server'));
app.use(express.json()); // adds a built-in JSON parser to Express
app.use(express.urlencoded({
extended: true
}));
app.listen(8080, () =>{
console.log("Running on port 8080");
})
// GP API details
// Configure .env file
require('dotenv').config()
let gpApiVersion = process.env.GP_API_VERSION
let app_id = process.env.GP_API_APP_ID
let app_key = process.env.GP_API_APP_KEY
let nonce = new Date().toISOString();
// API requests to GP API
const createSecret = (app_key, nonce) => {
let secretKey = sha512(nonce + app_key)
return secretKey
}
const getAccessToken = async() => {
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json"
}
let body = {
"app_id":app_id,
"secret": createSecret(app_key, nonce),
"grant_type": "client_credentials",
"nonce":nonce
}
let options = {
method:"POST",
body: JSON.stringify(body),
headers:headers
}
const response = await fetch("https://apis.sandbox.globalpay.com/ucp/accesstoken", options);
const json = await response.json()
return json
}
const googlePayAuth = async(googlePayBlob) => {
// console.log("Google Pay Token", googlePayBlob)
let accessToken = await getAccessToken()
let headers ={
"X-GP-Version": gpApiVersion,
"Content-Type": "application/json",
"Accept" : "application/json",
"Authorization" : "Bearer " + accessToken.token
}
console.log("I'm here")
let decodedBlob = JSON.parse(googlePayBlob, true)
console.log(decodedBlob)
let signature = decodedBlob.signature
let protocolVersion = decodedBlob.protocolVersion
let signedMessage = decodedBlob.signedMessage
let body = {
"account_name": "Transaction_Processing",
"type": "SALE",
"channel": "CNP",
"capture_mode": "AUTO",
"amount": "1999",
"currency": "USD",
"reference": "93459c78-f3f9-427c-84df-ca0584bb55bf",
"country": "US",
"ip_address": "123.123.123.123",
"stored_credential": {
"model": "UNSCHEDULED",
"reason": "",
"sequence": "SUBSEQUENT"
},
"initiator": "PAYER",
"payment_method": {
"name": "James Mason",
"entry_mode": "ECOM",
"digital_wallet": {
"payment_token": {
"signature": signature,
"protocolVersion": protocolVersion,
"signedMessage": signedMessage
},
"provider": "PAY_BY_GOOGLE"
}
}
}
let options = {
method:"POST",
body: JSON.stringify(body),
headers:headers
}
const response = await fetch("https://apis.sandbox.globalpay.com/ucp/transactions", options);
const json = await response.json()
return json
}
// POST routes
app.get('/api/accessToken', async (request, response) => {
// define a function to get Access Token
let accessToken = await getAccessToken()
response.send(accessToken)
})
app.post('/api/googlePayAuth', async (request, response) => {
const data = request.body
try {
let response = await googlePayAuth(data.paymentMethodData.tokenizationData.token)
console.log(response)
response.send(response)
}
catch (error) {
response.send(error)
}
})