Skip to content

Commit f179d2e

Browse files
committed
Implement polling for UK payments
1 parent 15c7892 commit f179d2e

File tree

5 files changed

+130
-4
lines changed

5 files changed

+130
-4
lines changed

rave_presentation/src/main/java/com/flutterwave/raveandroid/rave_presentation/uk/UkHandler.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.flutterwave.raveandroid.rave_remote.requests.RequeryRequestBody;
1818
import com.flutterwave.raveandroid.rave_remote.responses.ChargeResponse;
1919
import com.flutterwave.raveandroid.rave_remote.responses.FeeCheckResponse;
20+
import com.flutterwave.raveandroid.rave_remote.responses.PollingResponse;
2021
import com.flutterwave.raveandroid.rave_remote.responses.RequeryResponse;
2122

2223
import javax.inject.Inject;
@@ -38,9 +39,10 @@ public class UkHandler implements UkContract.Handler {
3839
RemoteRepository networkRequest;
3940
@Inject
4041
PayloadEncryptor payloadEncryptor;
42+
String txRef = null;
4143
private UkContract.Interactor mInteractor;
44+
4245
private boolean pollingCancelled = false;
43-
String txRef = null;
4446

4547
@Inject
4648
public UkHandler(UkContract.Interactor mInteractor) {
@@ -101,17 +103,57 @@ public void chargeUk(final Payload payload, final String encryptionKey) {
101103
@Override
102104
public void onSuccess(ChargeResponse response) {
103105

104-
mInteractor.showProgressIndicator(false);
105106

106107
if (response.getData() != null) {
107108
String amount = response.getAmount();
108109
String paymentCode = response.getPaymentCode();
109110
String flwRef = response.getFlwRef();
110-
mInteractor.showTransactionPage(amount, paymentCode, flwRef, txRef);
111+
if (amount != null && paymentCode != null) {
112+
mInteractor.showProgressIndicator(false);
113+
mInteractor.showTransactionPage(amount, paymentCode, flwRef, txRef);
114+
} else if (response.getPingUrl() != null) {
115+
callPingUrl(response.getPingUrl());
116+
} else {
117+
mInteractor.showProgressIndicator(false);
118+
mInteractor.onPaymentError(noResponse);
119+
}
120+
} else {
121+
mInteractor.showProgressIndicator(false);
122+
mInteractor.onPaymentError(noResponse);
123+
}
124+
125+
}
126+
127+
@Override
128+
public void onError(String message) {
129+
mInteractor.showProgressIndicator(false);
130+
mInteractor.onPaymentError(message);
131+
}
132+
});
133+
}
134+
135+
void callPingUrl(final String pingUrl) {
136+
137+
networkRequest.pollUrl(pingUrl, new ResultCallback<PollingResponse>() {
138+
@Override
139+
public void onSuccess(PollingResponse response) {
140+
141+
if (response.data != null) {
142+
if (response.getResponse() != null) {
143+
144+
mInteractor.showProgressIndicator(false);
145+
String amount = response.getAmount();
146+
String paymentCode = response.getPaymentCode();
147+
String flwRef = response.getFlwRef();
148+
if (amount != null && paymentCode != null)
149+
mInteractor.showTransactionPage(amount, paymentCode, flwRef, txRef);
150+
else mInteractor.onPaymentError(noResponse);
151+
} else callPingUrl(pingUrl);
111152
} else {
112153
mInteractor.onPaymentError(noResponse);
113154
}
114155

156+
115157
}
116158

117159
@Override

rave_remote/src/main/java/com/flutterwave/raveandroid/rave_remote/ApiService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import retrofit2.http.Body;
1616
import retrofit2.http.GET;
1717
import retrofit2.http.POST;
18+
import retrofit2.http.Url;
1819

1920
/**
2021
* Created by hamzafetuga on 18/07/2017.
@@ -30,6 +31,10 @@ public interface ApiService {
3031
// Call<ChargeResponse> charge(@Body ChargeRequestBody body);
3132
Call<String> chargeWithPolling(@Body ChargeRequestBody body);
3233

34+
@GET()
35+
// Call<ChargeResponse> charge(@Body ChargeRequestBody body);
36+
Call<String> pollUrl(@Url String url);
37+
3338
@POST("/flwv3-pug/getpaidx/api/validatecharge")
3439
Call<String> validateCardCharge(@Body ValidateChargeBody body);
3540
// Call<ChargeResponse> validateCardCharge(@Body ValidateChargeBody body);

rave_remote/src/main/java/com/flutterwave/raveandroid/rave_remote/RemoteRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.flutterwave.raveandroid.rave_remote.responses.FeeCheckResponse;
1818
import com.flutterwave.raveandroid.rave_remote.responses.LookupSavedCardsResponse;
1919
import com.flutterwave.raveandroid.rave_remote.responses.MobileMoneyChargeResponse;
20+
import com.flutterwave.raveandroid.rave_remote.responses.PollingResponse;
2021
import com.flutterwave.raveandroid.rave_remote.responses.RequeryResponse;
2122
import com.flutterwave.raveandroid.rave_remote.responses.SaBankAccountResponse;
2223
import com.flutterwave.raveandroid.rave_remote.responses.SaveCardResponse;
@@ -85,6 +86,18 @@ public void chargeWithPolling(ChargeRequestBody body, final ResultCallback callb
8586
);
8687
}
8788

89+
public void pollUrl(String url, final ResultCallback callback) {
90+
91+
Call<String> call = service.pollUrl(url);
92+
93+
executor.execute(
94+
call,
95+
new TypeToken<PollingResponse>() {
96+
}.getType(),
97+
new GenericNetworkCallback<PollingResponse>(callback)
98+
);
99+
}
100+
88101
public void chargeMobileMoneyWallet(ChargeRequestBody body, final ResultCallback callback) {
89102

90103
Call<String> call = service.charge(body);

rave_remote/src/main/java/com/flutterwave/raveandroid/rave_remote/responses/ChargeResponse.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public String getTxRef() {
5050
return (data == null) ? null : data.getTx_ref();
5151
}
5252

53+
public String getPingUrl() {
54+
return (data == null) ? null : data.ping_url;
55+
}
56+
5357
public static class AccountValidateInstructions {
5458
public String getInstruction() {
5559
return instruction;
@@ -65,6 +69,7 @@ public void setInstruction(String instruction) {
6569

6670
public static class Data {
6771

72+
String ping_url;
6873
Data data;
6974
String suggested_auth;
7075
String chargeResponseCode;
@@ -234,7 +239,7 @@ public String getAuthurl() {
234239
}
235240

236241
public String getFlwRef() {
237-
return flwRef;
242+
return flwRef == null ? flw_reference : flwRef;
238243
}
239244

240245
public void setValidateInstructions(AccountValidateInstructions validateInstructions) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.flutterwave.raveandroid.rave_remote.responses;
2+
3+
public class PollingResponse {
4+
5+
public Data data;
6+
7+
public ChargeResponse getResponse() {
8+
return data == null ? null : data.response_parsed;
9+
}
10+
11+
public String getAmount() {
12+
return getResponse() == null ? null : getResponse().getAmount();
13+
}
14+
15+
public String getPaymentCode() {
16+
return getResponse() == null ? null : getResponse().getPaymentCode();
17+
}
18+
19+
public String getFlwRef() {
20+
return (getResponse() == null) ? null : getResponse().getFlwRef();
21+
}
22+
23+
class Data {
24+
ChargeResponse response_parsed;
25+
}
26+
27+
}
28+
/*
29+
{
30+
"status": "success",
31+
"message": "REQSPONSE",
32+
"data": {
33+
"id": 288709,
34+
"reqid": "RCORE_CHREQ_19D59CBC397138B33690",
35+
"status": "completed",
36+
"response": "{\"data\":{\"amount\":\"1.02\",\"type\":\"paymentcode\",\"redirect\":false,\"transaction_date\":\"2020-10-14T12:02:15.218\",\"transaction_reference\":\"URF_1602673332764_7102735\",\"flw_reference\":\"ACH639111602673335221\",\"redirect_url\":null,\"payment_code\":\"GBP693A02C\",\"type_data\":\"GBP693A02C\",\"meta_data\":{\"account_number\":\"43271228\",\"sort_code\":\"040053\"}},\"response_code\":\"02\",\"response_message\":\"Transaction in progress\"}",
37+
"createdAt": "2020-10-14T11:02:14.000Z",
38+
"updatedAt": "2020-10-14T11:02:16.000Z",
39+
"deletedAt": null,
40+
"getResponse()": {
41+
"data": {
42+
"amount": "1.02",
43+
"type": "paymentcode",
44+
"redirect": false,
45+
"transaction_date": "2020-10-14T12:02:15.218",
46+
"transaction_reference": "URF_1602673332764_7102735",
47+
"flw_reference": "ACH639111602673335221",
48+
"redirect_url": null,
49+
"payment_code": "GBP693A02C",
50+
"type_data": "GBP693A02C",
51+
"meta_data": {
52+
"account_number": "43271228",
53+
"sort_code": "040053"
54+
}
55+
},
56+
"response_code": "02",
57+
"response_message": "Transaction in progress"
58+
}
59+
}
60+
}
61+
*/

0 commit comments

Comments
 (0)