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;
}
}
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;
}
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;
}
}
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;
}
}
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;
}
}
Aggregations