use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.
the class BraintreeGateway method createCustomer.
private HTTPResponse createCustomer(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
boolean hasException = false;
CustomerRequest customerRequest = new CustomerRequest();
customerRequest.firstName(customer.getFirstName()).lastName(customer.getLastName()).email(customer.getEmail()).phone(customer.getPhoneNumber());
CreditCardRequest card = customerRequest.creditCard();
card.cardholderName(customerCard.getName()).number(customerCard.getNumber()).cvv(customerCard.getCvv()).expirationDate(customerCard.getExpiryMonth() + "/" + customerCard.getExpiryYear());
CreditCardAddressRequest address = card.billingAddress();
address.countryCodeAlpha3(customer.getCountry().getCodeISO3()).postalCode(customer.getZip()).streetAddress(customer.getAddress()).region(customer.getState()).locality(customer.getCity());
Result<com.braintreegateway.Customer> createCustomer = null;
GeneralResponse successResponse = null;
ErrorResponse errorResponse = new ErrorResponse();
HTTPResponse httpResponse = new HTTPResponse();
try {
createCustomer = getGatewayObject(apiParameters).customer().create(customerRequest);
} catch (NotFoundException e) {
errorResponse.setMessage("No record found");
hasException = true;
} catch (AuthenticationException e) {
errorResponse.setMessage("Invalid credentials");
hasException = true;
} catch (AuthorizationException e) {
errorResponse.setMessage("Not authorized to perform this action");
hasException = true;
} catch (ConfigurationException e) {
errorResponse.setMessage("Configuratoin error");
hasException = true;
} catch (DownForMaintenanceException e) {
errorResponse.setMessage("Server is under maintenance");
hasException = true;
} catch (TimeoutException e) {
errorResponse.setMessage("Request time out");
hasException = true;
} catch (ForgedQueryStringException | InvalidChallengeException | InvalidSignatureException | UnexpectedException e) {
errorResponse.setMessage("Unexpected exception occured");
hasException = true;
} catch (ServerException e) {
errorResponse.setMessage("Server error");
hasException = true;
} catch (TooManyRequestsException e) {
errorResponse.setMessage("Too many requests");
hasException = true;
} catch (UpgradeRequiredException e) {
errorResponse.setMessage("Need to upgrade");
hasException = true;
}
if (hasException) {
processFinalResponse(null, httpResponse, successResponse, errorResponse);
return httpResponse;
}
if (createCustomer.isSuccess()) {
successResponse = new GeneralResponse();
successResponse.setMessage("Success");
successResponse.setTransactionId(createCustomer.getTarget().getId());
} else {
errorResponse.setMessage(createCustomer.getMessage());
}
processFinalResponse(null, httpResponse, successResponse, errorResponse);
return httpResponse;
}
use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.
the class BraintreeGateway method capture.
@Override
public HTTPResponse capture(JSONObject apiParameters, JSONObject captureParameters, float amount) {
boolean hasException = false;
CaptureResponse successResponse = null;
ErrorResponse errorResponse = new ErrorResponse();
HTTPResponse httpResponse = new HTTPResponse();
Result<Transaction> capture = null;
try {
capture = getGatewayObject(apiParameters).transaction().submitForSettlement(captureParameters.getString(ParamList.TRANSACTION_ID.getName()), new BigDecimal(Float.toString(amount)));
} catch (NotFoundException e) {
errorResponse.setMessage("No record found");
hasException = true;
} catch (AuthenticationException e) {
errorResponse.setMessage("Invalid credentials");
hasException = true;
} catch (AuthorizationException e) {
errorResponse.setMessage("Not authorized to perform this action");
hasException = true;
} catch (ConfigurationException e) {
errorResponse.setMessage("Configuratoin error");
hasException = true;
} catch (DownForMaintenanceException e) {
errorResponse.setMessage("Server is under maintenance");
hasException = true;
} catch (TimeoutException e) {
errorResponse.setMessage("Request time out");
hasException = true;
} catch (ForgedQueryStringException | InvalidChallengeException | InvalidSignatureException | UnexpectedException e) {
errorResponse.setMessage("Unexpected exception occured");
hasException = true;
} catch (ServerException e) {
errorResponse.setMessage("Server error");
hasException = true;
} catch (TooManyRequestsException e) {
errorResponse.setMessage("Too many requests");
hasException = true;
} catch (UpgradeRequiredException e) {
errorResponse.setMessage("Need to upgrade");
hasException = true;
}
if (hasException) {
processFinalResponse(null, httpResponse, successResponse, errorResponse);
return httpResponse;
}
if (capture.isSuccess()) {
successResponse = new CaptureResponse();
successResponse.setMessage("Success");
successResponse.setTransactionId(capture.getTarget().getId());
successResponse.setAmount(amount);
successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), capture.getTarget().getId()));
successResponse.setRefundParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), capture.getTarget().getId()));
} else {
errorResponse.setMessage(capture.getMessage());
}
processFinalResponse(null, httpResponse, successResponse, errorResponse);
return httpResponse;
}
use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.
the class BraintreeGateway method rebill.
@Override
public HTTPResponse rebill(JSONObject apiParameters, JSONObject rebillParameters, float amount) {
boolean hasException = false;
RebillResponse successResponse = null;
ErrorResponse errorResponse = new ErrorResponse();
HTTPResponse httpResponse = new HTTPResponse();
TransactionRequest transactionRequest = new TransactionRequest();
transactionRequest.customerId(rebillParameters.getString(CUSTOMER_ID)).amount(new BigDecimal(Float.toString(amount)));
transactionRequest.options().submitForSettlement(true);
Result<Transaction> sale = null;
try {
sale = getGatewayObject(apiParameters).transaction().sale(transactionRequest);
} catch (NotFoundException e) {
errorResponse.setMessage("No record found");
hasException = true;
} catch (AuthenticationException e) {
errorResponse.setMessage("Invalid credentials");
hasException = true;
} catch (AuthorizationException e) {
errorResponse.setMessage("Not authorized to perform this action");
hasException = true;
} catch (ConfigurationException e) {
errorResponse.setMessage("Configuratoin error");
hasException = true;
} catch (DownForMaintenanceException e) {
errorResponse.setMessage("Server is under maintenance");
hasException = true;
} catch (TimeoutException e) {
errorResponse.setMessage("Request time out");
hasException = true;
} catch (ForgedQueryStringException | InvalidChallengeException | InvalidSignatureException | UnexpectedException e) {
errorResponse.setMessage("Unexpected exception occured");
hasException = true;
} catch (ServerException e) {
errorResponse.setMessage("Server error");
hasException = true;
} catch (TooManyRequestsException e) {
errorResponse.setMessage("Too many requests");
hasException = true;
} catch (UpgradeRequiredException e) {
errorResponse.setMessage("Need to upgrade");
hasException = true;
}
if (hasException) {
processFinalResponse(null, httpResponse, successResponse, errorResponse);
return httpResponse;
}
if (sale.isSuccess()) {
successResponse = new RebillResponse();
successResponse.setMessage("Success");
successResponse.setTransactionId(sale.getTarget().getId());
successResponse.setAmount(amount);
successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), sale.getTarget().getId()));
successResponse.setRebillParams(new JSONObject().put(CUSTOMER_ID, rebillParameters.getString(CUSTOMER_ID)));
successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), sale.getTarget().getId()));
} else {
errorResponse.setMessage(sale.getMessage());
}
processFinalResponse(null, httpResponse, successResponse, errorResponse);
return httpResponse;
}
use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.
the class HTTPClient method httpPostWithBasicAuth.
/**
* This method is the wrapper of apache http client post request with basic Auth.
*
* @param url The url on which the request will be hit.
* @param postParams parameters which will be passed for post.
* @param contentType content-type in which data will be posted.
* @param charset the charset in which request will be posted if null is
* provided contentType charset will be used.
* @param authUserName the Auth username
* @param authPassword the Auth passord
* @return The HTTPResponse class.
* @see com.tranxactive.j2pay.net.HTTPResponse
* @see ContentType
*/
public static HTTPResponse httpPostWithBasicAuth(String url, String postParams, ContentType contentType, Charset charset, String authUserName, String authPassword) {
HTTPResponse hTTPResponse;
long startTime = System.currentTimeMillis(), endTime;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new StringEntity(postParams));
httpPost.addHeader("Content-Type", charset == null ? contentType.toString() : create(contentType.getMimeType(), charset).toString());
String auth = authUserName + ":" + authPassword;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
httpPost.addHeader(HttpHeaders.AUTHORIZATION, authHeader);
startTime = System.currentTimeMillis();
CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpPost);
endTime = System.currentTimeMillis();
hTTPResponse = new HTTPResponse(closeableHttpResponse.getStatusLine().getStatusCode(), EntityUtils.toString(closeableHttpResponse.getEntity()).replaceFirst("^\uFEFF", ""), endTime - startTime);
hTTPResponse.setRequestString(postParams);
EntityUtils.consume(closeableHttpResponse.getEntity());
} catch (IOException e) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setMessage("could not connect to host");
return new HTTPResponse(-1, errorResponse.getResponse().toString(), System.currentTimeMillis() - startTime);
}
return hTTPResponse;
}
Aggregations