telebirr-nodejs 1.0.3 → 1.0.4
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 +53 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,28 +8,30 @@ A simple, opinionated Node.js SDK for integrating **Telebirr C2B payments** with
|
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm install telebirr-nodejs
|
|
11
|
+
```
|
|
12
|
+
|
|
11
13
|
Required Configuration
|
|
12
14
|
Telebirr requires credentials and callback URLs.
|
|
13
15
|
You should never hard-code secrets. Always load them from process.env.
|
|
14
16
|
|
|
15
17
|
Required Credentials
|
|
16
|
-
| Name
|
|
18
|
+
| Name | Description |
|
|
17
19
|
| ---------------------- | ---------------------------- |
|
|
18
|
-
| `FABRIC_APP_ID`
|
|
19
|
-
| `FABRIC_APP_SECRET`
|
|
20
|
-
| `MERCHANT_APP_ID`
|
|
21
|
-
| `MERCHANT_CODE`
|
|
20
|
+
| `FABRIC_APP_ID` | Fabric application ID |
|
|
21
|
+
| `FABRIC_APP_SECRET` | Fabric application secret |
|
|
22
|
+
| `MERCHANT_APP_ID` | Merchant application ID |
|
|
23
|
+
| `MERCHANT_CODE` | Merchant code |
|
|
22
24
|
| `MERCHANT_PRIVATE_KEY` | RSA private key (PEM string) |
|
|
23
25
|
|
|
24
|
-
|
|
25
26
|
Required URLs
|
|
26
|
-
| Name
|
|
27
|
+
| Name | Description |
|
|
27
28
|
| ----------------------- | ------------------------------- |
|
|
28
|
-
| `TELEBIRR_NOTIFY_URL`
|
|
29
|
+
| `TELEBIRR_NOTIFY_URL` | Server-to-server callback URL |
|
|
29
30
|
| `TELEBIRR_REDIRECT_URL` | User redirect URL after payment |
|
|
30
31
|
|
|
31
|
-
|
|
32
32
|
Basic Setup
|
|
33
|
+
|
|
34
|
+
```bash
|
|
33
35
|
import { TelebirrClient } from "telebirr-nodejs";
|
|
34
36
|
|
|
35
37
|
const client = new TelebirrClient({
|
|
@@ -49,15 +51,18 @@ const client = new TelebirrClient({
|
|
|
49
51
|
IntegrationOption: "C2B",
|
|
50
52
|
});
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
```
|
|
53
55
|
|
|
54
56
|
Important
|
|
55
57
|
MERCHANT_PRIVATE_KEY must be the full PEM string, including:
|
|
56
58
|
|
|
59
|
+
```bash
|
|
60
|
+
|
|
57
61
|
-----BEGIN RSA PRIVATE KEY-----
|
|
58
62
|
...
|
|
59
63
|
-----END RSA PRIVATE KEY-----
|
|
60
64
|
|
|
65
|
+
```
|
|
61
66
|
|
|
62
67
|
Example API Routes
|
|
63
68
|
|
|
@@ -72,65 +77,52 @@ POST /payment/refund
|
|
|
72
77
|
Initiate Payment
|
|
73
78
|
Creates an order and redirects the user to the Telebirr checkout page.
|
|
74
79
|
|
|
80
|
+
```bash
|
|
75
81
|
app.post("/payment/initiate", async (req, res) => {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
res.redirect(checkoutUrl);
|
|
82
|
+
const checkoutUrl = await client.preOrder({
|
|
83
|
+
merchOrderId: "order123",
|
|
84
|
+
title: "Phone",
|
|
85
|
+
amount: "12",
|
|
86
|
+
callbackInfo: "from web checkout",
|
|
84
87
|
});
|
|
85
88
|
|
|
89
|
+
res.redirect(checkoutUrl);
|
|
90
|
+
});
|
|
86
91
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
Fabric token is fetched and cached
|
|
90
|
-
|
|
91
|
-
Order is signed and created
|
|
92
|
-
|
|
93
|
-
Checkout URL is generated
|
|
94
|
-
|
|
95
|
-
User is redirected to Telebirr
|
|
92
|
+
```
|
|
96
93
|
|
|
97
94
|
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.
|
|
98
95
|
|
|
99
96
|
Query Payment Status
|
|
100
97
|
Used to check the payment result using the merchant order ID.
|
|
101
98
|
|
|
99
|
+
```bash
|
|
102
100
|
app.get("/payment/status/:merchOrderId", async (req, res) => {
|
|
103
|
-
|
|
101
|
+
const { merchOrderId } = req.params;
|
|
104
102
|
|
|
105
|
-
|
|
103
|
+
const info = await client.queryOrder(merchOrderId);
|
|
106
104
|
|
|
107
|
-
|
|
105
|
+
res.json(info);
|
|
108
106
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
Typical Use Cases
|
|
112
|
-
Payment confirmation pages
|
|
113
|
-
|
|
114
|
-
Background reconciliation jobs
|
|
115
|
-
|
|
116
|
-
Admin dashboards
|
|
107
|
+
```
|
|
117
108
|
|
|
118
109
|
Refund Payment
|
|
119
110
|
Refunds a completed transaction.
|
|
120
111
|
|
|
112
|
+
```bash
|
|
113
|
+
|
|
121
114
|
app.post("/payment/refund", async (req, res) => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
115
|
+
const refundData = await client.refundOrder({
|
|
116
|
+
merchOrderId: "order123",
|
|
117
|
+
refundRequestNo: "original-transaction-id",
|
|
118
|
+
refundReason: "customer request",
|
|
119
|
+
amount: "12",
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
res.json(refundData);
|
|
130
123
|
});
|
|
131
124
|
|
|
132
|
-
|
|
133
|
-
refundRequestNo must be unique
|
|
125
|
+
```
|
|
134
126
|
|
|
135
127
|
Refund amount must not exceed the original payment amount
|
|
136
128
|
|
|
@@ -138,16 +130,17 @@ Simulator Mode
|
|
|
138
130
|
|
|
139
131
|
To use the simulator provided by this package, set the mode to simulate.
|
|
140
132
|
|
|
133
|
+
```bash
|
|
141
134
|
import { TelebirrClient } from "telebirr-nodejs";
|
|
142
135
|
|
|
143
136
|
const client = new TelebirrClient({
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
137
|
+
mode: "simulate",
|
|
138
|
+
notifyUrl: "https://example.com/notify",
|
|
139
|
+
redirectUrl: "https://example.com/redirect",
|
|
140
|
+
http: true,
|
|
141
|
+
IntegrationOption: "C2B",
|
|
149
142
|
});
|
|
150
|
-
|
|
143
|
+
```
|
|
151
144
|
|
|
152
145
|
This simulator is for learning and development purposes only.
|
|
153
146
|
The simulation server is provided by this package, not by Telebirr.
|
|
@@ -175,17 +168,18 @@ https://developer.ethiotelecom.et/docs/H5%20C2B%20Web%20Payment%20Integration%20
|
|
|
175
168
|
RSA Key Generation
|
|
176
169
|
You can generate private and public keys instantly.
|
|
177
170
|
|
|
171
|
+
```bash
|
|
178
172
|
import { generateKeys } from "telebirr-nodejs";
|
|
179
173
|
|
|
180
174
|
generateKeys({
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
175
|
+
dir: process.cwd(),
|
|
176
|
+
privateKeyName: "telefy_private.pem",
|
|
177
|
+
publicKeyName: "telefy_public.pem",
|
|
178
|
+
overwrite: false,
|
|
185
179
|
});
|
|
186
180
|
|
|
181
|
+
```
|
|
187
182
|
|
|
188
183
|
This will generate two .pem files in your root directory.
|
|
189
184
|
|
|
190
185
|
The merchantPrivateKey option always accepts a string, so you must read the private key file using Node.js fs and pass the value to the client configuration.
|
|
191
|
-
```
|