Search in sources :

Example 1 with ErrorResponse

use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.

the class HTTPClient method httpPost.

/**
 * This method is the wrapper of apache http client post request.
 *
 * @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 headers To add additional headers otherwise pass null
 * @return The HTTPResponse class.
 * @see com.tranxactive.j2pay.net.HTTPResponse
 * @see ContentType
 */
public static HTTPResponse httpPost(String url, String postParams, ContentType contentType, Charset charset, HashMap<String, String> headers) {
    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());
        if (headers != null) {
            headers.keySet().forEach((key) -> {
                httpPost.addHeader(key, headers.get(key));
            });
        }
        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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse)

Example 2 with ErrorResponse

use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.

the class BraintreeGateway method voidTransaction.

@Override
public HTTPResponse voidTransaction(JSONObject apiParameters, JSONObject voidParameters) {
    boolean hasException = false;
    VoidResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = new HTTPResponse();
    Result<Transaction> voidTransaction = null;
    try {
        voidTransaction = getGatewayObject(apiParameters).transaction().voidTransaction(voidParameters.getString(ParamList.TRANSACTION_ID.getName()));
    } 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 (voidTransaction.isSuccess()) {
        successResponse = new VoidResponse();
        successResponse.setMessage("Success");
        successResponse.setTransactionId(voidTransaction.getTarget().getId());
    } else {
        errorResponse.setMessage(voidTransaction.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) 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) ConfigurationException(com.braintreegateway.exceptions.ConfigurationException) VoidResponse(com.tranxactive.j2pay.gateways.responses.VoidResponse) ForgedQueryStringException(com.braintreegateway.exceptions.ForgedQueryStringException) DownForMaintenanceException(com.braintreegateway.exceptions.DownForMaintenanceException) TimeoutException(com.braintreegateway.exceptions.TimeoutException)

Example 3 with ErrorResponse

use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.

the class BraintreeGateway method refund.

@Override
public HTTPResponse refund(JSONObject apiParameters, JSONObject refundParameters, float amount) {
    boolean hasException = false;
    RefundResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = new HTTPResponse();
    Result<Transaction> refund = null;
    try {
        refund = getGatewayObject(apiParameters).transaction().refund(refundParameters.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 (refund.isSuccess()) {
        successResponse = new RefundResponse();
        successResponse.setMessage("Success");
        successResponse.setTransactionId(refund.getTarget().getId());
        successResponse.setAmount(amount);
        successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), refund.getTarget().getId()));
    } else {
        errorResponse.setMessage(refund.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) 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) RefundResponse(com.tranxactive.j2pay.gateways.responses.RefundResponse) TimeoutException(com.braintreegateway.exceptions.TimeoutException)

Example 4 with ErrorResponse

use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.

the class PayeezyGateway method voidTransaction.

@Override
public HTTPResponse voidTransaction(JSONObject apiParameters, JSONObject voidParameters) {
    JSONObject resp = null;
    String requestString = this.buildVoidParameters(apiParameters, voidParameters);
    VoidResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = HTTPClient.httpPost(this.getApiURL(), requestString, ContentType.APPLICATION_XML);
    if (httpResponse.getStatusCode() == -1) {
        return httpResponse;
    }
    if (httpResponse.getContent().trim().startsWith("<")) {
        resp = XMLHelper.toJson(httpResponse.getContent());
        boolean transactionError = resp.getJSONObject("TransactionResult").getBoolean("Transaction_Error");
        boolean transactionApproved = resp.getJSONObject("TransactionResult").getBoolean("Transaction_Approved");
        if (transactionApproved) {
            successResponse = new VoidResponse();
            successResponse.setMessage(resp.getJSONObject("TransactionResult").getString("Bank_Message"));
            successResponse.setTransactionId(resp.getJSONObject("TransactionResult").get("Transaction_Tag").toString());
        } else {
            errorResponse.setMessage(resp.getJSONObject("TransactionResult").get(transactionError ? "EXact_Message" : "Bank_Message").toString());
            errorResponse.setTransactionId(resp.getJSONObject("TransactionResult").optString("Transaction_Tag"));
        }
    } else {
        errorResponse.setMessage(httpResponse.getContent());
    }
    // final response.
    processFinalResponse(resp, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : JSONObject(org.json.JSONObject) VoidResponse(com.tranxactive.j2pay.gateways.responses.VoidResponse) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse)

Example 5 with ErrorResponse

use of com.tranxactive.j2pay.gateways.responses.ErrorResponse in project J2PAY by tranxactive.

the class PayeezyGateway method purchase.

@Override
public HTTPResponse purchase(JSONObject apiParameters, Customer customer, CustomerCard customerCard, Currency currency, float amount) {
    JSONObject resp = null;
    String requestString = this.buildPurchaseParameters(apiParameters, customer, customerCard, currency, amount);
    PurchaseResponse successResponse = null;
    ErrorResponse errorResponse = new ErrorResponse();
    HTTPResponse httpResponse = HTTPClient.httpPost(this.getApiURL(), requestString, ContentType.APPLICATION_XML);
    if (httpResponse.getStatusCode() == -1) {
        return httpResponse;
    }
    if (httpResponse.getContent().trim().startsWith("<")) {
        resp = XMLHelper.toJson(httpResponse.getContent());
        boolean transactionError = resp.getJSONObject("TransactionResult").getBoolean("Transaction_Error");
        boolean transactionApproved = resp.getJSONObject("TransactionResult").getBoolean("Transaction_Approved");
        if (transactionApproved) {
            successResponse = new PurchaseResponse();
            successResponse.setMessage(resp.getJSONObject("TransactionResult").getString("Bank_Message"));
            successResponse.setTransactionId(resp.getJSONObject("TransactionResult").get("Transaction_Tag").toString());
            successResponse.setAmount(amount);
            successResponse.setCurrencyCode(currency);
            successResponse.setCardValuesFrom(customerCard);
            successResponse.setRefundParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), successResponse.getTransactionId()).put("authorizationNumber", resp.getJSONObject("TransactionResult").get("Authorization_Num").toString()));
            successResponse.setVoidParams(new JSONObject().put(ParamList.TRANSACTION_ID.getName(), successResponse.getTransactionId()).put("authorizationNumber", resp.getJSONObject("TransactionResult").get("Authorization_Num").toString()).put(ParamList.AMOUNT.getName(), amount));
        } else {
            errorResponse.setMessage(resp.getJSONObject("TransactionResult").get(transactionError ? "EXact_Message" : "Bank_Message").toString());
            errorResponse.setTransactionId(resp.getJSONObject("TransactionResult").optString("Transaction_Tag"));
        }
    } else {
        errorResponse.setMessage(httpResponse.getContent());
    }
    // final response.
    processFinalResponse(resp, httpResponse, successResponse, errorResponse);
    return httpResponse;
}
Also used : JSONObject(org.json.JSONObject) HTTPResponse(com.tranxactive.j2pay.net.HTTPResponse) PurchaseResponse(com.tranxactive.j2pay.gateways.responses.PurchaseResponse) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse)

Aggregations

ErrorResponse (com.tranxactive.j2pay.gateways.responses.ErrorResponse)14 HTTPResponse (com.tranxactive.j2pay.net.HTTPResponse)12 JSONObject (org.json.JSONObject)10 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 TransactionRequest (com.braintreegateway.TransactionRequest)3 AuthResponse (com.tranxactive.j2pay.gateways.responses.AuthResponse)2