fapshi 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.
Files changed (3) hide show
  1. package/README.md +162 -0
  2. package/index.js +292 -0
  3. package/package.json +25 -0
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # FAPSHI Payment API Client
2
+
3
+ A client for interacting with the FAPSHI payment API. This module allows developers to initiate payments, check payment statuses, and manage transactions.
4
+
5
+ ## Installation
6
+
7
+ To install this module, you can use npm:
8
+
9
+ ```bash
10
+ npm install fapshi
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Here's how to use the FAPSHI module in your application:
16
+ Importing the Module.
17
+
18
+ ```js
19
+ const FAPSHI = require('fapshi');
20
+ ```
21
+
22
+ ## Initializing the Client
23
+
24
+ You need to create an instance of the FAPSHI class by providing your username and API key.
25
+
26
+ ````js
27
+ const fapshi = new FAPSHI('your_api_user', 'your_api_key');
28
+ ```
29
+
30
+ Available Methods
31
+ ---------------------
32
+ Here is an overview of the available methods in the client:
33
+
34
+ ### 1. initiatePay(data)
35
+
36
+ This method initiates the payment process.
37
+ Parameters:
38
+
39
+ data: An object with the necessary properties to initiate the payment.
40
+ Example:
41
+
42
+ ````js
43
+
44
+ const paymentData = {
45
+ amount: 1000, // Amount in XAF
46
+ email: "user@example.com",
47
+ userId: "user123",
48
+ externalId: "external_id_123",
49
+ redirectUrl: "http://example.com/redirect",
50
+ message: "Thank you for your payment"
51
+ };
52
+ const response = await fapshi.initiatePay(paymentData);
53
+ ````
54
+
55
+ ### 2. directPay(data)
56
+
57
+ This method sends a payment request directly to a mobile phone.
58
+
59
+ data: An object with the necessary properties to initiate the payment.
60
+
61
+ ````js
62
+ const directPayData = {
63
+ amount: 1000,
64
+ phone: "61234567",
65
+ medium: "mobile",
66
+ name: "John Doe",
67
+ email: "john@example.com",
68
+ userId: "user123",
69
+ externalId: "external_id_123",
70
+ message: "Payment for services"
71
+ };
72
+ const response = await fapshi.directPay(directPayData);
73
+ `````
74
+
75
+ ### 3. paymentStatus(transId)
76
+
77
+ Checks the status of a payment using the transaction ID.
78
+
79
+ Parameters:
80
+
81
+ transId: The transaction ID.
82
+
83
+ ````js
84
+ const statusResponse = await fapshi.paymentStatus("transaction_id");
85
+
86
+ ````
87
+
88
+ ### 4. expirePay(transId)
89
+
90
+ Expires a transaction.
91
+
92
+ Parameters:
93
+
94
+ transId: The transaction ID to expire.
95
+
96
+ ``js
97
+ const expireResponse = await fapshi.expirePay("transaction_id");
98
+ ``
99
+
100
+ ### 5. userTrans(userId)
101
+
102
+ Retrieves transactions for a specific user.
103
+
104
+ Parameters:
105
+
106
+ userId: The user's ID.
107
+
108
+ ````js
109
+ const transactions = await fapshi.userTrans("user123");
110
+ ````
111
+
112
+ ### 6. balance()
113
+
114
+ Retrieves the service balance.
115
+
116
+ ````js
117
+ const balanceResponse = await fapshi.balance();
118
+ ````
119
+
120
+ ### 7. payout(data)
121
+
122
+ Performs a payout to the specified phone number.
123
+
124
+ Parameters:
125
+
126
+ data: An object containing payout information.
127
+
128
+ ````js
129
+ const payoutData = {
130
+ amount: 1000,
131
+ phone: "61234567",
132
+ medium: "mobile",
133
+ name: "John Doe",
134
+ email: "john@example.com",
135
+ userId: "user123",
136
+ externalId: "external_id_123",
137
+ message: "Payout to user"
138
+ };
139
+ const payoutResponse = await fapshi.payout(payoutData);
140
+
141
+ ````
142
+
143
+ ### 8. search(params)
144
+
145
+ Searches transactions based on specified criteria.
146
+
147
+ Parameters:
148
+
149
+ params: An object containing search criteria such as status, medium, start, end, amt, limit, and sort
150
+
151
+ ````js
152
+ const searchParams = {
153
+ status: "successful",
154
+ medium: "mobile money",
155
+ start: "2023-01-01",
156
+ end: "2023-12-31",
157
+ amt: 100,
158
+ limit: 10,
159
+ sort: "asc"
160
+ };
161
+ const searchResults = await fapshi.search(searchParams);
162
+ ````
package/index.js ADDED
@@ -0,0 +1,292 @@
1
+ const axios = require("axios");
2
+ const baseUrl = "https://live.fapshi.com";
3
+
4
+
5
+ module.exports =class FAPSHI {
6
+ /**
7
+ *
8
+ * @param {string} user : user name get for an services
9
+ * @param {string} key : api key access
10
+ */
11
+ constructor(user, key) {
12
+ if(!user||key) throw new Error("Mention your user name and api key ");
13
+
14
+ this.apiuser = user;
15
+ this.key = key;
16
+
17
+ this.headers = {
18
+ apiuser: this.apiuser,
19
+ apikey: this.key,
20
+ };
21
+ }
22
+ /**
23
+ *This function returns an object with the link were a user is to be redirected in order to complete his payment
24
+
25
+ *Below is a parameter template. Just amount is required
26
+
27
+ data = {
28
+ "amount": Integer ,
29
+ "email": String,
30
+ "userId": String,
31
+ "externalId": String,
32
+ "redirectUrl": String,
33
+ "message": String
34
+ }
35
+ */
36
+ initiatePay(data) {
37
+ return new Promise(async function (resolve) {
38
+ try {
39
+ if (!data?.amount) resolve(error("amount required", 400));
40
+ if (!Number.isInteger(data.amount))
41
+ resolve(error("amount must be of type integer", 400));
42
+ if (data.amount < 100)
43
+ resolve(error("amount cannot be less than 100 XAF", 400));
44
+
45
+ const config = {
46
+ method: "post",
47
+ url: baseUrl + "/initiate-pay",
48
+ headers: headers,
49
+ data: data,
50
+ };
51
+ const response = await axios(config);
52
+ response.data.statusCode = response.status;
53
+ resolve(response.data);
54
+ } catch (e) {
55
+ e.response.data.statusCode = e?.response?.status;
56
+ resolve(e.response.data);
57
+ }
58
+ });
59
+ }
60
+
61
+ /**
62
+ *This function directly initiates a payment request to a user's mobile device and
63
+ returns an object with a transId property that is used to get the status of the payment
64
+
65
+ *Below is a parameter template. amount and phone are required
66
+
67
+ data = {
68
+ "amount": Integer ,
69
+ "phone": String ,
70
+ "medium": String,
71
+ "name": String,
72
+ "email": String,
73
+ "userId": String,
74
+ "externalId": String,
75
+ "message": String
76
+ }
77
+ */
78
+ directPay(data) {
79
+ return new Promise(async function (resolve) {
80
+ try {
81
+ if (!data?.amount) resolve(error("amount required", 400));
82
+ if (!Number.isInteger(data.amount))
83
+ resolve(error("amount must be of type integer", 400));
84
+ if (data.amount < 100)
85
+ resolve(error("amount cannot be less than 100 XAF", 400));
86
+ if (!data?.phone) resolve(error("phone number required", 400));
87
+ if (typeof data.phone !== "string")
88
+ resolve(error("phone must be of type string", 400));
89
+ if (!/^6[\d]{8}$/.test(data.phone))
90
+ resolve(error("invalid phone number", 400));
91
+
92
+ const config = {
93
+ method: "post",
94
+ url: baseUrl + "/direct-pay",
95
+ headers: headers,
96
+ data: data,
97
+ };
98
+ const response = await axios(config);
99
+ response.data.statusCode = response.status;
100
+ resolve(response.data);
101
+ } catch (e) {
102
+ e.response.data.statusCode = e?.response?.status;
103
+ resolve(e.response.data);
104
+ }
105
+ })}
106
+
107
+ /**
108
+ * This function returns an object containing the details of the transaction associated with the Id passed as parameter
109
+ */
110
+ paymentStatus(transId) {
111
+ return new Promise(async function (resolve) {
112
+ try {
113
+ if (!transId || typeof transId !== "string")
114
+ resolve(error("invalid type, string expected", 400));
115
+ if (!/^[a-zA-Z0-9]{8,10}$/.test(transId))
116
+ resolve(error("invalid transaction id", 400));
117
+
118
+ const config = {
119
+ method: "get",
120
+ url: baseUrl + "/payment-status/" + transId,
121
+ headers: headers,
122
+ };
123
+ const response = await axios(config);
124
+ response.data.statusCode = response.status;
125
+ resolve(response.data);
126
+ } catch (e) {
127
+ e.response.data.statusCode = e?.response?.status;
128
+ resolve(e.response.data);
129
+ }
130
+ });
131
+ }
132
+
133
+ /**
134
+ * This function expires the transaction associated with the Id passed as parameter and returns an object containing the details of the transaction
135
+ */
136
+ expirePay(transId) {
137
+ return new Promise(async function (resolve) {
138
+ try {
139
+ if (!transId || typeof transId !== "string")
140
+ resolve(error("invalid type, string expected", 400));
141
+ if (!/^[a-zA-Z0-9]{8,10}$/.test(transId))
142
+ resolve(error("invalid transaction id", 400));
143
+
144
+ const config = {
145
+ method: "post",
146
+ url: baseUrl + "/expire-pay",
147
+ data: { transId },
148
+ headers: headers,
149
+ };
150
+ const response = await axios(config);
151
+ response.data.statusCode = response.status;
152
+ resolve(response.data);
153
+ } catch (e) {
154
+ e.response.data.statusCode = e?.response?.status;
155
+ resolve(e.response.data);
156
+ }
157
+ });
158
+ }
159
+
160
+ /**
161
+ * This function returns an array of objects containing the transaction details of the user Id passed as parameter
162
+ */
163
+ userTrans(userId) {
164
+ return new Promise(async function (resolve) {
165
+ try {
166
+ if (!userId || typeof userId !== "string")
167
+ resolve(error("invalid type, string expected", 400));
168
+ if (!/^[a-zA-Z0-9-_]{1,100}$/.test(userId))
169
+ resolve(error("invalid user id", 400));
170
+
171
+ const config = {
172
+ method: "get",
173
+ url: baseUrl + "/transaction/" + userId,
174
+ headers: headers,
175
+ };
176
+ const response = await axios(config);
177
+ resolve(response.data);
178
+ } catch (e) {
179
+ e.response.data.statusCode = e?.response?.status;
180
+ resolve(e.response.data);
181
+ }
182
+ });
183
+ }
184
+
185
+ /**
186
+ * This function returns an object containing the balance of a service
187
+ */
188
+ balance() {
189
+ return new Promise(async function (resolve) {
190
+ try {
191
+ const config = {
192
+ method: "get",
193
+ url: baseUrl + "/balance",
194
+ headers: headers,
195
+ };
196
+ const response = await axios(config);
197
+ response.data.statusCode = response.status;
198
+ resolve(response.data);
199
+ } catch (e) {
200
+ e.response.data.statusCode = e?.response?.status;
201
+ resolve(e.response.data);
202
+ }
203
+ });
204
+ }
205
+
206
+ /**
207
+ *This function performs a payout to the phone number specified in the data parameter and
208
+ returns an object with a transId property that is used to get the status of the payment
209
+
210
+ *Note: In the live environment, payouts must be enabled for this to work. Contact support if needed
211
+
212
+ *Below is a parameter template. amount and phone are required
213
+
214
+ data = {
215
+ "amount": Integer ,
216
+ "phone": String ,
217
+ "medium": String,
218
+ "name": String,
219
+ "email": String,
220
+ "userId": String,
221
+ "externalId": String,
222
+ "message": String
223
+ }
224
+ */
225
+ payout(data) {
226
+ return new Promise(async function (resolve) {
227
+ try {
228
+ if (!data?.amount) return resolve(error("amount required", 400));
229
+ if (!Number.isInteger(data.amount))
230
+ return resolve(error("amount must be of type integer", 400));
231
+ if (data.amount < 100)
232
+ return resolve(error("amount cannot be less than 100 XAF", 400));
233
+ if (!data?.phone) return resolve(error("phone number required", 400));
234
+ if (typeof data.phone !== "string")
235
+ return resolve(error("phone must be of type string", 400));
236
+ if (!/^6[\d]{8}$/.test(data.phone))
237
+ return resolve(error("invalid phone number", 400));
238
+
239
+ const config = {
240
+ method: "post",
241
+ url: baseUrl + "/payout",
242
+ headers: headers,
243
+ data: data,
244
+ };
245
+ const response = await axios(config);
246
+ response.data.statusCode = response.status;
247
+ resolve(response.data);
248
+ } catch (e) {
249
+ e.response.data.statusCode = e?.response?.status;
250
+ resolve(e.response.data);
251
+ }
252
+ });
253
+ }
254
+
255
+ /**
256
+ *This function returns an array containing the transactions that satisfy
257
+ the criteria specified in the parameter passed to the function
258
+
259
+ *Below is a parameter template.
260
+
261
+ params = {
262
+ "status": enum [created, successful, failed, expired],
263
+ "medium": mobile money or orange money,
264
+ "start": Date in format yyyy-mm-dd,
265
+ "end": Date in format yyyy-mm-dd,
266
+ "amt": >= 100,
267
+ "limit": range(1, 100) default is 10,
268
+ "sort": asc || desc
269
+ }
270
+ */
271
+ search(params = {}) {
272
+ return new Promise(async function (resolve) {
273
+ try {
274
+ const config = {
275
+ method: "get",
276
+ url: baseUrl + "/search",
277
+ params: params,
278
+ headers: headers,
279
+ };
280
+ const response = await axios(config);
281
+ resolve(response.data);
282
+ } catch (e) {
283
+ e.response.data.statusCode = e?.response?.status;
284
+ resolve(e.response.data);
285
+ }
286
+ });
287
+ }
288
+ };
289
+
290
+ function error(message, statusCode) {
291
+ return { message, statusCode };
292
+ }
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "fapshi",
3
+ "version": "1.0.0",
4
+ "description": "\"Fapshi payment module\"",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node index.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/Warano02/fapshi.git"
12
+ },
13
+ "keywords": [
14
+ "fapshi",
15
+ "payememnt",
16
+ "cameroon",
17
+ "mtn",
18
+ "orange"
19
+ ],
20
+ "author": "Warano (carineteoi@gmail.com +237621092130)",
21
+ "license": "ISC",
22
+ "dependencies": {
23
+ "axios": "^1.8.4"
24
+ }
25
+ }