/*
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.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.Feature;
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 RenewAccountTransactionXmlSample {
final static String ApiUrl = "https://xmltest.propay.com/API/PropayAPI.aspx";
public static void main(String[] args) {
configureObjectMapper();
RenewAccountTransactionXmlSample program = new RenewAccountTransactionXmlSample();
TransactionRequest transactionRequest = program.getRequest();
RenewAccountTransactionRequest transaction = program.getRenewAccountTransactionRequest();
transactionRequest.getTransactions().add(transaction);
RenewAccountResponse response = program.sendRequest(transactionRequest);
System.out.println("Response:");
RenewAccountTransactionResponse transactionResponse = response.getTransactions().get(0);
System.out.println("Transaction type: " + transactionResponse.TransType);
System.out.println("Status: " + transactionResponse.status);
System.out.println("Account Number: " + transactionResponse.AccountNumber);
System.out.println("Tier: " + transactionResponse.TierName);
}
/**
* Get the affiliate credential id. This would normally be in a
* configuration file or database.
*/
private String getCertString() {
return "TiAuNrNwEjRnScCaE9RcTcS7ReI9NG";
}
/**
* Get the request container, setting up the authentication.
*
* @return The request container.
*/
private TransactionRequest getRequest() {
String certString = getCertString();
TransactionRequest transactionRequest = new TransactionRequest();
transactionRequest.CertificationString = certString;
transactionRequest.TerminalId = "ReI9NG";
return transactionRequest;
}
/**
* Get the transaction details, how much to refund, on which transaction.
*
*/
private RenewAccountTransactionRequest getRenewAccountTransactionRequest() {
RenewAccountTransactionRequest transaction = new RenewAccountTransactionRequest();
transaction.AccountNumber = "30829395";
transaction.CreditCardNumber = "41111111111111111";
transaction.ExpirationDate = "0519";
transaction.Cvv2 = "999";
transaction.Zip = "84043";
return transaction;
}
/**
* Send the refund transaction request.
*
* @param transactionRequest
* The transaction request.
* @return The transaction response.
*/
private RenewAccountResponse sendRequest(TransactionRequest transactionRequest) {
HttpRequestWithBody request = this.createRequest(ApiUrl);
request.body(transactionRequest);
RenewAccountResponse response = this.executeRequest(request, RenewAccountResponse.class);
return response;
}
/**
* Create the request instance. This ensures that the correct content type
* and accept headers are attached to each request.
*
* @param resourceUrl
* The URL of the resource.
* @return The request instance.
*/
public HttpRequestWithBody createRequest(String resourceUrl) {
HttpRequestWithBody request = Unirest.post(resourceUrl).header("accept", "text/xml").header("Content-Type",
"text/xml");
return request;
}
/**
* Execute a 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.
*/
public <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 XML 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 XmlMapper()
.configure(Feature.WRITE_XML_DECLARATION, true);
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
// 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 com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "XMLTrans")
public class Transaction {
@JacksonXmlProperty(localName = "transType")
public String TransType;
}
// -------------------------------------------------------------------------------------------- //
// TransactionRequest.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.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "XMLRequest")
public class TransactionRequest {
@JacksonXmlProperty(localName = "certStr")
public String CertificationString;
@JacksonXmlProperty(localName = "class")
public final String ClassType = "partner";
/**
* Omit unless specifically instructed by ProPay, Used to access the API
*/
@JacksonXmlProperty(localName = "termid")
public String TerminalId;
@JacksonXmlProperty(localName = "XMLTrans")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Transaction> Transactions;
public TransactionRequest() {
this.Transactions = new ArrayList<Transaction>();
}
/**
* @return the transactions
*/
@JacksonXmlProperty(localName = "XMLTrans")
@JacksonXmlElementWrapper(useWrapping = false)
public List<Transaction> getTransactions() {
return this.Transactions;
}
}
// -------------------------------------------------------------------------------------------- //
// RenewAccountTransactionRequest.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.dataformat.xml.annotation.JacksonXmlProperty;
public class RenewAccountTransactionRequest extends Transaction {
public RenewAccountTransactionRequest() {
this.TransType = "39";
}
/**
* The account number for the account to lookup.
*
* One of accountNumber, sourceEmail or externalId is required.
*/
@JacksonXmlProperty(localName = "accountNum")
public String AccountNumber;
/**
* The credit card CVV code.
*/
@JacksonXmlProperty(localName = "CVV2")
public String Cvv2;
/**
* The tier for the account after the renewal.
*
* Optional: If not specified the account's tier will not be changed.
*/
@JacksonXmlProperty(localName = "tier")
public String TierName;
/**
* The credit card number paying for the renewal fee.
*
* Optional: Accounts can be affiliate paid or pay the renewal fee from the
* account balance.
*/
@JacksonXmlProperty(localName = "ccNum")
public String CreditCardNumber;
/**
* The credit card expiration date.
*/
@JacksonXmlProperty(localName = "expDate")
public String ExpirationDate;
/**
* The billing zip code for US credit cards.
*
* This field is not used if paying with an international credit card.
*/
public String Zip;
}
// -------------------------------------------------------------------------------------------- //
// RenewAccountTransactionResponse.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.dataformat.xml.annotation.JacksonXmlProperty;
public class RenewAccountTransactionResponse extends Transaction {
public String status;
@JacksonXmlProperty(localName = "accountNum")
public String AccountNumber;
@JacksonXmlProperty(localName = "tier")
public String TierName;
}
// -------------------------------------------------------------------------------------------- //
// RenewAccountResponse.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.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName = "XMLResponse")
public class RenewAccountResponse {
@JacksonXmlProperty(localName = "XMLTrans")
@JacksonXmlElementWrapper(useWrapping = false)
private List<RenewAccountTransactionResponse> transactions;
/**
* @return the transactions
*/
public List<RenewAccountTransactionResponse> getTransactions() {
return transactions;
}
}
Go to Ruwix.com to learn the solution of the Rubik's Cube and other twisty puzzles like Pyraminx, Square-1 etc.