use of com.tranxactive.j2pay.net.HTTPResponse 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.net.HTTPResponse 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.net.HTTPResponse in project J2PAY by tranxactive.
the class StripeGateway method purchase.
@Override
public HTTPResponse purchase(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
HTTPResponse tokenHTTPResponse = HTTPClient.httpPostWithBasicAuth(apiURL + "/tokens", QueryStringHelper.toQueryString(JSONHelper.encode(this.buildTokenParameters(customer, customerCard, currency))), ContentType.APPLICATION_FORM_URLENCODED, null, apiParameters.getString("userName"), "");
PurchaseResponse successResponse = null;
ErrorResponse errorResponse = new ErrorResponse();
if (tokenHTTPResponse.getStatusCode() == -1) {
return tokenHTTPResponse;
}
JSONObject tokenJSONResponse = tokenHTTPResponse.getJSONResponse();
if (!(tokenHTTPResponse.getStatusCode() >= 200 && tokenHTTPResponse.getStatusCode() < 300)) {
errorResponse.setMessage(tokenJSONResponse.getJSONObject("error").getString("message"));
processFinalResponse(tokenJSONResponse, tokenHTTPResponse, successResponse, errorResponse);
return tokenHTTPResponse;
}
HTTPResponse saveCustomerHTTPResponse = HTTPClient.httpPostWithBasicAuth(apiURL + "/customers", QueryStringHelper.toQueryString(JSONHelper.encode(this.buildSaveCustomerParameters(tokenJSONResponse.getString("id")))), ContentType.APPLICATION_FORM_URLENCODED, null, apiParameters.getString("userName"), "");
if (saveCustomerHTTPResponse.getStatusCode() == -1) {
return saveCustomerHTTPResponse;
}
JSONObject saveCustomerJSONResponse = saveCustomerHTTPResponse.getJSONResponse();
if (!(saveCustomerHTTPResponse.getStatusCode() >= 200 && saveCustomerHTTPResponse.getStatusCode() < 300)) {
errorResponse.setMessage(saveCustomerJSONResponse.getJSONObject("error").getString("message"));
processFinalResponse(saveCustomerJSONResponse, saveCustomerHTTPResponse, successResponse, errorResponse);
return saveCustomerHTTPResponse;
}
JSONObject requestObject = this.buildPurchaseParameters(saveCustomerJSONResponse.getString("id"), currency, amount);
JSONObject responseObject;
String requestString;
requestObject = JSONHelper.encode(requestObject);
requestString = QueryStringHelper.toQueryString(requestObject);
HTTPResponse httpResponse;
httpResponse = HTTPClient.httpPostWithBasicAuth(this.apiURL + "/charges", requestString, ContentType.APPLICATION_FORM_URLENCODED, null, apiParameters.getString("userName"), "");
if (httpResponse.getStatusCode() == -1) {
return httpResponse;
}
responseObject = httpResponse.getJSONResponse();
if (httpResponse.getStatusCode() >= 200 && httpResponse.getStatusCode() < 300) {
successResponse = new PurchaseResponse();
successResponse.setMessage("Success");
successResponse.setTransactionId(responseObject.getString("id"));
successResponse.setCardValuesFrom(customerCard);
successResponse.setAmount(amount);
successResponse.setCurrencyCode(currency);
successResponse.setRebillParams(new JSONObject().put("customerId", saveCustomerJSONResponse.getString("id")).put("currency", currency));
successResponse.setVoidParams(new JSONObject().put("chargeId", responseObject.getString("id")).put("amount", amount));
successResponse.setRefundParams(new JSONObject().put("chargeId", responseObject.getString("id")));
} else {
errorResponse.setMessage(responseObject.getJSONObject("error").getString("message"));
errorResponse.setTransactionId(null);
}
// final response.
processFinalResponse(responseObject, httpResponse, successResponse, errorResponse);
return httpResponse;
}
use of com.tranxactive.j2pay.net.HTTPResponse in project J2PAY by tranxactive.
the class BillproGateway method purchase.
@Override
public HTTPResponse purchase(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
JSONObject resp;
int result;
String reference = getUniqueCustomerId();
String requestString = this.buildPurchaseParameters(apiParameters, reference, customer, customerCard, currency, amount);
PurchaseResponse successResponse = null;
ErrorResponse errorResponse = new ErrorResponse();
HTTPResponse httpResponse = HTTPClient.httpPost(url, requestString, ContentType.APPLICATION_XML);
if (httpResponse.getStatusCode() == -1) {
return httpResponse;
}
resp = XMLHelper.toJson(httpResponse.getContent());
result = resp.getJSONObject("Response").getInt("ResponseCode");
if (result == 100) {
successResponse = new PurchaseResponse();
successResponse.setMessage(resp.getJSONObject("Response").getString("Description"));
successResponse.setTransactionId(resp.getJSONObject("Response").get("TransactionID").toString());
successResponse.setAmount(amount);
successResponse.setCurrencyCode(currency);
successResponse.setCardValuesFrom(customerCard);
successResponse.setRebillParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), successResponse.getTransactionId()).put(REFERENCE, reference));
successResponse.setRefundParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), successResponse.getTransactionId()));
successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), successResponse.getTransactionId()));
} else {
errorResponse.setMessage(resp.getJSONObject("Response").get("Description").toString());
errorResponse.setTransactionId(resp.getJSONObject("Response").optString("TransactionID"));
}
// final response.
processFinalResponse(resp, httpResponse, successResponse, errorResponse);
return httpResponse;
}
use of com.tranxactive.j2pay.net.HTTPResponse in project J2PAY by tranxactive.
the class CheckoutGateway method rebill.
@Override
public HTTPResponse rebill(JSONObject apiParameters, JSONObject rebillParameters, float amount) {
JSONObject requestObject = this.buildRebillParameters(rebillParameters, amount);
JSONObject responseObject;
requestObject = JSONHelper.encode(requestObject);
HTTPResponse httpResponse;
RebillResponse successResponse = null;
ErrorResponse errorResponse = new ErrorResponse();
HashMap headers = new HashMap<String, String>();
headers.put("Authorization", apiParameters.getString("Authorization"));
httpResponse = HTTPClient.httpPost(this.getApiURL() + "/card", requestObject.toString(), ContentType.APPLICATION_JSON, headers);
if (httpResponse.getStatusCode() == -1) {
return httpResponse;
}
responseObject = httpResponse.getJSONResponse();
if (responseObject.has("responseCode") && (responseObject.get("responseCode").toString().startsWith("10"))) {
successResponse = new RebillResponse();
successResponse.setMessage(responseObject.get("responseMessage").toString());
successResponse.setTransactionId(responseObject.get("id").toString());
successResponse.setAmount(amount);
successResponse.setRebillParams(rebillParameters);
successResponse.setRefundParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), responseObject.get("id").toString()).put("trackId", rebillParameters.get("trackId").toString()));
successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), responseObject.get("id").toString()).put("trackId", rebillParameters.get("trackId").toString()));
} else {
errorResponse.setMessage(responseObject.get("message").toString());
errorResponse.setTransactionId(responseObject.optString("id"));
}
// final response.
processFinalResponse(responseObject, httpResponse, successResponse, errorResponse);
return httpResponse;
}
Aggregations