@tatogi/bog-payment-js 1.0.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 +89 -0
- package/package.json +47 -0
- package/src/BogPaymentClient.js +236 -0
- package/src/errors.js +9 -0
- package/src/index.js +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tato
|
|
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
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @tatogi/bog-payment-js
|
|
2
|
+
|
|
3
|
+
JavaScript client for Bank of Georgia iPay API.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tatogi/bog-payment-js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { BogPaymentClient } from "@tatogi/bog-payment-js";
|
|
15
|
+
|
|
16
|
+
const bog = new BogPaymentClient({
|
|
17
|
+
clientId: process.env.BOG_CLIENT_ID,
|
|
18
|
+
clientSecret: process.env.BOG_CLIENT_SECRET
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const order = await bog.createOrder(
|
|
22
|
+
{
|
|
23
|
+
callback_url: "https://example.com/bog/callback",
|
|
24
|
+
external_order_id: "order-1001",
|
|
25
|
+
purchase_units: {
|
|
26
|
+
total_amount: 100,
|
|
27
|
+
currency: "GEL",
|
|
28
|
+
basket: [
|
|
29
|
+
{
|
|
30
|
+
product_id: "product-1",
|
|
31
|
+
name: "T-Shirt",
|
|
32
|
+
quantity: 1,
|
|
33
|
+
unit_price: 100
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
redirect_urls: {
|
|
38
|
+
success: "https://example.com/checkout/success",
|
|
39
|
+
fail: "https://example.com/checkout/fail"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
idempotencyKey: crypto.randomUUID(),
|
|
44
|
+
acceptLanguage: "en"
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
console.log(order._links.redirect.href);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Configuration
|
|
52
|
+
|
|
53
|
+
- `clientId` (required)
|
|
54
|
+
- `clientSecret` (required)
|
|
55
|
+
- `authUrl` (optional)
|
|
56
|
+
default: `https://oauth2.bog.ge/auth/realms/bog/protocol/openid-connect/token`
|
|
57
|
+
- `baseUrl` (optional)
|
|
58
|
+
default: `https://ipay.ge/opay/api/v1`
|
|
59
|
+
- `timeoutMs` (optional)
|
|
60
|
+
default: `30000`
|
|
61
|
+
- `fetchImpl` (optional) for custom runtime fetch
|
|
62
|
+
|
|
63
|
+
## Methods
|
|
64
|
+
|
|
65
|
+
- `getAccessToken(forceRefresh = false)`
|
|
66
|
+
- `createOrder(payload, { idempotencyKey, acceptLanguage } = {})`
|
|
67
|
+
- `getOrderDetails(orderId)`
|
|
68
|
+
- `payWithSavedCard(parentOrderId, payload, { idempotencyKey, acceptLanguage } = {})`
|
|
69
|
+
- `saveCard(orderId, { idempotencyKey } = {})`
|
|
70
|
+
- `deleteSavedCard(orderId, { idempotencyKey })`
|
|
71
|
+
- `confirmPreAuthorization(orderId, payload = {})`
|
|
72
|
+
- `rejectPreAuthorization(orderId, payload = {})`
|
|
73
|
+
|
|
74
|
+
## Test
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npm test
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Publish
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
npm login
|
|
84
|
+
npm publish --access public
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
For automated publish from GitHub Actions:
|
|
88
|
+
- add repository secret `NPM_TOKEN`
|
|
89
|
+
- push a tag like `v1.0.0`
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tatogi/bog-payment-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "JavaScript client for Bank of Georgia iPay API",
|
|
5
|
+
"private": false,
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node test/client.test.js",
|
|
18
|
+
"prepublishOnly": "npm test",
|
|
19
|
+
"release:patch": "npm version patch && npm publish --access public",
|
|
20
|
+
"release:minor": "npm version minor && npm publish --access public",
|
|
21
|
+
"release:major": "npm version major && npm publish --access public"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"bog",
|
|
25
|
+
"bank-of-georgia",
|
|
26
|
+
"ipay",
|
|
27
|
+
"payments",
|
|
28
|
+
"javascript",
|
|
29
|
+
"node"
|
|
30
|
+
],
|
|
31
|
+
"author": "Tato",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/tatoGi/bog-payment-js.git"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/tatoGi/bog-payment-js/issues"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/tatoGi/bog-payment-js#readme",
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { BogPaymentError } from "./errors.js";
|
|
2
|
+
|
|
3
|
+
const DEFAULT_AUTH_URL =
|
|
4
|
+
"https://oauth2.bog.ge/auth/realms/bog/protocol/openid-connect/token";
|
|
5
|
+
const DEFAULT_BASE_URL = "https://ipay.ge/opay/api/v1";
|
|
6
|
+
|
|
7
|
+
function trimSlash(value) {
|
|
8
|
+
return String(value || "").replace(/\/+$/, "");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class BogPaymentClient {
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
this.clientId = options.clientId;
|
|
14
|
+
this.clientSecret = options.clientSecret;
|
|
15
|
+
this.authUrl = options.authUrl || DEFAULT_AUTH_URL;
|
|
16
|
+
this.baseUrl = trimSlash(options.baseUrl || DEFAULT_BASE_URL);
|
|
17
|
+
this.timeoutMs = options.timeoutMs ?? 30000;
|
|
18
|
+
this.fetchImpl = options.fetchImpl || globalThis.fetch;
|
|
19
|
+
this.cachedToken = null;
|
|
20
|
+
this.cachedTokenExpiresAt = 0;
|
|
21
|
+
|
|
22
|
+
if (!this.fetchImpl) {
|
|
23
|
+
throw new BogPaymentError(
|
|
24
|
+
"Fetch API is not available. Use Node 18+ or pass fetchImpl."
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async getAccessToken(forceRefresh = false) {
|
|
30
|
+
const now = Date.now();
|
|
31
|
+
if (
|
|
32
|
+
!forceRefresh &&
|
|
33
|
+
this.cachedToken &&
|
|
34
|
+
this.cachedTokenExpiresAt > now + 10_000
|
|
35
|
+
) {
|
|
36
|
+
return this.cachedToken;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!this.clientId || !this.clientSecret) {
|
|
40
|
+
throw new BogPaymentError(
|
|
41
|
+
"Missing BOG credentials: clientId and clientSecret are required."
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const body = new URLSearchParams({ grant_type: "client_credentials" });
|
|
46
|
+
|
|
47
|
+
const basicAuth = Buffer.from(
|
|
48
|
+
`${this.clientId}:${this.clientSecret}`,
|
|
49
|
+
"utf8"
|
|
50
|
+
).toString("base64");
|
|
51
|
+
|
|
52
|
+
const response = await this.#fetchWithTimeout(this.authUrl, {
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers: {
|
|
55
|
+
Authorization: `Basic ${basicAuth}`,
|
|
56
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
57
|
+
Accept: "application/json"
|
|
58
|
+
},
|
|
59
|
+
body
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const payload = await this.#parseJson(response);
|
|
63
|
+
if (!response.ok) {
|
|
64
|
+
throw new BogPaymentError("BOG authentication failed.", {
|
|
65
|
+
status: response.status,
|
|
66
|
+
body: payload
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.cachedToken = payload.access_token;
|
|
71
|
+
const expiresInSeconds = Number(payload.expires_in || 3600);
|
|
72
|
+
this.cachedTokenExpiresAt = now + expiresInSeconds * 1000;
|
|
73
|
+
|
|
74
|
+
return this.cachedToken;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async createOrder(payload, options = {}) {
|
|
78
|
+
return this.#authorizedRequest("/checkout/orders", {
|
|
79
|
+
method: "POST",
|
|
80
|
+
body: payload,
|
|
81
|
+
idempotencyKey: options.idempotencyKey,
|
|
82
|
+
acceptLanguage: options.acceptLanguage
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async getOrderDetails(orderId) {
|
|
87
|
+
if (!orderId) {
|
|
88
|
+
throw new BogPaymentError("orderId is required.");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return this.#authorizedRequest(`/checkout/payment/${orderId}`, {
|
|
92
|
+
method: "GET"
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async payWithSavedCard(parentOrderId, payload, options = {}) {
|
|
97
|
+
if (!parentOrderId) {
|
|
98
|
+
throw new BogPaymentError("parentOrderId is required.");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return this.#authorizedRequest(`/checkout/orders/${parentOrderId}/payments`, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
body: payload,
|
|
104
|
+
idempotencyKey: options.idempotencyKey,
|
|
105
|
+
acceptLanguage: options.acceptLanguage
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async saveCard(orderId, options = {}) {
|
|
110
|
+
if (!orderId) {
|
|
111
|
+
throw new BogPaymentError("orderId is required.");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return this.#authorizedRequest(`/checkout/orders/${orderId}/save-card`, {
|
|
115
|
+
method: "POST",
|
|
116
|
+
body: {},
|
|
117
|
+
idempotencyKey: options.idempotencyKey
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async deleteSavedCard(orderId, options = {}) {
|
|
122
|
+
if (!orderId) {
|
|
123
|
+
throw new BogPaymentError("orderId is required.");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!options.idempotencyKey) {
|
|
127
|
+
throw new BogPaymentError("idempotencyKey is required.");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return this.#authorizedRequest(`/checkout/orders/${orderId}/saved-card`, {
|
|
131
|
+
method: "DELETE",
|
|
132
|
+
idempotencyKey: options.idempotencyKey
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async confirmPreAuthorization(orderId, payload = {}) {
|
|
137
|
+
if (!orderId) {
|
|
138
|
+
throw new BogPaymentError("orderId is required.");
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return this.#authorizedRequest(
|
|
142
|
+
`/checkout/orders/${orderId}/preauthorization/confirm`,
|
|
143
|
+
{
|
|
144
|
+
method: "POST",
|
|
145
|
+
body: payload
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async rejectPreAuthorization(orderId, payload = {}) {
|
|
151
|
+
if (!orderId) {
|
|
152
|
+
throw new BogPaymentError("orderId is required.");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return this.#authorizedRequest(
|
|
156
|
+
`/checkout/orders/${orderId}/preauthorization/reject`,
|
|
157
|
+
{
|
|
158
|
+
method: "POST",
|
|
159
|
+
body: payload
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async #authorizedRequest(path, options = {}) {
|
|
165
|
+
const token = await this.getAccessToken();
|
|
166
|
+
const url = `${this.baseUrl}${path}`;
|
|
167
|
+
|
|
168
|
+
const headers = {
|
|
169
|
+
Accept: "application/json",
|
|
170
|
+
Authorization: `Bearer ${token}`,
|
|
171
|
+
...(options.body ? { "Content-Type": "application/json" } : {})
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
if (options.idempotencyKey) {
|
|
175
|
+
headers["Idempotency-Key"] = options.idempotencyKey;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (options.acceptLanguage) {
|
|
179
|
+
headers["Accept-Language"] = options.acceptLanguage;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const response = await this.#fetchWithTimeout(url, {
|
|
183
|
+
method: options.method || "GET",
|
|
184
|
+
headers,
|
|
185
|
+
body: options.body ? JSON.stringify(options.body) : undefined
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
const payload = await this.#parseJson(response);
|
|
189
|
+
if (!response.ok) {
|
|
190
|
+
throw new BogPaymentError("BOG API request failed.", {
|
|
191
|
+
status: response.status,
|
|
192
|
+
body: payload
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return payload;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async #fetchWithTimeout(url, options) {
|
|
200
|
+
const controller = new AbortController();
|
|
201
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
return await this.fetchImpl(url, {
|
|
205
|
+
...options,
|
|
206
|
+
signal: controller.signal
|
|
207
|
+
});
|
|
208
|
+
} catch (error) {
|
|
209
|
+
if (error.name === "AbortError") {
|
|
210
|
+
throw new BogPaymentError("BOG API request timed out.", {
|
|
211
|
+
code: "REQUEST_TIMEOUT"
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
throw new BogPaymentError(error.message || "BOG API network error.", {
|
|
216
|
+
code: "NETWORK_ERROR"
|
|
217
|
+
});
|
|
218
|
+
} finally {
|
|
219
|
+
clearTimeout(timer);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async #parseJson(response) {
|
|
224
|
+
const contentType = response.headers.get("content-type") || "";
|
|
225
|
+
if (contentType.includes("application/json")) {
|
|
226
|
+
return response.json();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const text = await response.text();
|
|
230
|
+
try {
|
|
231
|
+
return JSON.parse(text);
|
|
232
|
+
} catch {
|
|
233
|
+
return { raw: text };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
package/src/errors.js
ADDED
package/src/index.js
ADDED