bazik-sdk 1.0.1 → 1.0.2
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/CHANGELOG.md +2 -0
- package/README.md +25 -0
- package/package.json +1 -1
- package/src/constants.js +13 -0
- package/src/errors/BazikAuthError.js +12 -0
- package/src/errors/BazikError.js +21 -0
- package/src/errors/BazikInsufficientFundsError.js +12 -0
- package/src/errors/BazikRateLimitError.js +12 -0
- package/src/errors/BazikValidationError.js +12 -0
- package/src/helpers/validateAmount.js +23 -0
- package/src/helpers/validateRequired.js +21 -0
- package/src/helpers/validateWallet.js +19 -0
- package/src/http/request.js +37 -0
- package/src/index.js +6 -670
- package/src/modules/Bazik.js +235 -0
- package/src/modules/Payments.js +161 -0
- package/src/modules/Transfers.js +153 -0
- package/src/modules/Wallet.js +26 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -109,6 +109,31 @@ const natTransfer = await bazik.transfers.natcash({
|
|
|
109
109
|
});
|
|
110
110
|
```
|
|
111
111
|
|
|
112
|
+
### Withdraw to a MonCash Wallet
|
|
113
|
+
|
|
114
|
+
Send money from your Bazik account to a customer's MonCash wallet (payout).
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
const withdrawal = await bazik.payments.withdraw({
|
|
118
|
+
gdes: 500, // amount in HTG
|
|
119
|
+
wallet: "47556677", // recipient phone (8 or 11 digits)
|
|
120
|
+
customerFirstName: "Melissa",
|
|
121
|
+
customerLastName: "Francois",
|
|
122
|
+
description: "Weekly earnings",
|
|
123
|
+
referenceId: "PAYOUT-001",
|
|
124
|
+
customerEmail: "melissa@example.com",
|
|
125
|
+
webhookUrl: "https://mysite.com/webhook",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
console.log("Transaction:", withdrawal.transaction_id);
|
|
129
|
+
console.log("Status: ", withdrawal.status);
|
|
130
|
+
console.log("Total cost: ", withdrawal.total, withdrawal.currency);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Required fields: `gdes`, `wallet`, `customerFirstName`, `customerLastName`.
|
|
134
|
+
The other fields are optional. The same typed errors apply — wrap calls in
|
|
135
|
+
`try/catch` to handle `BazikInsufficientFundsError`, `BazikValidationError`, etc.
|
|
136
|
+
|
|
112
137
|
### Check Balance
|
|
113
138
|
|
|
114
139
|
```javascript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bazik-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Unofficial JavaScript/Node.js SDK for the Bazik API — MonCash & NatCash payments, transfers, and wallet management for Haiti.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"module": "src/index.mjs",
|
package/src/constants.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// ─── Constants ───────────────────────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
const DEFAULT_BASE_URL = "https://api.bazik.io";
|
|
6
|
+
const TOKEN_REFRESH_MARGIN_MS = 60 * 60 * 1000; // 1 hour before expiry
|
|
7
|
+
const MAX_MONCASH_AMOUNT = 75_000;
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
DEFAULT_BASE_URL,
|
|
11
|
+
TOKEN_REFRESH_MARGIN_MS,
|
|
12
|
+
MAX_MONCASH_AMOUNT,
|
|
13
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikError = require("./BazikError");
|
|
4
|
+
|
|
5
|
+
class BazikAuthError extends BazikError {
|
|
6
|
+
constructor(message, status, code, details) {
|
|
7
|
+
super(message, status, code, details);
|
|
8
|
+
this.name = "BazikAuthError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = BazikAuthError;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// ─── Errors ──────────────────────────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
class BazikError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} message
|
|
8
|
+
* @param {number} [status]
|
|
9
|
+
* @param {string} [code]
|
|
10
|
+
* @param {*} [details]
|
|
11
|
+
*/
|
|
12
|
+
constructor(message, status, code, details) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "BazikError";
|
|
15
|
+
this.status = status ?? null;
|
|
16
|
+
this.code = code ?? null;
|
|
17
|
+
this.details = details ?? null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = BazikError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikError = require("./BazikError");
|
|
4
|
+
|
|
5
|
+
class BazikInsufficientFundsError extends BazikError {
|
|
6
|
+
constructor(message, details) {
|
|
7
|
+
super(message, 402, "insufficient_funds", details);
|
|
8
|
+
this.name = "BazikInsufficientFundsError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = BazikInsufficientFundsError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikError = require("./BazikError");
|
|
4
|
+
|
|
5
|
+
class BazikRateLimitError extends BazikError {
|
|
6
|
+
constructor(message, details) {
|
|
7
|
+
super(message, 429, "rate_limit_exceeded", details);
|
|
8
|
+
this.name = "BazikRateLimitError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = BazikRateLimitError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikError = require("./BazikError");
|
|
4
|
+
|
|
5
|
+
class BazikValidationError extends BazikError {
|
|
6
|
+
constructor(message, details) {
|
|
7
|
+
super(message, 400, "validation_error", details);
|
|
8
|
+
this.name = "BazikValidationError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = BazikValidationError;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikValidationError = require("../errors/BazikValidationError");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validate a positive numeric amount.
|
|
7
|
+
* @param {number} amount
|
|
8
|
+
* @param {number} [max]
|
|
9
|
+
*/
|
|
10
|
+
function validateAmount(amount, max) {
|
|
11
|
+
if (typeof amount !== "number" || !isFinite(amount) || amount <= 0) {
|
|
12
|
+
throw new BazikValidationError(
|
|
13
|
+
`Invalid amount: ${amount}. Must be a positive number.`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
if (max !== undefined && amount > max) {
|
|
17
|
+
throw new BazikValidationError(
|
|
18
|
+
`Amount ${amount} exceeds maximum of ${max} HTG.`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = validateAmount;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikValidationError = require("../errors/BazikValidationError");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validate that required fields are present in an object.
|
|
7
|
+
* @param {Record<string, *>} obj
|
|
8
|
+
* @param {string[]} fields
|
|
9
|
+
*/
|
|
10
|
+
function validateRequired(obj, fields) {
|
|
11
|
+
const missing = fields.filter(
|
|
12
|
+
(f) => obj[f] === undefined || obj[f] === null || obj[f] === ""
|
|
13
|
+
);
|
|
14
|
+
if (missing.length > 0) {
|
|
15
|
+
throw new BazikValidationError(
|
|
16
|
+
`Missing required field(s): ${missing.join(", ")}`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = validateRequired;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const BazikValidationError = require("../errors/BazikValidationError");
|
|
4
|
+
|
|
5
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Validate that a wallet number is 8 or 11 digits.
|
|
9
|
+
* @param {string} wallet
|
|
10
|
+
*/
|
|
11
|
+
function validateWallet(wallet) {
|
|
12
|
+
if (typeof wallet !== "string" || !/^\d{8}(\d{3})?$/.test(wallet)) {
|
|
13
|
+
throw new BazikValidationError(
|
|
14
|
+
`Invalid wallet number "${wallet}". Must be 8 or 11 digits.`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = validateWallet;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// ─── HTTP Client (zero dependencies) ─────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal fetch wrapper.
|
|
7
|
+
* @param {string} url
|
|
8
|
+
* @param {RequestInit & { timeout?: number }} options
|
|
9
|
+
* @returns {Promise<{ status: number, data: * }>}
|
|
10
|
+
*/
|
|
11
|
+
async function request(url, options = {}) {
|
|
12
|
+
const { timeout = 30_000, ...fetchOptions } = options;
|
|
13
|
+
|
|
14
|
+
const controller = new AbortController();
|
|
15
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const res = await fetch(url, {
|
|
19
|
+
...fetchOptions,
|
|
20
|
+
signal: controller.signal,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
let data;
|
|
24
|
+
const contentType = res.headers.get("content-type") || "";
|
|
25
|
+
if (contentType.includes("application/json")) {
|
|
26
|
+
data = await res.json();
|
|
27
|
+
} else {
|
|
28
|
+
data = await res.text();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return { status: res.status, data };
|
|
32
|
+
} finally {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
module.exports = request;
|