Search in sources :

Example 1 with PaymentModule

use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.

the class ShoppingOrderPaymentController method paymentAction.

/**
 * Recalculates shipping and tax following a change in country or province
 *
 * @param order
 * @param request
 * @param response
 * @param locale
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/order/payment/{action}/{paymentmethod}.html" }, method = RequestMethod.POST)
@ResponseBody
public String paymentAction(@Valid @ModelAttribute(value = "order") ShopOrder order, @PathVariable String action, @PathVariable String paymentmethod, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    Language language = (Language) request.getAttribute("LANGUAGE");
    MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    String shoppingCartCode = getSessionAttribute(Constants.SHOPPING_CART, request);
    Validate.notNull(shoppingCartCode, "shoppingCartCode does not exist in the session");
    AjaxResponse ajaxResponse = new AjaxResponse();
    try {
        com.salesmanager.core.model.shoppingcart.ShoppingCart cart = shoppingCartFacade.getShoppingCartModel(shoppingCartCode, store);
        Set<ShoppingCartItem> items = cart.getLineItems();
        List<ShoppingCartItem> cartItems = new ArrayList<ShoppingCartItem>(items);
        order.setShoppingCartItems(cartItems);
        // validate order first
        Map<String, String> messages = new TreeMap<String, String>();
        orderFacade.validateOrder(order, new BeanPropertyBindingResult(order, "order"), messages, store, locale);
        if (CollectionUtils.isNotEmpty(messages.values())) {
            for (String key : messages.keySet()) {
                String value = messages.get(key);
                ajaxResponse.addValidationMessage(key, value);
            }
            ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_VALIDATION_FAILED);
            return ajaxResponse.toJSONString();
        }
        IntegrationConfiguration config = paymentService.getPaymentConfiguration(order.getPaymentModule(), store);
        IntegrationModule integrationModule = paymentService.getPaymentMethodByCode(store, order.getPaymentModule());
        // OrderTotalSummary orderTotalSummary =
        // orderFacade.calculateOrderTotal(store, order, language);
        OrderTotalSummary orderTotalSummary = super.getSessionAttribute(Constants.ORDER_SUMMARY, request);
        if (orderTotalSummary == null) {
            orderTotalSummary = orderFacade.calculateOrderTotal(store, order, language);
            super.setSessionAttribute(Constants.ORDER_SUMMARY, orderTotalSummary, request);
        }
        ShippingSummary summary = (ShippingSummary) request.getSession().getAttribute("SHIPPING_SUMMARY");
        if (summary != null) {
            order.setShippingSummary(summary);
        }
        if (action.equals(INIT_ACTION)) {
            if (paymentmethod.equals("PAYPAL")) {
                try {
                    PaymentModule module = paymentService.getPaymentModule("paypal-express-checkout");
                    PayPalExpressCheckoutPayment p = (PayPalExpressCheckoutPayment) module;
                    PaypalPayment payment = new PaypalPayment();
                    payment.setCurrency(store.getCurrency());
                    Transaction transaction = p.initPaypalTransaction(store, cartItems, orderTotalSummary, payment, config, integrationModule);
                    transactionService.create(transaction);
                    super.setSessionAttribute(Constants.INIT_TRANSACTION_KEY, transaction, request);
                    StringBuilder urlAppender = new StringBuilder();
                    urlAppender.append(coreConfiguration.getProperty("PAYPAL_EXPRESSCHECKOUT_REGULAR"));
                    urlAppender.append(transaction.getTransactionDetails().get("TOKEN"));
                    if (config.getEnvironment().equals(com.salesmanager.core.business.constants.Constants.PRODUCTION_ENVIRONMENT)) {
                        StringBuilder url = new StringBuilder().append(coreConfiguration.getProperty("PAYPAL_EXPRESSCHECKOUT_PRODUCTION")).append(urlAppender.toString());
                        ajaxResponse.addEntry("url", url.toString());
                    } else {
                        StringBuilder url = new StringBuilder().append(coreConfiguration.getProperty("PAYPAL_EXPRESSCHECKOUT_SANDBOX")).append(urlAppender.toString());
                        ajaxResponse.addEntry("url", url.toString());
                    }
                    // keep order in session when user comes back from pp
                    super.setSessionAttribute(Constants.ORDER, order, request);
                    ajaxResponse.setStatus(AjaxResponse.RESPONSE_OPERATION_COMPLETED);
                } catch (Exception e) {
                    ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
                }
            } else if (paymentmethod.equals("stripe3")) {
                try {
                    PaymentModule module = paymentService.getPaymentModule(paymentmethod);
                    Stripe3Payment p = (Stripe3Payment) module;
                    PaypalPayment payment = new PaypalPayment();
                    payment.setCurrency(store.getCurrency());
                    Transaction transaction = p.initTransaction(store, null, orderTotalSummary.getTotal(), null, config, integrationModule);
                    transactionService.create(transaction);
                    super.setSessionAttribute(Constants.INIT_TRANSACTION_KEY, transaction, request);
                    // keep order in session when user comes back from pp
                    super.setSessionAttribute(Constants.ORDER, order, request);
                    ajaxResponse.setStatus(AjaxResponse.RESPONSE_OPERATION_COMPLETED);
                    ajaxResponse.setDataMap(transaction.getTransactionDetails());
                } catch (Exception e) {
                    ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error while performing payment action " + action + " for payment method " + paymentmethod, e);
        ajaxResponse.setErrorMessage(e);
        ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
    }
    return ajaxResponse.toJSONString();
}
Also used : Stripe3Payment(com.salesmanager.core.business.modules.integration.payment.impl.Stripe3Payment) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) ArrayList(java.util.ArrayList) PayPalExpressCheckoutPayment(com.salesmanager.core.business.modules.integration.payment.impl.PayPalExpressCheckoutPayment) PaypalPayment(com.salesmanager.core.model.payments.PaypalPayment) PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) Language(com.salesmanager.core.model.reference.language.Language) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) AjaxResponse(com.salesmanager.core.business.utils.ajax.AjaxResponse) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) TreeMap(java.util.TreeMap) Transaction(com.salesmanager.core.model.payments.Transaction) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with PaymentModule

use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.

the class PaymentServiceImpl method savePaymentModuleConfiguration.

@Override
public void savePaymentModuleConfiguration(IntegrationConfiguration configuration, MerchantStore store) throws ServiceException {
    // validate entries
    try {
        String moduleCode = configuration.getModuleCode();
        PaymentModule module = paymentModules.get(moduleCode);
        if (module == null) {
            throw new ServiceException("Payment module " + moduleCode + " does not exist");
        }
        module.validateModuleConfiguration(configuration, store);
    } catch (IntegrationException ie) {
        throw ie;
    }
    try {
        Map<String, IntegrationConfiguration> modules = new HashMap<String, IntegrationConfiguration>();
        MerchantConfiguration merchantConfiguration = merchantConfigurationService.getMerchantConfiguration(Constants.PAYMENT_MODULES, store);
        if (merchantConfiguration != null) {
            if (!StringUtils.isBlank(merchantConfiguration.getValue())) {
                String decrypted = encryption.decrypt(merchantConfiguration.getValue());
                modules = ConfigurationModulesLoader.loadIntegrationConfigurations(decrypted);
            }
        } else {
            merchantConfiguration = new MerchantConfiguration();
            merchantConfiguration.setMerchantStore(store);
            merchantConfiguration.setKey(Constants.PAYMENT_MODULES);
        }
        modules.put(configuration.getModuleCode(), configuration);
        String configs = ConfigurationModulesLoader.toJSONString(modules);
        String encrypted = encryption.encrypt(configs);
        merchantConfiguration.setValue(encrypted);
        merchantConfigurationService.saveOrUpdate(merchantConfiguration);
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException) HashMap(java.util.HashMap) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) MerchantConfiguration(com.salesmanager.core.model.system.MerchantConfiguration) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationException(com.salesmanager.core.modules.integration.IntegrationException)

Example 3 with PaymentModule

use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.

the class PaymentServiceImpl method initTransaction.

@Override
public Transaction initTransaction(Order order, Customer customer, Payment payment, MerchantStore store) throws ServiceException {
    Validate.notNull(store);
    Validate.notNull(payment);
    Validate.notNull(order);
    Validate.notNull(order.getTotal());
    payment.setCurrency(store.getCurrency());
    BigDecimal amount = order.getTotal();
    // must have a shipping module configured
    Map<String, IntegrationConfiguration> modules = this.getPaymentModulesConfigured(store);
    if (modules == null) {
        throw new ServiceException("No payment module configured");
    }
    IntegrationConfiguration configuration = modules.get(payment.getModuleName());
    if (configuration == null) {
        throw new ServiceException("Payment module " + payment.getModuleName() + " is not configured");
    }
    if (!configuration.isActive()) {
        throw new ServiceException("Payment module " + payment.getModuleName() + " is not active");
    }
    PaymentModule module = this.paymentModules.get(order.getPaymentModuleCode());
    if (module == null) {
        throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " does not exist");
    }
    IntegrationModule integrationModule = getPaymentMethodByCode(store, payment.getModuleName());
    return module.initTransaction(store, customer, amount, payment, configuration, integrationModule);
}
Also used : PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) BigDecimal(java.math.BigDecimal) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule)

Example 4 with PaymentModule

use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.

the class PaymentServiceImpl method processRefund.

@Override
public Transaction processRefund(Order order, Customer customer, MerchantStore store, BigDecimal amount) throws ServiceException {
    Validate.notNull(customer);
    Validate.notNull(store);
    Validate.notNull(amount);
    Validate.notNull(order);
    Validate.notNull(order.getOrderTotal());
    BigDecimal orderTotal = order.getTotal();
    if (amount.doubleValue() > orderTotal.doubleValue()) {
        throw new ServiceException("Invalid amount, the refunded amount is greater than the total allowed");
    }
    String module = order.getPaymentModuleCode();
    Map<String, IntegrationConfiguration> modules = this.getPaymentModulesConfigured(store);
    if (modules == null) {
        throw new ServiceException("No payment module configured");
    }
    IntegrationConfiguration configuration = modules.get(module);
    if (configuration == null) {
        throw new ServiceException("Payment module " + module + " is not configured");
    }
    PaymentModule paymentModule = this.paymentModules.get(module);
    if (paymentModule == null) {
        throw new ServiceException("Payment module " + paymentModule + " does not exist");
    }
    boolean partial = false;
    if (amount.doubleValue() != order.getTotal().doubleValue()) {
        partial = true;
    }
    IntegrationModule integrationModule = getPaymentMethodByCode(store, module);
    // get the associated transaction
    Transaction refundable = transactionService.getRefundableTransaction(order);
    if (refundable == null) {
        throw new ServiceException("No refundable transaction for this order");
    }
    Transaction transaction = paymentModule.refund(partial, store, refundable, order, amount, configuration, integrationModule);
    transaction.setOrder(order);
    transactionService.create(transaction);
    OrderTotal refund = new OrderTotal();
    refund.setModule(Constants.OT_REFUND_MODULE_CODE);
    refund.setText(Constants.OT_REFUND_MODULE_CODE);
    refund.setTitle(Constants.OT_REFUND_MODULE_CODE);
    refund.setOrderTotalCode(Constants.OT_REFUND_MODULE_CODE);
    refund.setOrderTotalType(OrderTotalType.REFUND);
    refund.setValue(amount);
    refund.setSortOrder(100);
    refund.setOrder(order);
    order.getOrderTotal().add(refund);
    // update order total
    orderTotal = orderTotal.subtract(amount);
    // update ordertotal refund
    Set<OrderTotal> totals = order.getOrderTotal();
    for (OrderTotal total : totals) {
        if (total.getModule().equals(Constants.OT_TOTAL_MODULE_CODE)) {
            total.setValue(orderTotal);
        }
    }
    order.setTotal(orderTotal);
    order.setStatus(OrderStatus.REFUNDED);
    OrderStatusHistory orderHistory = new OrderStatusHistory();
    orderHistory.setOrder(order);
    orderHistory.setStatus(OrderStatus.REFUNDED);
    orderHistory.setDateAdded(new Date());
    order.getOrderHistory().add(orderHistory);
    orderService.saveOrUpdate(order);
    return transaction;
}
Also used : IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) BigDecimal(java.math.BigDecimal) Date(java.util.Date) PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) ServiceException(com.salesmanager.core.business.exception.ServiceException) Transaction(com.salesmanager.core.model.payments.Transaction) OrderTotal(com.salesmanager.core.model.order.OrderTotal) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule)

Example 5 with PaymentModule

use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.

the class PaymentServiceImpl method processCapturePayment.

@Override
public Transaction processCapturePayment(Order order, Customer customer, MerchantStore store) throws ServiceException {
    Validate.notNull(customer);
    Validate.notNull(store);
    Validate.notNull(order);
    // must have a shipping module configured
    Map<String, IntegrationConfiguration> modules = this.getPaymentModulesConfigured(store);
    if (modules == null) {
        throw new ServiceException("No payment module configured");
    }
    IntegrationConfiguration configuration = modules.get(order.getPaymentModuleCode());
    if (configuration == null) {
        throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " is not configured");
    }
    if (!configuration.isActive()) {
        throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " is not active");
    }
    PaymentModule module = this.paymentModules.get(order.getPaymentModuleCode());
    if (module == null) {
        throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " does not exist");
    }
    IntegrationModule integrationModule = getPaymentMethodByCode(store, order.getPaymentModuleCode());
    // TransactionType transactionType = payment.getTransactionType();
    // get the previous transaction
    Transaction trx = transactionService.getCapturableTransaction(order);
    if (trx == null) {
        throw new ServiceException("No capturable transaction for order id " + order.getId());
    }
    Transaction transaction = module.capture(store, customer, order, trx, configuration, integrationModule);
    transaction.setOrder(order);
    transactionService.create(transaction);
    OrderStatusHistory orderHistory = new OrderStatusHistory();
    orderHistory.setOrder(order);
    orderHistory.setStatus(OrderStatus.PROCESSED);
    orderHistory.setDateAdded(new Date());
    orderService.addOrderStatusHistory(order, orderHistory);
    order.setStatus(OrderStatus.PROCESSED);
    orderService.saveOrUpdate(order);
    return transaction;
}
Also used : PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) ServiceException(com.salesmanager.core.business.exception.ServiceException) Transaction(com.salesmanager.core.model.payments.Transaction) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule) Date(java.util.Date)

Aggregations

IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)7 PaymentModule (com.salesmanager.core.modules.integration.payment.model.PaymentModule)7 ServiceException (com.salesmanager.core.business.exception.ServiceException)6 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)6 Transaction (com.salesmanager.core.model.payments.Transaction)5 BigDecimal (java.math.BigDecimal)4 OrderStatusHistory (com.salesmanager.core.model.order.orderstatus.OrderStatusHistory)2 Date (java.util.Date)2 PayPalExpressCheckoutPayment (com.salesmanager.core.business.modules.integration.payment.impl.PayPalExpressCheckoutPayment)1 Stripe3Payment (com.salesmanager.core.business.modules.integration.payment.impl.Stripe3Payment)1 AjaxResponse (com.salesmanager.core.business.utils.ajax.AjaxResponse)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 OrderTotal (com.salesmanager.core.model.order.OrderTotal)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 CreditCardPayment (com.salesmanager.core.model.payments.CreditCardPayment)1 PaypalPayment (com.salesmanager.core.model.payments.PaypalPayment)1 TransactionType (com.salesmanager.core.model.payments.TransactionType)1 Language (com.salesmanager.core.model.reference.language.Language)1 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)1 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)1