/**
* 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 TimedPullSample {
/**
* 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();
TimedPullSample program = new TimedPullSample();
TimedPullResponse response = program.timedPull();
System.out.println("Transaction Status:" + response.Status);
System.out.println("Account number: " + response.AccountNumber);
System.out.println("Transaction number: " + response.TransactionNumber);
}
/**
* Execute a timed pull transaction.
*
* @return The timed pull transaction response.
*/
public TimedPullResponse timedPull() {
String resourceUrl = ApiUrl + "TimedPull";
final int FiveUsDollars = 500;
TimedPullRequest timedPullRequest = new TimedPullRequest();
timedPullRequest.TransactionNumber = this.getCardTransactionNumber();
timedPullRequest.Amount = FiveUsDollars;
timedPullRequest.AccountNumber = this.getSourceAccount();
timedPullRequest.ReceivingAccountNumber = this.getReceivingAccount();
timedPullRequest.InvoiceNumber = this.getInvoiceNumber();
timedPullRequest.Comment1 = "Wholesale cost";
timedPullRequest.Comment2 = "Tea Tree Oil";
HttpRequestWithBody request = this.createRequest(resourceUrl);
request.body(timedPullRequest);
return this.executeRequest(request, TimedPullResponse.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 "P1r305Pc4fy6S942m0p0la3c";
}
/**
* 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 long getReceivingAccount() {
return 97531246;
}
/**
* Get the account number for the source of the money.
*/
private long getSourceAccount() {
return 97531250;
}
/**
* Get the transaction number from which to split funds.
*
* This would normally be generated as a result of an earlier call, see
* ProtectPay API Manual section "Process a PaymentMethodId" or the ProPay
* API manual section "Process a Credit Card Transaction".
*/
private long getCardTransactionNumber() {
return 627;
}
/**
* 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();
HttpRequestWithBody restRequest = Unirest.put(resourceUrl).basicAuth(certString, null)
.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
// TimedPullRequest.java
import com.fasterxml.jackson.annotation.JsonProperty;
/*
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 for a timed pull transaction.
*/
public class TimedPullRequest {
/**
* The proPay account identifier. This is the ‘from’ account and the account
* upon which the credit card transaction was initially performed.
*/
@JsonProperty("accountNum")
public long AccountNumber;
/**
* The ProPay account identifier. This is the account to which the split
* will be sent when the transaction settles.
*/
@JsonProperty("recAccntNum")
public long ReceivingAccountNumber;
/**
* The amount to be split off to the receiving account number when the
* transaction settles. 100=$1.00.
*/
@JsonProperty("amount")
public long Amount;
/**
* The ProPay transaction identifier. It is when this transaction settles
* that the timed pull will occur.
*/
@JsonProperty("transNum")
public long TransactionNumber;
/**
* The invoice number for the transaction.
*/
public String InvoiceNumber;
/**
* The first comment.
*/
@JsonProperty("comment1")
public String Comment1;
/**
* The second comment.
*/
@JsonProperty("comment2")
public String Comment2;
}
// -------------------------------------------------------------------------------------------- //
// TimedPullResponse.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.
*/
/**
* Response to a timed pull request.
*/
public class TimedPullResponse {
/**
* The proPay account identifier. This is the ‘from’ account and the account
* upon which the credit card transaction was initially performed.
*/
public long AccountNumber;
/**
* The ProPay transaction identifier. It is when this transaction settles
* that the timed pull will occur.
*/
public long TransactionNumber;
/**
* The status.
*/
public String Status;
}