@payriff-sdk/node 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +267 -2
- package/dist/client.d.ts +25 -0
- package/dist/client.js +49 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.js +66 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +31 -0
- package/dist/resources/cards.d.ts +10 -0
- package/dist/resources/cards.js +41 -0
- package/dist/resources/invoices.d.ts +9 -0
- package/dist/resources/invoices.js +57 -0
- package/dist/resources/orders.d.ts +15 -0
- package/dist/resources/orders.js +87 -0
- package/dist/resources/paging.d.ts +6 -0
- package/dist/resources/paging.js +14 -0
- package/dist/resources/payments.d.ts +8 -0
- package/dist/resources/payments.js +31 -0
- package/dist/resources/payouts.d.ts +11 -0
- package/dist/resources/payouts.js +72 -0
- package/dist/resources/transactions.d.ts +7 -0
- package/dist/resources/transactions.js +45 -0
- package/dist/transport.d.ts +27 -0
- package/dist/transport.js +115 -0
- package/dist/types.d.ts +391 -0
- package/dist/types.js +2 -0
- package/dist/util.d.ts +6 -0
- package/dist/util.js +52 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +4 -0
- package/dist/webhooks.d.ts +2 -0
- package/dist/webhooks.js +23 -0
- package/package.json +33 -4
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OrdersApi = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
class OrdersApi {
|
|
6
|
+
transport;
|
|
7
|
+
defaultCurrency;
|
|
8
|
+
constructor(transport, defaultCurrency) {
|
|
9
|
+
this.transport = transport;
|
|
10
|
+
this.defaultCurrency = defaultCurrency;
|
|
11
|
+
}
|
|
12
|
+
async create(params) {
|
|
13
|
+
(0, util_1.required)(params, 'params');
|
|
14
|
+
const body = (0, util_1.compact)({
|
|
15
|
+
amount: (0, util_1.required)(params.amount, 'amount'),
|
|
16
|
+
currency: params.currency ?? this.defaultCurrency,
|
|
17
|
+
language: params.language,
|
|
18
|
+
operation: params.operation ?? 'PURCHASE',
|
|
19
|
+
description: params.description,
|
|
20
|
+
callbackUrl: params.callbackUrl,
|
|
21
|
+
redirectUrl: params.redirectUrl,
|
|
22
|
+
cardSave: params.cardSave,
|
|
23
|
+
threeDS: params.threeDS,
|
|
24
|
+
autoPaymentType: params.autoPaymentType,
|
|
25
|
+
installment: params.installment,
|
|
26
|
+
fullName: params.fullName,
|
|
27
|
+
phoneNumber: params.phoneNumber,
|
|
28
|
+
metadata: (0, util_1.nonEmpty)(params.metadata),
|
|
29
|
+
fields: (0, util_1.nonEmpty)(params.fields),
|
|
30
|
+
});
|
|
31
|
+
const raw = await this.transport.execute({
|
|
32
|
+
method: 'POST',
|
|
33
|
+
path: '/api/v3/orders',
|
|
34
|
+
headers: { 'X-REQUEST-RRN': params.requestRrn },
|
|
35
|
+
body,
|
|
36
|
+
});
|
|
37
|
+
if (!raw)
|
|
38
|
+
return raw;
|
|
39
|
+
const { comissionRate, ...rest } = raw;
|
|
40
|
+
return (0, util_1.compact)({ ...rest, commissionRate: rest.commissionRate ?? comissionRate });
|
|
41
|
+
}
|
|
42
|
+
async get(orderId) {
|
|
43
|
+
return this.transport.execute({ method: 'GET', path: `/api/v3/orders/${(0, util_1.pathSegment)(orderId, 'orderId')}` });
|
|
44
|
+
}
|
|
45
|
+
async getStatus(orderId) {
|
|
46
|
+
return this.transport.execute({ method: 'GET', path: `/api/v3/orders/${(0, util_1.pathSegment)(orderId, 'orderId')}/status` });
|
|
47
|
+
}
|
|
48
|
+
async getByRequestRrn(requestRrn) {
|
|
49
|
+
return this.transport.execute({ method: 'GET', path: `/api/v3/orders/${(0, util_1.pathSegment)(requestRrn, 'requestRrn')}/rrn` });
|
|
50
|
+
}
|
|
51
|
+
async expire(orderId) {
|
|
52
|
+
(0, util_1.pathSegment)(orderId, 'orderId');
|
|
53
|
+
await this.transport.execute({ method: 'PATCH', path: '/api/v3/expire-status', query: { orderId } });
|
|
54
|
+
}
|
|
55
|
+
async refund(params) {
|
|
56
|
+
(0, util_1.required)(params, 'params');
|
|
57
|
+
await this.transport.execute({
|
|
58
|
+
method: 'POST',
|
|
59
|
+
path: '/api/v3/refund',
|
|
60
|
+
body: (0, util_1.compact)({
|
|
61
|
+
orderId: (0, util_1.required)(params.orderId, 'orderId'),
|
|
62
|
+
amount: params.amount,
|
|
63
|
+
refundReason: params.refundReason,
|
|
64
|
+
callbackUrl: params.callbackUrl,
|
|
65
|
+
}),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async complete(params) {
|
|
69
|
+
(0, util_1.required)(params, 'params');
|
|
70
|
+
await this.transport.execute({
|
|
71
|
+
method: 'POST',
|
|
72
|
+
path: '/api/v3/complete',
|
|
73
|
+
body: (0, util_1.compact)({
|
|
74
|
+
orderId: (0, util_1.required)(params.orderId, 'orderId'),
|
|
75
|
+
amount: params.amount,
|
|
76
|
+
callbackUrl: params.callbackUrl,
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async downloadReceipt(orderIdOrRrn) {
|
|
81
|
+
return this.transport.download({
|
|
82
|
+
method: 'GET',
|
|
83
|
+
path: `/api/v3/acquiring/receipt/${(0, util_1.pathSegment)(orderIdOrRrn, 'orderIdOrRrn')}`,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.OrdersApi = OrdersApi;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_PAGE_SIZE = void 0;
|
|
4
|
+
exports.pageQuery = pageQuery;
|
|
5
|
+
exports.MAX_PAGE_SIZE = 20;
|
|
6
|
+
function pageQuery(params) {
|
|
7
|
+
const page = params.page ?? 0;
|
|
8
|
+
const size = params.size ?? 10;
|
|
9
|
+
if (!Number.isInteger(page) || page < 0)
|
|
10
|
+
throw new RangeError('page must be >= 0');
|
|
11
|
+
if (!Number.isInteger(size) || size < 1 || size > exports.MAX_PAGE_SIZE)
|
|
12
|
+
throw new RangeError(`size must be 1-${exports.MAX_PAGE_SIZE}`);
|
|
13
|
+
return { page, offset: size };
|
|
14
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Transport } from '../transport';
|
|
2
|
+
import type { AutoPayParams, AutoPayResponse, Currency } from '../types';
|
|
3
|
+
export declare class PaymentsApi {
|
|
4
|
+
private readonly transport;
|
|
5
|
+
private readonly defaultCurrency;
|
|
6
|
+
constructor(transport: Transport, defaultCurrency: Currency);
|
|
7
|
+
autoPay(params: AutoPayParams): Promise<AutoPayResponse>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PaymentsApi = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
class PaymentsApi {
|
|
6
|
+
transport;
|
|
7
|
+
defaultCurrency;
|
|
8
|
+
constructor(transport, defaultCurrency) {
|
|
9
|
+
this.transport = transport;
|
|
10
|
+
this.defaultCurrency = defaultCurrency;
|
|
11
|
+
}
|
|
12
|
+
async autoPay(params) {
|
|
13
|
+
(0, util_1.required)(params, 'params');
|
|
14
|
+
return this.transport.execute({
|
|
15
|
+
method: 'POST',
|
|
16
|
+
path: '/api/v3/autoPay',
|
|
17
|
+
headers: { 'X-REQUEST-RRN': params.requestRrn },
|
|
18
|
+
body: (0, util_1.compact)({
|
|
19
|
+
cardUuid: (0, util_1.required)(params.cardUuid, 'cardUuid'),
|
|
20
|
+
amount: (0, util_1.required)(params.amount, 'amount'),
|
|
21
|
+
operation: params.operation ?? 'PURCHASE',
|
|
22
|
+
currency: params.currency ?? this.defaultCurrency,
|
|
23
|
+
description: (0, util_1.required)(params.description, 'description'),
|
|
24
|
+
callbackUrl: params.callbackUrl,
|
|
25
|
+
threeDS: params.threeDS,
|
|
26
|
+
isOneCLickPayment: params.oneClickPayment,
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.PaymentsApi = PaymentsApi;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Transport } from '../transport';
|
|
2
|
+
import type { Page, PayoutFilter, PayoutParams, PayoutResult, PayoutStatus, PayoutSummary } from '../types';
|
|
3
|
+
export declare class PayoutsApi {
|
|
4
|
+
private readonly transport;
|
|
5
|
+
constructor(transport: Transport);
|
|
6
|
+
create(params: PayoutParams): Promise<PayoutResult>;
|
|
7
|
+
getByRequestRrn(requestRrn: string): Promise<PayoutStatus>;
|
|
8
|
+
checkCardholder(cardPan: string): Promise<string>;
|
|
9
|
+
list(filter?: PayoutFilter): Promise<Page<PayoutSummary>>;
|
|
10
|
+
downloadReceipt(requestRrn: string): Promise<Buffer>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PayoutsApi = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const paging_1 = require("./paging");
|
|
6
|
+
class PayoutsApi {
|
|
7
|
+
transport;
|
|
8
|
+
constructor(transport) {
|
|
9
|
+
this.transport = transport;
|
|
10
|
+
}
|
|
11
|
+
async create(params) {
|
|
12
|
+
(0, util_1.required)(params, 'params');
|
|
13
|
+
const transferAmount = (0, util_1.required)(params.transferAmount, 'transferAmount');
|
|
14
|
+
if (transferAmount < 1)
|
|
15
|
+
throw new RangeError('transferAmount must be at least 1');
|
|
16
|
+
const raw = await this.transport.execute({
|
|
17
|
+
method: 'POST',
|
|
18
|
+
path: '/api/v3/payout',
|
|
19
|
+
headers: { 'X-IDEMPOTENCY-KEY': params.idempotencyKey },
|
|
20
|
+
merchantEnvelope: true,
|
|
21
|
+
body: (0, util_1.compact)({
|
|
22
|
+
transferAmount,
|
|
23
|
+
description: (0, util_1.required)(params.description, 'description'),
|
|
24
|
+
fullName: (0, util_1.required)(params.fullName, 'fullName'),
|
|
25
|
+
finCode: (0, util_1.required)(params.finCode, 'finCode'),
|
|
26
|
+
cardPan: params.cardPan,
|
|
27
|
+
bankName: params.bankName,
|
|
28
|
+
cardType: params.cardType,
|
|
29
|
+
requestRrn: params.requestRrn,
|
|
30
|
+
customerCode: params.customerCode,
|
|
31
|
+
voen: params.voen,
|
|
32
|
+
birthDate: params.birthDate,
|
|
33
|
+
callbackUrl: params.callbackUrl,
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
if (!raw)
|
|
37
|
+
return raw;
|
|
38
|
+
const { _final, ...rest } = raw;
|
|
39
|
+
return (0, util_1.compact)({ finalState: _final, ...rest });
|
|
40
|
+
}
|
|
41
|
+
async getByRequestRrn(requestRrn) {
|
|
42
|
+
return this.transport.execute({ method: 'GET', path: `/api/v3/payout/info/${(0, util_1.pathSegment)(requestRrn, 'requestRrn')}` });
|
|
43
|
+
}
|
|
44
|
+
async checkCardholder(cardPan) {
|
|
45
|
+
const pan = String((0, util_1.required)(cardPan, 'cardPan')).replace(/[\s-]/g, '');
|
|
46
|
+
if (!/^\d{16}$/.test(pan))
|
|
47
|
+
throw new TypeError('cardPan must be a 16-digit number');
|
|
48
|
+
return this.transport.execute({ method: 'POST', path: '/api/v3/payout/check-cardholder', body: { cardPan: pan } });
|
|
49
|
+
}
|
|
50
|
+
async list(filter = {}) {
|
|
51
|
+
return this.transport.execute({
|
|
52
|
+
method: 'GET',
|
|
53
|
+
path: '/api/v3/payouts',
|
|
54
|
+
query: {
|
|
55
|
+
rrn: filter.rrn,
|
|
56
|
+
status: filter.status,
|
|
57
|
+
amount: filter.amount,
|
|
58
|
+
description: filter.description,
|
|
59
|
+
fullName: filter.fullName,
|
|
60
|
+
finCode: filter.finCode,
|
|
61
|
+
bankSource: filter.bankSource,
|
|
62
|
+
from: (0, util_1.formatFilterDate)(filter.from, 'from'),
|
|
63
|
+
to: (0, util_1.formatFilterDate)(filter.to, 'to'),
|
|
64
|
+
...(0, paging_1.pageQuery)(filter),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async downloadReceipt(requestRrn) {
|
|
69
|
+
return this.transport.download({ method: 'GET', path: `/api/v3/payout/receipt/${(0, util_1.pathSegment)(requestRrn, 'requestRrn')}` });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.PayoutsApi = PayoutsApi;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Transport } from '../transport';
|
|
2
|
+
import type { Page, Transaction, TransactionFilter } from '../types';
|
|
3
|
+
export declare class TransactionsApi {
|
|
4
|
+
private readonly transport;
|
|
5
|
+
constructor(transport: Transport);
|
|
6
|
+
list(filter?: TransactionFilter): Promise<Page<Transaction>>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TransactionsApi = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const paging_1 = require("./paging");
|
|
6
|
+
class TransactionsApi {
|
|
7
|
+
transport;
|
|
8
|
+
constructor(transport) {
|
|
9
|
+
this.transport = transport;
|
|
10
|
+
}
|
|
11
|
+
async list(filter = {}) {
|
|
12
|
+
const page = await this.transport.execute({
|
|
13
|
+
method: 'GET',
|
|
14
|
+
path: '/api/v3/transactions',
|
|
15
|
+
query: {
|
|
16
|
+
orderId: filter.orderId,
|
|
17
|
+
rrn: filter.rrn,
|
|
18
|
+
status: filter.status,
|
|
19
|
+
amount: filter.amount,
|
|
20
|
+
description: filter.description,
|
|
21
|
+
name: filter.name,
|
|
22
|
+
fullName: filter.fullName,
|
|
23
|
+
cardNumber: filter.cardNumber,
|
|
24
|
+
bookingId: filter.bookingId,
|
|
25
|
+
invoiceCode: filter.invoiceCode,
|
|
26
|
+
from: (0, util_1.formatFilterDate)(filter.from, 'from'),
|
|
27
|
+
to: (0, util_1.formatFilterDate)(filter.to, 'to'),
|
|
28
|
+
...(0, paging_1.pageQuery)(filter),
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
if (!page)
|
|
32
|
+
return page;
|
|
33
|
+
return { ...page, content: (page.content ?? []).map(toTransaction) };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.TransactionsApi = TransactionsApi;
|
|
37
|
+
function toTransaction(raw) {
|
|
38
|
+
const { extra_payment, card_brand, payment_route, payment_way, ...rest } = raw;
|
|
39
|
+
const renamed = { extraPayment: extra_payment, cardBrand: card_brand, paymentRoute: payment_route, paymentWay: payment_way };
|
|
40
|
+
for (const [key, value] of Object.entries(renamed)) {
|
|
41
|
+
if (value !== undefined && rest[key] === undefined)
|
|
42
|
+
rest[key] = value;
|
|
43
|
+
}
|
|
44
|
+
return rest;
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type Method = 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
2
|
+
export interface ApiRequest {
|
|
3
|
+
method: Method;
|
|
4
|
+
path: string;
|
|
5
|
+
query?: Record<string, string | number | undefined>;
|
|
6
|
+
headers?: Record<string, string | undefined>;
|
|
7
|
+
body?: unknown;
|
|
8
|
+
merchantEnvelope?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface TransportOptions {
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
secretKey: string;
|
|
13
|
+
merchantId?: string;
|
|
14
|
+
timeout: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class Transport {
|
|
17
|
+
private readonly options;
|
|
18
|
+
private readonly baseUrl;
|
|
19
|
+
constructor(options: TransportOptions);
|
|
20
|
+
execute<T>(request: ApiRequest): Promise<T>;
|
|
21
|
+
download(request: ApiRequest): Promise<Buffer>;
|
|
22
|
+
private send;
|
|
23
|
+
private readText;
|
|
24
|
+
private url;
|
|
25
|
+
private requestBody;
|
|
26
|
+
private unwrap;
|
|
27
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Transport = void 0;
|
|
4
|
+
const errors_1 = require("./errors");
|
|
5
|
+
const version_1 = require("./version");
|
|
6
|
+
const USER_AGENT = `payriff-node/${version_1.VERSION}`;
|
|
7
|
+
const MAX_ERROR_BODY = 500;
|
|
8
|
+
class Transport {
|
|
9
|
+
options;
|
|
10
|
+
baseUrl;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
this.baseUrl = options.baseUrl.replace(/\/+$/, '');
|
|
14
|
+
}
|
|
15
|
+
async execute(request) {
|
|
16
|
+
const response = await this.send(request, 'application/json');
|
|
17
|
+
return this.unwrap(response.status, await this.readText(response));
|
|
18
|
+
}
|
|
19
|
+
async download(request) {
|
|
20
|
+
const response = await this.send(request, 'application/pdf, application/json');
|
|
21
|
+
if (response.status >= 200 && response.status < 300) {
|
|
22
|
+
try {
|
|
23
|
+
return Buffer.from(await response.arrayBuffer());
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
throw new errors_1.PayriffConnectionError(`Payriff request failed: ${e.message}`, { cause: e });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
this.unwrap(response.status, await this.readText(response));
|
|
30
|
+
throw new errors_1.ApiError('Unexpected response', response.status);
|
|
31
|
+
}
|
|
32
|
+
async send(request, accept) {
|
|
33
|
+
const headers = {
|
|
34
|
+
Accept: accept,
|
|
35
|
+
'User-Agent': USER_AGENT,
|
|
36
|
+
Authorization: this.options.secretKey,
|
|
37
|
+
};
|
|
38
|
+
for (const [name, value] of Object.entries(request.headers ?? {})) {
|
|
39
|
+
if (value !== undefined)
|
|
40
|
+
headers[name] = value;
|
|
41
|
+
}
|
|
42
|
+
const body = this.requestBody(request);
|
|
43
|
+
if (body !== undefined)
|
|
44
|
+
headers['Content-Type'] = 'application/json';
|
|
45
|
+
try {
|
|
46
|
+
return await fetch(this.url(request), {
|
|
47
|
+
method: request.method,
|
|
48
|
+
headers,
|
|
49
|
+
body,
|
|
50
|
+
redirect: 'manual',
|
|
51
|
+
signal: AbortSignal.timeout(this.options.timeout),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
throw new errors_1.PayriffConnectionError(`Payriff request failed: ${e.message}`, { cause: e });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async readText(response) {
|
|
59
|
+
try {
|
|
60
|
+
return await response.text();
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
throw new errors_1.PayriffConnectionError(`Payriff request failed: ${e.message}`, { cause: e });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
url(request) {
|
|
67
|
+
const params = new URLSearchParams();
|
|
68
|
+
for (const [name, value] of Object.entries(request.query ?? {})) {
|
|
69
|
+
if (value !== undefined)
|
|
70
|
+
params.append(name, String(value));
|
|
71
|
+
}
|
|
72
|
+
const query = params.toString().replace(/\+/g, '%20');
|
|
73
|
+
return this.baseUrl + request.path + (query ? `?${query}` : '');
|
|
74
|
+
}
|
|
75
|
+
requestBody(request) {
|
|
76
|
+
if (request.merchantEnvelope) {
|
|
77
|
+
if (!this.options.merchantId) {
|
|
78
|
+
throw new Error('merchantId must be configured on Payriff for this operation');
|
|
79
|
+
}
|
|
80
|
+
return JSON.stringify({ merchant: this.options.merchantId, body: request.body ?? {} });
|
|
81
|
+
}
|
|
82
|
+
return request.body === undefined ? undefined : JSON.stringify(request.body);
|
|
83
|
+
}
|
|
84
|
+
unwrap(status, text) {
|
|
85
|
+
let root;
|
|
86
|
+
try {
|
|
87
|
+
root = text ? JSON.parse(text) : undefined;
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
root = undefined;
|
|
91
|
+
}
|
|
92
|
+
const ok = status >= 200 && status < 300;
|
|
93
|
+
if (!isObject(root) || !('code' in root)) {
|
|
94
|
+
throw new errors_1.ApiError(ok ? 'Unexpected response from Payriff' : truncate(text, status), status);
|
|
95
|
+
}
|
|
96
|
+
const code = asString(root.code);
|
|
97
|
+
const responseId = asString(root.responseId);
|
|
98
|
+
if (!ok || code !== errors_1.SUCCESS_CODE) {
|
|
99
|
+
throw (0, errors_1.toApiError)(asString(root.message), status, code, responseId);
|
|
100
|
+
}
|
|
101
|
+
return (root.payload ?? undefined);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.Transport = Transport;
|
|
105
|
+
function isObject(value) {
|
|
106
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
107
|
+
}
|
|
108
|
+
function asString(value) {
|
|
109
|
+
return value === undefined || value === null ? undefined : String(value);
|
|
110
|
+
}
|
|
111
|
+
function truncate(text, status) {
|
|
112
|
+
if (!text)
|
|
113
|
+
return `Payriff request failed (HTTP ${status})`;
|
|
114
|
+
return text.length > MAX_ERROR_BODY ? `${text.slice(0, MAX_ERROR_BODY)}...` : text;
|
|
115
|
+
}
|