Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added capacity to configure headers on the PRODUCER and CONSUMER endP… #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -38,13 +43,20 @@ 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";

//Supplementary data to be sent to Realex Payments. This will be returned in the HPP response.
private HashMap<String, String> supplementaryData = new HashMap<String, String>();

//Headers
private HashMap<String, String> callbackHeaders = new HashMap<String, String>();
private HashMap<String, String> producerHeaders = new HashMap<String, String>();

private static boolean isEncoded = false;

public static boolean isEncoded() {
Expand Down Expand Up @@ -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<HashMap<String, String>>(){}.getType();
String headersJSON = arg.getString(HPPRESPONSE_CONSUMER_HEADERS);
if (headersJSON != null)
hppManager.setCallbackHeaders((HashMap<String, String>) new Gson().fromJson(headersJSON, type));
headersJSON = arg.getString(HPPREQUEST_PRODUCER_HEADERS);
if (headersJSON != null)
hppManager.setProducerHeaders((HashMap<String, String>) new Gson().fromJson(headersJSON, type));


hppManager.merchantId = arg.getString(MERCHANT_ID);
hppManager.account = arg.getString(ACCOUNT);
hppManager.orderId = arg.getString(ORDER_ID);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -243,4 +269,34 @@ public HashMap<String, String> 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<String, String> getProducerHeaders() {
return producerHeaders;
}

public void setProducerHeaders(HashMap<String, String> 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<String, String> getCallbackHeaders() {
return callbackHeaders;
}

public void setCallbackHeaders(HashMap<String, String> headers) {
this.callbackHeaders = headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ public void onAttach(Activity activity) {

HashMap<String, String> 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 {

Expand Down Expand Up @@ -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<Response>() {
@Override
public void success(Response s, Response response) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.realexpayments.hpp;

import java.util.HashMap;
import java.util.Set;

import retrofit.RequestInterceptor;

/**
* author [email protected]
*
* Sets headers to a Request
*/
public class HPPRequestInterceptor implements RequestInterceptor {

private HashMap<String, String> headers;
private HPPRequestInterceptor() {};

public HPPRequestInterceptor(HashMap<String, String> headers) {

this.headers = headers;
}

@Override
public void intercept(RequestFacade request) {
if (headers != null) {
Set<String> keys = headers.keySet();
for (String key: keys)
{ String value;
value = headers.get(key);
request.addHeader(key,value);
}
}


}
}