telebirr-nodejs 1.0.4 → 1.0.6
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/README.md +98 -77
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,173 +10,194 @@ A simple, opinionated Node.js SDK for integrating **Telebirr C2B payments** with
|
|
|
10
10
|
npm install telebirr-nodejs
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Required Configuration
|
|
16
|
+
|
|
14
17
|
Telebirr requires credentials and callback URLs.
|
|
15
18
|
You should never hard-code secrets. Always load them from process.env.
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
### Required Credentials
|
|
23
|
+
|
|
24
|
+
| Name | Description |
|
|
19
25
|
| ---------------------- | ---------------------------- |
|
|
20
|
-
| `FABRIC_APP_ID`
|
|
21
|
-
| `FABRIC_APP_SECRET`
|
|
22
|
-
| `MERCHANT_APP_ID`
|
|
23
|
-
| `MERCHANT_CODE`
|
|
26
|
+
| `FABRIC_APP_ID` | Fabric application ID |
|
|
27
|
+
| `FABRIC_APP_SECRET` | Fabric application secret |
|
|
28
|
+
| `MERCHANT_APP_ID` | Merchant application ID |
|
|
29
|
+
| `MERCHANT_CODE` | Merchant code |
|
|
24
30
|
| `MERCHANT_PRIVATE_KEY` | RSA private key (PEM string) |
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
### Required URLs
|
|
35
|
+
|
|
36
|
+
| Name | Description |
|
|
28
37
|
| ----------------------- | ------------------------------- |
|
|
29
|
-
| `TELEBIRR_NOTIFY_URL`
|
|
38
|
+
| `TELEBIRR_NOTIFY_URL` | Server-to-server callback URL |
|
|
30
39
|
| `TELEBIRR_REDIRECT_URL` | User redirect URL after payment |
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
### Basic Setup
|
|
33
44
|
|
|
34
45
|
```bash
|
|
35
46
|
import { TelebirrClient } from "telebirr-nodejs";
|
|
36
47
|
|
|
37
48
|
const client = new TelebirrClient({
|
|
38
49
|
mode: "sandbox", // "simulate" | "sandbox" | "production"
|
|
39
|
-
|
|
40
50
|
appId: process.env.FABRIC_APP_ID!,
|
|
41
51
|
appSecret: process.env.FABRIC_APP_SECRET!,
|
|
42
|
-
|
|
43
52
|
merchantAppId: process.env.MERCHANT_APP_ID!,
|
|
44
53
|
merchantCode: process.env.MERCHANT_CODE!,
|
|
45
54
|
privateKey: process.env.MERCHANT_PRIVATE_KEY!,
|
|
46
|
-
|
|
47
55
|
notifyUrl: process.env.TELEBIRR_NOTIFY_URL!,
|
|
48
56
|
redirectUrl: process.env.TELEBIRR_REDIRECT_URL!,
|
|
49
|
-
|
|
50
57
|
http: true, // allow HTTP in simulator mode
|
|
51
58
|
IntegrationOption: "C2B",
|
|
52
59
|
});
|
|
53
|
-
|
|
54
60
|
```
|
|
55
61
|
|
|
56
|
-
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
### Important
|
|
65
|
+
|
|
57
66
|
MERCHANT_PRIVATE_KEY must be the full PEM string, including:
|
|
58
67
|
|
|
59
68
|
```bash
|
|
60
|
-
|
|
61
69
|
-----BEGIN RSA PRIVATE KEY-----
|
|
62
70
|
...
|
|
63
71
|
-----END RSA PRIVATE KEY-----
|
|
64
|
-
|
|
65
72
|
```
|
|
66
73
|
|
|
67
|
-
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
### Example API Routes
|
|
68
77
|
|
|
69
78
|
Below is a complete demo using three routes:
|
|
70
79
|
|
|
71
|
-
POST /payment/initiate
|
|
80
|
+
1. POST /payment/initiate
|
|
81
|
+
2. GET /payment/status/:merchOrderId
|
|
82
|
+
3. POST /payment/refund
|
|
72
83
|
|
|
73
|
-
|
|
84
|
+
---
|
|
74
85
|
|
|
75
|
-
|
|
86
|
+
### 1 Initiate Payment
|
|
76
87
|
|
|
77
|
-
Initiate Payment
|
|
78
88
|
Creates an order and redirects the user to the Telebirr checkout page.
|
|
79
89
|
|
|
80
90
|
```bash
|
|
81
91
|
app.post("/payment/initiate", async (req, res) => {
|
|
82
|
-
const checkoutUrl = await client.preOrder({
|
|
83
|
-
merchOrderId: "order123",
|
|
84
|
-
title: "Phone",
|
|
85
|
-
amount: "12",
|
|
86
|
-
callbackInfo: "from web checkout",
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
res.redirect(checkoutUrl);
|
|
92
|
+
const checkoutUrl = await client.preOrder({
|
|
93
|
+
merchOrderId: "order123",
|
|
94
|
+
title: "Phone",
|
|
95
|
+
amount: "12",
|
|
96
|
+
callbackInfo: "from web checkout",
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
res.redirect(checkoutUrl);
|
|
90
100
|
});
|
|
91
|
-
|
|
92
101
|
```
|
|
93
102
|
|
|
94
103
|
If you are using a frontend framework (for example React) and do not want to lose application state, return the checkout URL as JSON and let the frontend handle the redirection.
|
|
95
104
|
|
|
96
|
-
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
### 2 Query Payment Status
|
|
108
|
+
|
|
97
109
|
Used to check the payment result using the merchant order ID.
|
|
98
110
|
|
|
99
111
|
```bash
|
|
100
112
|
app.get("/payment/status/:merchOrderId", async (req, res) => {
|
|
101
|
-
const { merchOrderId } = req.
|
|
113
|
+
const { merchOrderId } = req.body;
|
|
102
114
|
|
|
103
|
-
const info = await client.queryOrder(merchOrderId);
|
|
115
|
+
const info = await client.queryOrder(merchOrderId);
|
|
104
116
|
|
|
105
|
-
res.json(info);
|
|
117
|
+
res.json(info);
|
|
106
118
|
});
|
|
107
119
|
```
|
|
108
120
|
|
|
109
|
-
|
|
121
|
+
Please refer to Telebirr’s official documentation to correctly handle the query order json response:
|
|
122
|
+
https://developer.ethiotelecom.et/docs/H5%20C2B%20Web%20Payment%20Integration%20Quick%20Guide/queryOrder
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
### 3 Refund Payment
|
|
127
|
+
|
|
110
128
|
Refunds a completed transaction.
|
|
111
129
|
|
|
112
130
|
```bash
|
|
113
|
-
|
|
114
131
|
app.post("/payment/refund", async (req, res) => {
|
|
115
|
-
const refundData = await client.refundOrder({
|
|
116
|
-
merchOrderId: "order123",
|
|
117
|
-
refundRequestNo: "original-transaction-id",
|
|
118
|
-
refundReason: "customer request",
|
|
119
|
-
amount: "12",
|
|
120
|
-
});
|
|
121
132
|
|
|
122
|
-
|
|
123
|
-
|
|
133
|
+
const refundData = await client.refundOrder({
|
|
134
|
+
merchOrderId: "order123",
|
|
135
|
+
refundRequestNo: "original-transaction-id",
|
|
136
|
+
refundReason: "customer request",
|
|
137
|
+
amount: "12",
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
res.json(refundData);
|
|
141
|
+
});
|
|
124
142
|
|
|
125
143
|
```
|
|
126
144
|
|
|
127
|
-
|
|
145
|
+
Please refer to Telebirr’s official documentation to correctly handle the refund order json response:
|
|
146
|
+
https://developer.ethiotelecom.et/docs/H5%20C2B%20Web%20Payment%20Integration%20Quick%20Guide/RefundOrder
|
|
147
|
+
|
|
148
|
+
---
|
|
128
149
|
|
|
129
|
-
Simulator Mode
|
|
150
|
+
### Simulator Mode
|
|
130
151
|
|
|
131
152
|
To use the simulator provided by this package, set the mode to simulate.
|
|
132
153
|
|
|
133
154
|
```bash
|
|
134
155
|
import { TelebirrClient } from "telebirr-nodejs";
|
|
135
156
|
|
|
136
|
-
const client = new TelebirrClient({
|
|
137
|
-
mode: "simulate",
|
|
138
|
-
notifyUrl: "https://example.com/notify",
|
|
139
|
-
redirectUrl: "https://example.com/redirect",
|
|
140
|
-
http: true,
|
|
141
|
-
IntegrationOption: "C2B",
|
|
142
|
-
});
|
|
157
|
+
const client = new TelebirrClient({
|
|
158
|
+
mode: "simulate",
|
|
159
|
+
notifyUrl: "https://example.com/notify",
|
|
160
|
+
redirectUrl: "https://example.com/redirect",
|
|
161
|
+
http: true,
|
|
162
|
+
IntegrationOption: "C2B",
|
|
163
|
+
});
|
|
164
|
+
|
|
143
165
|
```
|
|
144
166
|
|
|
145
167
|
This simulator is for learning and development purposes only.
|
|
146
|
-
The simulation server is provided by this package, not by Telebirr.
|
|
147
|
-
|
|
148
|
-
For real testing, use Telebirr’s sandbox mode and whitelist your public IP address in the Telebirr portal.
|
|
149
|
-
|
|
150
|
-
Supported Features
|
|
151
|
-
Fabric token handling
|
|
152
|
-
|
|
153
|
-
RSA-SHA256 request signing
|
|
168
|
+
The simulation server is provided by this package, not by Telebirr. For real testing, use Telebirr’s sandbox mode and whitelist your public IP address in the Telebirr portal.
|
|
154
169
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
Order query
|
|
170
|
+
---
|
|
158
171
|
|
|
159
|
-
|
|
172
|
+
### Supported Features
|
|
160
173
|
|
|
161
|
-
|
|
174
|
+
- Fabric token handling
|
|
175
|
+
- RSA-SHA256 request signing
|
|
176
|
+
- C2B checkout
|
|
177
|
+
- Order query
|
|
178
|
+
- Refunds
|
|
179
|
+
- Simulator support
|
|
180
|
+
- Notify URL Handling
|
|
162
181
|
|
|
163
|
-
Notify URL Handling
|
|
164
182
|
Please refer to Telebirr’s official documentation to correctly handle notify callbacks:
|
|
165
183
|
|
|
166
184
|
https://developer.ethiotelecom.et/docs/H5%20C2B%20Web%20Payment%20Integration%20Quick%20Guide/Notify_Callback
|
|
167
185
|
|
|
168
|
-
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
### RSA Key Generation
|
|
189
|
+
|
|
169
190
|
You can generate private and public keys instantly.
|
|
170
191
|
|
|
171
192
|
```bash
|
|
172
193
|
import { generateKeys } from "telebirr-nodejs";
|
|
173
194
|
|
|
174
|
-
generateKeys({
|
|
175
|
-
dir: process.cwd(),
|
|
176
|
-
privateKeyName: "telefy_private.pem",
|
|
177
|
-
publicKeyName: "telefy_public.pem",
|
|
178
|
-
overwrite: false,
|
|
179
|
-
});
|
|
195
|
+
generateKeys({
|
|
196
|
+
dir: process.cwd(),
|
|
197
|
+
privateKeyName: "telefy_private.pem",
|
|
198
|
+
publicKeyName: "telefy_public.pem",
|
|
199
|
+
overwrite: false,
|
|
200
|
+
});
|
|
180
201
|
|
|
181
202
|
```
|
|
182
203
|
|