Search in sources :

Example 1 with ReadableOrderConfirmation

use of com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation in project shopizer by shopizer-ecommerce.

the class OrderApi method checkout.

/**
 * Action for performing a checkout on a given shopping cart
 *
 * @param id
 * @param order
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/auth/cart/{code}/checkout" }, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public ReadableOrderConfirmation checkout(// shopping cart
@PathVariable final String code, // order
@Valid @RequestBody PersistableOrder order, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    try {
        Principal principal = request.getUserPrincipal();
        String userName = principal.getName();
        Customer customer = customerService.getByNick(userName);
        if (customer == null) {
            response.sendError(401, "Error while performing checkout customer not authorized");
            return null;
        }
        ShoppingCart cart = shoppingCartService.getByCode(code, merchantStore);
        if (cart == null) {
            throw new ResourceNotFoundException("Cart code " + code + " does not exist");
        }
        order.setShoppingCartId(cart.getId());
        // That is an existing customer purchasing
        order.setCustomerId(customer.getId());
        Order modelOrder = orderFacade.processOrder(order, customer, merchantStore, language, locale);
        Long orderId = modelOrder.getId();
        modelOrder.setId(orderId);
        return orderFacadeV1.orderConfirmation(modelOrder, customer, merchantStore, language);
    } catch (Exception e) {
        LOGGER.error("Error while processing checkout", e);
        try {
            response.sendError(503, "Error while processing checkout " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : PersistableAnonymousOrder(com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder) PersistableOrder(com.salesmanager.shop.model.order.v1.PersistableOrder) Order(com.salesmanager.core.model.order.Order) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Principal(java.security.Principal) CredentialsException(com.salesmanager.shop.store.security.services.CredentialsException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with ReadableOrderConfirmation

use of com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method orderConfirmation.

@Override
public ReadableOrderConfirmation orderConfirmation(Order order, Customer customer, MerchantStore store, Language language) {
    Validate.notNull(order, "Order cannot be null");
    Validate.notNull(customer, "Customer cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    ReadableOrderConfirmation orderConfirmation = new ReadableOrderConfirmation();
    ReadableCustomer readableCustomer = readableCustomerMapper.convert(customer, store, language);
    orderConfirmation.setBilling(readableCustomer.getBilling());
    orderConfirmation.setDelivery(readableCustomer.getDelivery());
    ReadableTotal readableTotal = new ReadableTotal();
    Set<OrderTotal> totals = order.getOrderTotal();
    List<ReadableOrderTotal> readableTotals = totals.stream().sorted(Comparator.comparingInt(OrderTotal::getSortOrder)).map(tot -> convertOrderTotal(tot, store, language)).collect(Collectors.toList());
    readableTotal.setTotals(readableTotals);
    Optional<ReadableOrderTotal> grandTotal = readableTotals.stream().filter(tot -> tot.getCode().equals("order.total.total")).findFirst();
    if (grandTotal.isPresent()) {
        readableTotal.setGrandTotal(grandTotal.get().getText());
    }
    orderConfirmation.setTotal(readableTotal);
    List<ReadableOrderProduct> products = order.getOrderProducts().stream().map(pr -> convertOrderProduct(pr, store, language)).collect(Collectors.toList());
    orderConfirmation.setProducts(products);
    if (!StringUtils.isBlank(order.getShippingModuleCode())) {
        StringBuilder optionCodeBuilder = new StringBuilder();
        try {
            optionCodeBuilder.append("module.shipping.").append(order.getShippingModuleCode());
            String shippingName = messages.getMessage(optionCodeBuilder.toString(), new String[] { store.getStorename() }, languageService.toLocale(language, store));
            orderConfirmation.setShipping(shippingName);
        } catch (Exception e) {
            // label not found
            LOGGER.warn("No shipping code found for " + optionCodeBuilder.toString());
        }
    }
    if (order.getPaymentType() != null) {
        orderConfirmation.setPayment(order.getPaymentType().name());
    }
    /**
     * Confirmation may be formatted
     */
    orderConfirmation.setId(order.getId());
    return orderConfirmation;
}
Also used : ReadableOrderTotalMapper(com.salesmanager.shop.mapper.order.ReadableOrderTotalMapper) Order(com.salesmanager.core.model.order.Order) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) ReadableCustomerMapper(com.salesmanager.shop.mapper.customer.ReadableCustomerMapper) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) LanguageService(com.salesmanager.core.business.services.reference.language.LanguageService) Language(com.salesmanager.core.model.reference.language.Language) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) LabelUtils(com.salesmanager.shop.utils.LabelUtils) Service(org.springframework.stereotype.Service) Logger(org.slf4j.Logger) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) Customer(com.salesmanager.core.model.customer.Customer) ReadableOrderConfirmation(com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation) Set(java.util.Set) Collectors(java.util.stream.Collectors) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) List(java.util.List) Validate(org.apache.commons.lang3.Validate) OrderFacade(com.salesmanager.shop.store.controller.order.facade.v1.OrderFacade) Optional(java.util.Optional) Comparator(java.util.Comparator) ReadableOrderProductMapper(com.salesmanager.shop.mapper.order.ReadableOrderProductMapper) ReadableTotal(com.salesmanager.shop.model.order.total.ReadableTotal) OrderTotal(com.salesmanager.core.model.order.OrderTotal) ReadableTotal(com.salesmanager.shop.model.order.total.ReadableTotal) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) ReadableOrderProduct(com.salesmanager.shop.model.order.ReadableOrderProduct) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) ReadableOrderConfirmation(com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation) ReadableOrderTotal(com.salesmanager.shop.model.order.total.ReadableOrderTotal) OrderTotal(com.salesmanager.core.model.order.OrderTotal)

Example 3 with ReadableOrderConfirmation

use of com.salesmanager.shop.model.order.v1.ReadableOrderConfirmation in project shopizer by shopizer-ecommerce.

the class OrderApi method checkout.

/**
 * Main checkout resource that will complete the order flow
 * @param code
 * @param order
 * @param merchantStore
 * @param language
 * @return
 */
@RequestMapping(value = { "/cart/{code}/checkout" }, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public ReadableOrderConfirmation checkout(// shopping cart
@PathVariable final String code, // order
@Valid @RequestBody PersistableAnonymousOrder order, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    Validate.notNull(order.getCustomer(), "Customer must not be null");
    ShoppingCart cart;
    try {
        cart = shoppingCartService.getByCode(code, merchantStore);
        if (cart == null) {
            throw new ResourceNotFoundException("Cart code " + code + " does not exist");
        }
        // security password validation
        PersistableCustomer presistableCustomer = order.getCustomer();
        if (!StringUtils.isBlank(presistableCustomer.getPassword())) {
            // validate customer password
            credentialsService.validateCredentials(presistableCustomer.getPassword(), presistableCustomer.getRepeatPassword(), merchantStore, language);
        }
        Customer customer = new Customer();
        customer = customerFacade.populateCustomerModel(customer, order.getCustomer(), merchantStore, language);
        if (!StringUtils.isBlank(presistableCustomer.getPassword())) {
            // check if customer already exist
            customer.setAnonymous(false);
            // username
            customer.setNick(customer.getEmailAddress());
            if (customerFacadev1.checkIfUserExists(customer.getNick(), merchantStore)) {
                // 409 Conflict
                throw new GenericRuntimeException("409", "Customer with email [" + customer.getEmailAddress() + "] is already registered");
            }
        }
        order.setShoppingCartId(cart.getId());
        Order modelOrder = orderFacade.processOrder(order, customer, merchantStore, language, LocaleUtils.getLocale(language));
        Long orderId = modelOrder.getId();
        // populate order confirmation
        order.setId(orderId);
        // set customer id
        order.getCustomer().setId(modelOrder.getCustomerId());
        return orderFacadeV1.orderConfirmation(modelOrder, customer, merchantStore, language);
    } catch (Exception e) {
        if (e instanceof CredentialsException) {
            throw new GenericRuntimeException("412", "Credentials creation Failed [" + e.getMessage() + "]");
        }
        String message = e.getMessage();
        if (StringUtils.isBlank(message)) {
            // exception type
            message = "APP-BACKEND";
            if (e.getCause() instanceof com.salesmanager.core.modules.integration.IntegrationException) {
                message = "Integration problen occured to complete order";
            }
        }
        throw new ServiceRuntimeException("Error during checkout [" + message + "]", e);
    }
}
Also used : PersistableAnonymousOrder(com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder) PersistableOrder(com.salesmanager.shop.model.order.v1.PersistableOrder) Order(com.salesmanager.core.model.order.Order) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) CredentialsException(com.salesmanager.shop.store.security.services.CredentialsException) CredentialsException(com.salesmanager.shop.store.security.services.CredentialsException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Customer (com.salesmanager.core.model.customer.Customer)3 Order (com.salesmanager.core.model.order.Order)3 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)3 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)2 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)2 ReadableOrder (com.salesmanager.shop.model.order.v0.ReadableOrder)2 PersistableAnonymousOrder (com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder)2 PersistableOrder (com.salesmanager.shop.model.order.v1.PersistableOrder)2 GenericRuntimeException (com.salesmanager.shop.store.api.exception.GenericRuntimeException)2 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)2 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)2 CredentialsException (com.salesmanager.shop.store.security.services.CredentialsException)2 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 LanguageService (com.salesmanager.core.business.services.reference.language.LanguageService)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 OrderTotal (com.salesmanager.core.model.order.OrderTotal)1 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)1 Language (com.salesmanager.core.model.reference.language.Language)1