use of com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder 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.v1.PersistableAnonymousOrder 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);
}
}
Aggregations