tbc-rando-sdk 0.1.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 +244 -0
- package/dist/index.cjs +672 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +532 -0
- package/dist/index.d.ts +532 -0
- package/dist/index.js +623 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rati Gigauri
|
|
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,244 @@
|
|
|
1
|
+
# tbc-rando-sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for the [TBC Bank Checkout (TPAY) API](https://developers.tbcbank.ge/docs/checkout-api-overview) — card payments, **installments** and **subscriptions** (recurring payments) for Node.js and Next.js.
|
|
4
|
+
|
|
5
|
+
- Zero runtime dependencies (uses the built-in `fetch`, Node ≥ 18.17)
|
|
6
|
+
- Automatic access-token caching and refresh (single token request even under concurrent load, transparent retry on 401)
|
|
7
|
+
- Typed request/response models with client-side validation before anything hits the network
|
|
8
|
+
- Handles TBC's wire-format quirks for you (`returnurl` lowercase, PascalCase `installmentProducts`)
|
|
9
|
+
- Webhook/callback helper that plugs straight into a Next.js App Router route
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install tbc-rando-sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
Credentials come from [developers.tbcbank.ge](https://developers.tbcbank.ge) (app `apikey`) and your TBC E-Commerce merchant dashboard (`client_id` / `client_secret`):
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { TbcCheckoutClient } from 'tbc-rando-sdk';
|
|
23
|
+
|
|
24
|
+
const tbc = new TbcCheckoutClient({
|
|
25
|
+
apiKey: process.env.TBC_API_KEY!,
|
|
26
|
+
clientId: process.env.TBC_CLIENT_ID!,
|
|
27
|
+
clientSecret: process.env.TBC_CLIENT_SECRET!,
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Create the client once (e.g. in a module) and reuse it — the ~24h access token is cached inside the instance. Server-side only: never expose these credentials to the browser. In Next.js use it in route handlers, server actions or server components.
|
|
32
|
+
|
|
33
|
+
## 1. Standard payment
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { getRedirectUrl } from 'tbc-rando-sdk';
|
|
37
|
+
|
|
38
|
+
const payment = await tbc.payments.create({
|
|
39
|
+
amount: { currency: 'GEL', total: 49.9 },
|
|
40
|
+
returnUrl: 'https://shop.example.ge/checkout/return',
|
|
41
|
+
callbackUrl: 'https://shop.example.ge/api/tbc/callback',
|
|
42
|
+
merchantPaymentId: 'ORDER-1042',
|
|
43
|
+
description: 'Order #1042',
|
|
44
|
+
language: 'EN',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Send the customer to the hosted TBC checkout page:
|
|
48
|
+
const checkoutUrl = getRedirectUrl(payment);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Then check the outcome (from your callback handler or by polling):
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { isFinalPaymentStatus, PaymentStatus } from 'tbc-rando-sdk';
|
|
55
|
+
|
|
56
|
+
const details = await tbc.payments.retrieve(payment.payId);
|
|
57
|
+
if (details.status === PaymentStatus.Succeeded) {
|
|
58
|
+
// fulfill the order
|
|
59
|
+
}
|
|
60
|
+
isFinalPaymentStatus(details.status); // stop polling when true
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Refunds / cancellations and pre-authorizations:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
await tbc.payments.cancel(payId); // full refund
|
|
67
|
+
await tbc.payments.cancel(payId, { amount: 10.5 }); // partial refund
|
|
68
|
+
|
|
69
|
+
// preAuth: true on create blocks the amount for up to 30 days…
|
|
70
|
+
await tbc.payments.completePreAuth(payId, 45.0); // …then capture (≤ blocked amount)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## 2. Installments
|
|
74
|
+
|
|
75
|
+
Installments are checkout payments restricted to payment method 8. Your merchant account needs an installment `campaignId`/`merchantKey` configured in the TBC back office. The customer picks the terms on TBC's page.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const payment = await tbc.installments.create({
|
|
79
|
+
returnUrl: 'https://shop.example.ge/checkout/return',
|
|
80
|
+
callbackUrl: 'https://shop.example.ge/api/tbc/callback',
|
|
81
|
+
merchantPaymentId: 'ORDER-1043',
|
|
82
|
+
products: [
|
|
83
|
+
{ name: 'Laptop', price: 2199, quantity: 1 },
|
|
84
|
+
{ name: 'Mouse', price: 89.5, quantity: 2 },
|
|
85
|
+
],
|
|
86
|
+
// total is computed as sum(price × quantity) = 2378.00; currency defaults to GEL
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const checkoutUrl = getRedirectUrl(payment);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The SDK validates product lines, computes the total with proper money rounding, and serializes products to the PascalCase wire format TBC expects (`{ Name, Price, Quantity }`).
|
|
93
|
+
|
|
94
|
+
## 3. Subscriptions (recurring payments)
|
|
95
|
+
|
|
96
|
+
TBC subscriptions are built on saved cards (card saving must be enabled for your merchant by TBC):
|
|
97
|
+
|
|
98
|
+
**Step 1 — the customer pays once and the card is saved:**
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const first = await tbc.subscriptions.start({
|
|
102
|
+
amount: { currency: 'GEL', total: 9.99 },
|
|
103
|
+
returnUrl: 'https://app.example.ge/subscribe/return',
|
|
104
|
+
callbackUrl: 'https://app.example.ge/api/tbc/callback',
|
|
105
|
+
description: 'Monthly plan',
|
|
106
|
+
saveCardToDate: '1230', // optional MMYY limit
|
|
107
|
+
});
|
|
108
|
+
// redirect the customer to getRedirectUrl(first)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Step 2 — after the payment succeeds, persist the card token:**
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const details = await tbc.payments.retrieve(first.payId);
|
|
115
|
+
const recId = details.recurringCard?.recId; // store this against the subscriber
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Step 3 — bill on your own schedule (cron, queue, etc.), no customer interaction:**
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const charge = await tbc.subscriptions.charge({
|
|
122
|
+
recId,
|
|
123
|
+
amount: 9.99,
|
|
124
|
+
currency: 'GEL',
|
|
125
|
+
initiator: 'merchant', // merchant-initiated (unattended) billing
|
|
126
|
+
merchantPaymentId: 'SUB-1042-2026-08',
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
if (charge.status === 'Succeeded') {
|
|
130
|
+
// extend the subscription
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
**Cancel the subscription (deletes the saved card):**
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
await tbc.subscriptions.cancel(recId);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Callbacks (webhooks) in Next.js
|
|
141
|
+
|
|
142
|
+
TBC POSTs a `PaymentId` to your `callbackUrl` when a payment reaches a final status. The body carries no status — the handler fetches the authoritative payment details from the API before invoking your listener, so forged callbacks can't inject a fake status.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
// app/api/tbc/callback/route.ts
|
|
146
|
+
import { tbc } from '@/lib/tbc'; // your shared TbcCheckoutClient instance
|
|
147
|
+
|
|
148
|
+
export const POST = tbc.webhooks.handler(async (payment) => {
|
|
149
|
+
switch (payment.status) {
|
|
150
|
+
case 'Succeeded':
|
|
151
|
+
await fulfillOrder(payment.payId);
|
|
152
|
+
break;
|
|
153
|
+
case 'Failed':
|
|
154
|
+
case 'Expired':
|
|
155
|
+
await markOrderFailed(payment.payId);
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Lower-level pieces if you're not on the App Router:
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import { parseCallbackBody, TBC_CALLBACK_IPS } from 'tbc-rando-sdk';
|
|
165
|
+
|
|
166
|
+
// pages/api/tbc-callback.ts
|
|
167
|
+
export default async function handler(req, res) {
|
|
168
|
+
const { payment } = await tbc.webhooks.resolveCallback(req.body);
|
|
169
|
+
// ...handle payment.status
|
|
170
|
+
res.status(200).send('OK');
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`TBC_CALLBACK_IPS` lists the four IPs TBC sends callbacks from, for firewall whitelisting; the route helper can also enforce them via `{ verifySourceIp: true }` (only when `X-Forwarded-For` is set by your own proxy).
|
|
175
|
+
|
|
176
|
+
## Error handling
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
import { TbcApiError, TbcAuthError, TbcValidationError } from 'tbc-rando-sdk';
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
await tbc.subscriptions.charge({ recId, amount: 9.99, currency: 'GEL', initiator: 'merchant' });
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (error instanceof TbcApiError) {
|
|
185
|
+
error.status; // HTTP status
|
|
186
|
+
error.resultCode; // e.g. 'decline_not_sufficient_funds', 'decline_expired_card'
|
|
187
|
+
error.systemCode; // TBC problem code, e.g. 'tpay.400.012'
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
- `TbcValidationError` — thrown client-side before any request is sent
|
|
193
|
+
- `TbcAuthError` — HTTP 401 (bad `apikey` or token; the SDK already retried once with a fresh token)
|
|
194
|
+
- `TbcApiError` — any other non-2xx API response, with `resultCode` for business declines ([reference](https://developers.tbcbank.ge/docs/result-code))
|
|
195
|
+
|
|
196
|
+
Decline codes are available as constants:
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { ResultCode } from 'tbc-rando-sdk';
|
|
200
|
+
|
|
201
|
+
if (error instanceof TbcApiError && error.resultCode === ResultCode.DeclineNotSufficientFunds) {
|
|
202
|
+
// ask the customer for another card
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Constants
|
|
207
|
+
|
|
208
|
+
`PaymentMethod`, `PaymentStatus`, `ResultCode`, `Currency` and `Language` are exported as enum-style `as const` objects (each name is also the matching TypeScript type). Unlike real TS `enum`s, plain strings stay assignable — `status === 'Succeeded'` and `status === PaymentStatus.Succeeded` both type-check, and the objects tree-shake cleanly.
|
|
209
|
+
|
|
210
|
+
## Payment methods
|
|
211
|
+
|
|
212
|
+
Restrict the checkout page via `methods`:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
import { PaymentMethod } from 'tbc-rando-sdk';
|
|
216
|
+
|
|
217
|
+
await tbc.payments.create({
|
|
218
|
+
amount: { currency: 'GEL', total: 20 },
|
|
219
|
+
returnUrl: '…',
|
|
220
|
+
methods: [PaymentMethod.Card, PaymentMethod.ApplePay, PaymentMethod.GooglePay],
|
|
221
|
+
});
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
| Constant | ID | Notes |
|
|
225
|
+
| --- | --- | --- |
|
|
226
|
+
| `PaymentMethod.WebQr` | 4 | QR / BNPL, needs back-office activation |
|
|
227
|
+
| `PaymentMethod.Card` | 5 | enabled by default |
|
|
228
|
+
| `PaymentMethod.InternetBank` | 7 | needs activation |
|
|
229
|
+
| `PaymentMethod.Installment` | 8 | needs campaign config; use `tbc.installments` |
|
|
230
|
+
| `PaymentMethod.ApplePay` | 9 | needs activation |
|
|
231
|
+
| `PaymentMethod.GooglePay` | 14 | needs activation |
|
|
232
|
+
|
|
233
|
+
## Development
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
npm test # vitest (56 tests, fully mocked — no network)
|
|
237
|
+
npm run typecheck # tsc --noEmit
|
|
238
|
+
npm run build # tsup → dist/ (ESM + CJS + d.ts)
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Notes & limitations
|
|
242
|
+
|
|
243
|
+
- TBC has no public sandbox; testing happens on production with small amounts ([docs](https://developers.tbcbank.ge/docs/checkout-testing-on-production)). `baseUrl` is configurable for mock servers.
|
|
244
|
+
- The split-payment endpoint (`/tpay/payments` with split) isn't wrapped yet; use `tbc.request()` directly if you need it.
|