/**
* 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 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 CreatePayerSample {
/**
* This URL would normally come from some configuration file or database.
*/
String baseUrl = "https://xmltestapi.propay.com/protectpay/";
public static void main(String[] args) {
configureObjectMapper();
CreatePayerSample program = new CreatePayerSample();
CreatePayerResponse response = program.createPayer();
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("Payer account id: " + response.ExternalAccountID);
}
public CreatePayerResponse createPayer() {
String resourceUrl = baseUrl + "Payers";
HttpRequestWithBody request = this.createRequest(resourceUrl);
CreatePayerRequest payer = this.buildRequest();
request.body(payer);
return this.executeRequest(request, CreatePayerResponse.class);
}
private CreatePayerRequest buildRequest() {
CreatePayerRequest payer = new CreatePayerRequest();
payer.Name = "John Travolta";
payer.EmailAddress = "saturday@nightfever.com";
payer.ExternalId1 = "Distributer 2";
payer.ExternalId2 = "Rep 35";
return payer;
}
/**
* Gets the authentication token. This would normally be in a configuration
* file or database.
*
* @return The authentication token.
*/
private String getAuthToken() {
return "EFA7EEDD-065C-4361-AC24-E13BCC4EF520";
}
/**
* Gets the biller's id. This would normally be in a configuration file or
* database.
*
*/
private String getBillerId() {
return "9876543210987654";
}
/**
* 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
// CreatePayerRequest.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.
*/
/**
* A request to create a ProtectPay payer, i.e. someone who is going to securely
* pay for a product/service.
*/
public class CreatePayerRequest {
/**
* A name to identify the payer.
*/
public String Name;
/**
* The payer's email address if supplied.
*/
public String EmailAddress;
/**
* Customer specific identifier.
*/
public String ExternalId1;
/**
* Customer specific identifier.
*/
public String ExternalId2;
}
// -------------------------------------------------------------------------------------------- //
// 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;
}
// -------------------------------------------------------------------------------------------- //
// CreatePayerResponse.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.
*/
/**
* A response to a create payer request.
*/
public class CreatePayerResponse {
/*
* The API Result from the method call.
*/
public Result RequestResult;
/**
* The ProtectPay ID for the created payer.
*
* Note: This is referenced in other methods as 'PayerAccountId' or
* 'PayerId'.
*/
public String ExternalAccountID;
}