Search in sources :

Example 16 with HTTPResponse

use of com.tranxactive.j2pay.net.HTTPResponse in project J2PAY by tranxactive.

the class AuthorizeGateway method purchase.

@Override
public HTTPResponse purchase(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
    JSONObject resp;
    String requestString = this.buildPurchaseParameters(apiParameters, customer, customerCard, currency, amount);
    PurchaseResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse;
    int result;
    httpResponse = HTTPClient.httpPost(this.getApiURL(), requestString, ContentType.APPLICATION_XML);
    if (httpResponse.getStatusCode() == -1) {
        return httpResponse;
    }
    resp = XMLHelper.toJson(httpResponse.getContent());
    if (resp.has("createTransactionResponse")) {
        if (resp.getJSONObject("createTransactionResponse").get("transactionResponse") instanceof JSONObject) {
            result = resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").getInt("responseCode");
            if (result == 1) {
                successResponse = new PurchaseResponse();
                successResponse.setMessage(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").getJSONObject("messages").getJSONObject("message").getString("description"));
                successResponse.setTransactionId(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").get("transId").toString());
                successResponse.setCardValuesFrom(customerCard);
                successResponse.setAmount(amount);
                successResponse.setCurrencyCode(currency);
                if (resp.getJSONObject("createTransactionResponse").getJSONObject("profileResponse").has("customerProfileId")) {
                    successResponse.setRebillParams(new JSONObject().put("customerProfileId", resp.getJSONObject("createTransactionResponse").getJSONObject("profileResponse").get("customerProfileId").toString()).put("paymentProfileId", resp.getJSONObject("createTransactionResponse").getJSONObject("profileResponse").getJSONObject("customerPaymentProfileIdList").get("numericString").toString()));
                } else {
                    successResponse.setRebillParams(null);
                }
                successResponse.setRefundParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").get("transId").toString()).put(ParamList.CARD_LAST_4.getName(), customerCard.getLast4()));
                successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").get("transId").toString()));
            } else {
                errorResponse.setMessage(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").getJSONObject("errors").getJSONObject("error").getString("errorText"));
            }
        } else {
            errorResponse.setMessage(resp.getJSONObject("createTransactionResponse").getJSONObject("messages").getJSONObject("message").getString("text"));
        }
    } else {
        errorResponse.setMessage(resp.getJSONObject("ErrorResponse").getJSONObject("messages").getJSONObject("message").getString("text"));
    }
    if (successResponse == null) {
        if (resp.has("createTransactionResponse") && resp.getJSONObject("createTransactionResponse").has("transactionResponse") && resp.getJSONObject("createTransactionResponse").get("transactionResponse") instanceof JSONObject && resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").has("transId")) {
            errorResponse.setTransactionId(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").optString("transId"));
        }
    }
    // final response.
    processFinalResponse(resp, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : JSONObject(org.json.JSONObject) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse)

Example 17 with HTTPResponse

use of com.tranxactive.j2pay.net.HTTPResponse in project J2PAY by tranxactive.

the class AuthorizeGateway method voidTransaction.

@Override
public HTTPResponse voidTransaction(JSONObject apiParameters, JSONObject voidParameters) {
    JSONObject resp;
    String requestString = this.buildVoidParameters(apiParameters, voidParameters);
    VoidResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse;
    int result;
    httpResponse = HTTPClient.httpPost(this.getApiURL(), requestString, ContentType.APPLICATION_XML);
    if (httpResponse.getStatusCode() == -1) {
        return httpResponse;
    }
    resp = XMLHelper.toJson(httpResponse.getContent());
    if (resp.has("createTransactionResponse")) {
        if (resp.getJSONObject("createTransactionResponse").get("transactionResponse") instanceof JSONObject) {
            result = resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").getInt("responseCode");
            if (result == 1) {
                successResponse = new VoidResponse();
                successResponse.setMessage(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").getJSONObject("messages").getJSONObject("message").getString("description"));
                successResponse.setTransactionId(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").get("transId").toString());
            } else {
                errorResponse.setMessage(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").getJSONObject("errors").getJSONObject("error").getString("errorText"));
            }
        } else {
            errorResponse.setMessage(resp.getJSONObject("createTransactionResponse").getJSONObject("messages").getJSONObject("message").getString("text"));
        }
    } else {
        errorResponse.setMessage(resp.getJSONObject("ErrorResponse").getJSONObject("messages").getJSONObject("message").getString("text"));
    }
    if (successResponse == null) {
        if (resp.has("createTransactionResponse") && resp.getJSONObject("createTransactionResponse").has("transactionResponse") && resp.getJSONObject("createTransactionResponse").get("transactionResponse") instanceof JSONObject && resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").has("transId")) {
            errorResponse.setTransactionId(resp.getJSONObject("createTransactionResponse").getJSONObject("transactionResponse").optString("transId"));
        }
    }
    // final response.
    processFinalResponse(resp, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : JSONObject(org.json.JSONObject) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse)

Example 18 with HTTPResponse

use of com.tranxactive.j2pay.net.HTTPResponse in project J2PAY by tranxactive.

the class BraintreeGateway method authorize.

@Override
public HTTPResponse authorize(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
    boolean hasException = false;
    HTTPResponse createCustomer = createCustomer(apiParameters, customer, customerCard, currency, amount);
    if (!createCustomer.isSuccessful()) {
        return createCustomer;
    }
    AuthResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = new HTTPResponse();
    String customerId = createCustomer.getJSONResponse().getJSONObject(ParamList.LIBRARY_RESPONSE.getName()).getString(ParamList.TRANSACTION_ID.getName());
    TransactionRequest transactionRequest = new TransactionRequest();
    transactionRequest.customerId(customerId).amount(new BigDecimal(Float.toString(amount)));
    transactionRequest.options().submitForSettlement(false);
    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 AuthResponse();
        successResponse.setMessage("Success");
        successResponse.setTransactionId(sale.getTarget().getId());
        successResponse.setCardValuesFrom(customerCard);
        successResponse.setAmount(amount);
        successResponse.setCurrencyCode(currency);
        successResponse.setRebillParams(new JSONObject().put(CUSTOMER_ID, customerId));
        successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), sale.getTarget().getId()));
        successResponse.setCaptureParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), sale.getTarget().getId()));
    } else {
        errorResponse.setMessage(sale.getMessage());
    }
    processFinalResponse(null, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : InvalidSignatureException(com.braintreegateway.exceptions.InvalidSignatureException) UnexpectedException(com.braintreegateway.exceptions.UnexpectedException) ServerException(com.braintreegateway.exceptions.ServerException) AuthenticationException(com.braintreegateway.exceptions.AuthenticationException) AuthorizationException(com.braintreegateway.exceptions.AuthorizationException) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse) NotFoundException(com.braintreegateway.exceptions.NotFoundException) TransactionRequest(com.braintreegateway.TransactionRequest) BigDecimal(java.math.BigDecimal) InvalidChallengeException(com.braintreegateway.exceptions.InvalidChallengeException) AuthResponse(com.tranxactive.j2pay.gateways.responses.AuthResponse) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse) UpgradeRequiredException(com.braintreegateway.exceptions.UpgradeRequiredException) TooManyRequestsException(com.braintreegateway.exceptions.TooManyRequestsException) Transaction(com.braintreegateway.Transaction) JSONObject(org.json.JSONObject) ConfigurationException(com.braintreegateway.exceptions.ConfigurationException) ForgedQueryStringException(com.braintreegateway.exceptions.ForgedQueryStringException) DownForMaintenanceException(com.braintreegateway.exceptions.DownForMaintenanceException) TimeoutException(com.braintreegateway.exceptions.TimeoutException)

Example 19 with HTTPResponse

use of com.tranxactive.j2pay.net.HTTPResponse in project J2PAY by tranxactive.

the class BraintreeGateway method purchase.

@Override
public HTTPResponse purchase(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
    boolean hasException = false;
    HTTPResponse createCustomer = createCustomer(apiParameters, customer, customerCard, currency, amount);
    if (!createCustomer.isSuccessful()) {
        return createCustomer;
    }
    PurchaseResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = new HTTPResponse();
    String customerId = createCustomer.getJSONResponse().getJSONObject(ParamList.LIBRARY_RESPONSE.getName()).getString(ParamList.TRANSACTION_ID.getName());
    TransactionRequest transactionRequest = new TransactionRequest();
    transactionRequest.customerId(customerId).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 PurchaseResponse();
        successResponse.setMessage("Success");
        successResponse.setTransactionId(sale.getTarget().getId());
        successResponse.setCardValuesFrom(customerCard);
        successResponse.setAmount(amount);
        successResponse.setCurrencyCode(currency);
        successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), sale.getTarget().getId()));
        successResponse.setRebillParams(new JSONObject().put(CUSTOMER_ID, customerId));
        successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), sale.getTarget().getId()));
    } else {
        errorResponse.setMessage(sale.getMessage());
    }
    processFinalResponse(null, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : InvalidSignatureException(com.braintreegateway.exceptions.InvalidSignatureException) UnexpectedException(com.braintreegateway.exceptions.UnexpectedException) ServerException(com.braintreegateway.exceptions.ServerException) AuthenticationException(com.braintreegateway.exceptions.AuthenticationException) AuthorizationException(com.braintreegateway.exceptions.AuthorizationException) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse) NotFoundException(com.braintreegateway.exceptions.NotFoundException) PurchaseResponse(com.tranxactive.j2pay.gateways.responses.PurchaseResponse) TransactionRequest(com.braintreegateway.TransactionRequest) BigDecimal(java.math.BigDecimal) InvalidChallengeException(com.braintreegateway.exceptions.InvalidChallengeException) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse) UpgradeRequiredException(com.braintreegateway.exceptions.UpgradeRequiredException) TooManyRequestsException(com.braintreegateway.exceptions.TooManyRequestsException) Transaction(com.braintreegateway.Transaction) JSONObject(org.json.JSONObject) ConfigurationException(com.braintreegateway.exceptions.ConfigurationException) ForgedQueryStringException(com.braintreegateway.exceptions.ForgedQueryStringException) DownForMaintenanceException(com.braintreegateway.exceptions.DownForMaintenanceException) TimeoutException(com.braintreegateway.exceptions.TimeoutException)

Example 20 with HTTPResponse

use of com.tranxactive.j2pay.net.HTTPResponse 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;
}
Also used : InvalidSignatureException(com.braintreegateway.exceptions.InvalidSignatureException) UnexpectedException(com.braintreegateway.exceptions.UnexpectedException) ServerException(com.braintreegateway.exceptions.ServerException) Customer(com.tranxactive.j2pay.gateways.parameters.Customer) AuthenticationException(com.braintreegateway.exceptions.AuthenticationException) AuthorizationException(com.braintreegateway.exceptions.AuthorizationException) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse) CreditCardAddressRequest(com.braintreegateway.CreditCardAddressRequest) NotFoundException(com.braintreegateway.exceptions.NotFoundException) InvalidChallengeException(com.braintreegateway.exceptions.InvalidChallengeException) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse) UpgradeRequiredException(com.braintreegateway.exceptions.UpgradeRequiredException) CustomerRequest(com.braintreegateway.CustomerRequest) TooManyRequestsException(com.braintreegateway.exceptions.TooManyRequestsException) ConfigurationException(com.braintreegateway.exceptions.ConfigurationException) ForgedQueryStringException(com.braintreegateway.exceptions.ForgedQueryStringException) CreditCardRequest(com.braintreegateway.CreditCardRequest) DownForMaintenanceException(com.braintreegateway.exceptions.DownForMaintenanceException) TimeoutException(com.braintreegateway.exceptions.TimeoutException)

Aggregations

HTTPResponse (com.tranxactive.j2pay.net.HTTPResponse)52 JSONObject (org.json.JSONObject)50 ErrorResponse (com.tranxactive.j2pay.gateways.responses.ErrorResponse)12 AuthenticationException (com.braintreegateway.exceptions.AuthenticationException)7 AuthorizationException (com.braintreegateway.exceptions.AuthorizationException)7 ConfigurationException (com.braintreegateway.exceptions.ConfigurationException)7 DownForMaintenanceException (com.braintreegateway.exceptions.DownForMaintenanceException)7 ForgedQueryStringException (com.braintreegateway.exceptions.ForgedQueryStringException)7 InvalidChallengeException (com.braintreegateway.exceptions.InvalidChallengeException)7 InvalidSignatureException (com.braintreegateway.exceptions.InvalidSignatureException)7 NotFoundException (com.braintreegateway.exceptions.NotFoundException)7 ServerException (com.braintreegateway.exceptions.ServerException)7 TimeoutException (com.braintreegateway.exceptions.TimeoutException)7 TooManyRequestsException (com.braintreegateway.exceptions.TooManyRequestsException)7 UnexpectedException (com.braintreegateway.exceptions.UnexpectedException)7 UpgradeRequiredException (com.braintreegateway.exceptions.UpgradeRequiredException)7 Transaction (com.braintreegateway.Transaction)6 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)4 TransactionRequest (com.braintreegateway.TransactionRequest)3