@payriff-sdk/node 0.0.0-stage → 0.1.3
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 +276 -2
- package/dist/card.d.ts +21 -0
- package/dist/card.js +86 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.js +42 -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 +8 -0
- package/dist/resources/invoices.js +55 -0
- package/dist/resources/orders.d.ts +14 -0
- package/dist/resources/orders.js +85 -0
- package/dist/resources/paging.d.ts +6 -0
- package/dist/resources/paging.js +14 -0
- package/dist/resources/payments.d.ts +10 -0
- package/dist/resources/payments.js +56 -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 +408 -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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Payriff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,277 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Payriff Node.js SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Node.js client for the Payriff merchant API: orders, direct (host-to-host) card payments, saved cards,
|
|
4
|
+
transactions, payouts and invoices.
|
|
5
|
+
|
|
6
|
+
- Node.js 20 or newer
|
|
7
|
+
- No runtime dependencies
|
|
8
|
+
- TypeScript types included
|
|
9
|
+
- Talks to `https://api.payriff.com`
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @payriff-sdk/node
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Works with any Node.js framework (Express, NestJS, Fastify, Next.js).
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { Payriff } from '@payriff-sdk/node';
|
|
23
|
+
|
|
24
|
+
const payriff = new Payriff({ appKey: process.env.PAYRIFF_APP_KEY! });
|
|
25
|
+
|
|
26
|
+
const order = await payriff.orders.create({
|
|
27
|
+
amount: 10,
|
|
28
|
+
description: 'Order #1001',
|
|
29
|
+
callbackUrl: 'https://shop.example/payriff/callback',
|
|
30
|
+
requestRrn: 'order-1001',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Send the customer to the hosted payment page
|
|
34
|
+
const paymentUrl = order.paymentUrl;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
CommonJS works too: `const { Payriff } = require('@payriff-sdk/node');`
|
|
38
|
+
|
|
39
|
+
Create one `Payriff` instance and reuse it.
|
|
40
|
+
|
|
41
|
+
## Configuration
|
|
42
|
+
|
|
43
|
+
| Option | Required | Default | Notes |
|
|
44
|
+
|---|---|---|---|
|
|
45
|
+
| `appKey` | yes | — | Application key from the Payriff dashboard. |
|
|
46
|
+
| `merchantId` | for payouts and invoices | — | Merchant ID (e.g. `ES1000000`). |
|
|
47
|
+
| `timeout` | no | `60000` | Request timeout in milliseconds. Bank operations can take tens of seconds. |
|
|
48
|
+
| `cardEncryptionKey` | no | built-in Payriff key | Only if Payriff rotates the card-encryption key. Base64 or PEM. |
|
|
49
|
+
|
|
50
|
+
Keep the app key in an environment variable or secret store. Never commit it.
|
|
51
|
+
|
|
52
|
+
## Orders
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const created = await payriff.orders.create({
|
|
56
|
+
amount: 25,
|
|
57
|
+
currency: 'AZN', // default AZN
|
|
58
|
+
operation: 'PRE_AUTH', // default PURCHASE
|
|
59
|
+
language: 'AZ',
|
|
60
|
+
description: 'Booking #77',
|
|
61
|
+
callbackUrl: 'https://shop.example/payriff/callback',
|
|
62
|
+
metadata: { bookingId: '77' },
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const info = await payriff.orders.get(created.orderId);
|
|
66
|
+
const byRef = await payriff.orders.getByRequestRrn('order-1001'); // the requestRrn you sent on create
|
|
67
|
+
|
|
68
|
+
await payriff.orders.complete({ orderId: created.orderId, amount: 25 });
|
|
69
|
+
await payriff.orders.refund({ orderId: created.orderId, amount: 5, refundReason: 'Partial return' });
|
|
70
|
+
await payriff.orders.expire(created.orderId); // cancel an unpaid order
|
|
71
|
+
|
|
72
|
+
const receiptPdf: Buffer = await payriff.orders.downloadReceipt(created.orderId);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`paymentStatus` is a string such as `APPROVED`, `DECLINED`, `PREAUTH_APPROVED` or `REFUNDED`. Values that
|
|
76
|
+
this SDK version does not know yet are passed through unchanged.
|
|
77
|
+
|
|
78
|
+
Amounts are JavaScript numbers in major units (`10.5` = 10.50 AZN).
|
|
79
|
+
|
|
80
|
+
## Direct payments (host-to-host)
|
|
81
|
+
|
|
82
|
+
You collect the card details yourself, and the SDK encrypts them before sending
|
|
83
|
+
(AES-256-GCM + RSA-OAEP). Raw card data never leaves your server in clear text, but your
|
|
84
|
+
systems still handle card data, so PCI DSS requirements apply to you.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const result = await payriff.payments.directPay({
|
|
88
|
+
amount: 1,
|
|
89
|
+
description: 'Order #1002',
|
|
90
|
+
callbackUrl: 'https://shop.example/payriff/callback',
|
|
91
|
+
requestRrn: 'order-1002',
|
|
92
|
+
card: {
|
|
93
|
+
pan: '4169 7413 3015 1979',
|
|
94
|
+
cardHolder: 'JOHN DOE',
|
|
95
|
+
expiryMonth: '11',
|
|
96
|
+
expiryYear: '27',
|
|
97
|
+
cvv: '123',
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
if (result.redirect) {
|
|
102
|
+
// 3-D Secure: send the customer's browser to result.redirectUrl.
|
|
103
|
+
// The final status arrives via your callback or payriff.orders.get(result.orderId).
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The SDK strips spaces and dashes from the card number, pads the month (`1` → `01`), expands a 2-digit
|
|
108
|
+
year (`27` → `2027`) and rejects malformed values before any network call.
|
|
109
|
+
|
|
110
|
+
### Charging a saved card
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const charge = await payriff.payments.autoPay({
|
|
114
|
+
cardUuid: savedCardUuid,
|
|
115
|
+
amount: 9.99,
|
|
116
|
+
description: 'Monthly subscription',
|
|
117
|
+
requestRrn: 'sub-2026-10',
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Saved cards
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { randomUUID } from 'node:crypto';
|
|
125
|
+
|
|
126
|
+
const session = await payriff.cards.save({
|
|
127
|
+
customerRef: 'customer-42',
|
|
128
|
+
callbackUrl: 'https://shop.example/payriff/card-saved',
|
|
129
|
+
idempotencyKey: randomUUID(),
|
|
130
|
+
});
|
|
131
|
+
// Redirect the customer to session.paymentUrl to verify the card.
|
|
132
|
+
|
|
133
|
+
const details = await payriff.cards.getSave(session.cardSaveId);
|
|
134
|
+
if (details.status === 'VERIFIED') {
|
|
135
|
+
const cardUuid = details.cardUuid; // store it; use with autoPay
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const cards = await payriff.cards.list('customer-42');
|
|
139
|
+
await payriff.cards.delete(cards[0].cardUuid);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Transactions
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const page = await payriff.transactions.list({
|
|
146
|
+
status: 'APPROVED',
|
|
147
|
+
from: '2026-09-01', // Date or YYYY-MM-DD
|
|
148
|
+
to: '2026-09-30',
|
|
149
|
+
page: 0,
|
|
150
|
+
size: 20, // server maximum is 20
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
for (const tx of page.content) {
|
|
154
|
+
console.log(tx.orderId, tx.amount);
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Payouts
|
|
159
|
+
|
|
160
|
+
Payouts require `merchantId` on the client.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const payriff = new Payriff({
|
|
164
|
+
appKey: process.env.PAYRIFF_APP_KEY!,
|
|
165
|
+
merchantId: process.env.PAYRIFF_MERCHANT_ID,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const maskedName = await payriff.payouts.checkCardholder('4169741330151979'); // e.g. "J*** D**"
|
|
169
|
+
|
|
170
|
+
const payout = await payriff.payouts.create({
|
|
171
|
+
transferAmount: 50, // minimum 1
|
|
172
|
+
description: 'Refund for order #1001',
|
|
173
|
+
fullName: 'JOHN DOE',
|
|
174
|
+
finCode: '1AB2C3D',
|
|
175
|
+
cardPan: '4169741330151979',
|
|
176
|
+
requestRrn: 'payout-1001',
|
|
177
|
+
idempotencyKey: 'payout-1001',
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const status = await payriff.payouts.getByRequestRrn('payout-1001');
|
|
181
|
+
const history = await payriff.payouts.list({ status: 'SUCCESS' });
|
|
182
|
+
const receipt = await payriff.payouts.downloadReceipt('payout-1001');
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Invoices
|
|
186
|
+
|
|
187
|
+
Invoices require `merchantId` on the client.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
const invoice = await payriff.invoices.create({
|
|
191
|
+
amount: 15,
|
|
192
|
+
fullName: 'JOHN DOE',
|
|
193
|
+
phoneNumber: '+994501234567',
|
|
194
|
+
description: 'Consultation',
|
|
195
|
+
expireDate: new Date(Date.now() + 7 * 24 * 3600 * 1000),
|
|
196
|
+
sendSms: true,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const link = invoice.paymentUrl; // share with the customer
|
|
200
|
+
const details = await payriff.invoices.get(invoice.invoiceUuid);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Callbacks
|
|
204
|
+
|
|
205
|
+
When an order changes state, Payriff POSTs JSON to the `callbackUrl` you set on the order.
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
import express from 'express';
|
|
209
|
+
import { parseOrderCallback } from '@payriff-sdk/node';
|
|
210
|
+
|
|
211
|
+
app.post('/payriff/callback', express.json(), async (req, res) => {
|
|
212
|
+
const notified = parseOrderCallback(req.body); // also accepts the raw string or Buffer
|
|
213
|
+
|
|
214
|
+
// Callbacks are not signed: confirm the state with Payriff before fulfilling.
|
|
215
|
+
const confirmed = await payriff.orders.get(notified.orderId);
|
|
216
|
+
if (confirmed.paymentStatus === 'APPROVED') {
|
|
217
|
+
// fulfil the order (make this idempotent: the same callback can arrive more than once)
|
|
218
|
+
}
|
|
219
|
+
res.sendStatus(200);
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Errors
|
|
224
|
+
|
|
225
|
+
Every SDK error extends `PayriffError`, which carries `httpStatus`, `code` (Payriff result code)
|
|
226
|
+
and `responseId` (quote it when contacting support).
|
|
227
|
+
|
|
228
|
+
| Error | When |
|
|
229
|
+
|---|---|
|
|
230
|
+
| `AuthenticationError` | App key rejected (`14010`, `14013`, `14014`, `14015`) |
|
|
231
|
+
| `ValidationError` | Invalid request (`15400` or HTTP 400) |
|
|
232
|
+
| `RequestRejectedError` | Business refusal (`01000`), e.g. application under review |
|
|
233
|
+
| `InsufficientBalanceError` | Not enough wallet balance for a payout (`01200`) |
|
|
234
|
+
| `PayoutLimitError` | Payout limit reached (`01300`, `01400`, `01500`) |
|
|
235
|
+
| `ApiError` | Any other failure reported by Payriff |
|
|
236
|
+
| `PayriffConnectionError` | No response: network error or timeout |
|
|
237
|
+
|
|
238
|
+
Payriff can report a failure with HTTP 200. The SDK checks the result code in the body, so you
|
|
239
|
+
only need to catch errors. Invalid arguments are rejected with a `TypeError` or `RangeError` before
|
|
240
|
+
any request is sent.
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
import { PayriffConnectionError, PayriffError, ValidationError } from '@payriff-sdk/node';
|
|
244
|
+
|
|
245
|
+
try {
|
|
246
|
+
await payriff.orders.refund({ orderId });
|
|
247
|
+
} catch (e) {
|
|
248
|
+
if (e instanceof ValidationError) {
|
|
249
|
+
console.warn(`Refund rejected: ${e.message} (${e.code})`);
|
|
250
|
+
} else if (e instanceof PayriffConnectionError) {
|
|
251
|
+
// Outcome unknown: check payriff.orders.get(orderId) before retrying
|
|
252
|
+
} else if (e instanceof PayriffError) {
|
|
253
|
+
console.error(`Payriff error ${e.code} responseId=${e.responseId}`);
|
|
254
|
+
} else {
|
|
255
|
+
throw e;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
### Retries
|
|
261
|
+
|
|
262
|
+
The SDK never retries on its own, because payment calls are not safe to repeat blindly. After a
|
|
263
|
+
`PayriffConnectionError`, look the operation up first (`orders.getByRequestRrn(...)`,
|
|
264
|
+
`payouts.getByRequestRrn(...)`) and retry only if it does not exist. Set `requestRrn` /
|
|
265
|
+
`idempotencyKey` on every request so that this lookup is possible.
|
|
266
|
+
|
|
267
|
+
## Building from source
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
npm install
|
|
271
|
+
npm test
|
|
272
|
+
npm run build
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## License
|
|
276
|
+
|
|
277
|
+
MIT
|
package/dist/card.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type KeyObject } from 'node:crypto';
|
|
2
|
+
import type { CardData } from './types';
|
|
3
|
+
export declare const PRODUCTION_CARD_ENCRYPTION_KEY: string;
|
|
4
|
+
export interface NormalizedCard {
|
|
5
|
+
pan: string;
|
|
6
|
+
cardHolder: string;
|
|
7
|
+
expiryYear: string;
|
|
8
|
+
expiryMonth: string;
|
|
9
|
+
cvv: string;
|
|
10
|
+
}
|
|
11
|
+
export interface EncryptedCard {
|
|
12
|
+
encryptedMessage: string;
|
|
13
|
+
secretKey: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function parsePublicKey(base64OrPem: string): KeyObject;
|
|
16
|
+
export declare function normalizeCard(card: CardData): NormalizedCard;
|
|
17
|
+
export declare class CardEncryptor {
|
|
18
|
+
private readonly publicKey;
|
|
19
|
+
constructor(publicKey: KeyObject);
|
|
20
|
+
encrypt(card: NormalizedCard): EncryptedCard;
|
|
21
|
+
}
|
package/dist/card.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CardEncryptor = exports.PRODUCTION_CARD_ENCRYPTION_KEY = void 0;
|
|
4
|
+
exports.parsePublicKey = parsePublicKey;
|
|
5
|
+
exports.normalizeCard = normalizeCard;
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
7
|
+
const util_1 = require("./util");
|
|
8
|
+
exports.PRODUCTION_CARD_ENCRYPTION_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxRq5a+44T6Dac60XmVRQ/7cpPyFsBnamXbJlRVJk8CnES5Re5tVMohyD0hZr'
|
|
9
|
+
+ '3zcQj+bxodYB4zZpQTPlrXvFBC3zz+rXnGlevxBQ6W2d3QC9q8vWH8p3ZOwTO3qVvDSHH9o+hMMRNbJ7kueq/KZlX/F+bjZ23CZw7iXE'
|
|
10
|
+
+ 'GQT3HYVYnnHsvpaguYDteWBag2sPPLLsVjeB3zhTfQ7OsWp5XTkDuRwLugHPvs6RHLcwGCnodukWyvwUaEUQR/kMGC+RbMsAIVkcLMP5'
|
|
11
|
+
+ 'csfR3Xo7Gi98+i44iLN00f7gE8QvEmvv8xDspyTAjDEL1a5gK7TijJ3yLG/Bwa1rr1uskYy7lwIDAQAB';
|
|
12
|
+
const AES_KEY_BYTES = 32;
|
|
13
|
+
const IV_BYTES = 12;
|
|
14
|
+
function parsePublicKey(base64OrPem) {
|
|
15
|
+
const body = base64OrPem
|
|
16
|
+
.replace('-----BEGIN PUBLIC KEY-----', '')
|
|
17
|
+
.replace('-----END PUBLIC KEY-----', '')
|
|
18
|
+
.replace(/\s/g, '');
|
|
19
|
+
try {
|
|
20
|
+
const key = (0, node_crypto_1.createPublicKey)({ key: Buffer.from(body, 'base64'), format: 'der', type: 'spki' });
|
|
21
|
+
if (key.asymmetricKeyType !== 'rsa')
|
|
22
|
+
throw new Error('not an RSA key');
|
|
23
|
+
return key;
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
throw new TypeError('Invalid RSA public key', { cause: e });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function normalizeCard(card) {
|
|
30
|
+
(0, util_1.required)(card, 'card');
|
|
31
|
+
const pan = digits(card.pan, 'pan');
|
|
32
|
+
if (pan.length < 12 || pan.length > 19)
|
|
33
|
+
throw new TypeError('pan must contain 12-19 digits');
|
|
34
|
+
const cardHolder = (0, util_1.required)(card.cardHolder, 'cardHolder').trim();
|
|
35
|
+
const expiryMonth = month(card.expiryMonth);
|
|
36
|
+
const expiryYear = year(card.expiryYear);
|
|
37
|
+
const cvv = digits(card.cvv, 'cvv');
|
|
38
|
+
if (cvv.length < 3 || cvv.length > 4)
|
|
39
|
+
throw new TypeError('cvv must contain 3-4 digits');
|
|
40
|
+
return { pan, cardHolder, expiryYear, expiryMonth, cvv };
|
|
41
|
+
}
|
|
42
|
+
function digits(value, field) {
|
|
43
|
+
const v = String((0, util_1.required)(value, field)).replace(/[\s-]/g, '');
|
|
44
|
+
if (!/^\d+$/.test(v))
|
|
45
|
+
throw new TypeError(`${field} must contain digits only`);
|
|
46
|
+
return v;
|
|
47
|
+
}
|
|
48
|
+
function month(value) {
|
|
49
|
+
const m = Number(digits(value, 'expiryMonth'));
|
|
50
|
+
if (m < 1 || m > 12)
|
|
51
|
+
throw new TypeError('expiryMonth must be 1-12');
|
|
52
|
+
return String(m).padStart(2, '0');
|
|
53
|
+
}
|
|
54
|
+
function year(value) {
|
|
55
|
+
const y = digits(value, 'expiryYear');
|
|
56
|
+
if (y.length === 2)
|
|
57
|
+
return `20${y}`;
|
|
58
|
+
if (y.length !== 4)
|
|
59
|
+
throw new TypeError('expiryYear must have 2 or 4 digits');
|
|
60
|
+
return y;
|
|
61
|
+
}
|
|
62
|
+
class CardEncryptor {
|
|
63
|
+
publicKey;
|
|
64
|
+
constructor(publicKey) {
|
|
65
|
+
this.publicKey = publicKey;
|
|
66
|
+
}
|
|
67
|
+
encrypt(card) {
|
|
68
|
+
const aesKey = (0, node_crypto_1.randomBytes)(AES_KEY_BYTES);
|
|
69
|
+
const iv = (0, node_crypto_1.randomBytes)(IV_BYTES);
|
|
70
|
+
const keyAndIv = Buffer.concat([aesKey, iv]);
|
|
71
|
+
const plaintext = Buffer.from(JSON.stringify(card), 'utf8');
|
|
72
|
+
try {
|
|
73
|
+
const cipher = (0, node_crypto_1.createCipheriv)('aes-256-gcm', aesKey, iv);
|
|
74
|
+
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
|
|
75
|
+
const secret = (0, node_crypto_1.publicEncrypt)({ key: this.publicKey, padding: node_crypto_1.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: 'sha256' }, keyAndIv);
|
|
76
|
+
return { encryptedMessage: ciphertext.toString('base64'), secretKey: secret.toString('base64') };
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
aesKey.fill(0);
|
|
80
|
+
iv.fill(0);
|
|
81
|
+
keyAndIv.fill(0);
|
|
82
|
+
plaintext.fill(0);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
exports.CardEncryptor = CardEncryptor;
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { CardsApi } from './resources/cards';
|
|
2
|
+
import { InvoicesApi } from './resources/invoices';
|
|
3
|
+
import { OrdersApi } from './resources/orders';
|
|
4
|
+
import { PaymentsApi } from './resources/payments';
|
|
5
|
+
import { PayoutsApi } from './resources/payouts';
|
|
6
|
+
import { TransactionsApi } from './resources/transactions';
|
|
7
|
+
export interface PayriffOptions {
|
|
8
|
+
appKey: string;
|
|
9
|
+
merchantId?: string;
|
|
10
|
+
timeout?: number;
|
|
11
|
+
cardEncryptionKey?: string;
|
|
12
|
+
baseUrl?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare class Payriff {
|
|
15
|
+
readonly orders: OrdersApi;
|
|
16
|
+
readonly payments: PaymentsApi;
|
|
17
|
+
readonly cards: CardsApi;
|
|
18
|
+
readonly transactions: TransactionsApi;
|
|
19
|
+
readonly payouts: PayoutsApi;
|
|
20
|
+
readonly invoices: InvoicesApi;
|
|
21
|
+
constructor(options: PayriffOptions);
|
|
22
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Payriff = void 0;
|
|
4
|
+
const card_1 = require("./card");
|
|
5
|
+
const cards_1 = require("./resources/cards");
|
|
6
|
+
const invoices_1 = require("./resources/invoices");
|
|
7
|
+
const orders_1 = require("./resources/orders");
|
|
8
|
+
const payments_1 = require("./resources/payments");
|
|
9
|
+
const payouts_1 = require("./resources/payouts");
|
|
10
|
+
const transactions_1 = require("./resources/transactions");
|
|
11
|
+
const transport_1 = require("./transport");
|
|
12
|
+
const PRODUCTION_URL = 'https://api.payriff.com';
|
|
13
|
+
class Payriff {
|
|
14
|
+
orders;
|
|
15
|
+
payments;
|
|
16
|
+
cards;
|
|
17
|
+
transactions;
|
|
18
|
+
payouts;
|
|
19
|
+
invoices;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
if (!options || typeof options.appKey !== 'string' || options.appKey.trim() === '') {
|
|
22
|
+
throw new TypeError('appKey is required');
|
|
23
|
+
}
|
|
24
|
+
const timeout = options.timeout ?? 60_000;
|
|
25
|
+
if (!Number.isFinite(timeout) || timeout <= 0)
|
|
26
|
+
throw new RangeError('timeout must be a positive number of milliseconds');
|
|
27
|
+
const transport = new transport_1.Transport({
|
|
28
|
+
baseUrl: options.baseUrl ?? PRODUCTION_URL,
|
|
29
|
+
appKey: options.appKey,
|
|
30
|
+
merchantId: options.merchantId,
|
|
31
|
+
timeout,
|
|
32
|
+
});
|
|
33
|
+
const cardEncryptor = new card_1.CardEncryptor((0, card_1.parsePublicKey)(options.cardEncryptionKey ?? card_1.PRODUCTION_CARD_ENCRYPTION_KEY));
|
|
34
|
+
this.orders = new orders_1.OrdersApi(transport);
|
|
35
|
+
this.payments = new payments_1.PaymentsApi(transport, cardEncryptor);
|
|
36
|
+
this.cards = new cards_1.CardsApi(transport);
|
|
37
|
+
this.transactions = new transactions_1.TransactionsApi(transport);
|
|
38
|
+
this.payouts = new payouts_1.PayoutsApi(transport);
|
|
39
|
+
this.invoices = new invoices_1.InvoicesApi(transport);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.Payriff = Payriff;
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare class PayriffError extends Error {
|
|
2
|
+
readonly httpStatus: number;
|
|
3
|
+
readonly code: string | undefined;
|
|
4
|
+
readonly responseId: string | undefined;
|
|
5
|
+
constructor(message: string, httpStatus: number, code?: string, responseId?: string, options?: {
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
export declare class ApiError extends PayriffError {
|
|
10
|
+
}
|
|
11
|
+
export declare class AuthenticationError extends ApiError {
|
|
12
|
+
}
|
|
13
|
+
export declare class ValidationError extends ApiError {
|
|
14
|
+
}
|
|
15
|
+
export declare class RequestRejectedError extends ApiError {
|
|
16
|
+
}
|
|
17
|
+
export declare class InsufficientBalanceError extends ApiError {
|
|
18
|
+
}
|
|
19
|
+
export declare class PayoutLimitError extends ApiError {
|
|
20
|
+
}
|
|
21
|
+
export declare class PayriffConnectionError extends PayriffError {
|
|
22
|
+
constructor(message: string, options?: {
|
|
23
|
+
cause?: unknown;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export declare const SUCCESS_CODE = "00000";
|
|
27
|
+
export declare function toApiError(message: string | undefined, httpStatus: number, code?: string, responseId?: string): ApiError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SUCCESS_CODE = exports.PayriffConnectionError = exports.PayoutLimitError = exports.InsufficientBalanceError = exports.RequestRejectedError = exports.ValidationError = exports.AuthenticationError = exports.ApiError = exports.PayriffError = void 0;
|
|
4
|
+
exports.toApiError = toApiError;
|
|
5
|
+
class PayriffError extends Error {
|
|
6
|
+
httpStatus;
|
|
7
|
+
code;
|
|
8
|
+
responseId;
|
|
9
|
+
constructor(message, httpStatus, code, responseId, options) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = new.target.name;
|
|
12
|
+
this.httpStatus = httpStatus;
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.responseId = responseId;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.PayriffError = PayriffError;
|
|
18
|
+
class ApiError extends PayriffError {
|
|
19
|
+
}
|
|
20
|
+
exports.ApiError = ApiError;
|
|
21
|
+
class AuthenticationError extends ApiError {
|
|
22
|
+
}
|
|
23
|
+
exports.AuthenticationError = AuthenticationError;
|
|
24
|
+
class ValidationError extends ApiError {
|
|
25
|
+
}
|
|
26
|
+
exports.ValidationError = ValidationError;
|
|
27
|
+
class RequestRejectedError extends ApiError {
|
|
28
|
+
}
|
|
29
|
+
exports.RequestRejectedError = RequestRejectedError;
|
|
30
|
+
class InsufficientBalanceError extends ApiError {
|
|
31
|
+
}
|
|
32
|
+
exports.InsufficientBalanceError = InsufficientBalanceError;
|
|
33
|
+
class PayoutLimitError extends ApiError {
|
|
34
|
+
}
|
|
35
|
+
exports.PayoutLimitError = PayoutLimitError;
|
|
36
|
+
class PayriffConnectionError extends PayriffError {
|
|
37
|
+
constructor(message, options) {
|
|
38
|
+
super(message, 0, undefined, undefined, options);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.PayriffConnectionError = PayriffConnectionError;
|
|
42
|
+
exports.SUCCESS_CODE = '00000';
|
|
43
|
+
const AUTH_CODES = new Set(['14010', '14013', '14014', '14015']);
|
|
44
|
+
const PAYOUT_LIMIT_CODES = new Set(['01300', '01400', '01500']);
|
|
45
|
+
function toApiError(message, httpStatus, code, responseId) {
|
|
46
|
+
const msg = message ? message : `Payriff request failed (HTTP ${httpStatus})`;
|
|
47
|
+
if (code !== undefined) {
|
|
48
|
+
if (AUTH_CODES.has(code))
|
|
49
|
+
return new AuthenticationError(msg, httpStatus, code, responseId);
|
|
50
|
+
if (PAYOUT_LIMIT_CODES.has(code))
|
|
51
|
+
return new PayoutLimitError(msg, httpStatus, code, responseId);
|
|
52
|
+
switch (code) {
|
|
53
|
+
case '01200':
|
|
54
|
+
return new InsufficientBalanceError(msg, httpStatus, code, responseId);
|
|
55
|
+
case '01000':
|
|
56
|
+
return new RequestRejectedError(msg, httpStatus, code, responseId);
|
|
57
|
+
case '15400':
|
|
58
|
+
return new ValidationError(msg, httpStatus, code, responseId);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (httpStatus === 401 || httpStatus === 403)
|
|
62
|
+
return new AuthenticationError(msg, httpStatus, code, responseId);
|
|
63
|
+
if (httpStatus === 400)
|
|
64
|
+
return new ValidationError(msg, httpStatus, code, responseId);
|
|
65
|
+
return new ApiError(msg, httpStatus, code, responseId);
|
|
66
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Payriff, type PayriffOptions } from './client';
|
|
2
|
+
export { PayriffError, ApiError, AuthenticationError, ValidationError, RequestRejectedError, InsufficientBalanceError, PayoutLimitError, PayriffConnectionError, } from './errors';
|
|
3
|
+
export { parseOrderCallback } from './webhooks';
|
|
4
|
+
export type { OrdersApi } from './resources/orders';
|
|
5
|
+
export type { PaymentsApi } from './resources/payments';
|
|
6
|
+
export type { CardsApi } from './resources/cards';
|
|
7
|
+
export type { TransactionsApi } from './resources/transactions';
|
|
8
|
+
export type { PayoutsApi } from './resources/payouts';
|
|
9
|
+
export type { InvoicesApi } from './resources/invoices';
|
|
10
|
+
export * from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.parseOrderCallback = exports.PayriffConnectionError = exports.PayoutLimitError = exports.InsufficientBalanceError = exports.RequestRejectedError = exports.ValidationError = exports.AuthenticationError = exports.ApiError = exports.PayriffError = exports.Payriff = void 0;
|
|
18
|
+
var client_1 = require("./client");
|
|
19
|
+
Object.defineProperty(exports, "Payriff", { enumerable: true, get: function () { return client_1.Payriff; } });
|
|
20
|
+
var errors_1 = require("./errors");
|
|
21
|
+
Object.defineProperty(exports, "PayriffError", { enumerable: true, get: function () { return errors_1.PayriffError; } });
|
|
22
|
+
Object.defineProperty(exports, "ApiError", { enumerable: true, get: function () { return errors_1.ApiError; } });
|
|
23
|
+
Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return errors_1.AuthenticationError; } });
|
|
24
|
+
Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_1.ValidationError; } });
|
|
25
|
+
Object.defineProperty(exports, "RequestRejectedError", { enumerable: true, get: function () { return errors_1.RequestRejectedError; } });
|
|
26
|
+
Object.defineProperty(exports, "InsufficientBalanceError", { enumerable: true, get: function () { return errors_1.InsufficientBalanceError; } });
|
|
27
|
+
Object.defineProperty(exports, "PayoutLimitError", { enumerable: true, get: function () { return errors_1.PayoutLimitError; } });
|
|
28
|
+
Object.defineProperty(exports, "PayriffConnectionError", { enumerable: true, get: function () { return errors_1.PayriffConnectionError; } });
|
|
29
|
+
var webhooks_1 = require("./webhooks");
|
|
30
|
+
Object.defineProperty(exports, "parseOrderCallback", { enumerable: true, get: function () { return webhooks_1.parseOrderCallback; } });
|
|
31
|
+
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Transport } from '../transport';
|
|
2
|
+
import type { CardSaveDetails, CardSaveParams, CardSaveResponse, SavedCard } from '../types';
|
|
3
|
+
export declare class CardsApi {
|
|
4
|
+
private readonly transport;
|
|
5
|
+
constructor(transport: Transport);
|
|
6
|
+
save(params: CardSaveParams): Promise<CardSaveResponse>;
|
|
7
|
+
getSave(cardSaveId: string): Promise<CardSaveDetails>;
|
|
8
|
+
list(customerRef: string): Promise<SavedCard[]>;
|
|
9
|
+
delete(cardUuid: string): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CardsApi = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
class CardsApi {
|
|
6
|
+
transport;
|
|
7
|
+
constructor(transport) {
|
|
8
|
+
this.transport = transport;
|
|
9
|
+
}
|
|
10
|
+
async save(params) {
|
|
11
|
+
(0, util_1.required)(params, 'params');
|
|
12
|
+
return this.transport.execute({
|
|
13
|
+
method: 'POST',
|
|
14
|
+
path: '/api/v3/cards/save',
|
|
15
|
+
headers: { 'X-Idempotency-Key': params.idempotencyKey },
|
|
16
|
+
body: (0, util_1.compact)({
|
|
17
|
+
customerRef: (0, util_1.required)(params.customerRef, 'customerRef'),
|
|
18
|
+
callbackUrl: (0, util_1.required)(params.callbackUrl, 'callbackUrl'),
|
|
19
|
+
description: params.description,
|
|
20
|
+
language: params.language,
|
|
21
|
+
metadata: (0, util_1.nonEmpty)(params.metadata),
|
|
22
|
+
}),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async getSave(cardSaveId) {
|
|
26
|
+
return this.transport.execute({ method: 'GET', path: `/api/v3/cards/save/${(0, util_1.pathSegment)(cardSaveId, 'cardSaveId')}` });
|
|
27
|
+
}
|
|
28
|
+
async list(customerRef) {
|
|
29
|
+
(0, util_1.pathSegment)(customerRef, 'customerRef');
|
|
30
|
+
const cards = await this.transport.execute({
|
|
31
|
+
method: 'GET',
|
|
32
|
+
path: '/api/v3/cards/save',
|
|
33
|
+
query: { customerRef },
|
|
34
|
+
});
|
|
35
|
+
return cards ?? [];
|
|
36
|
+
}
|
|
37
|
+
async delete(cardUuid) {
|
|
38
|
+
await this.transport.execute({ method: 'DELETE', path: `/api/v3/cards/${(0, util_1.pathSegment)(cardUuid, 'cardUuid')}` });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.CardsApi = CardsApi;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Transport } from '../transport';
|
|
2
|
+
import type { Invoice, InvoiceCreateParams, InvoiceDetails } from '../types';
|
|
3
|
+
export declare class InvoicesApi {
|
|
4
|
+
private readonly transport;
|
|
5
|
+
constructor(transport: Transport);
|
|
6
|
+
create(params: InvoiceCreateParams): Promise<Invoice>;
|
|
7
|
+
get(invoiceUuid: string): Promise<InvoiceDetails>;
|
|
8
|
+
}
|