Search in sources :

Example 1 with CredentialsException

use of com.salesmanager.shop.store.security.services.CredentialsException 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)1 Order (com.salesmanager.core.model.order.Order)1 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)1 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)1 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)1 ReadableOrder (com.salesmanager.shop.model.order.v0.ReadableOrder)1 PersistableAnonymousOrder (com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder)1 PersistableOrder (com.salesmanager.shop.model.order.v1.PersistableOrder)1 GenericRuntimeException (com.salesmanager.shop.store.api.exception.GenericRuntimeException)1 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)1 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)1 CredentialsException (com.salesmanager.shop.store.security.services.CredentialsException)1 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1