Search in sources :

Example 16 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart 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 17 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class OrderPaymentApi method init.

@RequestMapping(value = { "/cart/{code}/payment/init" }, method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableTransaction init(@Valid @RequestBody PersistablePayment payment, @PathVariable String code, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) throws Exception {
    ShoppingCart cart = shoppingCartService.getByCode(code, merchantStore);
    if (cart == null) {
        throw new ResourceNotFoundException("Cart code " + code + " does not exist");
    }
    PersistablePaymentPopulator populator = new PersistablePaymentPopulator();
    populator.setPricingService(pricingService);
    Payment paymentModel = new Payment();
    populator.populate(payment, paymentModel, merchantStore, language);
    Transaction transactionModel = paymentService.initTransaction(null, paymentModel, merchantStore);
    ReadableTransaction transaction = new ReadableTransaction();
    ReadableTransactionPopulator trxPopulator = new ReadableTransactionPopulator();
    trxPopulator.setOrderService(orderService);
    trxPopulator.setPricingService(pricingService);
    trxPopulator.populate(transactionModel, transaction, merchantStore, language);
    return transaction;
}
Also used : PersistablePayment(com.salesmanager.shop.model.order.transaction.PersistablePayment) Payment(com.salesmanager.core.model.payments.Payment) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableTransaction(com.salesmanager.shop.model.order.transaction.ReadableTransaction) Transaction(com.salesmanager.core.model.payments.Transaction) ReadableTransactionPopulator(com.salesmanager.shop.populator.order.transaction.ReadableTransactionPopulator) ReadableTransaction(com.salesmanager.shop.model.order.transaction.ReadableTransaction) PersistablePaymentPopulator(com.salesmanager.shop.populator.order.transaction.PersistablePaymentPopulator) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 18 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class OrderShippingApi method shipping.

/**
 * Get shipping quote for a given shopping cart
 *
 * @param id
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/auth/cart/{code}/shipping" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableShippingSummary shipping(@PathVariable final String code, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) {
    try {
        Locale locale = request.getLocale();
        Principal principal = request.getUserPrincipal();
        String userName = principal.getName();
        // get customer id
        Customer customer = customerService.getByNick(userName);
        if (customer == null) {
            response.sendError(503, "Error while getting user details to calculate shipping quote");
        }
        ShoppingCart cart = shoppingCartFacade.getShoppingCartModel(code, merchantStore);
        if (cart == null) {
            response.sendError(404, "Cart code " + code + " does not exist");
        }
        if (cart.getCustomerId() == null) {
            response.sendError(404, "Cart code " + code + " does not exist for exist for user " + userName);
        }
        if (cart.getCustomerId().longValue() != customer.getId().longValue()) {
            response.sendError(404, "Cart code " + code + " does not exist for exist for user " + userName);
        }
        ShippingQuote quote = orderFacade.getShippingQuote(customer, cart, merchantStore, language);
        ShippingSummary summary = orderFacade.getShippingSummary(quote, merchantStore, language);
        ReadableShippingSummary shippingSummary = new ReadableShippingSummary();
        ReadableShippingSummaryPopulator populator = new ReadableShippingSummaryPopulator();
        populator.setPricingService(pricingService);
        populator.populate(summary, shippingSummary, merchantStore, language);
        List<ShippingOption> options = quote.getShippingOptions();
        if (!CollectionUtils.isEmpty(options)) {
            for (ShippingOption shipOption : options) {
                StringBuilder moduleName = new StringBuilder();
                moduleName.append("module.shipping.").append(shipOption.getShippingModuleCode());
                String carrier = messages.getMessage(moduleName.toString(), new String[] { merchantStore.getStorename() }, locale);
                String note = messages.getMessage(moduleName.append(".note").toString(), locale, "");
                shipOption.setDescription(carrier);
                shipOption.setNote(note);
                // option name
                if (!StringUtils.isBlank(shipOption.getOptionCode())) {
                    // try to get the translate
                    StringBuilder optionCodeBuilder = new StringBuilder();
                    try {
                        optionCodeBuilder.append("module.shipping.").append(shipOption.getShippingModuleCode());
                        String optionName = messages.getMessage(optionCodeBuilder.toString(), locale);
                        shipOption.setOptionName(optionName);
                    } catch (Exception e) {
                        // label not found
                        LOGGER.warn("No shipping code found for " + optionCodeBuilder.toString());
                    }
                }
            }
            shippingSummary.setShippingOptions(options);
        }
        return shippingSummary;
    } catch (Exception e) {
        LOGGER.error("Error while getting shipping quote", e);
        try {
            response.sendError(503, "Error while getting shipping quote" + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : Locale(java.util.Locale) Customer(com.salesmanager.core.model.customer.Customer) ReadableShippingSummaryPopulator(com.salesmanager.shop.populator.order.ReadableShippingSummaryPopulator) ShippingOption(com.salesmanager.core.model.shipping.ShippingOption) ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ReadableShippingSummary(com.salesmanager.shop.model.order.shipping.ReadableShippingSummary) ReadableShippingSummary(com.salesmanager.shop.model.order.shipping.ReadableShippingSummary) Principal(java.security.Principal) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 19 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class OrderShippingApi method shipping.

/**
 * Get shipping quote based on postal code
 * @param code
 * @param address
 * @param merchantStore
 * @param language
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/cart/{code}/shipping" }, method = RequestMethod.POST)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableShippingSummary shipping(@PathVariable final String code, @RequestBody AddressLocation address, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) throws Exception {
    try {
        Locale locale = request.getLocale();
        ShoppingCart cart = shoppingCartFacade.getShoppingCartModel(code, merchantStore);
        if (cart == null) {
            response.sendError(404, "Cart id " + code + " does not exist");
        }
        Delivery addr = new Delivery();
        addr.setPostalCode(address.getPostalCode());
        Country c = countryService.getByCode(address.getCountryCode());
        if (c == null) {
            c = merchantStore.getCountry();
        }
        addr.setCountry(c);
        Customer temp = new Customer();
        temp.setAnonymous(true);
        temp.setDelivery(addr);
        ShippingQuote quote = orderFacade.getShippingQuote(temp, cart, merchantStore, language);
        ShippingSummary summary = orderFacade.getShippingSummary(quote, merchantStore, language);
        ReadableShippingSummary shippingSummary = new ReadableShippingSummary();
        ReadableShippingSummaryPopulator populator = new ReadableShippingSummaryPopulator();
        populator.setPricingService(pricingService);
        populator.populate(summary, shippingSummary, merchantStore, language);
        List<ShippingOption> options = quote.getShippingOptions();
        if (!CollectionUtils.isEmpty(options)) {
            for (ShippingOption shipOption : options) {
                StringBuilder moduleName = new StringBuilder();
                moduleName.append("module.shipping.").append(shipOption.getShippingModuleCode());
                String carrier = messages.getMessage(moduleName.toString(), new String[] { merchantStore.getStorename() }, locale);
                String note = messages.getMessage(moduleName.append(".note").toString(), locale, "");
                shipOption.setDescription(carrier);
                shipOption.setNote(note);
                // option name
                if (!StringUtils.isBlank(shipOption.getOptionCode())) {
                    // try to get the translate
                    StringBuilder optionCodeBuilder = new StringBuilder();
                    try {
                        optionCodeBuilder.append("module.shipping.").append(shipOption.getShippingModuleCode());
                        String optionName = messages.getMessage(optionCodeBuilder.toString(), new String[] { merchantStore.getStorename() }, locale);
                        shipOption.setOptionName(optionName);
                    } catch (Exception e) {
                        // label not found
                        LOGGER.warn("No shipping code found for " + optionCodeBuilder.toString());
                    }
                }
            }
            shippingSummary.setShippingOptions(options);
        }
        return shippingSummary;
    } catch (Exception e) {
        LOGGER.error("Error while getting shipping quote", e);
        try {
            response.sendError(503, "Error while getting shipping quote" + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : Locale(java.util.Locale) Customer(com.salesmanager.core.model.customer.Customer) ReadableShippingSummaryPopulator(com.salesmanager.shop.populator.order.ReadableShippingSummaryPopulator) ShippingOption(com.salesmanager.core.model.shipping.ShippingOption) ShippingQuote(com.salesmanager.core.model.shipping.ShippingQuote) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ReadableShippingSummary(com.salesmanager.shop.model.order.shipping.ReadableShippingSummary) Country(com.salesmanager.core.model.reference.country.Country) ReadableShippingSummary(com.salesmanager.shop.model.order.shipping.ReadableShippingSummary) Delivery(com.salesmanager.core.model.common.Delivery) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 20 with ShoppingCart

use of com.salesmanager.core.model.shoppingcart.ShoppingCart in project shopizer by shopizer-ecommerce.

the class OrderTotalApi method calculateTotal.

/**
 * Public api
 * @param id
 * @param quote
 * @param merchantStore
 * @param language
 * @param response
 * @return
 */
@RequestMapping(value = { "/cart/{code}/total" }, method = RequestMethod.GET)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "String", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "String", defaultValue = "en") })
public ReadableOrderTotalSummary calculateTotal(@PathVariable final String code, @RequestParam(value = "quote", required = false) Long quote, @ApiIgnore MerchantStore merchantStore, // possible postal code, province and country
@ApiIgnore Language language, HttpServletResponse response) {
    try {
        ShoppingCart shoppingCart = shoppingCartFacade.getShoppingCartModel(code, merchantStore);
        if (shoppingCart == null) {
            response.sendError(404, "Cart code " + code + " does not exist");
            return null;
        }
        ShippingSummary shippingSummary = null;
        // get shipping quote if asked for
        if (quote != null) {
            shippingSummary = shippingQuoteService.getShippingSummary(quote, merchantStore);
        }
        OrderTotalSummary orderTotalSummary = null;
        OrderSummary orderSummary = new OrderSummary();
        orderSummary.setShippingSummary(shippingSummary);
        List<ShoppingCartItem> itemsSet = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
        orderSummary.setProducts(itemsSet);
        orderTotalSummary = orderService.caculateOrderTotal(orderSummary, merchantStore, language);
        ReadableOrderTotalSummary returnSummary = new ReadableOrderTotalSummary();
        ReadableOrderSummaryPopulator populator = new ReadableOrderSummaryPopulator();
        populator.setMessages(messages);
        populator.setPricingService(pricingService);
        populator.populate(orderTotalSummary, returnSummary, merchantStore, language);
        return returnSummary;
    } catch (Exception e) {
        LOGGER.error("Error while calculating order summary", e);
        try {
            response.sendError(503, "Error while calculating order summary " + e.getMessage());
        } catch (Exception ignore) {
        }
        return null;
    }
}
Also used : ReadableOrderSummaryPopulator(com.salesmanager.shop.populator.order.ReadableOrderSummaryPopulator) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) OrderTotalSummary(com.salesmanager.core.model.order.OrderTotalSummary) OrderSummary(com.salesmanager.core.model.order.OrderSummary) ShippingSummary(com.salesmanager.core.model.shipping.ShippingSummary) ArrayList(java.util.ArrayList) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem) ReadableOrderTotalSummary(com.salesmanager.shop.model.order.ReadableOrderTotalSummary) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)38 ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)18 ServiceException (com.salesmanager.core.business.exception.ServiceException)11 Customer (com.salesmanager.core.model.customer.Customer)11 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)8 HashSet (java.util.HashSet)8 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)7 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)6 ShoppingCartDataPopulator (com.salesmanager.shop.populator.shoppingCart.ShoppingCartDataPopulator)6 ProductAttribute (com.salesmanager.core.model.catalog.product.attribute.ProductAttribute)5 FinalPrice (com.salesmanager.core.model.catalog.product.price.FinalPrice)5 ShippingSummary (com.salesmanager.core.model.shipping.ShippingSummary)5 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)5 LocalDate (java.time.LocalDate)5 ConversionException (com.salesmanager.core.business.exception.ConversionException)4