-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
171 lines (151 loc) · 4.49 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
const base64 = require('js-base64');
const {customAlphabet} = require('nanoid');
const nanoid = customAlphabet('1234567890abcdefABCDEF', 32);
const rp = require('request-promise');
const fs = require('fs');
class SberQr {
constructor(
{
client_id,
client_secret,
pkcs12_filename,
pkcs12_password,
member_id,
terminal_id,
sbp_member_id,
},
) {
const encoded_cred = base64.encode(`${client_id}:${client_secret}`);
this.config = {
client_id,
client_secret,
encoded_cred,
pkcs12_filename,
pkcs12_password,
member_id,
terminal_id,
sbp_member_id,
};
}
__generateRqUID() {
return nanoid();
}
__getAgentOptions() {
return {
pfx: Buffer.isBuffer(this.config.pkcs12_filename) ? this.config.pkcs12_filename : fs.readFileSync(this.config.pkcs12_filename),
passphrase: this.config.pkcs12_password,
};
}
async getToken(scope) {
const form = {
grant_type: 'client_credentials',
scope: scope,
};
let client_id = this.config.client_id;
const rqUID = this.__generateRqUID();
let reqOptions = {
method: 'POST',
url: 'https://api.sberbank.ru:8443/prod/tokens/v2/oauth',
headers: {
accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
RqUID: rqUID,
'Authorization': `Basic ${this.config.encoded_cred}`,
'x-ibm-client-id': client_id,
},
form,
agentOptions: this.__getAgentOptions(),
};
let token;
await rp(reqOptions)
.then(res => JSON.parse(res))
.then(res => {
token = res;
});
return {
token,
rqUID,
};
}
/**
* Создать заказ на оплату
*
* @param {{order_sum: number, order_number: string, description: string, currency: string}} options_param
*/
async creteOrder(options_param) {
const {token, rqUID} = await this.getToken('https://api.sberbank.ru/qr/order.create');
const date = new Date().toISOString().slice(0, 19) + 'Z';
let order_sum = options_param.order_sum * 100; // в копейках!!
let reqOptions = {
url: 'https://api.sberbank.ru:8443/prod/qr/order/v3/creation',
method: 'POST',
headers: {
accept: 'application/json',
Authorization: `Bearer ${token.access_token}`,
'x-Introspect-RqUID': rqUID,
'RqUID': rqUID,
'X-IBM-Client-Id': this.config.client_id,
'Content-Type': 'application/json',
},
body: JSON.stringify({
rq_uid: rqUID,
rq_tm: date,
member_id: this.config.member_id,
order_number: options_param.order_number,
order_create_date: date,
order_params_type: options_param.order_params_type,
id_qr: this.config.terminal_id,
order_sum,
currency: options_param.currency,
description: options_param.description,
sbp_member_id: this.config.sbp_member_id,
}),
agentOptions: this.__getAgentOptions(),
};
let order_info;
await rp(reqOptions)
.then(res => JSON.parse(res))
.then(res => {
order_info = res;
});
return order_info;
};
/**
* Узнать статус оплаты
*
* @param {{partner_order_number: string, order_id: string}} args
*
* order_id - номер заказа в сбере присвоенный сбером
* partner_order_number - номер заказа присвоенный при создании нами в методе creteOrder
*/
async getOrderStatus(args) {
const {token, rqUID} = await this.getToken('https://api.sberbank.ru/qr/order.status');
const reqOptions = {
url: 'https://api.sberbank.ru:8443/prod/qr/order/v3/status',
method: 'POST',
headers: {
Authorization: `Bearer ${token.access_token}`,
'x-Introspect-RqUID': `${rqUID}`,
'RqUID': rqUID,
'X-IBM-Client-Id': `${this.config.client_id}`,
'Content-type': 'application/json',
},
body: JSON.stringify({
rq_uid: rqUID,
rq_tm: `${new Date().toISOString().slice(0, 19) + 'Z'}`,
order_id: args.order_id,
"tid": this.config.terminal_id,
"partner_order_number": args.partner_order_number,
}),
agentOptions: this.__getAgentOptions(),
};
let status;
await rp(reqOptions)
.then(res => JSON.parse(res))
.then(res => {
status = res;
});
return status;
}
}
module.exports = SberQr;