/*
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 ProcessPaymentMethodSample {
/**
* 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();
ProcessPaymentMethodSample program = new ProcessPaymentMethodSample();
ProcessPaymentMethodTransactionResponse response = program.processPaymentMethodTransaction();
Result requestResult = response.RequestResult;
System.out.println("Result: " + requestResult.ResultValue);
System.out.println("Result code: " + requestResult.ResultCode);
System.out.println("Result message: " + requestResult.ResultMessage);
Transaction transactionDetail = response.Transaction;
if (transactionDetail != null) {
Result transactionResult = transactionDetail.ResultCode;
System.out.println("Transaction Result: " + transactionResult.ResultValue);
System.out.println("Transaction Result code: " + transactionResult.ResultCode);
System.out.println("Transaction History Id: " + transactionDetail.TransactionHistoryId);
System.out.println("Authorization Code: " + transactionDetail.AuthorizationCode);
System.out.println("AVS Code: " + transactionDetail.AvsCode);
System.out.println("Currency Converted Amount: " + transactionDetail.CurrencyConvertedAmount);
System.out.println("Currency Converted Currency Code: " + transactionDetail.CurrencyConvertedCurrencyCode);
System.out.println("Currency Conversion Rate: " + transactionDetail.CurrencyConversionRate);
System.out.println("Gross Amount: " + transactionDetail.GrossAmount);
System.out.println("Gross Amount Less Net Amount: " + transactionDetail.GrossAmountLessNetAmount);
System.out.println("Net Amount: " + transactionDetail.NetAmount);
System.out.println("Per Transaction Fee: " + transactionDetail.PerTransactionFee);
System.out.println("Rate: " + transactionDetail.Rate);
System.out.println("Transaction Id: " + transactionDetail.TransactionId);
System.out.println("Transaction Result: " + transactionDetail.TransactionResult);
}
}
/**
* Creates a payment method.
*
* @return The payment method response.
*/
private ProcessPaymentMethodTransactionResponse processPaymentMethodTransaction() {
ProcessPaymentMethodTransaction paymentMethodTransaction = this.buildRequest();
String resourceUrl = this.getUrl(paymentMethodTransaction);
HttpRequestWithBody request = this.createRequest(baseUrl + resourceUrl);
request.body(paymentMethodTransaction);
return this.executeRequest(request, ProcessPaymentMethodTransactionResponse.class);
}
/**
* Gets the authentication token. This would normally be in a configuration
* file or database.
*
* @return The authentication token.
*/
private String getAuthToken() {
return "16dfe8d7-889b-4380-925f-9c2c6ea4d930";
}
/**
* Gets the biller's id. This would normally be in a configuration file or
* database.
*
* @return
*/
private String getBillerId() {
return "2781086379225246";
}
/**
* 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 String getPayerAccountId() {
return "6192936083671743";
}
/**
* Gets the payment method id.
*
* This would normally be in a database or generated as a result of an
* earlier call, see ProtectPay API Manual section "Create a
* PaymentMethodId" or "Payer Management Interface Methods"
*/
private String getPaymentMethodId() {
return "dc74a8a3-35c1-40bc-9abb-096a3c397fa4";
}
/**
* 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;
}
/**
* Builds the request data.
*
* @returns The request data.
*/
private ProcessPaymentMethodTransaction buildRequest() {
String payerAccountId = this.getPayerAccountId();
String paymentMethodId = this.getPaymentMethodId();
final int TenUsDollars = 1000;
ProcessPaymentMethodTransaction processPaymentMethodTransaction = new ProcessPaymentMethodTransaction();
processPaymentMethodTransaction.PayerAccountId = payerAccountId;
processPaymentMethodTransaction.PaymentMethodId = paymentMethodId;
processPaymentMethodTransaction.Amount = TenUsDollars;
processPaymentMethodTransaction.CurrencyCode = "USD";
processPaymentMethodTransaction.IsRecurringPayment = false;
return processPaymentMethodTransaction;
}
/**
* Gets the proper token URL for the given request.
*
* @returns The URL to be added to the base URL.
*/
private String getUrl(ProcessPaymentMethodTransaction paymentMethodTransaction) {
String payerId = paymentMethodTransaction.PayerAccountId;
return "/Payers/" + payerId + "/PaymentMethods/ProcessedTransactions/";
}
/**
* 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
// ProcessPaymentMethodTransaction.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 processing a tokenized payment method.
*
* This example only shows required fields. See the ProtectPay API Manual under
* "Process a PaymentMethodId" for additional optional fields.
*/
public class ProcessPaymentMethodTransaction {
/**
* A flag indicating whether this payment is recurring, i.e. repeats on a
* scheduled basis.
*/
public boolean IsRecurringPayment;
/**
* The id of the tokenized payment method to process.
*/
public String PaymentMethodId;
/**
* The amount to charge in pennies (USD) or smallest unit of currency
* (non-USD) without decimals.
*
* e.g. $1.00 USD is 100 ¥10 JPY is 10 since Japanese Yen does not use a
* decimal place.
*/
public int Amount;
/**
* The 3 character currency code (see
* https://en.wikipedia.org/wiki/ISO_4217)
*/
public String CurrencyCode;
/**
* The id of the merchant processing profile to use.
*
* Important if the account has multiple processing gateways, e.g. for
* different countries.
*
*/
public int MerchantProfileId;
/**
* The account id of the payer that has the payment method.
*/
public String PayerAccountId;
}
// -------------------------------------------------------------------------------------------- //
// 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;
}
// -------------------------------------------------------------------------------------------- //
// Transaction.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 java.math.BigDecimal;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Transaction details.
*
*/
public class Transaction {
/**
* The transaction history id in the ProPay system.
*/
public String TransactionHistoryId;
/**
* The transaction authorization code from the issuing bank.
*/
public String AuthorizationCode;
/**
* The address verification system (AVS) code.
*
* Only present if billing information is present on payment method, or in
* the request.
*/
@JsonProperty("AVSCode")
public String AvsCode;
/**
* The card verification value (CVV) response code.
*/
@JsonProperty("CVVResponseCode")
public String CvvResponseCode;
/**
* The amount in the settled currency.
*/
public int CurrencyConvertedAmount;
/**
* The settled currency code.
*/
public String CurrencyConvertedCurrencyCode;
/**
* The conversion rate from the requested currency to the settled currency.
* e.g. USD to USD = 1, JPY to USD = 0.0090 (as of 5/17/2017)
*/
public BigDecimal CurrencyConversionRate;
/**
* The gross amount in the settled currency.
*/
@JsonProperty("GrossAmt")
public Integer GrossAmount;
/**
* The gross amount less net amount in the settled currency.
*/
@JsonProperty("GrossAmtLessNetAmt")
public Integer GrossAmountLessNetAmount;
/**
* The net amount in the settled currency.
*/
@JsonProperty("NetAmt")
public Integer NetAmount;
/**
* The per transaction fee in the settled currency.
*/
@JsonProperty("PerTransFee")
public Integer PerTransactionFee;
/**
* The percentage fee, if any.
*/
public BigDecimal Rate;
/**
* Result information from the transaction.
*/
public Result ResultCode;
/**
* The transaction number assigned by the processor.
*/
public String TransactionId;
/**
* The transaction result as reported by the processor.
*/
public String TransactionResult;
}
// -------------------------------------------------------------------------------------------- //
// ProcessPaymentMethodTransactionResponse.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 value returned from call to process a payment method.
*
*/
public class ProcessPaymentMethodTransactionResponse {
/**
* The transaction details
*/
public Transaction Transaction;
/**
* The result of the request.
*/
public Result RequestResult;
}