/*
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.UUID;
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 CreateHostedTransactionSample {
/**
* This URL would normally come from some configuration file or database.
*/
final String baseUrl = "https://xmltestapi.propay.com/protectpay/";
public static void main(String[] args) {
configureObjectMapper();
CreateHostedTransactionSample program = new CreateHostedTransactionSample();
CreateHostedTransactionResponse response = program.createHostedTransaction();
Result requestResult = response.Result;
System.out.println("Result: " + requestResult.ResultValue);
System.out.println("Result code: " + requestResult.ResultCode);
System.out.println("Result message: " + requestResult.ResultMessage);
System.out.println("Hosted transaction identifier: " + response.HostedTransactionIdentifier);
}
public CreateHostedTransactionResponse createHostedTransaction() {
String resourceUrl = baseUrl + "HostedTransactions";
HttpRequestWithBody request = this.createRequest(resourceUrl);
CreateHostedTransactionRequest hostedTransactionRequest = this.buildRequest();
request.body(hostedTransactionRequest);
return this.executeRequest(request, CreateHostedTransactionResponse.class);
}
private CreateHostedTransactionRequest buildRequest() {
final long TenDollars = 1000;
long payerId = this.getPayerAccountId();
String invoiceNumber = this.getInvoiceNumber();
CreateHostedTransactionRequest request = new CreateHostedTransactionRequest();
request.PayerAccountId = payerId;
request.Amount = TenDollars;
request.CurrencyCode = "USD";
request.Comment1 = "PropaySdkCreateHostedTransaction 1";
request.Comment2 = "PropaySdkCreateHostedTransaction 2";
request.InvoiceNumber = invoiceNumber;
request.AuthOnly = true;
request.ProcessCard = true;
request.StoreCard = true;
request.OnlyStoreCardOnSuccessfulProcess = true;
request.CardHolderNameRequirementType = CreateHostedTransactionRequest.RequirementType.Required;
request.SecurityCodeRequirementType = CreateHostedTransactionRequest.RequirementType.Required;
request.AvsRequirementType = CreateHostedTransactionRequest.RequirementType.Required;
request.CssUrl = "https://epay.propay.com/App_Themes/default/Menu.css";
return request;
}
/**
* 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";
}
/**
* Gets the payer's id.
*
* This would normally be in a database or generated as a result of an
* earlier call, see ProtectPay API Manual sections "Create a PayerId" or
* "Create TempToken".
*/
private long getPayerAccountId() {
return 1234567890789787L;
}
/**
* Get the invoice number.
*
* This is whatever invoice numbering your company uses.
*/
private String getInvoiceNumber() {
return UUID.randomUUID().toString();
}
/**
* 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
// CreateHostedTransactionRequest.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.
*/
/**
* Request to create a hosted transaction so your customer can use the Hosted
* Payment Page (HPP) to enter payment information.
*/
public class CreateHostedTransactionRequest {
public enum RequirementType {
/**
* The section is required (it is displayed, and user must fill it out).
*/
Required(1),
/**
* The section is optional (it is displayed, and user may or may not
* fill it out).
*/
Optional(2),
/**
* The section is hidden (it is not displayed to the user).
*/
Hidden(3);
/**
* The integer value for the enumeration.
*/
private int value;
private RequirementType(int value) {
this.value = value;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
}
/**
* Optional value to designate the payer of the payment method.
*/
public long PayerAccountId;
/**
* Optional value to designate the merchant profile id that should be used
* to process the transaction.
*/
public long MerchantProfileId;
/**
* The amount of the transaction.
*/
public long Amount;
/**
* The currency code of the transaction.
*/
public String CurrencyCode;
/**
* Comment to associate with the transaction.
*/
public String Comment1;
/**
* Comment to associate with the transaction.
*/
public String Comment2;
/**
* The invoice number of the transaction.
*/
public String InvoiceNumber;
/**
* Whether to perform an authorize only transaction (true) or to
* capture/settle the transaction (false).
*
* If true the ProcessCard field is ignored.
*/
public boolean AuthOnly;
/**
* Whether to capture/settle the transaction.
*
* If AuthOnly is true this field is ignored.
*/
public boolean ProcessCard;
/**
* Whether to always store the card information.
*
* This is ignored if the OnlyStoreCardOnSuccessfulProcess field is true.
*/
public boolean StoreCard;
/**
* Whether to save the tokenized card only if the card transaction succeeds.
*/
public boolean OnlyStoreCardOnSuccessfulProcess;
/**
* Indicates if the 'CardHolderName' section should be required, optional,
* or hidden.
*/
public RequirementType CardHolderNameRequirementType;
/**
* Indicates if the 'SecurityCode' section should be required, optional, or
* hidden.
*/
public RequirementType SecurityCodeRequirementType;
/**
* Indicates if the 'AVS' section should be required, optional, or hidden.
*/
public RequirementType AvsRequirementType;
/**
* The URL to use for the CSS that drives the look of the page (max 1024
* characters).
*/
public String CssUrl;
}
// -------------------------------------------------------------------------------------------- //
// 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;
}
// -------------------------------------------------------------------------------------------- //
// CreateHostedTransactionResponse.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.
*/
public class CreateHostedTransactionResponse {
/**
* The API Result from the method call.
*/
public Result Result;
/**
* The identifier used to get the results of the hosted transaction after it
* is completed.
*/
public String HostedTransactionIdentifier;
}