Search in sources :

Example 1 with PaypalPayment

use of com.salesmanager.core.model.payments.PaypalPayment 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)

Aggregations

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 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 PaypalPayment (com.salesmanager.core.model.payments.PaypalPayment)1 Transaction (com.salesmanager.core.model.payments.Transaction)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 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)1 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)1 PaymentModule (com.salesmanager.core.modules.integration.payment.model.PaymentModule)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1