Search in sources :

Example 21 with OrderPayment

use of org.broadleafcommerce.core.payment.domain.OrderPayment in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method addPaymentToOrder.

@Test(groups = { "addPaymentToOrder" }, dataProvider = "basicPaymentInfo", dataProviderClass = PaymentInfoDataProvider.class, dependsOnGroups = { "checkOrderItems" })
@Rollback(false)
@Transactional
public void addPaymentToOrder(OrderPayment paymentInfo) {
    Order order = orderService.findOrderById(orderId);
    orderService.addPaymentToOrder(order, paymentInfo, null);
    order = orderService.findOrderById(orderId);
    OrderPayment payment = order.getPayments().get(order.getPayments().indexOf(paymentInfo));
    assert payment != null;
    assert payment.getOrder() != null;
    assert payment.getOrder().equals(order);
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with OrderPayment

use of org.broadleafcommerce.core.payment.domain.OrderPayment in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method testOrderPaymentInfos.

@Test(groups = { "testOrderPaymentInfos" }, dataProvider = "basicPaymentInfo", dataProviderClass = PaymentInfoDataProvider.class)
@Transactional
public void testOrderPaymentInfos(OrderPayment info) throws PricingException {
    Customer customer = customerService.saveCustomer(createNamedCustomer());
    Order order = orderService.createNewCartForCustomer(customer);
    info = orderService.addPaymentToOrder(order, info, null);
    boolean foundInfo = false;
    assert order.getPayments() != null;
    for (OrderPayment testInfo : order.getPayments()) {
        if (testInfo.equals(info)) {
            foundInfo = true;
        }
    }
    assert foundInfo == true;
    assert orderService.findPaymentsForOrder(order) != null;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Customer(org.broadleafcommerce.profile.core.domain.Customer) OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with OrderPayment

use of org.broadleafcommerce.core.payment.domain.OrderPayment in project BroadleafCommerce by BroadleafCommerce.

the class PaymentInfoDataProvider method provideBasicSalesPaymentInfo.

@DataProvider(name = "basicPaymentInfo")
public static Object[][] provideBasicSalesPaymentInfo() {
    OrderPayment sop = new OrderPaymentImpl();
    sop.setAmount(new Money(BigDecimal.valueOf(10.99)));
    sop.setReferenceNumber("987654321");
    sop.setType(PaymentType.CREDIT_CARD);
    return new Object[][] { { sop } };
}
Also used : Money(org.broadleafcommerce.common.money.Money) OrderPaymentImpl(org.broadleafcommerce.core.payment.domain.OrderPaymentImpl) OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment) DataProvider(org.testng.annotations.DataProvider)

Example 24 with OrderPayment

use of org.broadleafcommerce.core.payment.domain.OrderPayment in project BroadleafCommerce by BroadleafCommerce.

the class ConfirmPaymentsRollbackHandler method rollbackState.

@Override
public void rollbackState(Activity<ProcessContext<CheckoutSeed>> activity, ProcessContext<CheckoutSeed> processContext, Map<String, Object> stateConfiguration) throws RollbackFailureException {
    CheckoutSeed seed = processContext.getSeedData();
    if (paymentConfigurationServiceProvider == null) {
        throw new RollbackFailureException("There is no rollback service configured for the payment gateway configuration, cannot rollback unconfirmed" + " payments");
    }
    Map<OrderPayment, PaymentTransaction> rollbackResponseTransactions = new HashMap<>();
    Collection<PaymentTransaction> transactions = (Collection<PaymentTransaction>) stateConfiguration.get(ValidateAndConfirmPaymentActivity.ROLLBACK_TRANSACTIONS);
    if (CollectionUtils.isNotEmpty(transactions)) {
        for (PaymentTransaction tx : transactions) {
            PaymentRequestDTO rollbackRequest = transactionToPaymentRequestDTOService.translatePaymentTransaction(tx.getAmount(), tx);
            PaymentGatewayConfigurationService cfg = paymentConfigurationServiceProvider.getGatewayConfigurationService(tx.getOrderPayment().getGatewayType());
            try {
                PaymentResponseDTO responseDTO = null;
                if (PaymentTransactionType.AUTHORIZE.equals(tx.getType())) {
                    if (cfg.getRollbackService() != null) {
                        responseDTO = cfg.getRollbackService().rollbackAuthorize(rollbackRequest);
                    }
                } else if (PaymentTransactionType.AUTHORIZE_AND_CAPTURE.equals(tx.getType())) {
                    if (cfg.getRollbackService() != null) {
                        responseDTO = cfg.getRollbackService().rollbackAuthorizeAndCapture(rollbackRequest);
                    }
                } else {
                    LOG.warn("The transaction with id " + tx.getId() + " will NOT be rolled back as it is not an AUTHORIZE or AUTHORIZE_AND_CAPTURE transaction but is" + " of type " + tx.getType() + ". If you need to roll back transactions of this type then provide a customized rollback handler for" + " confirming transactions.");
                }
                if (responseDTO != null) {
                    PaymentTransaction transaction = orderPaymentService.createTransaction();
                    transaction.setAmount(responseDTO.getAmount());
                    transaction.setRawResponse(responseDTO.getRawResponse());
                    transaction.setSuccess(responseDTO.isSuccessful());
                    transaction.setType(responseDTO.getPaymentTransactionType());
                    transaction.setParentTransaction(tx);
                    transaction.setOrderPayment(tx.getOrderPayment());
                    transaction.setAdditionalFields(responseDTO.getResponseMap());
                    rollbackResponseTransactions.put(tx.getOrderPayment(), transaction);
                    if (!responseDTO.isSuccessful()) {
                        LOG.fatal("Unable to rollback transaction with id " + tx.getId() + ". The call was unsuccessful with" + " raw response: " + responseDTO.getRawResponse());
                    }
                }
            } catch (PaymentException e) {
                throw new RollbackFailureException("The transaction with id " + tx.getId() + " encountered and exception when it was attempted to roll back" + " its confirmation", e);
            }
        }
        Order order = seed.getOrder();
        List<OrderPayment> paymentsToInvalidate = new ArrayList<>();
        // Add the new rollback transactions to the appropriate payment and mark the payment as invalid.
        // If there was a failed transaction rolling back we will need to throw a RollbackFailureException after saving the
        // Transaction Response to the DB
        boolean rollbackFailure = false;
        for (OrderPayment payment : order.getPayments()) {
            if (rollbackResponseTransactions.containsKey(payment)) {
                PaymentTransaction rollbackTX = rollbackResponseTransactions.get(payment);
                payment.addTransaction(rollbackTX);
                payment = orderPaymentService.save(payment);
                paymentsToInvalidate.add(payment);
                if (!rollbackTX.getSuccess()) {
                    rollbackFailure = true;
                }
            }
        }
        for (OrderPayment payment : paymentsToInvalidate) {
            paymentGatewayCheckoutService.markPaymentAsInvalid(payment.getId());
            orderPaymentService.save(payment);
        }
        if (rollbackFailure) {
            throw new RollbackFailureException("The ConfirmPaymentsRollbackHandler encountered and exception when it " + "attempted to roll back a transaction on one of the payments. Please see LOG for details.");
        }
        try {
            processContext.getSeedData().setOrder(orderService.save(order, false));
        } catch (PricingException e) {
            throw new RollbackFailureException("Unable to save the order with invalidated payments.");
        }
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) RollbackFailureException(org.broadleafcommerce.core.workflow.state.RollbackFailureException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment) PaymentTransaction(org.broadleafcommerce.core.payment.domain.PaymentTransaction) PaymentException(org.broadleafcommerce.common.vendor.service.exception.PaymentException) PricingException(org.broadleafcommerce.core.pricing.service.exception.PricingException) Collection(java.util.Collection) PaymentRequestDTO(org.broadleafcommerce.common.payment.dto.PaymentRequestDTO) PaymentGatewayConfigurationService(org.broadleafcommerce.common.payment.service.PaymentGatewayConfigurationService) PaymentResponseDTO(org.broadleafcommerce.common.payment.dto.PaymentResponseDTO)

Example 25 with OrderPayment

use of org.broadleafcommerce.core.payment.domain.OrderPayment in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafPaymentInfoController method addTemporaryOrderPayment.

protected void addTemporaryOrderPayment(PaymentInfoForm paymentForm, Order cart) {
    // A Temporary Order Payment will be created to hold the billing address.
    // The Payment Gateway will send back any validated address and
    // the PaymentGatewayCheckoutService will persist a new payment of type CREDIT_CARD when it applies it to the Order
    OrderPayment tempOrderPayment = orderPaymentService.create();
    tempOrderPayment.setType(PaymentType.CREDIT_CARD);
    tempOrderPayment.setPaymentGatewayType(PaymentGatewayType.TEMPORARY);
    tempOrderPayment.setBillingAddress(paymentForm.getAddress());
    tempOrderPayment.setOrder(cart);
    cart.getPayments().add(tempOrderPayment);
}
Also used : OrderPayment(org.broadleafcommerce.core.payment.domain.OrderPayment)

Aggregations

OrderPayment (org.broadleafcommerce.core.payment.domain.OrderPayment)33 Order (org.broadleafcommerce.core.order.domain.Order)16 PaymentTransaction (org.broadleafcommerce.core.payment.domain.PaymentTransaction)11 Money (org.broadleafcommerce.common.money.Money)7 ArrayList (java.util.ArrayList)6 Transactional (org.springframework.transaction.annotation.Transactional)6 PaymentResponseDTO (org.broadleafcommerce.common.payment.dto.PaymentResponseDTO)5 Address (org.broadleafcommerce.profile.core.domain.Address)5 HashMap (java.util.HashMap)4 CheckoutException (org.broadleafcommerce.core.checkout.service.exception.CheckoutException)3 Customer (org.broadleafcommerce.profile.core.domain.Customer)3 Test (org.testng.annotations.Test)3 PaymentGatewayType (org.broadleafcommerce.common.payment.PaymentGatewayType)2 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)2 PaymentGatewayConfigurationService (org.broadleafcommerce.common.payment.service.PaymentGatewayConfigurationService)2 AdminPresentationMergeOverride (org.broadleafcommerce.common.presentation.override.AdminPresentationMergeOverride)2 OrderPaymentImpl (org.broadleafcommerce.core.payment.domain.OrderPaymentImpl)2 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)2 CustomerAddress (org.broadleafcommerce.profile.core.domain.CustomerAddress)2 Collection (java.util.Collection)1