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.
- package/index.js +285 -259
- 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
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
257
|
-
|
|
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
|
-
|
|
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
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
292
|
-
}
|
|
317
|
+
return { message, statusCode };
|
|
318
|
+
}
|