use of com.salesmanager.shop.model.order.v0.PersistableOrder in project shopizer by shopizer-ecommerce.
the class PersistableOrderApiPopulator method populate.
@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
/* Validate.notNull(currencyService,"currencyService must be set");
Validate.notNull(customerService,"customerService must be set");
Validate.notNull(shoppingCartService,"shoppingCartService must be set");
Validate.notNull(productService,"productService must be set");
Validate.notNull(productAttributeService,"productAttributeService must be set");
Validate.notNull(digitalProductService,"digitalProductService must be set");*/
Validate.notNull(source.getPayment(), "Payment cannot be null");
try {
if (target == null) {
target = new Order();
}
// target.setLocale(LocaleUtils.getLocale(store));
target.setLocale(LocaleUtils.getLocale(store));
Currency currency = null;
try {
currency = currencyService.getByCode(source.getCurrency());
} catch (Exception e) {
throw new ConversionException("Currency not found for code " + source.getCurrency());
}
if (currency == null) {
throw new ConversionException("Currency not found for code " + source.getCurrency());
}
// Customer
Customer customer = null;
if (source.getCustomerId() != null && source.getCustomerId().longValue() > 0) {
Long customerId = source.getCustomerId();
customer = customerService.getById(customerId);
if (customer == null) {
throw new ConversionException("Curstomer with id " + source.getCustomerId() + " does not exist");
}
target.setCustomerId(customerId);
} else {
if (source instanceof PersistableAnonymousOrder) {
PersistableCustomer persistableCustomer = ((PersistableAnonymousOrder) source).getCustomer();
customer = new Customer();
customer = customerPopulator.populate(persistableCustomer, customer, store, language);
} else {
throw new ConversionException("Curstomer details or id not set in request");
}
}
target.setCustomerEmailAddress(customer.getEmailAddress());
Delivery delivery = customer.getDelivery();
target.setDelivery(delivery);
Billing billing = customer.getBilling();
target.setBilling(billing);
if (source.getAttributes() != null && source.getAttributes().size() > 0) {
Set<OrderAttribute> attrs = new HashSet<OrderAttribute>();
for (com.salesmanager.shop.model.order.OrderAttribute attribute : source.getAttributes()) {
OrderAttribute attr = new OrderAttribute();
attr.setKey(attribute.getKey());
attr.setValue(attribute.getValue());
attr.setOrder(target);
attrs.add(attr);
}
target.setOrderAttributes(attrs);
}
target.setDatePurchased(new Date());
target.setCurrency(currency);
target.setCurrencyValue(new BigDecimal(0));
target.setMerchant(store);
target.setChannel(OrderChannel.API);
// need this
target.setStatus(OrderStatus.ORDERED);
target.setPaymentModuleCode(source.getPayment().getPaymentModule());
target.setPaymentType(PaymentType.valueOf(source.getPayment().getPaymentType()));
target.setCustomerAgreement(source.isCustomerAgreement());
// force this to true, cannot perform this activity from the API
target.setConfirmedAddress(true);
if (!StringUtils.isBlank(source.getComments())) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(null);
statusHistory.setOrder(target);
statusHistory.setComments(source.getComments());
target.getOrderHistory().add(statusHistory);
}
return target;
} catch (Exception e) {
throw new ConversionException(e);
}
}
use of com.salesmanager.shop.model.order.v0.PersistableOrder 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;
}
use of com.salesmanager.shop.model.order.v0.PersistableOrder 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;
}
}
Aggregations