diff --git a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/ApiAdapter.java b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/ApiAdapter.java index be64bcc..d580dc9 100644 --- a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/ApiAdapter.java +++ b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/ApiAdapter.java @@ -4,6 +4,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.util.HashMap; + import retrofit.RestAdapter; import retrofit.android.AndroidLog; import retrofit.converter.GsonConverter; @@ -16,12 +18,24 @@ class ApiAdapter { public static final String RETROFIT_TAG = "HPPRetrofit"; public static IHPPServerAPI getAdapter(String endpoint) { - RestAdapter restAdapter = new RestAdapter.Builder() - .setEndpoint(endpoint) + return getAdapter(endpoint,null); + } + + public static IHPPServerAPI getAdapter(String endpoint,HashMap headers) { + + RestAdapter.Builder builder = new RestAdapter.Builder(); + builder.setEndpoint(endpoint) .setLogLevel(RestAdapter.LogLevel.FULL) .setLog(new AndroidLog(RETROFIT_TAG)) - .setConverter(new GsonConverter(getGson())) - .build(); + .setConverter(new GsonConverter(getGson())); + + if (headers != null && headers.size() != 0) { + HPPRequestInterceptor myInterceptor = new HPPRequestInterceptor(headers); + builder.setRequestInterceptor(myInterceptor); + } + + RestAdapter restAdapter = builder.build(); + return restAdapter.create(IHPPServerAPI.class); } diff --git a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManager.java b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManager.java index 11b7b31..e8fb2a1 100644 --- a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManager.java +++ b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManager.java @@ -3,7 +3,12 @@ import android.app.Fragment; import android.os.Bundle; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; import java.util.HashMap; +import java.util.Map; /** * The main object the host app creates. @@ -38,6 +43,9 @@ public class HPPManager extends HPPResponse { public static final String HPPRESPONSE_CONSUMER_URL = "HPPRESPONSE_CONSUMER_URL"; public static final String HPPURL = "HPPURL"; + public static final String HPPREQUEST_PRODUCER_HEADERS ="HPPREQUEST_PRODUCER_HEADERS"; + public static final String HPPRESPONSE_CONSUMER_HEADERS = "HPPRESPONSE_CONSUMER_HEADERS"; + private String hppRequestProducerURL = ""; private String hppResponseConsumerURL = ""; private String hppURL = "https://pay.realexpayments.com/pay"; @@ -45,6 +53,10 @@ public class HPPManager extends HPPResponse { //Supplementary data to be sent to Realex Payments. This will be returned in the HPP response. private HashMap supplementaryData = new HashMap(); + //Headers + private HashMap callbackHeaders = new HashMap(); + private HashMap producerHeaders = new HashMap(); + private static boolean isEncoded = false; public static boolean isEncoded() { @@ -135,6 +147,16 @@ public static HPPManager createFromBundle(Bundle arg) { hppManager.hppResponseConsumerURL = arg.getString(HPPRESPONSE_CONSUMER_URL); hppManager.hppURL = arg.getString(HPPURL); + //reinflate headers from JSON + Type type = new TypeToken>(){}.getType(); + String headersJSON = arg.getString(HPPRESPONSE_CONSUMER_HEADERS); + if (headersJSON != null) + hppManager.setCallbackHeaders((HashMap) new Gson().fromJson(headersJSON, type)); + headersJSON = arg.getString(HPPREQUEST_PRODUCER_HEADERS); + if (headersJSON != null) + hppManager.setProducerHeaders((HashMap) new Gson().fromJson(headersJSON, type)); + + hppManager.merchantId = arg.getString(MERCHANT_ID); hppManager.account = arg.getString(ACCOUNT); hppManager.orderId = arg.getString(ORDER_ID); @@ -180,6 +202,10 @@ public Fragment newInstance() { args.putString(HPPRESPONSE_CONSUMER_URL, hppResponseConsumerURL); args.putString(HPPURL, hppURL); + //save headers as JSON + args.putString(HPPREQUEST_PRODUCER_HEADERS,new Gson().toJson(getProducerHeaders())); + args.putString(HPPRESPONSE_CONSUMER_HEADERS,new Gson().toJson(getCallbackHeaders())); + args.putString(MERCHANT_ID, merchantId); args.putString(ORDER_ID, orderId); args.putString(AMOUNT, amount); @@ -243,4 +269,34 @@ public HashMap getMap() { public void setSupplementaryData(String key, String value) { supplementaryData.put(key,value); } + + /** + * adds a header to be used in the Request to the producer + * @param key + * @param value + */ + public void addHppRequestProducerHeader(String key, String value) {producerHeaders.put(key,value); } + + public HashMap getProducerHeaders() { + return producerHeaders; + } + + public void setProducerHeaders(HashMap headers) { + this.producerHeaders = headers; + } + + /** + * adds a header to be used in the Callback request + * @param key + * @param value + */ + public void addHppRequestCallbackHeader(String key, String value) {callbackHeaders.put(key,value); } + + public HashMap getCallbackHeaders() { + return callbackHeaders; + } + + public void setCallbackHeaders(HashMap headers) { + this.callbackHeaders = headers; + } } diff --git a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManagerFragment.java b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManagerFragment.java index 1a20895..c95cdbc 100644 --- a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManagerFragment.java +++ b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPManagerFragment.java @@ -90,7 +90,8 @@ public void onAttach(Activity activity) { HashMap parameters = hppManager.getMap(); - ApiAdapter.getAdapter(getHostPath(hppManager.getHppRequestProducerURL())).getHPPRequest(getRelativePathEncoded(hppManager.getHppRequestProducerURL()), parameters, this); + ApiAdapter.getAdapter(getHostPath(hppManager.getHppRequestProducerURL()),hppManager.getProducerHeaders()) + .getHPPRequest(getRelativePathEncoded(hppManager.getHppRequestProducerURL()), parameters, this); } else { @@ -305,7 +306,8 @@ public void callbackHandler(String data, String url) { if (!isResultReceived && data.length() > 0) { isResultReceived = true; - ApiAdapter.getAdapter(getHostPath(hppManager.getHppResponseConsumerURL())).getConsumerRequest(getRelativePathEncoded(hppManager.getHppResponseConsumerURL()), + ApiAdapter.getAdapter(getHostPath(hppManager.getHppResponseConsumerURL()),hppManager.getCallbackHeaders()) + .getConsumerRequest(getRelativePathEncoded(hppManager.getHppResponseConsumerURL()), data, new Callback() { @Override public void success(Response s, Response response) { diff --git a/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPRequestInterceptor.java b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPRequestInterceptor.java new file mode 100644 index 0000000..513d227 --- /dev/null +++ b/rxp-hpp-android/src/main/java/com/realexpayments/hpp/HPPRequestInterceptor.java @@ -0,0 +1,36 @@ +package com.realexpayments.hpp; + +import java.util.HashMap; +import java.util.Set; + +import retrofit.RequestInterceptor; + +/** + * author dbures@tmb.cat + * + * Sets headers to a Request + */ +public class HPPRequestInterceptor implements RequestInterceptor { + + private HashMap headers; + private HPPRequestInterceptor() {}; + + public HPPRequestInterceptor(HashMap headers) { + + this.headers = headers; + } + + @Override + public void intercept(RequestFacade request) { + if (headers != null) { + Set keys = headers.keySet(); + for (String key: keys) + { String value; + value = headers.get(key); + request.addHeader(key,value); + } + } + + + } +}