-
Notifications
You must be signed in to change notification settings - Fork 4
/
Mpesa.java
342 lines (277 loc) · 14.6 KB
/
Mpesa.java
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package whatsapp;
import okhttp3.*;
import org.json.*;
import java.io.IOException;
import java.util.Base64;
public class Mpesa {
private String appKey;
private String appSecret;
public String JSONSTK="";
public Mpesa(String app_key, String app_secret){
appKey=app_key;
appSecret=app_secret;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public void setJsonKey(String jsonKey) {
this.JSONSTK = jsonKey;
}
public String getJsonKey() {
return JSONSTK;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public String authenticate() throws IOException {
String appKeySecret = appKey + ":" + appSecret;
byte[] bytes = appKeySecret.getBytes("ISO-8859-1");
String encoded = Base64.getEncoder().encodeToString(bytes);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials")
.get()
.addHeader("authorization", "Basic "+encoded)
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
JSONObject jsonObject=new JSONObject(response.body().string());
System.out.println(""
+ "\n First Authenticate String to print Acess Token"
+ "\n"+jsonObject.getString("access_token"));
return jsonObject.getString("access_token");
}
public String C2BSimulation( String shortCode, String commandID, String amount, String MSISDN, String billRefNumber) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("ShortCode", shortCode);
jsonObject.put("CommandID", commandID);
jsonObject.put("Amount", amount);
jsonObject.put("Msisdn", MSISDN);
jsonObject.put("BillRefNumber", billRefNumber);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);//Print JSON request
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer "+authenticate())
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
return response.body().toString(); //Print Response
}
public String B2CRequest( String initiatorName, String securityCredential,String commandID, String amount, String partyA,String partyB, String remarks, String queueTimeOutURL, String resultURL, String occassion) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("InitiatorName", initiatorName);
jsonObject.put("SecurityCredential", securityCredential);
jsonObject.put("CommandID", commandID);
jsonObject.put("Amount", amount);
jsonObject.put("PartyA", partyA);
jsonObject.put("PartyB", partyB);
jsonObject.put("Remarks", remarks);
jsonObject.put("QueueTimeOutURL", queueTimeOutURL);
jsonObject.put("ResultURL", resultURL);
jsonObject.put("Occassion", occassion);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url( "https://sandbox.safaricom.co.ke/mpesa/reversal/v1/request")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer "+authenticate())
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
return response.body().toString();
}
public String B2BRequest( String initiatorName, String accountReference,String securityCredential,String commandID, String senderIdentifierType,String receiverIdentifierType,float amount, String partyA,String partyB, String remarks, String queueTimeOutURL, String resultURL, String occassion) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("Initiator", initiatorName);
jsonObject.put("SecurityCredential", securityCredential);
jsonObject.put("CommandID", commandID);
jsonObject.put("SenderIdentifierType", senderIdentifierType);
jsonObject.put("RecieverIdentifierType",receiverIdentifierType);
jsonObject.put("Amount", amount);
jsonObject.put("PartyA", partyA);
jsonObject.put("PartyB", partyB);
jsonObject.put("Remarks", remarks);
jsonObject.put("AccountReference", accountReference);
jsonObject.put("QueueTimeOutURL", queueTimeOutURL);
jsonObject.put("ResultURL", resultURL);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/safaricom/b2b/v1/paymentrequest")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer "+authenticate())
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String STKPushSimulation(String businessShortCode, String password, String timestamp,String transactionType, String amount, String phoneNumber, String partyA, String partyB, String callBackURL,String accountReference, String transactionDesc) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("BusinessShortCode", businessShortCode);
jsonObject.put("Password", password);
jsonObject.put("Timestamp", timestamp);
jsonObject.put("TransactionType", transactionType);
jsonObject.put("Amount",amount);
jsonObject.put("PartyA", partyA);
jsonObject.put("PartyB", partyB);
jsonObject.put("PhoneNumber", phoneNumber);
jsonObject.put("CallBackURL", callBackURL);
jsonObject.put("AccountReference", accountReference);
jsonObject.put("TransactionDesc", transactionDesc);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);//Print JSON request
setJsonKey(requestJson);
System.out.println("");
OkHttpClient client = new OkHttpClient();
String url="https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest";
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url(url)
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer "+authenticate())
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
//return response.body().toString();
System.out.println(response.toString());
return response.toString();
}
public String STKPushTransactionStatus( String businessShortCode, String password, String timestamp, String checkoutRequestID) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("BusinessShortCode", businessShortCode);
jsonObject.put("Password", password);
jsonObject.put("Timestamp", timestamp);
jsonObject.put("CheckoutRequestID", checkoutRequestID);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);//Print JSON request
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query")
.post(body)
.addHeader("authorization", "Bearer "+authenticate())
.addHeader("content-type", "application/json")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
return response.body().toString();
}
public String reversal(String initiator, String securityCredential, String commandID, String transactionID, String amount, String receiverParty, String recieverIdentifierType, String resultURL,String queueTimeOutURL, String remarks, String ocassion) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("Initiator", initiator);
jsonObject.put("SecurityCredential", securityCredential);
jsonObject.put("CommandID", commandID);
jsonObject.put("TransactionID", transactionID);
jsonObject.put("Amount",amount);
jsonObject.put("ReceiverParty", receiverParty);
jsonObject.put("RecieverIdentifierType", recieverIdentifierType);
jsonObject.put("QueueTimeOutURL", queueTimeOutURL);
jsonObject.put("ResultURL", resultURL);
jsonObject.put("Remarks", remarks);
jsonObject.put("Occasion", ocassion);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/safaricom/reversal/v1/request")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer xNA3e9KhKQ8qkdTxJJo7IDGkpFNV")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
return response.body().string();
}
public String balanceInquiry(String initiator, String commandID, String securityCredential, String partyA, String identifierType, String remarks, String queueTimeOutURL, String resultURL) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("Initiator", initiator);
jsonObject.put("SecurityCredential", securityCredential);
jsonObject.put("CommandID", commandID);
jsonObject.put("PartyA", partyA);
jsonObject.put("IdentifierType",identifierType);
jsonObject.put("Remarks", remarks);
jsonObject.put("QueueTimeOutURL", queueTimeOutURL);
jsonObject.put("ResultURL", resultURL);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/safaricom/accountbalance/v1/query")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer fwu89P2Jf6MB1A2VJoouPg0BFHFM")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "2aa448be-7d56-a796-065f-b378ede8b136")
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
public String registerURL(String shortCode, String responseType, String confirmationURL, String validationURL) throws IOException {
JSONArray jsonArray=new JSONArray();
JSONObject jsonObject=new JSONObject();
jsonObject.put("ShortCode", shortCode);
jsonObject.put("ResponseType", responseType);
jsonObject.put("ConfirmationURL", confirmationURL);
jsonObject.put("ValidationURL", validationURL);
jsonArray.put(jsonObject);
String requestJson=jsonArray.toString().replaceAll("[\\[\\]]","");
System.out.println(requestJson);
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, requestJson);
Request request = new Request.Builder()
.url("https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Bearer "+authenticate())
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
System.out.println(response.body().string());
return response.body().string();
}
}