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