Search in sources :

Example 1 with CreditCardType

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

the class OrderFacadeImpl method processOrderModel.

/**
 * Commit an order
 * @param order
 * @param customer
 * @param transaction
 * @param store
 * @param language
 * @return
 * @throws ServiceException
 */
private Order processOrderModel(ShopOrder order, Customer customer, Transaction transaction, MerchantStore store, Language language) throws ServiceException {
    try {
        if (order.isShipToBillingAdress()) {
            // customer shipping is billing
            PersistableCustomer orderCustomer = order.getCustomer();
            Address billing = orderCustomer.getBilling();
            orderCustomer.setDelivery(billing);
        }
        Order modelOrder = new Order();
        modelOrder.setDatePurchased(new Date());
        modelOrder.setBilling(customer.getBilling());
        modelOrder.setDelivery(customer.getDelivery());
        modelOrder.setPaymentModuleCode(order.getPaymentModule());
        modelOrder.setPaymentType(PaymentType.valueOf(order.getPaymentMethodType()));
        modelOrder.setShippingModuleCode(order.getShippingModule());
        modelOrder.setCustomerAgreement(order.isCustomerAgreed());
        // set the store
        modelOrder.setLocale(LocaleUtils.getLocale(store));
        // locale based
        // on the
        // country for
        // order $
        // formatting
        List<ShoppingCartItem> shoppingCartItems = order.getShoppingCartItems();
        Set<OrderProduct> orderProducts = new LinkedHashSet<OrderProduct>();
        if (!StringUtils.isBlank(order.getComments())) {
            OrderStatusHistory statusHistory = new OrderStatusHistory();
            statusHistory.setStatus(OrderStatus.ORDERED);
            statusHistory.setOrder(modelOrder);
            statusHistory.setDateAdded(new Date());
            statusHistory.setComments(order.getComments());
            modelOrder.getOrderHistory().add(statusHistory);
        }
        OrderProductPopulator orderProductPopulator = new OrderProductPopulator();
        orderProductPopulator.setDigitalProductService(digitalProductService);
        orderProductPopulator.setProductAttributeService(productAttributeService);
        orderProductPopulator.setProductService(productService);
        String shoppingCartCode = null;
        for (ShoppingCartItem item : shoppingCartItems) {
            if (shoppingCartCode == null && item.getShoppingCart() != null) {
                shoppingCartCode = item.getShoppingCart().getShoppingCartCode();
            }
            /**
             * Before processing order quantity of item must be > 0
             */
            Product product = productService.getById(item.getProductId());
            if (product == null) {
                throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
            }
            LOGGER.debug("Validate inventory");
            for (ProductAvailability availability : product.getAvailabilities()) {
                if (availability.getRegion().equals(Constants.ALL_REGIONS)) {
                    int qty = availability.getProductQuantity();
                    if (qty < item.getQuantity()) {
                        throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
                    }
                }
            }
            OrderProduct orderProduct = new OrderProduct();
            orderProduct = orderProductPopulator.populate(item, orderProduct, store, language);
            orderProduct.setOrder(modelOrder);
            orderProducts.add(orderProduct);
        }
        modelOrder.setOrderProducts(orderProducts);
        OrderTotalSummary summary = order.getOrderTotalSummary();
        List<com.salesmanager.core.model.order.OrderTotal> totals = summary.getTotals();
        // re-order totals
        Collections.sort(totals, new Comparator<com.salesmanager.core.model.order.OrderTotal>() {

            public int compare(com.salesmanager.core.model.order.OrderTotal x, com.salesmanager.core.model.order.OrderTotal y) {
                if (x.getSortOrder() == y.getSortOrder())
                    return 0;
                return x.getSortOrder() < y.getSortOrder() ? -1 : 1;
            }
        });
        Set<com.salesmanager.core.model.order.OrderTotal> modelTotals = new LinkedHashSet<com.salesmanager.core.model.order.OrderTotal>();
        for (com.salesmanager.core.model.order.OrderTotal total : totals) {
            total.setOrder(modelOrder);
            modelTotals.add(total);
        }
        modelOrder.setOrderTotal(modelTotals);
        modelOrder.setTotal(order.getOrderTotalSummary().getTotal());
        // order misc objects
        modelOrder.setCurrency(store.getCurrency());
        modelOrder.setMerchant(store);
        // customer object
        orderCustomer(customer, modelOrder, language);
        // populate shipping information
        if (!StringUtils.isBlank(order.getShippingModule())) {
            modelOrder.setShippingModuleCode(order.getShippingModule());
        }
        String paymentType = order.getPaymentMethodType();
        Payment payment = new Payment();
        payment.setPaymentType(PaymentType.valueOf(paymentType));
        payment.setAmount(order.getOrderTotalSummary().getTotal());
        payment.setModuleName(order.getPaymentModule());
        payment.setCurrency(modelOrder.getCurrency());
        if (order.getPayment() != null && order.getPayment().get("paymentToken") != null) {
            // set
            // token
            String paymentToken = order.getPayment().get("paymentToken");
            Map<String, String> paymentMetaData = new HashMap<String, String>();
            payment.setPaymentMetaData(paymentMetaData);
            paymentMetaData.put("paymentToken", paymentToken);
        }
        if (PaymentType.CREDITCARD.name().equals(paymentType)) {
            payment = new CreditCardPayment();
            ((CreditCardPayment) payment).setCardOwner(order.getPayment().get("creditcard_card_holder"));
            ((CreditCardPayment) payment).setCredidCardValidationNumber(order.getPayment().get("creditcard_card_cvv"));
            ((CreditCardPayment) payment).setCreditCardNumber(order.getPayment().get("creditcard_card_number"));
            ((CreditCardPayment) payment).setExpirationMonth(order.getPayment().get("creditcard_card_expirationmonth"));
            ((CreditCardPayment) payment).setExpirationYear(order.getPayment().get("creditcard_card_expirationyear"));
            Map<String, String> paymentMetaData = order.getPayment();
            payment.setPaymentMetaData(paymentMetaData);
            payment.setPaymentType(PaymentType.valueOf(paymentType));
            payment.setAmount(order.getOrderTotalSummary().getTotal());
            payment.setModuleName(order.getPaymentModule());
            payment.setCurrency(modelOrder.getCurrency());
            CreditCardType creditCardType = null;
            String cardType = order.getPayment().get("creditcard_card_type");
            // supported credit cards
            if (CreditCardType.AMEX.name().equalsIgnoreCase(cardType)) {
                creditCardType = CreditCardType.AMEX;
            } else if (CreditCardType.VISA.name().equalsIgnoreCase(cardType)) {
                creditCardType = CreditCardType.VISA;
            } else if (CreditCardType.MASTERCARD.name().equalsIgnoreCase(cardType)) {
                creditCardType = CreditCardType.MASTERCARD;
            } else if (CreditCardType.DINERS.name().equalsIgnoreCase(cardType)) {
                creditCardType = CreditCardType.DINERS;
            } else if (CreditCardType.DISCOVERY.name().equalsIgnoreCase(cardType)) {
                creditCardType = CreditCardType.DISCOVERY;
            }
            ((CreditCardPayment) payment).setCreditCard(creditCardType);
            if (creditCardType != null) {
                CreditCard cc = new CreditCard();
                cc.setCardType(creditCardType);
                cc.setCcCvv(((CreditCardPayment) payment).getCredidCardValidationNumber());
                cc.setCcOwner(((CreditCardPayment) payment).getCardOwner());
                cc.setCcExpires(((CreditCardPayment) payment).getExpirationMonth() + "-" + ((CreditCardPayment) payment).getExpirationYear());
                // hash credit card number
                if (!StringUtils.isBlank(cc.getCcNumber())) {
                    String maskedNumber = CreditCardUtils.maskCardNumber(order.getPayment().get("creditcard_card_number"));
                    cc.setCcNumber(maskedNumber);
                    modelOrder.setCreditCard(cc);
                }
            }
        }
        if (PaymentType.PAYPAL.name().equals(paymentType)) {
            // check for previous transaction
            if (transaction == null) {
                throw new ServiceException("payment.error");
            }
            payment = new com.salesmanager.core.model.payments.PaypalPayment();
            ((com.salesmanager.core.model.payments.PaypalPayment) payment).setPayerId(transaction.getTransactionDetails().get("PAYERID"));
            ((com.salesmanager.core.model.payments.PaypalPayment) payment).setPaymentToken(transaction.getTransactionDetails().get("TOKEN"));
        }
        modelOrder.setShoppingCartCode(shoppingCartCode);
        modelOrder.setPaymentModuleCode(order.getPaymentModule());
        payment.setModuleName(order.getPaymentModule());
        if (transaction != null) {
            orderService.processOrder(modelOrder, customer, order.getShoppingCartItems(), summary, payment, store);
        } else {
            orderService.processOrder(modelOrder, customer, order.getShoppingCartItems(), summary, payment, transaction, store);
        }
        return modelOrder;
    } catch (ServiceException se) {
        // may be invalid credit card
        throw se;
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Address(com.salesmanager.shop.model.customer.address.Address) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) OrderProductPopulator(com.salesmanager.shop.populator.order.OrderProductPopulator) ReadableOrderProductPopulator(com.salesmanager.shop.populator.order.ReadableOrderProductPopulator) HashMap(java.util.HashMap) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) ShippingProduct(com.salesmanager.core.model.shipping.ShippingProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) Product(com.salesmanager.core.model.catalog.product.Product) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) ShopOrder(com.salesmanager.shop.model.order.ShopOrder) Order(com.salesmanager.core.model.order.Order) CreditCardType(com.salesmanager.core.model.payments.CreditCardType) Date(java.util.Date) LocalDate(java.time.LocalDate) CreditCard(com.salesmanager.core.model.order.payment.CreditCard) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ConversionException(com.salesmanager.core.business.exception.ConversionException) CreditCardPayment(com.salesmanager.core.model.payments.CreditCardPayment) CreditCardPayment(com.salesmanager.core.model.payments.CreditCardPayment) Payment(com.salesmanager.core.model.payments.Payment) ServiceException(com.salesmanager.core.business.exception.ServiceException) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) ReadableOrderStatusHistory(com.salesmanager.shop.model.order.history.ReadableOrderStatusHistory) PersistableOrderStatusHistory(com.salesmanager.shop.model.order.history.PersistableOrderStatusHistory) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 2 with CreditCardType

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

the class OrderFacadeImpl method validateOrder.

@Override
public void validateOrder(ShopOrder order, BindingResult bindingResult, Map<String, String> messagesResult, MerchantStore store, Locale locale) throws ServiceException {
    Validate.notNull(messagesResult, "messagesResult should not be null");
    try {
        // validate order shipping and billing
        if (StringUtils.isBlank(order.getCustomer().getBilling().getFirstName())) {
            FieldError error = new FieldError("customer.billing.firstName", "customer.billing.firstName", messages.getMessage("NotEmpty.customer.firstName", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.firstName", messages.getMessage("NotEmpty.customer.firstName", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getLastName())) {
            FieldError error = new FieldError("customer.billing.lastName", "customer.billing.lastName", messages.getMessage("NotEmpty.customer.lastName", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.lastName", messages.getMessage("NotEmpty.customer.lastName", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getEmailAddress())) {
            FieldError error = new FieldError("customer.emailAddress", "customer.emailAddress", messages.getMessage("NotEmpty.customer.emailAddress", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.emailAddress", messages.getMessage("NotEmpty.customer.emailAddress", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getAddress())) {
            FieldError error = new FieldError("customer.billing.address", "customer.billing.address", messages.getMessage("NotEmpty.customer.billing.address", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.address", messages.getMessage("NotEmpty.customer.billing.address", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getCity())) {
            FieldError error = new FieldError("customer.billing.city", "customer.billing.city", messages.getMessage("NotEmpty.customer.billing.city", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.city", messages.getMessage("NotEmpty.customer.billing.city", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getCountry())) {
            FieldError error = new FieldError("customer.billing.country", "customer.billing.country", messages.getMessage("NotEmpty.customer.billing.country", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.country", messages.getMessage("NotEmpty.customer.billing.country", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getZone()) && StringUtils.isBlank(order.getCustomer().getBilling().getStateProvince())) {
            FieldError error = new FieldError("customer.billing.stateProvince", "customer.billing.stateProvince", messages.getMessage("NotEmpty.customer.billing.stateProvince", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.stateProvince", messages.getMessage("NotEmpty.customer.billing.stateProvince", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getPhone())) {
            FieldError error = new FieldError("customer.billing.phone", "customer.billing.phone", messages.getMessage("NotEmpty.customer.billing.phone", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.phone", messages.getMessage("NotEmpty.customer.billing.phone", locale));
        }
        if (StringUtils.isBlank(order.getCustomer().getBilling().getPostalCode())) {
            FieldError error = new FieldError("customer.billing.postalCode", "customer.billing.postalCode", messages.getMessage("NotEmpty.customer.billing.postalCode", locale));
            bindingResult.addError(error);
            messagesResult.put("customer.billing.postalCode", messages.getMessage("NotEmpty.customer.billing.postalCode", locale));
        }
        if (!order.isShipToBillingAdress()) {
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getFirstName())) {
                FieldError error = new FieldError("customer.delivery.firstName", "customer.delivery.firstName", messages.getMessage("NotEmpty.customer.shipping.firstName", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.firstName", messages.getMessage("NotEmpty.customer.shipping.firstName", locale));
            }
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getLastName())) {
                FieldError error = new FieldError("customer.delivery.lastName", "customer.delivery.lastName", messages.getMessage("NotEmpty.customer.shipping.lastName", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.lastName", messages.getMessage("NotEmpty.customer.shipping.lastName", locale));
            }
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getAddress())) {
                FieldError error = new FieldError("customer.delivery.address", "customer.delivery.address", messages.getMessage("NotEmpty.customer.shipping.address", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.address", messages.getMessage("NotEmpty.customer.shipping.address", locale));
            }
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getCity())) {
                FieldError error = new FieldError("customer.delivery.city", "customer.delivery.city", messages.getMessage("NotEmpty.customer.shipping.city", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.city", messages.getMessage("NotEmpty.customer.shipping.city", locale));
            }
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getCountry())) {
                FieldError error = new FieldError("customer.delivery.country", "customer.delivery.country", messages.getMessage("NotEmpty.customer.shipping.country", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.country", messages.getMessage("NotEmpty.customer.shipping.country", locale));
            }
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getZone()) && StringUtils.isBlank(order.getCustomer().getDelivery().getStateProvince())) {
                FieldError error = new FieldError("customer.delivery.stateProvince", "customer.delivery.stateProvince", messages.getMessage("NotEmpty.customer.shipping.stateProvince", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.stateProvince", messages.getMessage("NotEmpty.customer.shipping.stateProvince", locale));
            }
            if (StringUtils.isBlank(order.getCustomer().getDelivery().getPostalCode())) {
                FieldError error = new FieldError("customer.delivery.postalCode", "customer.delivery.postalCode", messages.getMessage("NotEmpty.customer.shipping.postalCode", locale));
                bindingResult.addError(error);
                messagesResult.put("customer.delivery.postalCode", messages.getMessage("NotEmpty.customer.shipping.postalCode", locale));
            }
        }
        if (bindingResult.hasErrors()) {
            return;
        }
        String paymentType = order.getPaymentMethodType();
        // validate payment
        if (paymentType == null) {
            ServiceException serviceException = new ServiceException(ServiceException.EXCEPTION_VALIDATION, "payment.required");
            throw serviceException;
        }
        // validate shipping
        if (shippingService.requiresShipping(order.getShoppingCartItems(), store) && order.getSelectedShippingOption() == null) {
            ServiceException serviceException = new ServiceException(ServiceException.EXCEPTION_VALIDATION, "shipping.required");
            throw serviceException;
        }
        // pre-validate credit card
        if (PaymentType.CREDITCARD.name().equals(paymentType) && "true".equals(coreConfiguration.getProperty("VALIDATE_CREDIT_CARD"))) {
            String cco = order.getPayment().get("creditcard_card_holder");
            String cvv = order.getPayment().get("creditcard_card_cvv");
            String ccn = order.getPayment().get("creditcard_card_number");
            String ccm = order.getPayment().get("creditcard_card_expirationmonth");
            String ccd = order.getPayment().get("creditcard_card_expirationyear");
            if (StringUtils.isBlank(cco) || StringUtils.isBlank(cvv) || StringUtils.isBlank(ccn) || StringUtils.isBlank(ccm) || StringUtils.isBlank(ccd)) {
                ObjectError error = new ObjectError("creditcard", messages.getMessage("messages.error.creditcard", locale));
                bindingResult.addError(error);
                messagesResult.put("creditcard", messages.getMessage("messages.error.creditcard", locale));
                return;
            }
            CreditCardType creditCardType = null;
            String cardType = order.getPayment().get("creditcard_card_type");
            if (cardType.equalsIgnoreCase(CreditCardType.AMEX.name())) {
                creditCardType = CreditCardType.AMEX;
            } else if (cardType.equalsIgnoreCase(CreditCardType.VISA.name())) {
                creditCardType = CreditCardType.VISA;
            } else if (cardType.equalsIgnoreCase(CreditCardType.MASTERCARD.name())) {
                creditCardType = CreditCardType.MASTERCARD;
            } else if (cardType.equalsIgnoreCase(CreditCardType.DINERS.name())) {
                creditCardType = CreditCardType.DINERS;
            } else if (cardType.equalsIgnoreCase(CreditCardType.DISCOVERY.name())) {
                creditCardType = CreditCardType.DISCOVERY;
            }
            if (creditCardType == null) {
                ServiceException serviceException = new ServiceException(ServiceException.EXCEPTION_VALIDATION, "cc.type");
                throw serviceException;
            }
        }
    } catch (ServiceException se) {
        LOGGER.error("Error while commiting order", se);
        throw se;
    }
}
Also used : ObjectError(org.springframework.validation.ObjectError) ServiceException(com.salesmanager.core.business.exception.ServiceException) FieldError(org.springframework.validation.FieldError) CreditCardType(com.salesmanager.core.model.payments.CreditCardType)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)2 CreditCardType (com.salesmanager.core.model.payments.CreditCardType)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 Product (com.salesmanager.core.model.catalog.product.Product)1 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)1 Order (com.salesmanager.core.model.order.Order)1 OrderTotalSummary (com.salesmanager.core.model.order.OrderTotalSummary)1 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)1 OrderStatusHistory (com.salesmanager.core.model.order.orderstatus.OrderStatusHistory)1 CreditCard (com.salesmanager.core.model.order.payment.CreditCard)1 CreditCardPayment (com.salesmanager.core.model.payments.CreditCardPayment)1 Payment (com.salesmanager.core.model.payments.Payment)1 ShippingProduct (com.salesmanager.core.model.shipping.ShippingProduct)1 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)1 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)1 Address (com.salesmanager.shop.model.customer.address.Address)1 PersistableOrderProduct (com.salesmanager.shop.model.order.PersistableOrderProduct)1 ReadableOrderProduct (com.salesmanager.shop.model.order.ReadableOrderProduct)1 ShopOrder (com.salesmanager.shop.model.order.ShopOrder)1 PersistableOrderStatusHistory (com.salesmanager.shop.model.order.history.PersistableOrderStatusHistory)1