telebirr-nodejs 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/README.md +79 -76
- package/package.json +2 -4
package/README.md
CHANGED
|
@@ -1,58 +1,60 @@
|
|
|
1
|
-
telebirr-nodejs
|
|
1
|
+
# telebirr-nodejs
|
|
2
2
|
|
|
3
|
-
A simple, opinionated Node.js SDK for integrating Telebirr C2B payments with automatic request signing, Fabric token handling, and checkout URL generation.
|
|
3
|
+
A simple, opinionated Node.js SDK for integrating **Telebirr C2B payments** with automatic request signing, Fabric token handling, and checkout URL generation.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
npm install telebirr-node
|
|
5
|
+
---
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Installation
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
```bash
|
|
10
|
+
npm install telebirr-nodejs
|
|
11
|
+
Required Configuration
|
|
12
|
+
Telebirr requires credentials and callback URLs.
|
|
11
13
|
You should never hard-code secrets. Always load them from process.env.
|
|
12
14
|
|
|
13
15
|
Required Credentials
|
|
14
|
-
Name
|
|
15
|
-
FABRIC_APP_ID
|
|
16
|
-
FABRIC_APP_SECRET
|
|
17
|
-
MERCHANT_APP_ID
|
|
18
|
-
MERCHANT_CODE
|
|
19
|
-
MERCHANT_PRIVATE_KEY
|
|
16
|
+
Name Description
|
|
17
|
+
FABRIC_APP_ID Fabric application ID
|
|
18
|
+
FABRIC_APP_SECRET Fabric application secret
|
|
19
|
+
MERCHANT_APP_ID Merchant application ID
|
|
20
|
+
MERCHANT_CODE Merchant code
|
|
21
|
+
MERCHANT_PRIVATE_KEY RSA private key (PEM string)
|
|
22
|
+
|
|
20
23
|
Required URLs
|
|
21
|
-
Name
|
|
22
|
-
TELEBIRR_NOTIFY_URL
|
|
23
|
-
TELEBIRR_REDIRECT_URL
|
|
24
|
+
Name Description
|
|
25
|
+
TELEBIRR_NOTIFY_URL Server-to-server callback URL
|
|
26
|
+
TELEBIRR_REDIRECT_URL User redirect URL after payment
|
|
27
|
+
|
|
24
28
|
Basic Setup
|
|
29
|
+
ts
|
|
30
|
+
Copy code
|
|
25
31
|
import { TelebirrClient } from "telebirr-nodejs";
|
|
26
32
|
|
|
27
33
|
const client = new TelebirrClient({
|
|
28
|
-
mode: "sandbox", // "simulate" | "sandbox" | "production"
|
|
34
|
+
mode: "sandbox", // "simulate" | "sandbox" | "production"
|
|
29
35
|
|
|
30
|
-
appId: process.env.FABRIC_APP_ID!,
|
|
31
|
-
appSecret: process.env.FABRIC_APP_SECRET!,
|
|
36
|
+
appId: process.env.FABRIC_APP_ID!,
|
|
37
|
+
appSecret: process.env.FABRIC_APP_SECRET!,
|
|
32
38
|
|
|
33
|
-
merchantAppId: process.env.MERCHANT_APP_ID!,
|
|
34
|
-
merchantCode: process.env.MERCHANT_CODE!,
|
|
35
|
-
privateKey: process.env.MERCHANT_PRIVATE_KEY!,
|
|
39
|
+
merchantAppId: process.env.MERCHANT_APP_ID!,
|
|
40
|
+
merchantCode: process.env.MERCHANT_CODE!,
|
|
41
|
+
privateKey: process.env.MERCHANT_PRIVATE_KEY!,
|
|
36
42
|
|
|
37
|
-
notifyUrl: process.env.TELEBIRR_NOTIFY_URL!,
|
|
38
|
-
redirectUrl: process.env.TELEBIRR_REDIRECT_URL!,
|
|
43
|
+
notifyUrl: process.env.TELEBIRR_NOTIFY_URL!,
|
|
44
|
+
redirectUrl: process.env.TELEBIRR_REDIRECT_URL!,
|
|
39
45
|
|
|
40
|
-
http: true, // allow HTTP in simulator mode
|
|
41
|
-
IntegrationOption: "C2B",
|
|
46
|
+
http: true, // allow HTTP in simulator mode
|
|
47
|
+
IntegrationOption: "C2B",
|
|
42
48
|
});
|
|
43
|
-
|
|
44
49
|
Important
|
|
45
|
-
|
|
46
50
|
MERCHANT_PRIVATE_KEY must be the full PEM string, including:
|
|
47
51
|
|
|
52
|
+
vbnet
|
|
53
|
+
Copy code
|
|
48
54
|
-----BEGIN RSA PRIVATE KEY-----
|
|
49
|
-
|
|
50
|
-
and
|
|
51
|
-
|
|
55
|
+
...
|
|
52
56
|
-----END RSA PRIVATE KEY-----
|
|
53
|
-
|
|
54
57
|
Example API Routes
|
|
55
|
-
|
|
56
58
|
Below is a complete demo using three routes:
|
|
57
59
|
|
|
58
60
|
POST /payment/initiate
|
|
@@ -62,22 +64,21 @@ GET /payment/status/:merchOrderId
|
|
|
62
64
|
POST /payment/refund
|
|
63
65
|
|
|
64
66
|
Initiate Payment
|
|
65
|
-
|
|
66
67
|
Creates an order and redirects the user to the Telebirr checkout page.
|
|
67
68
|
|
|
69
|
+
ts
|
|
70
|
+
Copy code
|
|
68
71
|
app.post("/payment/initiate", async (req, res) => {
|
|
69
|
-
const checkoutUrl = await client.preOrder({
|
|
70
|
-
merchOrderId: "order123",
|
|
71
|
-
title: "Phone",
|
|
72
|
-
amount: "12",
|
|
73
|
-
callbackInfo: "from web checkout",
|
|
72
|
+
const checkoutUrl = await client.preOrder({
|
|
73
|
+
merchOrderId: "order123",
|
|
74
|
+
title: "Phone",
|
|
75
|
+
amount: "12",
|
|
76
|
+
callbackInfo: "from web checkout",
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
res.redirect(checkoutUrl);
|
|
74
80
|
});
|
|
75
|
-
|
|
76
|
-
res.redirect(checkoutUrl);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
81
|
What happens internally
|
|
80
|
-
|
|
81
82
|
Fabric token is fetched and cached
|
|
82
83
|
|
|
83
84
|
Order is signed and created
|
|
@@ -86,22 +87,21 @@ Checkout URL is generated
|
|
|
86
87
|
|
|
87
88
|
User is redirected to Telebirr
|
|
88
89
|
|
|
89
|
-
If you are using a frontend framework (
|
|
90
|
+
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.
|
|
90
91
|
|
|
91
92
|
Query Payment Status
|
|
92
|
-
|
|
93
93
|
Used to check the payment result using the merchant order ID.
|
|
94
94
|
|
|
95
|
+
ts
|
|
96
|
+
Copy code
|
|
95
97
|
app.get("/payment/status/:merchOrderId", async (req, res) => {
|
|
96
|
-
const { merchOrderId } = req.params;
|
|
98
|
+
const { merchOrderId } = req.params;
|
|
97
99
|
|
|
98
|
-
const info = await client.queryOrder(merchOrderId);
|
|
100
|
+
const info = await client.queryOrder(merchOrderId);
|
|
99
101
|
|
|
100
|
-
res.json(info);
|
|
102
|
+
res.json(info);
|
|
101
103
|
});
|
|
102
|
-
|
|
103
104
|
Typical Use Cases
|
|
104
|
-
|
|
105
105
|
Payment confirmation pages
|
|
106
106
|
|
|
107
107
|
Background reconciliation jobs
|
|
@@ -109,47 +109,45 @@ Background reconciliation jobs
|
|
|
109
109
|
Admin dashboards
|
|
110
110
|
|
|
111
111
|
Refund Payment
|
|
112
|
-
|
|
113
112
|
Refunds a completed transaction.
|
|
114
113
|
|
|
114
|
+
ts
|
|
115
|
+
Copy code
|
|
115
116
|
app.post("/payment/refund", async (req, res) => {
|
|
116
|
-
const refundData = await client.refundOrder({
|
|
117
|
-
merchOrderId: "order123",
|
|
118
|
-
refundRequestNo: "original-transaction-id",
|
|
119
|
-
refundReason: "customer request",
|
|
120
|
-
amount: "12",
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
res.json(refundData);
|
|
117
|
+
const refundData = await client.refundOrder({
|
|
118
|
+
merchOrderId: "order123",
|
|
119
|
+
refundRequestNo: "original-transaction-id",
|
|
120
|
+
refundReason: "customer request",
|
|
121
|
+
amount: "12",
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
res.json(refundData);
|
|
124
125
|
});
|
|
125
|
-
|
|
126
126
|
Notes
|
|
127
|
-
|
|
128
127
|
refundRequestNo must be unique
|
|
129
128
|
|
|
130
129
|
Refund amount must not exceed the original payment amount
|
|
131
130
|
|
|
132
131
|
Simulator Mode
|
|
133
|
-
|
|
134
132
|
To use the simulator provided by this package, set the mode to simulate.
|
|
135
133
|
|
|
134
|
+
ts
|
|
135
|
+
Copy code
|
|
136
136
|
import { TelebirrClient } from "telebirr-nodejs";
|
|
137
137
|
|
|
138
138
|
const client = new TelebirrClient({
|
|
139
|
-
mode: "simulate",
|
|
140
|
-
notifyUrl: "https://example.com/notify",
|
|
141
|
-
redirectUrl: "https://example.com/redirect",
|
|
142
|
-
http: true,
|
|
143
|
-
IntegrationOption: "C2B",
|
|
139
|
+
mode: "simulate",
|
|
140
|
+
notifyUrl: "https://example.com/notify",
|
|
141
|
+
redirectUrl: "https://example.com/redirect",
|
|
142
|
+
http: true,
|
|
143
|
+
IntegrationOption: "C2B",
|
|
144
144
|
});
|
|
145
|
-
|
|
146
145
|
This simulator is for learning and development purposes only.
|
|
147
146
|
The simulation server is provided by this package, not by Telebirr.
|
|
148
147
|
|
|
149
148
|
For real testing, use Telebirr’s sandbox mode and whitelist your public IP address in the Telebirr portal.
|
|
150
149
|
|
|
151
150
|
Supported Features
|
|
152
|
-
|
|
153
151
|
Fabric token handling
|
|
154
152
|
|
|
155
153
|
RSA-SHA256 request signing
|
|
@@ -163,19 +161,24 @@ Refunds
|
|
|
163
161
|
Simulator support
|
|
164
162
|
|
|
165
163
|
Notify URL Handling
|
|
166
|
-
|
|
167
164
|
Please refer to Telebirr’s official documentation to correctly handle notify callbacks:
|
|
168
165
|
|
|
169
166
|
https://developer.ethiotelecom.et/docs/H5%20C2B%20Web%20Payment%20Integration%20Quick%20Guide/Notify_Callback
|
|
170
167
|
|
|
171
|
-
|
|
168
|
+
RSA Key Generation
|
|
169
|
+
You can generate private and public keys instantly.
|
|
170
|
+
|
|
171
|
+
ts
|
|
172
|
+
Copy code
|
|
172
173
|
import { generateKeys } from "telebirr-nodejs";
|
|
173
174
|
|
|
174
175
|
generateKeys({
|
|
175
|
-
dir: process.cwd(),
|
|
176
|
-
privateKeyName: "telefy_private.pem",
|
|
177
|
-
publicKeyName: "telefy_public.pem",
|
|
178
|
-
overwrite: false,
|
|
176
|
+
dir: process.cwd(),
|
|
177
|
+
privateKeyName: "telefy_private.pem",
|
|
178
|
+
publicKeyName: "telefy_public.pem",
|
|
179
|
+
overwrite: false,
|
|
179
180
|
});
|
|
181
|
+
This will generate two .pem files in your root directory.
|
|
180
182
|
|
|
181
|
-
|
|
183
|
+
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.
|
|
184
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "telebirr-nodejs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Unofficial Telebirr Node.js SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"typescript": "^5.9.3"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"axios": "^1.13.2",
|
|
27
26
|
"nanoid": "^5.1.6"
|
|
28
27
|
},
|
|
29
28
|
"keywords": [
|
|
@@ -31,8 +30,7 @@
|
|
|
31
30
|
"ethiopia",
|
|
32
31
|
"payment",
|
|
33
32
|
"fintech",
|
|
34
|
-
"nodejs"
|
|
35
|
-
"sdk"
|
|
33
|
+
"nodejs"
|
|
36
34
|
],
|
|
37
35
|
"author": "chernet manaye",
|
|
38
36
|
"license": "MIT"
|