-
Notifications
You must be signed in to change notification settings - Fork 2
/
przelewy24.mjs
287 lines (225 loc) · 7.83 KB
/
przelewy24.mjs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import request from 'request-promise-native'
import crypto from 'crypto'
import queryString from 'query-string'
export default class Przelewy24 {
constructor(merchantId, posId, salt, testMode = false) {
this.form = {
p24_merchant_id: merchantId,
p24_pos_id: posId,
p24_api_version: this.P24_VERSION
}
this.salt = salt
this.testMode = testMode
this.host = testMode ? this.HOST_DEMO : this.HOST_LIVE
this.products = []
}
// Constants
get P24_VERSION() {
return '3.2'
}
get HOST_LIVE() {
return 'https://secure.przelewy24.pl/'
}
get HOST_DEMO() {
return 'https://sandbox.przelewy24.pl/'
}
get REQUIRED_FIELDS_REGISTER() {
return ['p24_merchant_id', 'p24_pos_id', 'p24_session_id', 'p24_amount', 'p24_currency', 'p24_description', 'p24_email', 'p24_country', 'p24_url_return']
}
get REQUIRED_FIELDS_VERIFY() {
return ['p24_merchant_id', 'p24_pos_id', 'p24_session_id', 'p24_amount', 'p24_currency', 'p24_order_id']
}
// Field-filing methods
setSessionId(value) {
if (value.length === 0 || value.length > 100) {
throw new Error('p24_session_id value must have from 0 to 100 characters.')
}
this.form.p24_session_id = value
}
setAmount(value) {
this.form.p24_amount = value
}
setCurrency(value) {
if (value.length !== 3) {
throw new Error('p24_currency value must have from 3 characters.')
}
this.form.p24_currency = value
}
setDescription(value) {
if (value.length === 0 || value.length > 1024) {
throw new Error('p24_description value must have from 0 to 1024 characters.')
}
this.form.p24_description = value
}
setEmail(value) {
if (value.length === 0 || value.length > 50) {
throw new Error('p24_email value must have from 0 to 50 characters.')
}
this.form.p24_email = value
}
setClient(value) {
if (value.length === 0 || value.length > 50) {
throw new Error('p24_client value must have from 0 to 50 characters.')
}
this.form.p24_client = value
}
setAddress(value) {
if (value.length === 0 || value.length > 80) {
throw new Error('p24_address value must have from 0 to 80 characters.')
}
this.form.p24_address = value
}
setZip(value) {
if (value.length === 0 || value.length > 10) {
throw new Error('p24_zip value must have from 0 to 10 characters.')
}
this.form.p24_zip = value
}
setCity(value) {
if (value.length === 0 || value.length > 50) {
throw new Error('p24_city value must have from 0 to 50 characters.')
}
this.form.p24_city = value
}
setCountry(value) {
if (value.length === 0 || value.length > 2) {
throw new Error('p24_country value must have from 0 to 2 characters.')
}
this.form.p24_country = value
}
setPhone(value) {
if (value.length === 0 || value.length > 12) {
throw new Error('p24_phone value must have from 0 to 12 characters.')
}
this.form.p24_phone = value
}
setLanguage(value) {
if (value.length !== 2) {
throw new Error('p24_language value must have from 2 characters.')
}
this.form.p24_language = value
}
setMethod(value) {
this.form.p24_method = value
}
setUrlReturn(value) {
if (value.length === 0 || value.length > 250) {
throw new Error('p24_url_return value must have from 0 to 250 characters.')
}
this.form.p24_url_return = value
}
setUrlStatus(value) {
if (value.length === 0 || value.length > 250) {
throw new Error('p24_url_status value must have from 0 to 250 characters.')
}
this.form.p24_url_status = value
}
setTimeLimit(value) {
this.form.p24_time_limit = value
}
setWaitForResult(value) {
this.form.p24_wait_for_result = value
}
setChannel(value) {
this.form.p24_channel = value
}
setShipping(value) {
this.form.p24_shipping = value
}
setTransferLabel(value) {
if (value.length === 0 || value.length > 20) {
throw new Error('p24_transfer_label value must have from 20 characters.')
}
this.form.p24_transfer_label = value
}
setOrderId(value) {
this.form.p24_order_id = value
}
// Custom methods
addProduct(name, description, quantity, price, number = null) {
this.products.push({
name: name,
description: description,
quantity: quantity,
price: price,
number: number
})
}
addProducts(products) {
this.products = this.products.concat(products)
}
loadProductsToForm() {
for (let i = 0; i < this.products.length; i++) {
const product = this.products[i]
const id = i + 1
this.form["p24_name_" + id] = product.name
this.form["p24_quantity_" + id] = product.quantity
this.form["p24_price_" + id] = product.price
if (product.description !== null) {
this.form["p24_description_" + id] = product.description
}
if (product.number !== null) {
this.form["p24_number_" + id] = product.number
}
}
}
getCrc() {
const data = this.form.p24_session_id + '|' + this.form.p24_pos_id + '|' + this.form.p24_amount + '|' + this.form.p24_currency + '|' + this.salt
return crypto.createHash('md5').update(data).digest("hex");
}
// Main methods
register() {
return new Promise(async (resolve, reject) => {
for (const field of this.REQUIRED_FIELDS_REGISTER) {
if (this.form[field] === undefined) {
return reject(new Error(field + ' field is missing.'))
}
}
this.form.p24_sign = this.getCrc()
this.loadProductsToForm()
try {
const rawResponse = await request.post(this.host + "trnRegister", { form: this.form })
if (rawResponse) {
const res = queryString.parse(rawResponse)
if (res.token) {
return resolve(res.token)
} else {
throw new Error(res.errorMessage)
}
} else {
throw new Error('Error happened while registering payment, please try again.')
}
} catch (e) {
return reject(e)
}
})
}
getPayByLinkUrl(token) {
return this.host + 'trnRequest/' + token
}
verify(sign) {
return new Promise(async (resolve, reject) => {
for (const field of this.REQUIRED_FIELDS_VERIFY) {
if (this.form[field] === undefined) {
return reject(new Error(field + ' field is missing.'))
}
}
this.form.p24_sign = sign
try {
const rawResponse = await request.post(this.host + "trnVerify", { form: this.form })
if (rawResponse) {
const res = queryString.parse(rawResponse)
if (res.error === '0') {
return resolve()
} else {
throw new Error(res.errorMessage)
}
} else {
throw new Error('Error happened while verifying payment, please try again.')
}
} catch (e) {
return reject(e)
}
})
}
}