@singularity-payments/express 0.1.0 → 1.0.0-alpha.3
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/dist/index.d.mts +69 -1
- package/dist/index.d.ts +69 -1
- package/dist/index.js +19 -27
- package/dist/index.mjs +19 -27
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Request, Response, NextFunction, Router } from 'express';
|
|
2
2
|
import { MpesaClient, MpesaConfig, MpesaClientOptions } from '@singularity-payments/core';
|
|
3
|
-
export { AccountBalanceCallback, AccountBalanceRequest, AccountBalanceResponse, B2BCallback, B2BRequest, B2BResponse, B2CCallback, B2CRequest, B2CResponse, C2BCallback, C2BRegisterRequest, C2BRegisterResponse,
|
|
3
|
+
export { AccountBalanceCallback, AccountBalanceRequest, AccountBalanceResponse, B2BCallback, B2BRequest, B2BResponse, B2CCallback, B2CRequest, B2CResponse, C2BCallback, C2BRegisterRequest, C2BRegisterResponse, GeneralTransactionStatusRequest, GeneralTransactionStatusResponse, MpesaClient, MpesaClientOptions, MpesaConfig, ParsedC2BCallback, ParsedCallbackData, ReversalCallback, ReversalRequest, ReversalResponse, STKCallback, STKPushRequest, STKPushResponse, TransactionStatusCallback, TransactionStatusRequest, TransactionStatusResponse } from '@singularity-payments/core';
|
|
4
4
|
|
|
5
5
|
interface MpesaMiddleware {
|
|
6
6
|
(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
@@ -32,9 +32,77 @@ interface MpesaRouteHandlers {
|
|
|
32
32
|
|
|
33
33
|
declare function createMpesaHandlers(client: MpesaClient): MpesaRouteHandlers;
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Create M-Pesa instance for Express
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // lib/mpesa.ts
|
|
41
|
+
* import { createMpesa } from "@singularity-payments/express";
|
|
42
|
+
*
|
|
43
|
+
* export const mpesa = createMpesa({
|
|
44
|
+
* consumerKey: process.env.MPESA_CONSUMER_KEY!,
|
|
45
|
+
* consumerSecret: process.env.MPESA_CONSUMER_SECRET!,
|
|
46
|
+
* passkey: process.env.MPESA_PASSKEY!,
|
|
47
|
+
* shortcode: process.env.MPESA_SHORTCODE!,
|
|
48
|
+
* environment: "sandbox",
|
|
49
|
+
* callbackUrl: `${process.env.APP_URL}/api/mpesa/callback`,
|
|
50
|
+
* resultUrl: `${process.env.APP_URL}/api/mpesa`,
|
|
51
|
+
* timeoutUrl: `${process.env.APP_URL}/api/mpesa`,
|
|
52
|
+
* }, {
|
|
53
|
+
* callbackOptions: {
|
|
54
|
+
* onSuccess: async (data) => {
|
|
55
|
+
* console.log("STK Payment successful:", data);
|
|
56
|
+
* // Save to database
|
|
57
|
+
* },
|
|
58
|
+
* onFailure: async (data) => {
|
|
59
|
+
* console.log("STK Payment failed:", data);
|
|
60
|
+
* },
|
|
61
|
+
* onB2CResult: async (data) => {
|
|
62
|
+
* console.log("B2C Result:", data);
|
|
63
|
+
* },
|
|
64
|
+
* onB2BResult: async (data) => {
|
|
65
|
+
* console.log("B2B Result:", data);
|
|
66
|
+
* },
|
|
67
|
+
* onAccountBalance: async (data) => {
|
|
68
|
+
* console.log("Balance:", data);
|
|
69
|
+
* },
|
|
70
|
+
* onTransactionStatus: async (data) => {
|
|
71
|
+
* console.log("Transaction Status:", data);
|
|
72
|
+
* },
|
|
73
|
+
* onReversal: async (data) => {
|
|
74
|
+
* console.log("Reversal:", data);
|
|
75
|
+
* },
|
|
76
|
+
* }
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // routes/mpesa.ts
|
|
83
|
+
* import express from "express";
|
|
84
|
+
* import { mpesa } from "../lib/mpesa";
|
|
85
|
+
*
|
|
86
|
+
* const router = express.Router();
|
|
87
|
+
*
|
|
88
|
+
* // Use the built-in router helper
|
|
89
|
+
* mpesa.router(router);
|
|
90
|
+
*
|
|
91
|
+
* export default router;
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
35
94
|
declare function createMpesa(config: MpesaConfig, options?: MpesaClientOptions): {
|
|
95
|
+
/**
|
|
96
|
+
* M-Pesa client instance for making API calls
|
|
97
|
+
*/
|
|
36
98
|
client: MpesaClient;
|
|
99
|
+
/**
|
|
100
|
+
* Individual handler functions for custom routing
|
|
101
|
+
*/
|
|
37
102
|
handlers: MpesaRouteHandlers;
|
|
103
|
+
/**
|
|
104
|
+
* Helper function to automatically register all routes
|
|
105
|
+
*/
|
|
38
106
|
router: (expressRouter: Router) => Router;
|
|
39
107
|
};
|
|
40
108
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Request, Response, NextFunction, Router } from 'express';
|
|
2
2
|
import { MpesaClient, MpesaConfig, MpesaClientOptions } from '@singularity-payments/core';
|
|
3
|
-
export { AccountBalanceCallback, AccountBalanceRequest, AccountBalanceResponse, B2BCallback, B2BRequest, B2BResponse, B2CCallback, B2CRequest, B2CResponse, C2BCallback, C2BRegisterRequest, C2BRegisterResponse,
|
|
3
|
+
export { AccountBalanceCallback, AccountBalanceRequest, AccountBalanceResponse, B2BCallback, B2BRequest, B2BResponse, B2CCallback, B2CRequest, B2CResponse, C2BCallback, C2BRegisterRequest, C2BRegisterResponse, GeneralTransactionStatusRequest, GeneralTransactionStatusResponse, MpesaClient, MpesaClientOptions, MpesaConfig, ParsedC2BCallback, ParsedCallbackData, ReversalCallback, ReversalRequest, ReversalResponse, STKCallback, STKPushRequest, STKPushResponse, TransactionStatusCallback, TransactionStatusRequest, TransactionStatusResponse } from '@singularity-payments/core';
|
|
4
4
|
|
|
5
5
|
interface MpesaMiddleware {
|
|
6
6
|
(req: Request, res: Response, next: NextFunction): Promise<void>;
|
|
@@ -32,9 +32,77 @@ interface MpesaRouteHandlers {
|
|
|
32
32
|
|
|
33
33
|
declare function createMpesaHandlers(client: MpesaClient): MpesaRouteHandlers;
|
|
34
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Create M-Pesa instance for Express
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* // lib/mpesa.ts
|
|
41
|
+
* import { createMpesa } from "@singularity-payments/express";
|
|
42
|
+
*
|
|
43
|
+
* export const mpesa = createMpesa({
|
|
44
|
+
* consumerKey: process.env.MPESA_CONSUMER_KEY!,
|
|
45
|
+
* consumerSecret: process.env.MPESA_CONSUMER_SECRET!,
|
|
46
|
+
* passkey: process.env.MPESA_PASSKEY!,
|
|
47
|
+
* shortcode: process.env.MPESA_SHORTCODE!,
|
|
48
|
+
* environment: "sandbox",
|
|
49
|
+
* callbackUrl: `${process.env.APP_URL}/api/mpesa/callback`,
|
|
50
|
+
* resultUrl: `${process.env.APP_URL}/api/mpesa`,
|
|
51
|
+
* timeoutUrl: `${process.env.APP_URL}/api/mpesa`,
|
|
52
|
+
* }, {
|
|
53
|
+
* callbackOptions: {
|
|
54
|
+
* onSuccess: async (data) => {
|
|
55
|
+
* console.log("STK Payment successful:", data);
|
|
56
|
+
* // Save to database
|
|
57
|
+
* },
|
|
58
|
+
* onFailure: async (data) => {
|
|
59
|
+
* console.log("STK Payment failed:", data);
|
|
60
|
+
* },
|
|
61
|
+
* onB2CResult: async (data) => {
|
|
62
|
+
* console.log("B2C Result:", data);
|
|
63
|
+
* },
|
|
64
|
+
* onB2BResult: async (data) => {
|
|
65
|
+
* console.log("B2B Result:", data);
|
|
66
|
+
* },
|
|
67
|
+
* onAccountBalance: async (data) => {
|
|
68
|
+
* console.log("Balance:", data);
|
|
69
|
+
* },
|
|
70
|
+
* onTransactionStatus: async (data) => {
|
|
71
|
+
* console.log("Transaction Status:", data);
|
|
72
|
+
* },
|
|
73
|
+
* onReversal: async (data) => {
|
|
74
|
+
* console.log("Reversal:", data);
|
|
75
|
+
* },
|
|
76
|
+
* }
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* // routes/mpesa.ts
|
|
83
|
+
* import express from "express";
|
|
84
|
+
* import { mpesa } from "../lib/mpesa";
|
|
85
|
+
*
|
|
86
|
+
* const router = express.Router();
|
|
87
|
+
*
|
|
88
|
+
* // Use the built-in router helper
|
|
89
|
+
* mpesa.router(router);
|
|
90
|
+
*
|
|
91
|
+
* export default router;
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
35
94
|
declare function createMpesa(config: MpesaConfig, options?: MpesaClientOptions): {
|
|
95
|
+
/**
|
|
96
|
+
* M-Pesa client instance for making API calls
|
|
97
|
+
*/
|
|
36
98
|
client: MpesaClient;
|
|
99
|
+
/**
|
|
100
|
+
* Individual handler functions for custom routing
|
|
101
|
+
*/
|
|
37
102
|
handlers: MpesaRouteHandlers;
|
|
103
|
+
/**
|
|
104
|
+
* Helper function to automatically register all routes
|
|
105
|
+
*/
|
|
38
106
|
router: (expressRouter: Router) => Router;
|
|
39
107
|
};
|
|
40
108
|
|
package/dist/index.js
CHANGED
|
@@ -76,11 +76,7 @@ function createMpesaHandlers(client) {
|
|
|
76
76
|
b2cResult: async (req, res) => {
|
|
77
77
|
try {
|
|
78
78
|
const body = req.body;
|
|
79
|
-
const
|
|
80
|
-
const response = await client.handleB2CResult(
|
|
81
|
-
body,
|
|
82
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
83
|
-
);
|
|
79
|
+
const response = await client.handleB2CCallback(body);
|
|
84
80
|
res.status(200).json(response);
|
|
85
81
|
} catch (error) {
|
|
86
82
|
console.error("B2C Result error:", error);
|
|
@@ -109,11 +105,7 @@ function createMpesaHandlers(client) {
|
|
|
109
105
|
b2bResult: async (req, res) => {
|
|
110
106
|
try {
|
|
111
107
|
const body = req.body;
|
|
112
|
-
const
|
|
113
|
-
const response = await client.handleB2BResult(
|
|
114
|
-
body,
|
|
115
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
116
|
-
);
|
|
108
|
+
const response = await client.handleB2BCallback(body);
|
|
117
109
|
res.status(200).json(response);
|
|
118
110
|
} catch (error) {
|
|
119
111
|
console.error("B2B Result error:", error);
|
|
@@ -142,12 +134,8 @@ function createMpesaHandlers(client) {
|
|
|
142
134
|
balanceResult: async (req, res) => {
|
|
143
135
|
try {
|
|
144
136
|
const body = req.body;
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
res.status(200).json({
|
|
148
|
-
ResultCode: 0,
|
|
149
|
-
ResultDesc: "Accepted"
|
|
150
|
-
});
|
|
137
|
+
const response = await client.handleAccountBalanceCallback(body);
|
|
138
|
+
res.status(200).json(response);
|
|
151
139
|
} catch (error) {
|
|
152
140
|
console.error("Balance Result error:", error);
|
|
153
141
|
res.status(200).json({
|
|
@@ -175,11 +163,7 @@ function createMpesaHandlers(client) {
|
|
|
175
163
|
reversalResult: async (req, res) => {
|
|
176
164
|
try {
|
|
177
165
|
const body = req.body;
|
|
178
|
-
const
|
|
179
|
-
const response = await client.handleReversalResult(
|
|
180
|
-
body,
|
|
181
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
182
|
-
);
|
|
166
|
+
const response = await client.handleReversalCallback(body);
|
|
183
167
|
res.status(200).json(response);
|
|
184
168
|
} catch (error) {
|
|
185
169
|
console.error("Reversal Result error:", error);
|
|
@@ -208,11 +192,7 @@ function createMpesaHandlers(client) {
|
|
|
208
192
|
statusResult: async (req, res) => {
|
|
209
193
|
try {
|
|
210
194
|
const body = req.body;
|
|
211
|
-
const
|
|
212
|
-
const response = await client.handleTransactionStatusResult(
|
|
213
|
-
body,
|
|
214
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
215
|
-
);
|
|
195
|
+
const response = await client.handleTransactionStatusCallback(body);
|
|
216
196
|
res.status(200).json(response);
|
|
217
197
|
} catch (error) {
|
|
218
198
|
console.error("Status Result error:", error);
|
|
@@ -469,7 +449,6 @@ function createMpesa(config, options) {
|
|
|
469
449
|
const router = (expressRouter) => {
|
|
470
450
|
expressRouter.post("/stk-push", handlers.stkPush);
|
|
471
451
|
expressRouter.post("/stk-query", handlers.stkQuery);
|
|
472
|
-
expressRouter.post("/callback", handlers.stkCallback);
|
|
473
452
|
expressRouter.post("/b2c", handlers.b2c);
|
|
474
453
|
expressRouter.post("/b2b", handlers.b2b);
|
|
475
454
|
expressRouter.post("/balance", handlers.balance);
|
|
@@ -477,8 +456,12 @@ function createMpesa(config, options) {
|
|
|
477
456
|
expressRouter.post("/reversal", handlers.reversal);
|
|
478
457
|
expressRouter.post("/register-c2b", handlers.registerC2B);
|
|
479
458
|
expressRouter.post("/generate-qr", handlers.generateQR);
|
|
459
|
+
expressRouter.post("/callback", handlers.stkCallback);
|
|
460
|
+
expressRouter.post("/stk-callback", handlers.stkCallback);
|
|
480
461
|
expressRouter.post("/c2b-validation", handlers.c2bValidation);
|
|
462
|
+
expressRouter.post("/validation", handlers.c2bValidation);
|
|
481
463
|
expressRouter.post("/c2b-confirmation", handlers.c2bConfirmation);
|
|
464
|
+
expressRouter.post("/confirmation", handlers.c2bConfirmation);
|
|
482
465
|
expressRouter.post("/b2c-result", handlers.b2cResult);
|
|
483
466
|
expressRouter.post("/b2c-timeout", handlers.b2cTimeout);
|
|
484
467
|
expressRouter.post("/b2b-result", handlers.b2bResult);
|
|
@@ -492,8 +475,17 @@ function createMpesa(config, options) {
|
|
|
492
475
|
return expressRouter;
|
|
493
476
|
};
|
|
494
477
|
return {
|
|
478
|
+
/**
|
|
479
|
+
* M-Pesa client instance for making API calls
|
|
480
|
+
*/
|
|
495
481
|
client,
|
|
482
|
+
/**
|
|
483
|
+
* Individual handler functions for custom routing
|
|
484
|
+
*/
|
|
496
485
|
handlers,
|
|
486
|
+
/**
|
|
487
|
+
* Helper function to automatically register all routes
|
|
488
|
+
*/
|
|
497
489
|
router
|
|
498
490
|
};
|
|
499
491
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -50,11 +50,7 @@ function createMpesaHandlers(client) {
|
|
|
50
50
|
b2cResult: async (req, res) => {
|
|
51
51
|
try {
|
|
52
52
|
const body = req.body;
|
|
53
|
-
const
|
|
54
|
-
const response = await client.handleB2CResult(
|
|
55
|
-
body,
|
|
56
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
57
|
-
);
|
|
53
|
+
const response = await client.handleB2CCallback(body);
|
|
58
54
|
res.status(200).json(response);
|
|
59
55
|
} catch (error) {
|
|
60
56
|
console.error("B2C Result error:", error);
|
|
@@ -83,11 +79,7 @@ function createMpesaHandlers(client) {
|
|
|
83
79
|
b2bResult: async (req, res) => {
|
|
84
80
|
try {
|
|
85
81
|
const body = req.body;
|
|
86
|
-
const
|
|
87
|
-
const response = await client.handleB2BResult(
|
|
88
|
-
body,
|
|
89
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
90
|
-
);
|
|
82
|
+
const response = await client.handleB2BCallback(body);
|
|
91
83
|
res.status(200).json(response);
|
|
92
84
|
} catch (error) {
|
|
93
85
|
console.error("B2B Result error:", error);
|
|
@@ -116,12 +108,8 @@ function createMpesaHandlers(client) {
|
|
|
116
108
|
balanceResult: async (req, res) => {
|
|
117
109
|
try {
|
|
118
110
|
const body = req.body;
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
res.status(200).json({
|
|
122
|
-
ResultCode: 0,
|
|
123
|
-
ResultDesc: "Accepted"
|
|
124
|
-
});
|
|
111
|
+
const response = await client.handleAccountBalanceCallback(body);
|
|
112
|
+
res.status(200).json(response);
|
|
125
113
|
} catch (error) {
|
|
126
114
|
console.error("Balance Result error:", error);
|
|
127
115
|
res.status(200).json({
|
|
@@ -149,11 +137,7 @@ function createMpesaHandlers(client) {
|
|
|
149
137
|
reversalResult: async (req, res) => {
|
|
150
138
|
try {
|
|
151
139
|
const body = req.body;
|
|
152
|
-
const
|
|
153
|
-
const response = await client.handleReversalResult(
|
|
154
|
-
body,
|
|
155
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
156
|
-
);
|
|
140
|
+
const response = await client.handleReversalCallback(body);
|
|
157
141
|
res.status(200).json(response);
|
|
158
142
|
} catch (error) {
|
|
159
143
|
console.error("Reversal Result error:", error);
|
|
@@ -182,11 +166,7 @@ function createMpesaHandlers(client) {
|
|
|
182
166
|
statusResult: async (req, res) => {
|
|
183
167
|
try {
|
|
184
168
|
const body = req.body;
|
|
185
|
-
const
|
|
186
|
-
const response = await client.handleTransactionStatusResult(
|
|
187
|
-
body,
|
|
188
|
-
typeof ipAddress === "string" ? ipAddress : void 0
|
|
189
|
-
);
|
|
169
|
+
const response = await client.handleTransactionStatusCallback(body);
|
|
190
170
|
res.status(200).json(response);
|
|
191
171
|
} catch (error) {
|
|
192
172
|
console.error("Status Result error:", error);
|
|
@@ -445,7 +425,6 @@ function createMpesa(config, options) {
|
|
|
445
425
|
const router = (expressRouter) => {
|
|
446
426
|
expressRouter.post("/stk-push", handlers.stkPush);
|
|
447
427
|
expressRouter.post("/stk-query", handlers.stkQuery);
|
|
448
|
-
expressRouter.post("/callback", handlers.stkCallback);
|
|
449
428
|
expressRouter.post("/b2c", handlers.b2c);
|
|
450
429
|
expressRouter.post("/b2b", handlers.b2b);
|
|
451
430
|
expressRouter.post("/balance", handlers.balance);
|
|
@@ -453,8 +432,12 @@ function createMpesa(config, options) {
|
|
|
453
432
|
expressRouter.post("/reversal", handlers.reversal);
|
|
454
433
|
expressRouter.post("/register-c2b", handlers.registerC2B);
|
|
455
434
|
expressRouter.post("/generate-qr", handlers.generateQR);
|
|
435
|
+
expressRouter.post("/callback", handlers.stkCallback);
|
|
436
|
+
expressRouter.post("/stk-callback", handlers.stkCallback);
|
|
456
437
|
expressRouter.post("/c2b-validation", handlers.c2bValidation);
|
|
438
|
+
expressRouter.post("/validation", handlers.c2bValidation);
|
|
457
439
|
expressRouter.post("/c2b-confirmation", handlers.c2bConfirmation);
|
|
440
|
+
expressRouter.post("/confirmation", handlers.c2bConfirmation);
|
|
458
441
|
expressRouter.post("/b2c-result", handlers.b2cResult);
|
|
459
442
|
expressRouter.post("/b2c-timeout", handlers.b2cTimeout);
|
|
460
443
|
expressRouter.post("/b2b-result", handlers.b2bResult);
|
|
@@ -468,8 +451,17 @@ function createMpesa(config, options) {
|
|
|
468
451
|
return expressRouter;
|
|
469
452
|
};
|
|
470
453
|
return {
|
|
454
|
+
/**
|
|
455
|
+
* M-Pesa client instance for making API calls
|
|
456
|
+
*/
|
|
471
457
|
client,
|
|
458
|
+
/**
|
|
459
|
+
* Individual handler functions for custom routing
|
|
460
|
+
*/
|
|
472
461
|
handlers,
|
|
462
|
+
/**
|
|
463
|
+
* Helper function to automatically register all routes
|
|
464
|
+
*/
|
|
473
465
|
router
|
|
474
466
|
};
|
|
475
467
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@singularity-payments/express",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.3",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"express": ">=4.0.0"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@singularity-payments/core": "0.
|
|
34
|
+
"@singularity-payments/core": "1.0.0-alpha.3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/express": "^4.17.21",
|