Search in sources :

Example 1 with RefundTransactionRequestType

use of urn.ebay.api.PayPalAPI.RefundTransactionRequestType in project shopizer by shopizer-ecommerce.

the class PayPalExpressCheckoutPayment method refund.

@Override
public Transaction refund(boolean partial, MerchantStore store, Transaction transaction, Order order, BigDecimal amount, IntegrationConfiguration configuration, IntegrationModule module) throws IntegrationException {
    try {
        Validate.notNull(transaction, "Transaction cannot be null");
        Validate.notNull(transaction.getTransactionDetails().get("TRANSACTIONID"), "Transaction details must contain a TRANSACTIONID");
        Validate.notNull(order, "Order must not be null");
        Validate.notNull(order.getCurrency(), "Order nust contain Currency object");
        String mode = "sandbox";
        String env = configuration.getEnvironment();
        if (Constants.PRODUCTION_ENVIRONMENT.equals(env)) {
            mode = "production";
        }
        RefundTransactionRequestType refundTransactionRequest = new RefundTransactionRequestType();
        refundTransactionRequest.setVersion("104.0");
        RefundTransactionReq refundRequest = new RefundTransactionReq();
        refundRequest.setRefundTransactionRequest(refundTransactionRequest);
        Map<String, String> configurationMap = new HashMap<String, String>();
        configurationMap.put("mode", mode);
        configurationMap.put("acct1.UserName", configuration.getIntegrationKeys().get("username"));
        configurationMap.put("acct1.Password", configuration.getIntegrationKeys().get("api"));
        configurationMap.put("acct1.Signature", configuration.getIntegrationKeys().get("signature"));
        PayPalAPIInterfaceServiceService service = new PayPalAPIInterfaceServiceService(configurationMap);
        RefundType refundType = RefundType.FULL;
        if (partial) {
            refundType = RefundType.PARTIAL;
        }
        refundTransactionRequest.setRefundType(refundType);
        BasicAmountType refundAmount = new BasicAmountType();
        refundAmount.setValue(pricingService.getStringAmount(amount, store));
        refundAmount.setCurrencyID(urn.ebay.apis.eBLBaseComponents.CurrencyCodeType.fromValue(order.getCurrency().getCode()));
        refundTransactionRequest.setAmount(refundAmount);
        refundTransactionRequest.setTransactionID(transaction.getTransactionDetails().get("TRANSACTIONID"));
        RefundTransactionResponseType refundTransactionResponse = service.refundTransaction(refundRequest);
        String refundAck = refundTransactionResponse.getAck().getValue();
        if (!"Success".equals(refundAck)) {
            LOGGER.error("Wrong value from transaction commit " + refundAck);
            throw new IntegrationException(ServiceException.EXCEPTION_TRANSACTION_DECLINED, "Paypal refund transaction code [" + refundTransactionResponse.getErrors().get(0).getErrorCode() + "], message-> " + refundTransactionResponse.getErrors().get(0).getShortMessage());
        }
        Transaction newTransaction = new Transaction();
        newTransaction.setAmount(amount);
        newTransaction.setTransactionDate(new Date());
        newTransaction.setTransactionType(TransactionType.REFUND);
        newTransaction.setPaymentType(PaymentType.PAYPAL);
        newTransaction.getTransactionDetails().put("TRANSACTIONID", refundTransactionResponse.getRefundTransactionID());
        transaction.getTransactionDetails().put("CORRELATION", refundTransactionResponse.getCorrelationID());
        return newTransaction;
    } catch (Exception e) {
        if (e instanceof IntegrationException) {
            throw (IntegrationException) e;
        } else {
            throw new IntegrationException(e);
        }
    }
}
Also used : RefundTransactionResponseType(urn.ebay.api.PayPalAPI.RefundTransactionResponseType) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) HashMap(java.util.HashMap) RefundType(urn.ebay.apis.eBLBaseComponents.RefundType) BasicAmountType(urn.ebay.apis.CoreComponentTypes.BasicAmountType) PayPalAPIInterfaceServiceService(urn.ebay.api.PayPalAPI.PayPalAPIInterfaceServiceService) Date(java.util.Date) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) RefundTransactionReq(urn.ebay.api.PayPalAPI.RefundTransactionReq) Transaction(com.salesmanager.core.model.payments.Transaction) RefundTransactionRequestType(urn.ebay.api.PayPalAPI.RefundTransactionRequestType)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)1 Transaction (com.salesmanager.core.model.payments.Transaction)1 IntegrationException (com.salesmanager.core.modules.integration.IntegrationException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 PayPalAPIInterfaceServiceService (urn.ebay.api.PayPalAPI.PayPalAPIInterfaceServiceService)1 RefundTransactionReq (urn.ebay.api.PayPalAPI.RefundTransactionReq)1 RefundTransactionRequestType (urn.ebay.api.PayPalAPI.RefundTransactionRequestType)1 RefundTransactionResponseType (urn.ebay.api.PayPalAPI.RefundTransactionResponseType)1 BasicAmountType (urn.ebay.apis.CoreComponentTypes.BasicAmountType)1 RefundType (urn.ebay.apis.eBLBaseComponents.RefundType)1