-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (153 loc) · 3.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import axios from "axios";
import { stringify } from "qs";
class ApiError extends Error {
constructor(message, status) {
super(message);
this.name = "ApiError";
this.status = status;
}
}
class TimeoutError extends Error {
constructor(message) {
super(message);
this.name = "TimeoutError";
}
}
const requestPayment = async (muid, phone_number, amount) => {
try {
const data = stringify({
muid: muid,
phone_number: phone_number,
amount: amount,
});
const config = {
method: "post",
url: "https://api.moneyunify.com/v2/request_payment",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
};
const response = await axios(config);
if (response.status !== 200) {
throw new ApiError("Failed to request payment", response.status);
}
return response.data;
} catch (error) {
if (error.response) {
throw new ApiError(error.response.data.message, error.response.status);
} else if (error.request) {
throw new Error("No response received from the server");
} else {
throw new Error(error.message);
}
}
};
const verifyTransaction = async (muid, reference) => {
try {
const data = stringify({
muid: muid,
reference: reference,
});
const config = {
method: "post",
url: "https://api.moneyunify.com/v2/verify_transaction",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
};
const response = await axios(config);
if (response.status !== 200) {
throw new ApiError("Failed to verify transaction", response.status);
}
return response.data;
} catch (error) {
if (error.response) {
throw new ApiError(error.response.data.message, error.response.status);
} else if (error.request) {
throw new Error("No response received from the server");
} else {
throw new Error(error.message);
}
}
};
const sendMoney = async (
muid,
email,
first_name,
last_name,
phone_number,
transaction_details,
) => {
try {
const data = stringify({
muid: muid,
email: email,
first_name: first_name,
last_name: last_name,
phone_number: phone_number,
transaction_details: transaction_details,
});
const config = {
method: "post",
url: "https://api.moneyunify.com/v2/send_money",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
};
const response = await axios(config);
if (response.status !== 200) {
throw new ApiError("Failed to send money", response.status);
}
return response.data;
} catch (error) {
if (error.response) {
throw new ApiError(error.response.data.message, error.response.status);
} else if (error.request) {
throw new Error("No response received from the server");
} else {
throw new Error(error.message);
}
}
};
const pollTransactionStatus = async (
muid,
reference,
interval = 5000,
maxAttempts = 12,
) => {
let attempts = 0;
const poll = async (resolve, reject) => {
try {
const response = await verifyTransaction(muid, reference);
if (response.data.status === "successful") {
resolve(response);
} else if (
response.data.status === "failed" ||
response.data.status === "declined"
) {
reject(
new ApiError("Transaction failed or was declined", response.status),
);
} else if (attempts >= maxAttempts) {
reject(new TimeoutError("Transaction verification timed out"));
} else {
attempts += 1;
setTimeout(poll, interval, resolve, reject);
}
} catch (error) {
reject(error);
}
};
return new Promise(poll);
};
export default {
requestPayment,
verifyTransaction,
sendMoney,
pollTransactionStatus,
ApiError,
TimeoutError,
};