fapshi 1.0.2 → 1.0.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.
Files changed (2) hide show
  1. package/index.js +285 -259
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,292 +1,318 @@
1
1
  const axios = require("axios");
2
2
  const baseUrl = "https://live.fapshi.com";
3
3
 
4
-
5
- module.exports =class FAPSHI {
4
+ module.exports = class FAPSHI {
6
5
  /**
7
6
  *
8
7
  * @param {string} user : user name get for an services
9
8
  * @param {string} key : api key access
10
9
  */
11
10
  constructor(user, key) {
12
- if(!user||!key) throw new Error("Mention your user name and api key ");
13
-
11
+ if (!user || !key) throw new Error("Mention your user name and api key ");
12
+
14
13
  this.apiuser = user;
15
14
  this.key = key;
16
-
15
+
17
16
  this.headers = {
18
17
  apiuser: this.apiuser,
19
18
  apikey: this.key,
20
19
  };
21
20
  }
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
21
 
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));
22
+ /**
23
+ * This function returns an object with the link where 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
+ const self = this;
38
+ return new Promise(async function (resolve) {
39
+ try {
40
+ console.log("headers :" + JSON.stringify(self.headers));
41
+ if (!data?.amount) return resolve(error("amount required", 400));
42
+ if (!Number.isInteger(data.amount))
43
+ return resolve(error("amount must be of type integer", 400));
44
+ if (data.amount < 100)
45
+ return resolve(error("amount cannot be less than 100 XAF", 400));
117
46
 
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
- }
47
+ const config = {
48
+ method: "post",
49
+ url: baseUrl + "/initiate-pay",
50
+ headers: self.headers,
51
+ data: data,
52
+ };
53
+ const response = await axios(config);
54
+ response.data.statusCode = response.status;
55
+ resolve(response.data);
56
+ } catch (e) {
57
+ console.log(e);
58
+ const statusCode = e.response ? e.response.status : 500;
59
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
60
+ resolve(error(message, statusCode));
61
+ }
62
+ });
63
+ }
132
64
 
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));
65
+ /**
66
+ * This function directly initiates a payment request to a user's mobile device and
67
+ * returns an object with a transId property that is used to get the status of the payment
68
+ *
69
+ * Below is a parameter template. amount and phone are required
70
+ *
71
+ * data = {
72
+ * "amount": Integer ,
73
+ * "phone": String ,
74
+ * "medium": String,
75
+ * "name": String,
76
+ * "email": String,
77
+ * "userId": String,
78
+ * "externalId": String,
79
+ * "message": String
80
+ * }
81
+ */
82
+ directPay(data) {
83
+ const self = this;
84
+ return new Promise(async function (resolve) {
85
+ try {
86
+ if (!data?.amount) return resolve(error("amount required", 400));
87
+ if (!Number.isInteger(data.amount))
88
+ return resolve(error("amount must be of type integer", 400));
89
+ if (data.amount < 100)
90
+ return resolve(error("amount cannot be less than 100 XAF", 400));
91
+ if (!data?.phone) return resolve(error("phone number required", 400));
92
+ if (typeof data.phone !== "string")
93
+ return resolve(error("phone must be of type string", 400));
94
+ if (!/^6[\d]{8}$/.test(data.phone))
95
+ return resolve(error("invalid phone number", 400));
143
96
 
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
- }
97
+ const config = {
98
+ method: "post",
99
+ url: baseUrl + "/direct-pay",
100
+ headers: self.headers,
101
+ data: data,
102
+ };
103
+ const response = await axios(config);
104
+ response.data.statusCode = response.status;
105
+ resolve(response.data);
106
+ } catch (e) {
107
+ console.log(e);
108
+ const statusCode = e.response ? e.response.status : 500;
109
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
110
+ resolve(error(message, statusCode));
111
+ }
112
+ });
113
+ }
159
114
 
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));
115
+ /**
116
+ * This function returns an object containing the details of the transaction associated with the Id passed as parameter
117
+ */
118
+ paymentStatus(transId) {
119
+ const self = this;
120
+ return new Promise(async function (resolve) {
121
+ try {
122
+ if (!transId || typeof transId !== "string")
123
+ return resolve(error("invalid type, string expected", 400));
124
+ if (!/^[a-zA-Z0-9]{8,10}$/.test(transId))
125
+ return resolve(error("invalid transaction id", 400));
170
126
 
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
- }
127
+ const config = {
128
+ method: "get",
129
+ url: baseUrl + "/payment-status/" + transId,
130
+ headers: self.headers,
131
+ };
132
+ const response = await axios(config);
133
+ response.data.statusCode = response.status;
134
+ resolve(response.data);
135
+ } catch (e) {
136
+ console.log(e);
137
+ const statusCode = e.response ? e.response.status : 500;
138
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
139
+ resolve(error(message, statusCode));
140
+ }
141
+ });
142
+ }
184
143
 
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
- }
144
+ /**
145
+ * This function expires the transaction associated with the Id passed as parameter and returns an object containing the details of the transaction
146
+ */
147
+ expirePay(transId) {
148
+ const self = this;
149
+ return new Promise(async function (resolve) {
150
+ try {
151
+ if (!transId || typeof transId !== "string")
152
+ return resolve(error("invalid type, string expected", 400));
153
+ if (!/^[a-zA-Z0-9]{8,10}$/.test(transId))
154
+ return resolve(error("invalid transaction id", 400));
205
155
 
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
156
+ const config = {
157
+ method: "post",
158
+ url: baseUrl + "/expire-pay",
159
+ data: { transId },
160
+ headers: self.headers,
161
+ };
162
+ const response = await axios(config);
163
+ response.data.statusCode = response.status;
164
+ resolve(response.data);
165
+ } catch (e) {
166
+ console.log(e);
167
+ const statusCode = e.response ? e.response.status : 500;
168
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
169
+ resolve(error(message, statusCode));
170
+ }
171
+ });
172
+ }
211
173
 
212
- *Below is a parameter template. amount and phone are required
174
+ /**
175
+ * This function returns an array of objects containing the transaction details of the user Id passed as parameter
176
+ */
177
+ userTrans(userId) {
178
+ const self = this;
179
+ return new Promise(async function (resolve) {
180
+ try {
181
+ if (!userId || typeof userId !== "string")
182
+ return resolve(error("invalid type, string expected", 400));
183
+ if (!/^[a-zA-Z0-9-_]{1,100}$/.test(userId))
184
+ return resolve(error("invalid user id", 400));
213
185
 
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));
186
+ const config = {
187
+ method: "get",
188
+ url: baseUrl + "/transaction/" + userId,
189
+ headers: self.headers,
190
+ };
191
+ const response = await axios(config);
192
+ resolve(response.data);
193
+ } catch (e) {
194
+ console.log(e);
195
+ const statusCode = e.response ? e.response.status : 500;
196
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
197
+ resolve(error(message, statusCode));
198
+ }
199
+ });
200
+ }
238
201
 
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
- }
202
+ /**
203
+ * This function returns an object containing the balance of a service
204
+ */
205
+ balance() {
206
+ const self = this;
207
+ return new Promise(async function (resolve) {
208
+ try {
209
+ const config = {
210
+ method: "get",
211
+ url: baseUrl + "/balance",
212
+ headers: self.headers,
213
+ };
214
+ const response = await axios(config);
215
+ response.data.statusCode = response.status;
216
+ resolve(response.data);
217
+ } catch (e) {
218
+ console.log(e);
219
+ const statusCode = e.response ? e.response.status : 500;
220
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
221
+ resolve(error(message, statusCode));
222
+ }
223
+ });
224
+ }
254
225
 
255
- /**
256
- *This function returns an array containing the transactions that satisfy
257
- the criteria specified in the parameter passed to the function
226
+ /**
227
+ * This function performs a payout to the phone number specified in the data parameter and
228
+ * returns an object with a transId property that is used to get the status of the payment
229
+ *
230
+ * Note: In the live environment, payouts must be enabled for this to work. Contact support if needed
231
+ *
232
+ * Below is a parameter template. amount and phone are required
233
+ *
234
+ * data = {
235
+ * "amount": Integer ,
236
+ * "phone": String ,
237
+ * "medium": String,
238
+ * "name": String,
239
+ * "email": String,
240
+ * "userId": String,
241
+ * "externalId": String,
242
+ * "message": String
243
+ * }
244
+ */
245
+ payout(data) {
246
+ const self = this;
247
+ return new Promise(async function (resolve) {
248
+ try {
249
+ if (!data?.amount) return resolve(error("amount required", 400));
250
+ if (!Number.isInteger(data.amount))
251
+ return resolve(error("amount must be of type integer", 400));
252
+ if (data.amount < 100)
253
+ return resolve(error("amount cannot be less than 100 XAF", 400));
254
+ if (!data?.phone) return resolve(error("phone number required", 400));
255
+ if (typeof data.phone !== "string")
256
+ return resolve(error("phone must be of type string", 400));
257
+ if (!/^6[\d]{8}$/.test(data.phone))
258
+ return resolve(error("invalid phone number", 400));
258
259
 
259
- *Below is a parameter template.
260
+ const config = {
261
+ method: "post",
262
+ url: baseUrl + "/payout",
263
+ headers: self.headers,
264
+ data: data,
265
+ };
266
+ const response = await axios(config);
267
+ response.data.statusCode = response.status;
268
+ resolve(response.data);
269
+ } catch (e) {
270
+ console.log(e);
271
+ const statusCode = e.response ? e.response.status : 500;
272
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
273
+ resolve(error(message, statusCode));
274
+ }
275
+ });
276
+ }
260
277
 
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
- }
278
+ /**
279
+ * This function returns an array containing the transactions that satisfy
280
+ * the criteria specified in the parameter passed to the function
281
+ *
282
+ * Below is a parameter template.
283
+ *
284
+ * params = {
285
+ * "status": enum [created, successful, failed, expired],
286
+ * "medium": mobile money or orange money,
287
+ * "start": Date in format yyyy-mm-dd,
288
+ * "end": Date in format yyyy-mm-dd,
289
+ * "amt": >= 100,
290
+ * "limit": range(1, 100) default is 10,
291
+ * "sort": asc || desc
292
+ * }
293
+ */
294
+ search(params = {}) {
295
+ const self = this;
296
+ return new Promise(async function (resolve) {
297
+ try {
298
+ const config = {
299
+ method: "get",
300
+ url: baseUrl + "/search",
301
+ params: params,
302
+ headers: self.headers,
303
+ };
304
+ const response = await axios(config);
305
+ resolve(response.data);
306
+ } catch (e) {
307
+ console.log(e);
308
+ const statusCode = e.response ? e.response.status : 500;
309
+ const message = e.response && e.response.data ? e.response.data.message : "Erreur inconnue";
310
+ resolve(error(message, statusCode));
311
+ }
312
+ });
313
+ }
288
314
  };
289
315
 
290
316
  function error(message, statusCode) {
291
- return { message, statusCode };
292
- }
317
+ return { message, statusCode };
318
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fapshi",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "\"Fapshi payment module\"",
5
5
  "main": "index.js",
6
6
  "scripts": {