-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
206 lines (189 loc) · 6.02 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
const axios = require('axios');
const qs = require('fast-querystring');
const fs = require('fs');
const https = require('https');
const path = require('path');
const TEST_ENTRY = 'https://3dsec.sberbank.ru/payment/rest/';
const ENTRY = 'https://securepayments.sberbank.ru/payment/rest/';
const ACTIONS = {
register: 'register.do',
getOrderStatusExtended: 'getOrderStatusExtended.do',
refund: 'refund.do',
getBindings: 'getBindings.do',
unBindCard: 'unBindCard.do',
paymentOrderBinding: 'paymentOrderBinding.do',
};
// Support Russian Trusted Root CA and Russian Trusted Sub CA certificates
axios.defaults.httpsAgent = new https.Agent({
ca: [
fs.readFileSync(
path.resolve(__dirname, './russian_trusted_root_ca_pem.crt')
),
fs.readFileSync(
path.resolve(__dirname, './russian_trusted_sub_ca_pem.crt')
),
],
});
/**
* @typedef {object} Credentials
* @property {string} userName - username
* @property {string} password - password
*/
class Acquiring {
/**
* @param {Credentials} credentials
* @param {string} returnUrl - use macro {order} for ID of order
* @param {boolean} test - use test entry
*/
constructor(credentials, returnUrl, test = false) {
this.returnUrl = returnUrl;
this.credentials = credentials;
this.entry = test ? TEST_ENTRY : ENTRY;
}
/**
* Creating new order
* @param {string} orderNumber
* @param {number} amount
* @param {string} description
* @param {object} otherParams
*/
async register(orderNumber, amount, description = '', otherParams = {}) {
const data = this.buildData({
...otherParams,
orderNumber,
amount: Math.round(amount * 100),
description,
returnUrl: this.returnUrl.replace(/\{order\}/g, orderNumber),
});
const response = await this.POST(ACTIONS.register, data);
return this.parse(response);
}
/**
* Creating auto payment request
* @param {string} mdOrder
* @param {number} bindingId
* @param {string} ip
*/
async paymentOrderBinding(mdOrder, bindingId, ip) {
const data = this.buildData({
mdOrder,
bindingId,
ip
});
const response = await this.POST(ACTIONS.paymentOrderBinding, data);
return this.parse(response);
}
/**
* Checking if order exists and getting its status
* Provide only one value - for orderId OR for orderNumber
* @param {string|null} orderId
* @param {string|null} orderNumber
* @returns {Promise<number|null>} status of the order or null unless it exists
* !!! result can be 0 - it is REGISTERED_BUT_NOT_PAID status !!!
* !!! always check with '===' whether it is null or isn't null !!!
*/
async status(orderId, orderNumber = null) {
const SBER_ERROR_NO_SUCH_ORDER_ID = 6;
try {
const response = await this.get(orderId, orderNumber);
return response.orderStatus;
} catch (error) {
if (parseInt(error.sberErrorCode) === SBER_ERROR_NO_SUCH_ORDER_ID) {
return null;
}
throw error;
}
}
/**
* Get info on order
* Provide only one value - for orderId OR for orderNumber
* @param {string|null} orderId
* @param {string|null} orderNumber
* @returns {Promise<object>} response
*/
async get(orderId, orderNumber = null) {
const data = this.buildData(orderId ? { orderId } : { orderNumber });
const response = await this.POST(ACTIONS.getOrderStatusExtended, data);
return this.parse(response);
}
/**
* Возврат
* @param {string} orderId Номер заказа в платежной системе.
* @param {number} amount Сумма платежа (500.23). 0 для возврата на всю сумму.
* @param {object|null} jsonParams Дополнительные параметры запроса.
* @returns {Promise<object>} response
*/
async refund(orderId, amount, jsonParams = null) {
const params = {
orderId,
amount: amount * 100,
};
if (jsonParams) {
params.jsonParams = JSON.stringify(jsonParams);
}
const data = this.buildData(params);
const response = await this.POST(ACTIONS.refund, data);
return this.parse(response);
}
/**
* Запрос списка всех связок клиента
* @param {string} clientId Номер (идентификатор) клиента в системе магазина.
* @param {"C"|"I"|"R"|"CR"|undefined} bindingType Тип связки. Значение по умолчанию 'C'.
* @param {string|undefined} bindingId Идентификатор связки.
* @returns {Promise<object>} response
*/
async getBindings(clientId, bindingType = 'C', bindingId) {
const params = {
clientId,
bindingType,
bindingId,
};
const data = this.buildData(params);
const response = await this.POST(ACTIONS.getBindings, data);
return this.parse(response);
}
/**
* Запрос деактивации связки
* @param {string} bindingId Идентификатор связки.
* @returns {Promise<object>} response
*/
async unBindCard(bindingId) {
const data = this.buildData({ bindingId });
const response = await this.POST(ACTIONS.unBindCard, data);
return this.parse(response);
}
/**
* Parse response data
* @param {object} response
*/
parse(response) {
const status = response.status;
if (status === 200) {
const data = response.data;
if (parseInt(data.errorCode)) {
const error = new Error(data.errorMessage);
error.sberErrorCode = data.errorCode;
throw error;
}
return data;
} else {
throw new Error(`HTTP error ${status}`);
}
}
/**
* Send POST
* @param {string} action
* @param {object} data
*/
async POST(action, data) {
return await axios.post(this.entry + action, qs.stringify(data));
}
/**
* Add technical parameters to data
* @param {object} parameters
*/
buildData(parameters = {}) {
return { ...parameters, ...this.credentials };
}
}
module.exports = Acquiring;