/**
* 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.Random;
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 ProPayToProPaySample {
/**
* This URL would normally come from some configuration file or database.
*/
final static String ApiUrl = "https://xmltestapi.propay.com/propayapi/";
public static void main(String[] args) {
configureObjectMapper();
ProPayToProPaySample program = new ProPayToProPaySample();
ProPayToProPayResponse response = program.propayToPropay();
System.out.println("Transaction Status:" + response.Status);
System.out.println("Account number: " + response.AccountNumber);
System.out.println("Transaction number: " + response.TransactionNumber);
}
/**
* Execute a ProPay to ProPay transaction.
*
* @return The ProPay to ProPay transaction response.
*/
public ProPayToProPayResponse propayToPropay() {
String resourceUrl = ApiUrl + "ProPayToProPay";
final int TenUsDollars = 1000;
ProPayToProPayRequest ppToPpRequest = new ProPayToProPayRequest();
ppToPpRequest.Amount = TenUsDollars;
ppToPpRequest.ReceivingAccountNumber = this.getReceivingAccount();
ppToPpRequest.InvoiceNumber = this.getInvoiceNumber();
ppToPpRequest.Comment1 = "Product Sales commission";
ppToPpRequest.Comment2 = "May 2017";
HttpRequestWithBody request = this.createRequest(resourceUrl);
request.body(ppToPpRequest);
return this.executeRequest(request, ProPayToProPayResponse.class);
}
/**
* The source account's certificate string.
*
* This is different than the affiliate's certstring.
*
* Normally this would be in a database or configuration file.
*/
private String getCertString() {
return "D835BBB96D23476BBD1E2A79736EC3";
}
/**
* The affiliate term id.
*
* Normally this would be in a database or configuration file.
*/
private String getTermId() {
return "commissions";
}
/**
* Gets a random invoice number.
*
* This would normally come from an internal invoice number tracking system.
*
* @return The invoice number.
*/
private String getInvoiceNumber() {
Random rand = new Random();
int invoiceNumber = rand.nextInt(100) + 1;
return String.valueOf(invoiceNumber);
}
/**
* Get the account number for the account receiving the money.
*
* @return The receiving account number.
*/
private String getReceivingAccount() {
return "30829395";
}
/**
* 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 certString = this.getCertString();
String termId = this.getTermId();
HttpRequestWithBody restRequest = Unirest.put(resourceUrl).basicAuth(certString, termId)
.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
// ProPayToProPayRequest.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.annotation.JsonProperty;
public class ProPayToProPayRequest {
@JsonProperty("amount")
public int Amount;
@JsonProperty("invNum")
public String InvoiceNumber;
@JsonProperty("recAccntNum")
public String ReceivingAccountNumber;
@JsonProperty("comment1")
public String Comment1;
@JsonProperty("comment2")
public String Comment2;
}
// -------------------------------------------------------------------------------------------- //
// ProPayToProPayResponse.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 ProPayToProPayResponse {
public String Status;
public String AccountNumber;
public int TransactionNumber;
}