/*
ProPay provides the following code "AS IS."
ProPay makes no warranties and ProPay disclaims all warranties and conditions, express, implied or statutory,
including without limitation the implied warranties of title, non-infringement, merchantability, and fitness for a particular purpose.
ProPay does not warrant that the code will be uninterrupted or error free,
nor does ProPay make any warranty as to the performance or any results that may be obtained by use of the code.
*/
import java.io.IOException;
import java.util.ArrayList;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.ObjectMapper;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import com.mashape.unirest.request.HttpRequestWithBody;
public class CreateMerchantProfileSample {
/**
* This URL would normally come from some configuration file or database.
*/
//String baseUrl = "https://xmltestapi.propay.com/protectpay/";
String baseUrl = "http://localhost:5219/API/";
public static void main(String[] args) {
configureObjectMapper();
CreateMerchantProfileSample program = new CreateMerchantProfileSample();
MerchantProfileResponse response = program.createMerchantProfile();
System.out.println("Result: " + response.RequestResult.ResultValue);
System.out.println("Result code: " + response.RequestResult.ResultCode);
System.out.println("Result message: " + response.RequestResult.ResultMessage);
System.out.println("Profile id: " + response.ProfileId);
}
public MerchantProfileResponse createMerchantProfile() {
String resourceUrl = baseUrl + "MerchantProfiles";
MerchantProfile profile = this.buildRequest();
HttpRequestWithBody request = this.createRequest(resourceUrl);
request.body(profile);
return this.executeRequest(request, MerchantProfileResponse.class);
}
/**
* Gets the authentication token. This would normally be in a configuration
* file or database.
*
* @return The authentication token.
*/
private String getAuthToken() {
return "5b8d6753-4aff-4771-b3ae-c831a072a45c";
}
/**
* Gets the biller's id. This would normally be in a configuration file or
* database.
*
* @return
*/
private String getBillerId() {
return "4526293327582082";
}
/**
* Builds the request data.
*
* @returns The request data.
*/
private MerchantProfile buildRequest() {
MerchantProfile profile = new MerchantProfile();
profile.ProfileName = "ProPay";
profile.PaymentProcessor = "LegacyProPay";
profile.ProcessorData = new ProcessorData[4];
ArrayList<ProcessorData> processorData = new ArrayList<ProcessorData>();
ProcessorData certStr = new ProcessorData();
certStr.ProcessorField = "certStr";
certStr.Value = "6fcb7017034141c3992e5d6755edbc";
ProcessorData termId = new ProcessorData();
termId.ProcessorField = "termId";
termId.Value = "c90a84580a";
ProcessorData accountNum = new ProcessorData();
accountNum.ProcessorField = "accountNum";
accountNum.Value = "32685511";
ProcessorData forceRecurring = new ProcessorData();
forceRecurring.ProcessorField = "forceRecurring";
forceRecurring.Value = "false";
processorData.add(certStr);
processorData.add(termId);
processorData.add(accountNum);
processorData.add(forceRecurring);
profile.ProcessorData = processorData.toArray(profile.ProcessorData);
return profile;
}
/**
* Create the request instance. This ensures that the authentication header
* is attached to each request.
*
* @param resourceUrl
* The URL of the REST resource.
* @return The GetRequest instance.
*/
private HttpRequestWithBody createRequest(String resourceUrl) {
String authToken = this.getAuthToken();
String billerId = this.getBillerId();
HttpRequestWithBody restRequest = Unirest.put(resourceUrl).basicAuth(billerId, authToken)
.header("accept", "application/json").header("Content-Type", "application/json");
return restRequest;
}
/**
* Execute a REST request.
*
* @param request
* The request to perform.
* @param responseClass
* The type instance of the return type.
* @return An instance of type T or null if there was an error.
*/
private <T> T executeRequest(HttpRequest request, Class<T> responseClass) {
try {
HttpResponse<T> response = request.asObject(responseClass);
if (response.getStatus() != 200) { // HTTP OK response code
System.out.println(response.getStatusText());
return null;
}
return response.getBody();
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
/**
* Configures the mapping between JSON and Classes.
*
* This is boilerplate Unirest & Jackson configuration. It should only need
* to be done once in a full solution.
*/
private static void configureObjectMapper() {
Unirest.setObjectMapper(new ObjectMapper() {
private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
public <T> T readValue(String value, Class<T> valueType) {
try {
return jacksonObjectMapper.readValue(value, valueType);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String writeValue(Object value) {
try {
return jacksonObjectMapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
});
}
}
// -------------------------------------------------------------------------------------------- //
// Object files
// ProcessorData.java
/**
* ProPay provides the following code "AS IS." ProPay makes no warranties and
* ProPay disclaims all warranties and conditions, express, implied or
* statutory, including without limitation the implied warranties of title,
* non-infringement, merchantability, and fitness for a particular purpose.
* ProPay does not warrant that the code will be uninterrupted or error free,
* nor does ProPay make any warranty as to the performance or any results that
* may be obtained by use of the code.
*/
/**
* Class for processor specific values.
*/
public class ProcessorData {
/**
* The processor specific field name.
*/
public String ProcessorField;
/**
* The value for the processor specific field.
*/
public String Value;
}
// -------------------------------------------------------------------------------------------- //
// MerchantProfile.java
/*
ProPay provides the following code "AS IS."
ProPay makes no warranties and ProPay disclaims all warranties and conditions, express, implied or statutory,
including without limitation the implied warranties of title, non-infringement, merchantability, and fitness for a particular purpose.
ProPay does not warrant that the code will be uninterrupted or error free,
nor does ProPay make any warranty as to the performance or any results that may be obtained by use of the code.
*/
/**
* Data for creating a merchant profile.
*
*/
public class MerchantProfile {
/**
* The name of the profile.
*/
public String ProfileName;
/**
* The payment gateway where transactions will be sent.
*
* See ProtectPay API Appending B for valid processors.
*/
public String PaymentProcessor;
/**
* The processor specific data fields.
*/
public ProcessorData[] ProcessorData;
}
// -------------------------------------------------------------------------------------------- //
// Result.java
/*
ProPay provides the following code "AS IS."
ProPay makes no warranties and ProPay disclaims all warranties and conditions, express, implied or statutory,
including without limitation the implied warranties of title, non-infringement, merchantability, and fitness for a particular purpose.
ProPay does not warrant that the code will be uninterrupted or error free,
nor does ProPay make any warranty as to the performance or any results that may be obtained by use of the code.
*/
/**
* The result of the call.
*/
public class Result {
/**
* The result of the transaction
*
* Will always be SUCCESS or FAILURE
*/
public String ResultValue;
/**
* The result code of the transaction
*
*
* Will be a two-digit string with only numbers. Allows "00" as a response.
*/
public String ResultCode;
/**
* The English-text message of what went wrong (if anything)
*
*
* The documentation shows the empty string being returned in the success
* cases.
*/
public String ResultMessage;
}
// -------------------------------------------------------------------------------------------- //
// MerchantProfileResponse.java
/*
ProPay provides the following code "AS IS."
ProPay makes no warranties and ProPay disclaims all warranties and conditions, express, implied or statutory,
including without limitation the implied warranties of title, non-infringement, merchantability, and fitness for a particular purpose.
ProPay does not warrant that the code will be uninterrupted or error free,
nor does ProPay make any warranty as to the performance or any results that may be obtained by use of the code.
*/
//import com.fasterxml.jackson.annotation.JsonProperty;
/**
* The value returned from a call to create a merchant profile.
*/
public class MerchantProfileResponse {
/**
* The API Result from the request.
*/
public Result RequestResult;
/**
* The merchant profile id for future processing.
*/
public String ProfileId;
}