@openfn/language-mpesa 1.0.0

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.
@@ -0,0 +1,39 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "properties": {
4
+ "baseUrl": {
5
+ "title": "Base URL",
6
+ "description": "The base URL (https://api.safaricom.co.ke)",
7
+ "format": "uri",
8
+ "minLength": 1,
9
+ "examples": ["https://api.safaricom.co.ke"]
10
+ },
11
+ "short_code": {
12
+ "title": "Short code",
13
+ "type": "string",
14
+ "description": "This is the organization's shortcode used to identify YOUR organization. Usually a 5 to 6-digit account number",
15
+ "examples": ["654321"]
16
+ },
17
+ "pass_key": {
18
+ "title": "Pass key",
19
+ "type": "string",
20
+ "description": "Your app's pass key sent to you via email once you go live",
21
+ "examples": ["bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919"]
22
+ },
23
+ "consumer_key": {
24
+ "title": "Consumer key",
25
+ "type": "string",
26
+ "description": "Your app's consumer key sent to you via email once you go live",
27
+ "examples": ["GA2PNftcXvv2uC2VdgNjxXyvF8eZ4td6iBsL7N7N2ZFnXzf3"]
28
+ },
29
+ "consumer_secret": {
30
+ "title": "Consumer key",
31
+ "type": "string",
32
+ "description": "Your app's consumer secret sent to you via email once you go live",
33
+ "examples": ["zKjr2j0jjihCtzIwzGtgnV0YgP82jZf1hY9RW6HygntWG6AGuf4tAUe3E5tPtiyA"]
34
+ }
35
+ },
36
+ "type": "object",
37
+ "additionalProperties": true,
38
+ "required": ["short_code", "pass_key", "consumer_key", "consumer_secret"]
39
+ }
package/dist/index.cjs ADDED
@@ -0,0 +1,237 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+
25
+ // src/index.js
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ buyGoods: () => buyGoods,
29
+ checkTransactionStatus: () => checkTransactionStatus,
30
+ cursor: () => import_language_common2.cursor,
31
+ dataPath: () => import_language_common2.dataPath,
32
+ dataValue: () => import_language_common2.dataValue,
33
+ dateFns: () => import_language_common2.dateFns,
34
+ default: () => src_default,
35
+ each: () => import_language_common2.each,
36
+ field: () => import_language_common2.field,
37
+ fields: () => import_language_common2.fields,
38
+ fn: () => import_language_common2.fn,
39
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
40
+ merge: () => import_language_common2.merge,
41
+ registerUrl: () => registerUrl,
42
+ remitTax: () => remitTax,
43
+ request: () => request2,
44
+ sourceValue: () => import_language_common2.sourceValue,
45
+ stkPush: () => stkPush
46
+ });
47
+ module.exports = __toCommonJS(src_exports);
48
+
49
+ // src/Adaptor.js
50
+ var Adaptor_exports = {};
51
+ __export(Adaptor_exports, {
52
+ buyGoods: () => buyGoods,
53
+ checkTransactionStatus: () => checkTransactionStatus,
54
+ cursor: () => import_language_common2.cursor,
55
+ dataPath: () => import_language_common2.dataPath,
56
+ dataValue: () => import_language_common2.dataValue,
57
+ dateFns: () => import_language_common2.dateFns,
58
+ each: () => import_language_common2.each,
59
+ field: () => import_language_common2.field,
60
+ fields: () => import_language_common2.fields,
61
+ fn: () => import_language_common2.fn,
62
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
63
+ merge: () => import_language_common2.merge,
64
+ registerUrl: () => registerUrl,
65
+ remitTax: () => remitTax,
66
+ request: () => request2,
67
+ sourceValue: () => import_language_common2.sourceValue,
68
+ stkPush: () => stkPush
69
+ });
70
+ var import_util2 = require("@openfn/language-common/util");
71
+
72
+ // src/Utils.js
73
+ var import_language_common = require("@openfn/language-common");
74
+ var import_util = require("@openfn/language-common/util");
75
+ var import_node_path = __toESM(require("path"), 1);
76
+ var defaultBaseUrl = "https://api.safaricom.co.ke";
77
+ var access_token;
78
+ var prepareNextState = (state, response) => {
79
+ const { body, ...responseWithoutBody } = response;
80
+ if (!state.references) {
81
+ state.references = [];
82
+ }
83
+ return {
84
+ ...(0, import_language_common.composeNextState)(state, response.body),
85
+ response: responseWithoutBody
86
+ };
87
+ };
88
+ var pad = (num) => num < 10 ? String(num).padStart(2, "0") : num;
89
+ var getTimestamp = () => {
90
+ const date = new Date();
91
+ return date.getFullYear().toString() + pad(date.getMonth() + 1) + pad(date.getDate()) + pad(date.getHours()) + pad(date.getMinutes()) + pad(date.getSeconds());
92
+ };
93
+ var getAccessToken = async (configuration) => {
94
+ const { consumer_key, consumer_secret, baseUrl = defaultBaseUrl } = configuration;
95
+ const auth_header = (0, import_util.makeBasicAuthHeader)(consumer_key, consumer_secret);
96
+ const { body: { access_token: access_token2 } } = await (0, import_util.request)("GET", "/oauth/v1/generate?grant_type=client_credentials", {
97
+ headers: {
98
+ ...auth_header,
99
+ "Content-Type": "application/json"
100
+ },
101
+ baseUrl
102
+ });
103
+ return access_token2;
104
+ };
105
+ var request = async (state, method, path, options) => {
106
+ var _a;
107
+ const { baseUrl = defaultBaseUrl } = state.configuration;
108
+ const errors = {
109
+ 404: "Page not found"
110
+ };
111
+ if (!access_token && !((_a = options.headers) == null ? void 0 : _a.Authorization)) {
112
+ access_token = await getAccessToken(state.configuration);
113
+ }
114
+ let opts = {
115
+ parseAs: "json",
116
+ errors,
117
+ baseUrl,
118
+ ...options,
119
+ headers: {
120
+ "content-type": "application/json",
121
+ "Authorization": `Bearer ${access_token}`,
122
+ ...options.headers
123
+ }
124
+ };
125
+ const safePath = import_node_path.default.join(path);
126
+ return (0, import_util.request)(method, safePath, opts).then(import_util.logResponse);
127
+ };
128
+
129
+ // src/Adaptor.js
130
+ var import_language_common2 = require("@openfn/language-common");
131
+ function stkPush(data, options = {}) {
132
+ return async (state) => {
133
+ const { short_code, pass_key } = state.configuration;
134
+ const timestamp = getTimestamp();
135
+ const password = short_code + pass_key + timestamp;
136
+ const body = {
137
+ ...data,
138
+ BusinessShortCode: short_code,
139
+ Password: Buffer.from(password).toString("base64"),
140
+ Timestamp: timestamp,
141
+ TransactionType: "CustomerPayBillOnline"
142
+ };
143
+ console.log("Initiating STK Push request...");
144
+ const response = await request(state, "POST", "/mpesa/stkpush/v1/processrequest", { ...options, body });
145
+ return prepareNextState(state, response);
146
+ };
147
+ }
148
+ function checkTransactionStatus(data, options = {}) {
149
+ return async (state) => {
150
+ const { short_code, pass_key } = state.configuration;
151
+ const timestamp = getTimestamp();
152
+ const password = short_code + pass_key + timestamp;
153
+ const body = {
154
+ BusinessShortCode: short_code,
155
+ Password: Buffer.from(password).toString("base64"),
156
+ Timestamp: timestamp,
157
+ CommandID: "TransactionStatusQuery",
158
+ ...data
159
+ };
160
+ console.log("Initiating check transaction request...");
161
+ const response = await request(state, "POST", "/mpesa/transactionstatus/v1/query", { ...options, body });
162
+ return prepareNextState(state, response);
163
+ };
164
+ }
165
+ function registerUrl(data, options) {
166
+ return async (state) => {
167
+ console.log("Initiating register URL request...");
168
+ const response = await request(state, "POST", "/mpesa/c2b/v1/registerurl", { ...options, body: { ...data } });
169
+ return prepareNextState(state, response);
170
+ };
171
+ }
172
+ function remitTax(data, options = {}) {
173
+ return async (state) => {
174
+ const body = {
175
+ SenderIdentifierType: "4",
176
+ RecieverIdentifierType: "4",
177
+ PartyB: 572572,
178
+ CommandID: "PayTaxToKRA",
179
+ ...data
180
+ };
181
+ console.log("Initiating remit tax request...");
182
+ const response = await request(state, "POST", "/mpesa/b2b/v1/remittax", { ...options, body });
183
+ return prepareNextState(state, response);
184
+ };
185
+ }
186
+ function buyGoods(data, options = {}) {
187
+ return async (state) => {
188
+ const body = {
189
+ CommandID: "BusinessBuyGoods",
190
+ SenderIdentifierType: "4",
191
+ RecieverIdentifierType: "4",
192
+ ...data
193
+ };
194
+ console.log("Initiating buy goods request...");
195
+ const response = await request(state, "POST", "/mpesa/b2b/v1/paymentrequest", { ...options, body });
196
+ return prepareNextState(state, response);
197
+ };
198
+ }
199
+ function request2(method, path, body, options = {}) {
200
+ return async (state) => {
201
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedoptions] = (0, import_util2.expandReferences)(state, method, path, body, options);
202
+ console.log(`Initiating ${method} request to: ${resolvedPath}`);
203
+ const response = await request(
204
+ state,
205
+ resolvedMethod,
206
+ resolvedPath,
207
+ {
208
+ body: resolvedBody,
209
+ ...resolvedoptions
210
+ }
211
+ );
212
+ return prepareNextState(state, response);
213
+ };
214
+ }
215
+
216
+ // src/index.js
217
+ var src_default = Adaptor_exports;
218
+ // Annotate the CommonJS export names for ESM import in node:
219
+ 0 && (module.exports = {
220
+ buyGoods,
221
+ checkTransactionStatus,
222
+ cursor,
223
+ dataPath,
224
+ dataValue,
225
+ dateFns,
226
+ each,
227
+ field,
228
+ fields,
229
+ fn,
230
+ lastReferenceValue,
231
+ merge,
232
+ registerUrl,
233
+ remitTax,
234
+ request,
235
+ sourceValue,
236
+ stkPush
237
+ });
package/dist/index.js ADDED
@@ -0,0 +1,211 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/Adaptor.js
8
+ var Adaptor_exports = {};
9
+ __export(Adaptor_exports, {
10
+ buyGoods: () => buyGoods,
11
+ checkTransactionStatus: () => checkTransactionStatus,
12
+ cursor: () => cursor,
13
+ dataPath: () => dataPath,
14
+ dataValue: () => dataValue,
15
+ dateFns: () => dateFns,
16
+ each: () => each,
17
+ field: () => field,
18
+ fields: () => fields,
19
+ fn: () => fn,
20
+ lastReferenceValue: () => lastReferenceValue,
21
+ merge: () => merge,
22
+ registerUrl: () => registerUrl,
23
+ remitTax: () => remitTax,
24
+ request: () => request2,
25
+ sourceValue: () => sourceValue,
26
+ stkPush: () => stkPush
27
+ });
28
+ import { expandReferences } from "@openfn/language-common/util";
29
+
30
+ // src/Utils.js
31
+ import { composeNextState } from "@openfn/language-common";
32
+ import {
33
+ request as commonRequest,
34
+ logResponse,
35
+ makeBasicAuthHeader
36
+ } from "@openfn/language-common/util";
37
+ import nodepath from "path";
38
+ var defaultBaseUrl = "https://api.safaricom.co.ke";
39
+ var access_token;
40
+ var prepareNextState = (state, response) => {
41
+ const { body, ...responseWithoutBody } = response;
42
+ if (!state.references) {
43
+ state.references = [];
44
+ }
45
+ return {
46
+ ...composeNextState(state, response.body),
47
+ response: responseWithoutBody
48
+ };
49
+ };
50
+ var pad = (num) => num < 10 ? String(num).padStart(2, "0") : num;
51
+ var getTimestamp = () => {
52
+ const date = new Date();
53
+ return date.getFullYear().toString() + pad(date.getMonth() + 1) + pad(date.getDate()) + pad(date.getHours()) + pad(date.getMinutes()) + pad(date.getSeconds());
54
+ };
55
+ var getAccessToken = async (configuration) => {
56
+ const { consumer_key, consumer_secret, baseUrl = defaultBaseUrl } = configuration;
57
+ const auth_header = makeBasicAuthHeader(consumer_key, consumer_secret);
58
+ const { body: { access_token: access_token2 } } = await commonRequest("GET", "/oauth/v1/generate?grant_type=client_credentials", {
59
+ headers: {
60
+ ...auth_header,
61
+ "Content-Type": "application/json"
62
+ },
63
+ baseUrl
64
+ });
65
+ return access_token2;
66
+ };
67
+ var request = async (state, method, path, options) => {
68
+ var _a;
69
+ const { baseUrl = defaultBaseUrl } = state.configuration;
70
+ const errors = {
71
+ 404: "Page not found"
72
+ };
73
+ if (!access_token && !((_a = options.headers) == null ? void 0 : _a.Authorization)) {
74
+ access_token = await getAccessToken(state.configuration);
75
+ }
76
+ let opts = {
77
+ parseAs: "json",
78
+ errors,
79
+ baseUrl,
80
+ ...options,
81
+ headers: {
82
+ "content-type": "application/json",
83
+ "Authorization": `Bearer ${access_token}`,
84
+ ...options.headers
85
+ }
86
+ };
87
+ const safePath = nodepath.join(path);
88
+ return commonRequest(method, safePath, opts).then(logResponse);
89
+ };
90
+
91
+ // src/Adaptor.js
92
+ import {
93
+ dataPath,
94
+ dataValue,
95
+ dateFns,
96
+ cursor,
97
+ each,
98
+ field,
99
+ fields,
100
+ fn,
101
+ lastReferenceValue,
102
+ merge,
103
+ sourceValue
104
+ } from "@openfn/language-common";
105
+ function stkPush(data, options = {}) {
106
+ return async (state) => {
107
+ const { short_code, pass_key } = state.configuration;
108
+ const timestamp = getTimestamp();
109
+ const password = short_code + pass_key + timestamp;
110
+ const body = {
111
+ ...data,
112
+ BusinessShortCode: short_code,
113
+ Password: Buffer.from(password).toString("base64"),
114
+ Timestamp: timestamp,
115
+ TransactionType: "CustomerPayBillOnline"
116
+ };
117
+ console.log("Initiating STK Push request...");
118
+ const response = await request(state, "POST", "/mpesa/stkpush/v1/processrequest", { ...options, body });
119
+ return prepareNextState(state, response);
120
+ };
121
+ }
122
+ function checkTransactionStatus(data, options = {}) {
123
+ return async (state) => {
124
+ const { short_code, pass_key } = state.configuration;
125
+ const timestamp = getTimestamp();
126
+ const password = short_code + pass_key + timestamp;
127
+ const body = {
128
+ BusinessShortCode: short_code,
129
+ Password: Buffer.from(password).toString("base64"),
130
+ Timestamp: timestamp,
131
+ CommandID: "TransactionStatusQuery",
132
+ ...data
133
+ };
134
+ console.log("Initiating check transaction request...");
135
+ const response = await request(state, "POST", "/mpesa/transactionstatus/v1/query", { ...options, body });
136
+ return prepareNextState(state, response);
137
+ };
138
+ }
139
+ function registerUrl(data, options) {
140
+ return async (state) => {
141
+ console.log("Initiating register URL request...");
142
+ const response = await request(state, "POST", "/mpesa/c2b/v1/registerurl", { ...options, body: { ...data } });
143
+ return prepareNextState(state, response);
144
+ };
145
+ }
146
+ function remitTax(data, options = {}) {
147
+ return async (state) => {
148
+ const body = {
149
+ SenderIdentifierType: "4",
150
+ RecieverIdentifierType: "4",
151
+ PartyB: 572572,
152
+ CommandID: "PayTaxToKRA",
153
+ ...data
154
+ };
155
+ console.log("Initiating remit tax request...");
156
+ const response = await request(state, "POST", "/mpesa/b2b/v1/remittax", { ...options, body });
157
+ return prepareNextState(state, response);
158
+ };
159
+ }
160
+ function buyGoods(data, options = {}) {
161
+ return async (state) => {
162
+ const body = {
163
+ CommandID: "BusinessBuyGoods",
164
+ SenderIdentifierType: "4",
165
+ RecieverIdentifierType: "4",
166
+ ...data
167
+ };
168
+ console.log("Initiating buy goods request...");
169
+ const response = await request(state, "POST", "/mpesa/b2b/v1/paymentrequest", { ...options, body });
170
+ return prepareNextState(state, response);
171
+ };
172
+ }
173
+ function request2(method, path, body, options = {}) {
174
+ return async (state) => {
175
+ const [resolvedMethod, resolvedPath, resolvedBody, resolvedoptions] = expandReferences(state, method, path, body, options);
176
+ console.log(`Initiating ${method} request to: ${resolvedPath}`);
177
+ const response = await request(
178
+ state,
179
+ resolvedMethod,
180
+ resolvedPath,
181
+ {
182
+ body: resolvedBody,
183
+ ...resolvedoptions
184
+ }
185
+ );
186
+ return prepareNextState(state, response);
187
+ };
188
+ }
189
+
190
+ // src/index.js
191
+ var src_default = Adaptor_exports;
192
+ export {
193
+ buyGoods,
194
+ checkTransactionStatus,
195
+ cursor,
196
+ dataPath,
197
+ dataValue,
198
+ dateFns,
199
+ src_default as default,
200
+ each,
201
+ field,
202
+ fields,
203
+ fn,
204
+ lastReferenceValue,
205
+ merge,
206
+ registerUrl,
207
+ remitTax,
208
+ request2 as request,
209
+ sourceValue,
210
+ stkPush
211
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@openfn/language-mpesa",
3
+ "version": "1.0.0",
4
+ "description": "OpenFn mpesa adaptor",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/index.js",
9
+ "types": "./types/index.d.ts",
10
+ "require": "./dist/index.cjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "author": "Open Function Group",
15
+ "license": "LGPLv3",
16
+ "files": [
17
+ "dist/",
18
+ "types/",
19
+ "ast.json",
20
+ "configuration-schema.json"
21
+ ],
22
+ "dependencies": {
23
+ "@openfn/language-common": "2.3.1"
24
+ },
25
+ "devDependencies": {
26
+ "assertion-error": "2.0.0",
27
+ "chai": "4.3.6",
28
+ "deep-eql": "4.1.1",
29
+ "esno": "^0.16.3",
30
+ "mocha": "^10.7.3",
31
+ "rimraf": "3.0.2",
32
+ "undici": "^5.22.1"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/openfn/adaptors.git"
37
+ },
38
+ "types": "types/index.d.ts",
39
+ "main": "dist/index.cjs",
40
+ "scripts": {
41
+ "build": "pnpm clean && build-adaptor mpesa",
42
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
43
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
44
+ "clean": "rimraf dist types docs",
45
+ "pack": "pnpm pack --pack-destination ../../dist",
46
+ "lint": "eslint src"
47
+ }
48
+ }