Search in sources :

Example 36 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class OrderRESTController method createOrder.

/**
 * This method is for adding order to the system. Generally used for the purpose of migration only
 * This method won't process any payment nor create transactions
 * @param store
 * @param order
 * @param request
 * @param response
 * @return
 * @throws Exception
 * Use v1 methods
 */
@RequestMapping(value = "/{store}/order", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
@Deprecated
public PersistableOrder createOrder(@PathVariable final String store, @Valid @RequestBody PersistableOrder order, HttpServletRequest request, HttpServletResponse response) throws Exception {
    MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    if (merchantStore != null) {
        if (!merchantStore.getCode().equals(store)) {
            merchantStore = null;
        }
    }
    if (merchantStore == null) {
        merchantStore = merchantStoreService.getByCode(store);
    }
    if (merchantStore == null) {
        LOGGER.error("Merchant store is null for code " + store);
        response.sendError(503, "Merchant store is null for code " + store);
        return null;
    }
    PersistableCustomer cust = order.getCustomer();
    if (cust != null) {
        Customer customer = new Customer();
        /*			CustomerPopulator populator = new CustomerPopulator();
			populator.setCountryService(countryService);
			populator.setCustomerOptionService(customerOptionService);
			populator.setCustomerOptionValueService(customerOptionValueService);
			populator.setLanguageService(languageService);
			populator.setZoneService(zoneService);
			populator.setGroupService(groupService);*/
        customerPopulator.populate(cust, customer, merchantStore, merchantStore.getDefaultLanguage());
        customerService.save(customer);
        cust.setId(customer.getId());
    }
    Order modelOrder = new Order();
    PersistableOrderPopulator populator = new PersistableOrderPopulator();
    populator.setDigitalProductService(digitalProductService);
    populator.setProductAttributeService(productAttributeService);
    populator.setProductService(productService);
    populator.populate(order, modelOrder, merchantStore, merchantStore.getDefaultLanguage());
    orderService.save(modelOrder);
    order.setId(modelOrder.getId());
    return order;
}
Also used : Order(com.salesmanager.core.model.order.Order) PersistableOrder(com.salesmanager.shop.model.order.v0.PersistableOrder) PersistableOrderPopulator(com.salesmanager.shop.populator.order.PersistableOrderPopulator) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore)

Example 37 with Customer

use of com.salesmanager.core.model.customer.Customer 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 38 with Customer

use of com.salesmanager.core.model.customer.Customer 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 39 with Customer

use of com.salesmanager.core.model.customer.Customer 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 40 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class OrderApi method list.

/**
 * List orders for authenticated customers
 *
 * @param start
 * @param count
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = { "/auth/orders" }, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public ReadableOrderList list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "count", required = false) Integer count, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Principal principal = request.getUserPrincipal();
    String userName = principal.getName();
    Customer customer = customerService.getByNick(userName);
    if (customer == null) {
        response.sendError(401, "Error while listing orders, customer not authorized");
        return null;
    }
    if (page == null) {
        page = new Integer(0);
    }
    if (count == null) {
        count = new Integer(100);
    }
    ReadableCustomer readableCustomer = new ReadableCustomer();
    ReadableCustomerPopulator customerPopulator = new ReadableCustomerPopulator();
    customerPopulator.populate(customer, readableCustomer, merchantStore, language);
    ReadableOrderList returnList = orderFacade.getReadableOrderList(merchantStore, customer, page, count, language);
    if (returnList == null) {
        returnList = new ReadableOrderList();
    }
    List<ReadableOrder> orders = returnList.getOrders();
    if (!CollectionUtils.isEmpty(orders)) {
        for (ReadableOrder order : orders) {
            order.setCustomer(readableCustomer);
        }
    }
    return returnList;
}
Also used : ReadableOrderList(com.salesmanager.shop.model.order.v0.ReadableOrderList) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Principal(java.security.Principal) ReadableCustomerPopulator(com.salesmanager.shop.populator.customer.ReadableCustomerPopulator) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) 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)71 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)33 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)32 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 Language (com.salesmanager.core.model.reference.language.Language)26 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)17 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)17 ConversionException (com.salesmanager.core.business.exception.ConversionException)16 ServiceException (com.salesmanager.core.business.exception.ServiceException)16 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)16 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)12 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)12 Authentication (org.springframework.security.core.Authentication)12 Date (java.util.Date)11 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)10 ArrayList (java.util.ArrayList)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 Product (com.salesmanager.core.model.catalog.product.Product)9 Country (com.salesmanager.core.model.reference.country.Country)9