/*
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 RefundTransactionSample {
/**
* 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();
RefundTransactionSample program = new RefundTransactionSample();
RefundTransactionResponse response = program.refundTransaction();
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 RefundTransactionResponse refundTransaction() {
RefundTransactionRequest refundRequest = this.buildRequest();
HttpRequestWithBody request = this.createRequest(baseUrl + "/VoidedTransactions/");
request.body(refundRequest);
return this.executeRequest(request, RefundTransactionResponse.class);
}
/**
* 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.
*
* @return
*/
private String getBillerId() {
return "9876543210987654";
}
/**
* Gets the transaction history id.
*
* This would normally be in a database or generated as a result of an
* earlier call, see ProtectPay API Manual sections "PaymentMethodId
* Processing Authorization Methods" or "PaymentMethodId Processing Capture
* Methods".
*/
private String getTransactionHistoryId() {
return "11151220";
}
/**
* 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 RefundTransactionRequest buildRequest() {
String transactionHistoryId = this.getTransactionHistoryId();
RefundTransactionRequest refundRequest = new RefundTransactionRequest();
refundRequest.TransactionHistoryId = transactionHistoryId;
refundRequest.Comment1 = "Customer return";
return refundRequest;
}
/**
* 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
// RefundTransactionRequest.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 void a transaction.
*
*/
public class RefundTransactionRequest {
/**
* The transaction history id in the ProPay system.
*/
public String TransactionHistoryId;
/**
* Optional comment 1.
*/
public String Comment1;
/**
* Optional comment 2
*/
public String Comment2;
}
// -------------------------------------------------------------------------------------------- //
// 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;
}
// -------------------------------------------------------------------------------------------- //
// RefundTransactionResponse.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 void a transaction.
*
*/
public class RefundTransactionResponse {
/**
* The transaction details
*/
public Transaction Transaction;
/**
* The result of the request.
*/
public Result RequestResult;
}