-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentRequestHelper.java
More file actions
111 lines (97 loc) · 3.68 KB
/
PaymentRequestHelper.java
File metadata and controls
111 lines (97 loc) · 3.68 KB
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
package com.app.paymentapp.PaymentLibrary;
import android.content.Context;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
public class PaymentRequestHelper {
/*
Declare for get application activity.
*/
private Context context;
private Map<String, String> map = new HashMap<>();
private String username;
private String password;
private String payment_url;
private DataCallback callback;
// This is the constructor of this helper class
public PaymentRequestHelper(Context context) {
this.context = context;
}
/*
Get the response of payment request API.
If the status of the response is Success the URL will be open on webView.
In case of fail or server error, it will show the error Message.
*/
public void CallPaymentRequestApi(final DataCallback callback) {
this.callback = callback;
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest request = new StringRequest(Request.Method.POST, payment_url, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
callback.onSuccess(response);
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onError(error + "");
}
}) {
// Authentication of Payment Request.
@Override
public Map<String, String> getHeaders() {
Map<String, String> header = new HashMap<>();
String creds = String.format("%s:%s", username, password);
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
header.put("Authorization", auth);
return header;
}
// Get the Payment request information parameters
@Override
protected Map<String, String> getParams() {
// Add Payment Request Params here
return map;
}
};
queue.add(request);
}
// Set the parameters of payment request API
public void addParam(Map<String, String> params) {
this.map = params;
}
// Set the valid username and password for the gateway authentication
public void setAuthParam(String user, String pass) {
this.username = user;
this.password = pass;
}
// Set the payment request URL
public void setPaymentUrl(String url) {
this.payment_url = url;
}
//This interface is not used for outside the class.
//this will use within payment request helper class only.
//this is the callback function used to get onSuccess response.
public interface DataCallback extends BaseCallback {
void onSuccess(String data);
}
// Handle fail repsonse
public interface BaseCallback {
void onError(String msg);
}
}