Search in sources :

Example 1 with RefundResponse

use of com.tranxactive.j2pay.gateways.responses.RefundResponse 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 2 with RefundResponse

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

the class PayeezyGateway method refund.

@Override
public HTTPResponse refund(JSONObject apiParameters, JSONObject refundParameters, float amount) {
    JSONObject resp = null;
    String requestString = this.buildRefundParameters(apiParameters, refundParameters, amount);
    RefundResponse 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 RefundResponse();
            successResponse.setMessage(resp.getJSONObject("TransactionResult").getString("Bank_Message"));
            successResponse.setTransactionId(resp.getJSONObject("TransactionResult").get("Transaction_Tag").toString());
            successResponse.setAmount(amount);
            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) RefundResponse(com.tranxactive.j2pay.gateways.responses.RefundResponse) ErrorResponse(com.tranxactive.j2pay.gateways.responses.ErrorResponse)

Aggregations

ErrorResponse (com.tranxactive.j2pay.gateways.responses.ErrorResponse)2 RefundResponse (com.tranxactive.j2pay.gateways.responses.RefundResponse)2 HTTPResponse (com.tranxactive.j2pay.net.HTTPResponse)2 JSONObject (org.json.JSONObject)2 Transaction (com.braintreegateway.Transaction)1 AuthenticationException (com.braintreegateway.exceptions.AuthenticationException)1 AuthorizationException (com.braintreegateway.exceptions.AuthorizationException)1 ConfigurationException (com.braintreegateway.exceptions.ConfigurationException)1 DownForMaintenanceException (com.braintreegateway.exceptions.DownForMaintenanceException)1 ForgedQueryStringException (com.braintreegateway.exceptions.ForgedQueryStringException)1 InvalidChallengeException (com.braintreegateway.exceptions.InvalidChallengeException)1 InvalidSignatureException (com.braintreegateway.exceptions.InvalidSignatureException)1 NotFoundException (com.braintreegateway.exceptions.NotFoundException)1 ServerException (com.braintreegateway.exceptions.ServerException)1 TimeoutException (com.braintreegateway.exceptions.TimeoutException)1 TooManyRequestsException (com.braintreegateway.exceptions.TooManyRequestsException)1 UnexpectedException (com.braintreegateway.exceptions.UnexpectedException)1 UpgradeRequiredException (com.braintreegateway.exceptions.UpgradeRequiredException)1 BigDecimal (java.math.BigDecimal)1