-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
241 lines (219 loc) · 7.19 KB
/
App.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Buffer } from "buffer";
import { StatusBar } from "expo-status-bar";
import React, { useState } from "react";
import {
ActivityIndicator,
StyleSheet,
Text,
View,
Picker,
Button,
} from "react-native";
import { Base64 } from "js-base64";
export default function App() {
const [isLoading, setIsLoading] = useState(false);
const user = "c4f6d33d-ea76-4609-b816-a99dc801b532"; // replace with your provisioned user id or find help here(https://gist.github.com/chaiwa-berian/5294fdf1360247cf4561c95c8fa740d4)
const apiKey = "1e79774dabd944b4b113c30aaef5b2c8"; // replace with your provisioned apikey or find help here(https://gist.github.com/chaiwa-berian/5294fdf1360247cf4561c95c8fa740d4)
const [bearerToken, setBearerToken] = useState({
accessToken: "",
expiresIn: "",
issuedAt: "",
expiresAt: "",
});
const [selectedValue, setSelectedValue] = useState("CreateBearerToken");
const baseURL = "https://sandbox.momodeveloper.mtn.com";
const [responseObject, setResponseObject] = useState({});
let tokenExpired = () => {
if (bearerToken.expiresAt != "") {
return bearerToken.expiresAt < Date.now() ? "true" : "false";
}
return;
};
// Step 1. Get Access Token
async function createNewBearerToken() {
try {
let requestPath = "/collection/token/";
let url = baseURL + requestPath;
let userApiKeyPair = `${user}:${apiKey}`;
let auth = await Base64.encode(userApiKeyPair); //you need to encode api user and api key as Base64
let requestHeaders = {
Accept: "application/json",
Authorization: "Basic " + auth,
"Ocp-Apim-Subscription-Key": "b44728c249c24d8bb11d8b8592f4f5a7", //replace with your subscription key
"X-Target-Environment": "sandbox",
};
let response = await fetch(url, {
method: "POST",
headers: requestHeaders,
mode: "cors",
});
let responseData = response.status == 200 ? await response.json() : null;
setResponseObject({
status: response.status,
ok: response.ok,
statusText: response.statusText,
data: responseData,
});
if (response.status == 200) {
let now = new Date();
let time_milliseconds = now.getTime() + responseData.expires_in * 1000;
setBearerToken({
accessToken: responseData.access_token,
expiresIn: responseData.expires_in,
issuedAt: now.toString(),
expiresAt: time_milliseconds,
});
}
} catch (error) {
setResponseObject({
status: error.status || null,
ok: error.ok || null,
statusText: error.statusText || null,
data: error.message,
});
console.log("Error in create new token method:", error);
} finally {
setIsLoading(false);
}
}
// Step 2. Request To Pay
async function requestToPay() {
try {
let requestPath = "/collection/v1_0/requesttopay";
let url = baseURL + requestPath;
let xRefResponse = await fetch(
"https://www.uuidgenerator.net/api/version4",
{
method: "GET",
}
);
let xRefId = await xRefResponse.text();
console.log("xRef: ", xRefId);
let requestBody = JSON.stringify(
{
amount: "700.0",
currency: "EUR",
externalId: "097411060",
payer: {
partyIdType: "MSISDN",
partyId: "260962217314",
},
payerMessage: "SILC Savings",
payeeNote: "SILC Savings",
},
null,
2
);
let contentLength = Buffer.byteLength(requestBody, "utf8");
let token = `Bearer ${bearerToken.accessToken}`;
let newRequestHeaders = new Headers();
newRequestHeaders.append("Accept", "application/json");
newRequestHeaders.append("X-Reference-Id", xRefId);
newRequestHeaders.append(
"Ocp-Apim-Subscription-Key",
"b44728c249c24d8bb11d8b8592f4f5a7"
);
newRequestHeaders.append("X-Target-Environment", "sandbox");
newRequestHeaders.append("Host", "sandbox.momodeveloper.mtn.com");
newRequestHeaders.append("Content-Type", "application/json");
newRequestHeaders.append("Content-Length", contentLength);
newRequestHeaders.append("Authorization", token);
let request = new Request(url, {
method: "POST",
headers: newRequestHeaders,
body: requestBody,
});
let response = await fetch(request);
console.log("request headers: ", JSON.stringify(request.headers));
let responseData = await response.text();
setResponseObject({
status: response.status,
ok: response.ok,
statusText: response.statusText,
data: responseData || null,
});
console.log("Request to pay respose:", JSON.stringify(response));
} catch (error) {
setResponseObject({
status: error.status || null,
ok: error.ok || null,
statusText: error.statusText || null,
data: error.message,
});
console.log("Error in requestToPay method:", error);
} finally {
setIsLoading(false);
}
}
return (
<View style={styles.container}>
<Text h1 style={{ fontWeight: "bold" }}>
MTN MoMo API Client Demo!
</Text>
<Text h2 style={{ fontWeight: "bold" }}>
Access Token
</Text>
<Text>{bearerToken.accessToken}</Text>
<Text h3>Issued at: {bearerToken.issuedAt}</Text>
<Text h3>
Expires at:{" "}
{bearerToken.expiresAt == ""
? ""
: new Date(bearerToken.expiresAt).toString()}
</Text>
<Text h3>Token Expired?: {tokenExpired()}</Text>
<Picker
selectedValue={selectedValue}
onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}
style={{ height: 50, width: 250 }}
>
<Picker.Item label="Get New Access Token" value="CreateBearerToken" />
<Picker.Item label="Request To Pay" value="RequestToPay" />
</Picker>
<Button
title="Run"
onPress={() => {
if (selectedValue == "CreateBearerToken") {
setIsLoading(true);
createNewBearerToken();
} else if (selectedValue == "RequestToPay") {
setIsLoading(true);
requestToPay();
}
}}
/>
<View style={styles.subcontainer}>
<Text h1 style={{ fontWeight: "bold" }}>
SERVER RESPONSE
</Text>
{isLoading ? (
<ActivityIndicator />
) : (
<View style={styles.subcontainer}>
<Text
h2
>{`Status: ${responseObject.status}; OK: ${responseObject.ok}; StatusText: ${responseObject.statusText}`}</Text>
<Text h2 style={{ fontWeight: "bold" }}>
Response Data
</Text>
<Text>{JSON.stringify(responseObject.data)}</Text>
</View>
)}
</View>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
subcontainer: {
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});