-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpaypay-rest-sdk.ts
More file actions
246 lines (224 loc) · 9.42 KB
/
paypay-rest-sdk.ts
File metadata and controls
246 lines (224 loc) · 9.42 KB
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
/* Copyright 2020, Robosoft */
'use strict';
/*
* Main file, methods that are exposed to end-user
*/
import { auth } from './auth';
import { config } from './conf';
import { httpsClient } from './httpsClient';
import CryptoJS from 'crypto-js';
import { v4 as uuidv4 } from 'uuid';
interface HttpsClientMessage {
(message: string): void;
}
class PayPayRestSDK {
private static options: any = '';
constructor() {}
/**
* Set authentication passed by end-user
* @param {string} clientId API_KEY provided by end-user
* @param {string} clientSecret API_SECRET provided by end-user
*/
public configure(clientConfig: { clientId: string; clientSecret: string }) {
auth.setAuth(clientConfig.clientId, clientConfig.clientSecret);
}
private static createAuthHeader = (method: string, resourceUrl: string, body: any, auth: any) => {
const epoch = Date.now();
const nonce = uuidv4();
let contentType = 'application/json';
let payload = JSON.stringify(body);
let isempty : any = [undefined, null, "", 'undefined', 'null'];
if (isempty.includes(payload)) {
contentType = 'empty';
payload = 'empty';
} else {
let md5 = CryptoJS.algo.MD5.create();
md5.update(contentType);
md5.update(payload);
payload = md5
.finalize()
.toString(CryptoJS.enc.Base64)
.toString("utf-8");
}
const signatureRawList = [resourceUrl, method, nonce, epoch, contentType, payload];
const signatureRawData = signatureRawList.join("\n");
const hashed = CryptoJS.HmacSHA256(signatureRawData,auth.clientSecret);
const hashed64 = CryptoJS.enc.Base64.stringify(hashed);
const headList = [auth.clientId, hashed64, nonce, epoch, payload];
const header = headList.join(":");
return `hmac OPA-Auth:${header}`;
}
private static setHttpsOptions(header: string) {
PayPayRestSDK.options = {
hostname: config.getHostname(),
port: config.getPortNumber(),
path: '',
method: '',
headers: {
Authorization: header,
},
};
config.setHttpsOptions(this.options);
}
private static paypaySetupOptions(nameApi: string, nameMethod: string, input: any) {
let queryParams = [];
PayPayRestSDK.options = config.getHttpsOptions();
this.options.path = config.getHttpsPath(nameApi, nameMethod);
this.options.method = config.getHttpsMethod(nameApi, nameMethod);
const authHeader = this.createAuthHeader(this.options.method,
this.options.path,
input,
auth)
if (false === config.getHttpsOptions() || undefined === config.getHttpsOptions()) {
this.setHttpsOptions(authHeader);
}
if (this.options.method === 'POST') {
this.options.headers['Content-Type'] = 'application/json';
this.options.headers['Content-Length'] = Buffer.byteLength(JSON.stringify(input));
} else {
queryParams = this.options.path.match(/{\w+}/g);
queryParams.forEach((q: any, n: string | number) => {
this.options.path = this.options.path.replace(q, input[n]);
});
}
return this.options;
}
/**
* Create a dynamic QR Code to receive payments
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public qrCodeCreate(payload: any, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'QRCODE_CREATE', payload), payload, (result: any) => {
callback(result);
});
}
/**
* Delete a created dynamic QR Code
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of codeId : QR Code that is to be deleted
*/
public qrCodeDelete(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'QRCODE_DELETE', inputParams), '', (result: any) => {
callback(result);
});
}
/**
* Get payment details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getPaymentDetails(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'GET_PAYMENT_DETAILS', inputParams), '', (result: any) => {
callback(result);
});
}
/**
* Cancel a payment
* This api is used in case, while creating a payment, the client can not determine the status of the payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public paymentCancel(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'CANCEL_PAYMENT', inputParams), '', (result: any) => {
callback(result);
});
}
/**
* Capture a payment authorization
* This api is used to capture the payment authorization for a payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public paymentAuthCapture(payload: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'PAYMENT_AUTH_CAPTURE', payload), payload.toString(), (result: any) => {
callback(result);
});
}
/**
* Revert a payment authorization
* The following api is used when a payment needs to be cancelled. For example, when the user cancels the order
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public paymentAuthRevert(payload: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'PAYMENT_AUTH_REVERT', payload), payload.toString(), (result: any) => {
callback(result);
});
}
/**
* Refund a payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Object} payload JSON object payload
*/
public paymentRefund(payload: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'REFUND_PAYMENT', payload), payload.toString(), (result: any) => {
callback(result);
});
}
/**
* Get refund details
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getRefundDetails(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_PAYMENT', 'GET_REFUND_DETAILS', inputParams), '', (result: any) => {
callback(result);
});
}
/**
* Check user wallet balance
* Check if user has enough balance to make a payment
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Array} inputParams Array of userAuthorizationId, amount, currency, productType
*/
public checkUserWalletBalance(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_WALLET', 'CHECK_BALANCE', inputParams), '', (result: any) => {
callback(result);
});
}
/**
* Direct user to the authorization web page
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Array} inputParams Array of apiKey, jwtToken
*/
public authorization(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_DIRECT_DEBIT', 'AUTHORIZATION', inputParams), '', (result: any) => {
callback(result);
});
}
/**
* Receive the user authorization result
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {Array} inputParams Array of apiKey, jwtToken
*/
public authorizationResult(inputParams: Array<string | number>, callback: HttpsClientMessage): void {
httpsClient.httpsCall(PayPayRestSDK.paypaySetupOptions('API_DIRECT_DEBIT', 'AUTHORIZATION_RESULT', inputParams), '', (result: any) => {
callback(result);
});
}
}
/**
* These are methods and variables that are exposed to end-user
*/
export let payPayRestSDK = new PayPayRestSDK();