Search in sources :

Example 36 with Transaction

use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.

the class TransactionServiceImpl method listTransactions.

@Override
public List<Transaction> listTransactions(Order order) throws ServiceException {
    List<Transaction> transactions = transactionRepository.findByOrder(order.getId());
    ObjectMapper mapper = new ObjectMapper();
    for (Transaction transaction : transactions) {
        if (!StringUtils.isBlank(transaction.getDetails())) {
            try {
                @SuppressWarnings("unchecked") Map<String, String> objects = mapper.readValue(transaction.getDetails(), Map.class);
                transaction.setTransactionDetails(objects);
            } catch (Exception e) {
                throw new ServiceException(e);
            }
        }
    }
    return transactions;
}
Also used : Transaction(com.salesmanager.core.model.payments.Transaction) ServiceException(com.salesmanager.core.business.exception.ServiceException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 37 with Transaction

use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.

the class TransactionServiceImpl method getRefundableTransaction.

@Override
public Transaction getRefundableTransaction(Order order) throws ServiceException {
    List<Transaction> transactions = transactionRepository.findByOrder(order.getId());
    Map<String, Transaction> finalTransactions = new HashMap<String, Transaction>();
    Transaction finalTransaction = null;
    for (Transaction transaction : transactions) {
        if (transaction.getTransactionType().name().equals(TransactionType.AUTHORIZECAPTURE.name())) {
            finalTransactions.put(TransactionType.AUTHORIZECAPTURE.name(), transaction);
            continue;
        }
        if (transaction.getTransactionType().name().equals(TransactionType.CAPTURE.name())) {
            finalTransactions.put(TransactionType.CAPTURE.name(), transaction);
            continue;
        }
        if (transaction.getTransactionType().name().equals(TransactionType.REFUND.name())) {
            // check transaction id
            Transaction previousRefund = finalTransactions.get(TransactionType.REFUND.name());
            if (previousRefund != null) {
                Date previousDate = previousRefund.getTransactionDate();
                Date currentDate = transaction.getTransactionDate();
                if (previousDate.before(currentDate)) {
                    finalTransactions.put(TransactionType.REFUND.name(), transaction);
                    continue;
                }
            } else {
                finalTransactions.put(TransactionType.REFUND.name(), transaction);
                continue;
            }
        }
    }
    if (finalTransactions.containsKey(TransactionType.AUTHORIZECAPTURE.name())) {
        finalTransaction = finalTransactions.get(TransactionType.AUTHORIZECAPTURE.name());
    }
    if (finalTransactions.containsKey(TransactionType.CAPTURE.name())) {
        finalTransaction = finalTransactions.get(TransactionType.CAPTURE.name());
    }
    if (finalTransaction != null && !StringUtils.isBlank(finalTransaction.getDetails())) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            @SuppressWarnings("unchecked") Map<String, String> objects = mapper.readValue(finalTransaction.getDetails(), Map.class);
            finalTransaction.setTransactionDetails(objects);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
    return finalTransaction;
}
Also used : Transaction(com.salesmanager.core.model.payments.Transaction) ServiceException(com.salesmanager.core.business.exception.ServiceException) HashMap(java.util.HashMap) Date(java.util.Date) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 38 with Transaction

use of com.salesmanager.core.model.payments.Transaction in project shopizer by shopizer-ecommerce.

the class OrderPaymentApi method init.

@RequestMapping(value = { "/auth/cart/{code}/payment/init" }, method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableTransaction init(@Valid @RequestBody PersistablePayment payment, @PathVariable String code, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        Principal principal = request.getUserPrincipal();
        String userName = principal.getName();
        Customer customer = customerService.getByNick(userName);
        if (customer == null) {
            response.sendError(401, "Error while initializing the payment customer not authorized");
            return null;
        }
        ShoppingCart cart = shoppingCartService.getByCode(code, merchantStore);
        if (cart == null) {
            throw new ResourceNotFoundException("Cart code " + code + " does not exist");
        }
        if (cart.getCustomerId() == null) {
            response.sendError(404, "Cart code " + code + " does not exist for exist for user " + userName);
            return null;
        }
        if (cart.getCustomerId().longValue() != customer.getId().longValue()) {
            response.sendError(404, "Cart code " + code + " does not exist for exist for user " + userName);
            return null;
        }
        PersistablePaymentPopulator populator = new PersistablePaymentPopulator();
        populator.setPricingService(pricingService);
        Payment paymentModel = new Payment();
        populator.populate(payment, paymentModel, merchantStore, language);
        Transaction transactionModel = paymentService.initTransaction(customer, paymentModel, merchantStore);
        ReadableTransaction transaction = new ReadableTransaction();
        ReadableTransactionPopulator trxPopulator = new ReadableTransactionPopulator();
        trxPopulator.setOrderService(orderService);
        trxPopulator.setPricingService(pricingService);
        trxPopulator.populate(transactionModel, transaction, merchantStore, language);
        return transaction;
    } catch (Exception e) {
        LOGGER.error("Error while initializing the payment", e);
        try {
            response.sendError(503, "Error while initializing the payment " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : PersistablePayment(com.salesmanager.shop.model.order.transaction.PersistablePayment) Payment(com.salesmanager.core.model.payments.Payment) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableTransaction(com.salesmanager.shop.model.order.transaction.ReadableTransaction) Transaction(com.salesmanager.core.model.payments.Transaction) ReadableTransactionPopulator(com.salesmanager.shop.populator.order.transaction.ReadableTransactionPopulator) Customer(com.salesmanager.core.model.customer.Customer) ReadableTransaction(com.salesmanager.shop.model.order.transaction.ReadableTransaction) PersistablePaymentPopulator(com.salesmanager.shop.populator.order.transaction.PersistablePaymentPopulator) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Principal(java.security.Principal) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Transaction (com.salesmanager.core.model.payments.Transaction)38 Date (java.util.Date)25 IntegrationException (com.salesmanager.core.modules.integration.IntegrationException)17 ServiceException (com.salesmanager.core.business.exception.ServiceException)15 HashMap (java.util.HashMap)10 AuthenticationException (com.stripe.exception.AuthenticationException)9 CardException (com.stripe.exception.CardException)9 InvalidRequestException (com.stripe.exception.InvalidRequestException)9 StripeException (com.stripe.exception.StripeException)9 BraintreeGateway (com.braintreegateway.BraintreeGateway)5 Environment (com.braintreegateway.Environment)5 Order (com.salesmanager.core.model.order.Order)5 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)5 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)5 PaymentModule (com.salesmanager.core.modules.integration.payment.model.PaymentModule)5 ReadableTransaction (com.salesmanager.shop.model.order.transaction.ReadableTransaction)5 PaymentIntent (com.stripe.model.PaymentIntent)5 BigDecimal (java.math.BigDecimal)5 ArrayList (java.util.ArrayList)5 ValidationError (com.braintreegateway.ValidationError)4