use of com.salesmanager.core.model.order.orderstatus.OrderStatusHistory in project shopizer by shopizer-ecommerce.
the class PaymentServiceImpl method processRefund.
@Override
public Transaction processRefund(Order order, Customer customer, MerchantStore store, BigDecimal amount) throws ServiceException {
Validate.notNull(customer);
Validate.notNull(store);
Validate.notNull(amount);
Validate.notNull(order);
Validate.notNull(order.getOrderTotal());
BigDecimal orderTotal = order.getTotal();
if (amount.doubleValue() > orderTotal.doubleValue()) {
throw new ServiceException("Invalid amount, the refunded amount is greater than the total allowed");
}
String module = order.getPaymentModuleCode();
Map<String, IntegrationConfiguration> modules = this.getPaymentModulesConfigured(store);
if (modules == null) {
throw new ServiceException("No payment module configured");
}
IntegrationConfiguration configuration = modules.get(module);
if (configuration == null) {
throw new ServiceException("Payment module " + module + " is not configured");
}
PaymentModule paymentModule = this.paymentModules.get(module);
if (paymentModule == null) {
throw new ServiceException("Payment module " + paymentModule + " does not exist");
}
boolean partial = false;
if (amount.doubleValue() != order.getTotal().doubleValue()) {
partial = true;
}
IntegrationModule integrationModule = getPaymentMethodByCode(store, module);
// get the associated transaction
Transaction refundable = transactionService.getRefundableTransaction(order);
if (refundable == null) {
throw new ServiceException("No refundable transaction for this order");
}
Transaction transaction = paymentModule.refund(partial, store, refundable, order, amount, configuration, integrationModule);
transaction.setOrder(order);
transactionService.create(transaction);
OrderTotal refund = new OrderTotal();
refund.setModule(Constants.OT_REFUND_MODULE_CODE);
refund.setText(Constants.OT_REFUND_MODULE_CODE);
refund.setTitle(Constants.OT_REFUND_MODULE_CODE);
refund.setOrderTotalCode(Constants.OT_REFUND_MODULE_CODE);
refund.setOrderTotalType(OrderTotalType.REFUND);
refund.setValue(amount);
refund.setSortOrder(100);
refund.setOrder(order);
order.getOrderTotal().add(refund);
// update order total
orderTotal = orderTotal.subtract(amount);
// update ordertotal refund
Set<OrderTotal> totals = order.getOrderTotal();
for (OrderTotal total : totals) {
if (total.getModule().equals(Constants.OT_TOTAL_MODULE_CODE)) {
total.setValue(orderTotal);
}
}
order.setTotal(orderTotal);
order.setStatus(OrderStatus.REFUNDED);
OrderStatusHistory orderHistory = new OrderStatusHistory();
orderHistory.setOrder(order);
orderHistory.setStatus(OrderStatus.REFUNDED);
orderHistory.setDateAdded(new Date());
order.getOrderHistory().add(orderHistory);
orderService.saveOrUpdate(order);
return transaction;
}
use of com.salesmanager.core.model.order.orderstatus.OrderStatusHistory 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.core.model.order.orderstatus.OrderStatusHistory in project shopizer by shopizer-ecommerce.
the class PersistableOrderPopulator method populate.
@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
Validate.notNull(productService, "productService must be set");
Validate.notNull(digitalProductService, "digitalProductService must be set");
Validate.notNull(productAttributeService, "productAttributeService must be set");
Validate.notNull(customerService, "customerService must be set");
Validate.notNull(countryService, "countryService must be set");
Validate.notNull(zoneService, "zoneService must be set");
Validate.notNull(currencyService, "currencyService must be set");
try {
Map<String, Country> countriesMap = countryService.getCountriesMap(language);
Map<String, Zone> zonesMap = zoneService.getZones(language);
/**
* customer *
*/
PersistableCustomer customer = source.getCustomer();
if (customer != null) {
if (customer.getId() != null && customer.getId() > 0) {
Customer modelCustomer = customerService.getById(customer.getId());
if (modelCustomer == null) {
throw new ConversionException("Customer id " + customer.getId() + " does not exists");
}
if (modelCustomer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
throw new ConversionException("Customer id " + customer.getId() + " does not exists for store " + store.getCode());
}
target.setCustomerId(modelCustomer.getId());
target.setBilling(modelCustomer.getBilling());
target.setDelivery(modelCustomer.getDelivery());
target.setCustomerEmailAddress(source.getCustomer().getEmailAddress());
}
}
target.setLocale(LocaleUtils.getLocale(store));
CreditCard creditCard = source.getCreditCard();
if (creditCard != null) {
String maskedNumber = CreditCardUtils.maskCardNumber(creditCard.getCcNumber());
creditCard.setCcNumber(maskedNumber);
target.setCreditCard(creditCard);
}
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());
}
target.setCurrency(currency);
target.setDatePurchased(source.getDatePurchased());
// target.setCurrency(store.getCurrency());
target.setCurrencyValue(new BigDecimal(0));
target.setMerchant(store);
target.setStatus(source.getOrderStatus());
target.setPaymentModuleCode(source.getPaymentModule());
target.setPaymentType(source.getPaymentType());
target.setShippingModuleCode(source.getShippingModule());
target.setCustomerAgreement(source.isCustomerAgreed());
target.setConfirmedAddress(source.isConfirmedAddress());
if (source.getPreviousOrderStatus() != null) {
List<OrderStatus> orderStatusList = source.getPreviousOrderStatus();
for (OrderStatus status : orderStatusList) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(status);
statusHistory.setOrder(target);
target.getOrderHistory().add(statusHistory);
}
}
if (!StringUtils.isBlank(source.getComments())) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(null);
statusHistory.setOrder(target);
statusHistory.setComments(source.getComments());
target.getOrderHistory().add(statusHistory);
}
List<PersistableOrderProduct> products = source.getOrderProductItems();
if (CollectionUtils.isEmpty(products)) {
throw new ConversionException("Requires at least 1 PersistableOrderProduct");
}
com.salesmanager.shop.populator.order.PersistableOrderProductPopulator orderProductPopulator = new PersistableOrderProductPopulator();
orderProductPopulator.setProductAttributeService(productAttributeService);
orderProductPopulator.setProductService(productService);
orderProductPopulator.setDigitalProductService(digitalProductService);
for (PersistableOrderProduct orderProduct : products) {
OrderProduct modelOrderProduct = new OrderProduct();
orderProductPopulator.populate(orderProduct, modelOrderProduct, store, language);
target.getOrderProducts().add(modelOrderProduct);
}
List<OrderTotal> orderTotals = source.getTotals();
if (CollectionUtils.isNotEmpty(orderTotals)) {
for (OrderTotal total : orderTotals) {
com.salesmanager.core.model.order.OrderTotal totalModel = new com.salesmanager.core.model.order.OrderTotal();
totalModel.setModule(total.getModule());
totalModel.setOrder(target);
totalModel.setOrderTotalCode(total.getCode());
totalModel.setTitle(total.getTitle());
totalModel.setValue(total.getValue());
target.getOrderTotal().add(totalModel);
}
}
} catch (Exception e) {
throw new ConversionException(e);
}
return target;
}
use of com.salesmanager.core.model.order.orderstatus.OrderStatusHistory in project shopizer by shopizer-ecommerce.
the class OrderFacadeImpl method processOrderModel.
/**
* Commit an order
* @param order
* @param customer
* @param transaction
* @param store
* @param language
* @return
* @throws ServiceException
*/
private Order processOrderModel(ShopOrder order, Customer customer, Transaction transaction, MerchantStore store, Language language) throws ServiceException {
try {
if (order.isShipToBillingAdress()) {
// customer shipping is billing
PersistableCustomer orderCustomer = order.getCustomer();
Address billing = orderCustomer.getBilling();
orderCustomer.setDelivery(billing);
}
Order modelOrder = new Order();
modelOrder.setDatePurchased(new Date());
modelOrder.setBilling(customer.getBilling());
modelOrder.setDelivery(customer.getDelivery());
modelOrder.setPaymentModuleCode(order.getPaymentModule());
modelOrder.setPaymentType(PaymentType.valueOf(order.getPaymentMethodType()));
modelOrder.setShippingModuleCode(order.getShippingModule());
modelOrder.setCustomerAgreement(order.isCustomerAgreed());
// set the store
modelOrder.setLocale(LocaleUtils.getLocale(store));
// locale based
// on the
// country for
// order $
// formatting
List<ShoppingCartItem> shoppingCartItems = order.getShoppingCartItems();
Set<OrderProduct> orderProducts = new LinkedHashSet<OrderProduct>();
if (!StringUtils.isBlank(order.getComments())) {
OrderStatusHistory statusHistory = new OrderStatusHistory();
statusHistory.setStatus(OrderStatus.ORDERED);
statusHistory.setOrder(modelOrder);
statusHistory.setDateAdded(new Date());
statusHistory.setComments(order.getComments());
modelOrder.getOrderHistory().add(statusHistory);
}
OrderProductPopulator orderProductPopulator = new OrderProductPopulator();
orderProductPopulator.setDigitalProductService(digitalProductService);
orderProductPopulator.setProductAttributeService(productAttributeService);
orderProductPopulator.setProductService(productService);
String shoppingCartCode = null;
for (ShoppingCartItem item : shoppingCartItems) {
if (shoppingCartCode == null && item.getShoppingCart() != null) {
shoppingCartCode = item.getShoppingCart().getShoppingCartCode();
}
/**
* Before processing order quantity of item must be > 0
*/
Product product = productService.getById(item.getProductId());
if (product == null) {
throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
}
LOGGER.debug("Validate inventory");
for (ProductAvailability availability : product.getAvailabilities()) {
if (availability.getRegion().equals(Constants.ALL_REGIONS)) {
int qty = availability.getProductQuantity();
if (qty < item.getQuantity()) {
throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
}
}
}
OrderProduct orderProduct = new OrderProduct();
orderProduct = orderProductPopulator.populate(item, orderProduct, store, language);
orderProduct.setOrder(modelOrder);
orderProducts.add(orderProduct);
}
modelOrder.setOrderProducts(orderProducts);
OrderTotalSummary summary = order.getOrderTotalSummary();
List<com.salesmanager.core.model.order.OrderTotal> totals = summary.getTotals();
// re-order totals
Collections.sort(totals, new Comparator<com.salesmanager.core.model.order.OrderTotal>() {
public int compare(com.salesmanager.core.model.order.OrderTotal x, com.salesmanager.core.model.order.OrderTotal y) {
if (x.getSortOrder() == y.getSortOrder())
return 0;
return x.getSortOrder() < y.getSortOrder() ? -1 : 1;
}
});
Set<com.salesmanager.core.model.order.OrderTotal> modelTotals = new LinkedHashSet<com.salesmanager.core.model.order.OrderTotal>();
for (com.salesmanager.core.model.order.OrderTotal total : totals) {
total.setOrder(modelOrder);
modelTotals.add(total);
}
modelOrder.setOrderTotal(modelTotals);
modelOrder.setTotal(order.getOrderTotalSummary().getTotal());
// order misc objects
modelOrder.setCurrency(store.getCurrency());
modelOrder.setMerchant(store);
// customer object
orderCustomer(customer, modelOrder, language);
// populate shipping information
if (!StringUtils.isBlank(order.getShippingModule())) {
modelOrder.setShippingModuleCode(order.getShippingModule());
}
String paymentType = order.getPaymentMethodType();
Payment payment = new Payment();
payment.setPaymentType(PaymentType.valueOf(paymentType));
payment.setAmount(order.getOrderTotalSummary().getTotal());
payment.setModuleName(order.getPaymentModule());
payment.setCurrency(modelOrder.getCurrency());
if (order.getPayment() != null && order.getPayment().get("paymentToken") != null) {
// set
// token
String paymentToken = order.getPayment().get("paymentToken");
Map<String, String> paymentMetaData = new HashMap<String, String>();
payment.setPaymentMetaData(paymentMetaData);
paymentMetaData.put("paymentToken", paymentToken);
}
if (PaymentType.CREDITCARD.name().equals(paymentType)) {
payment = new CreditCardPayment();
((CreditCardPayment) payment).setCardOwner(order.getPayment().get("creditcard_card_holder"));
((CreditCardPayment) payment).setCredidCardValidationNumber(order.getPayment().get("creditcard_card_cvv"));
((CreditCardPayment) payment).setCreditCardNumber(order.getPayment().get("creditcard_card_number"));
((CreditCardPayment) payment).setExpirationMonth(order.getPayment().get("creditcard_card_expirationmonth"));
((CreditCardPayment) payment).setExpirationYear(order.getPayment().get("creditcard_card_expirationyear"));
Map<String, String> paymentMetaData = order.getPayment();
payment.setPaymentMetaData(paymentMetaData);
payment.setPaymentType(PaymentType.valueOf(paymentType));
payment.setAmount(order.getOrderTotalSummary().getTotal());
payment.setModuleName(order.getPaymentModule());
payment.setCurrency(modelOrder.getCurrency());
CreditCardType creditCardType = null;
String cardType = order.getPayment().get("creditcard_card_type");
// supported credit cards
if (CreditCardType.AMEX.name().equalsIgnoreCase(cardType)) {
creditCardType = CreditCardType.AMEX;
} else if (CreditCardType.VISA.name().equalsIgnoreCase(cardType)) {
creditCardType = CreditCardType.VISA;
} else if (CreditCardType.MASTERCARD.name().equalsIgnoreCase(cardType)) {
creditCardType = CreditCardType.MASTERCARD;
} else if (CreditCardType.DINERS.name().equalsIgnoreCase(cardType)) {
creditCardType = CreditCardType.DINERS;
} else if (CreditCardType.DISCOVERY.name().equalsIgnoreCase(cardType)) {
creditCardType = CreditCardType.DISCOVERY;
}
((CreditCardPayment) payment).setCreditCard(creditCardType);
if (creditCardType != null) {
CreditCard cc = new CreditCard();
cc.setCardType(creditCardType);
cc.setCcCvv(((CreditCardPayment) payment).getCredidCardValidationNumber());
cc.setCcOwner(((CreditCardPayment) payment).getCardOwner());
cc.setCcExpires(((CreditCardPayment) payment).getExpirationMonth() + "-" + ((CreditCardPayment) payment).getExpirationYear());
// hash credit card number
if (!StringUtils.isBlank(cc.getCcNumber())) {
String maskedNumber = CreditCardUtils.maskCardNumber(order.getPayment().get("creditcard_card_number"));
cc.setCcNumber(maskedNumber);
modelOrder.setCreditCard(cc);
}
}
}
if (PaymentType.PAYPAL.name().equals(paymentType)) {
// check for previous transaction
if (transaction == null) {
throw new ServiceException("payment.error");
}
payment = new com.salesmanager.core.model.payments.PaypalPayment();
((com.salesmanager.core.model.payments.PaypalPayment) payment).setPayerId(transaction.getTransactionDetails().get("PAYERID"));
((com.salesmanager.core.model.payments.PaypalPayment) payment).setPaymentToken(transaction.getTransactionDetails().get("TOKEN"));
}
modelOrder.setShoppingCartCode(shoppingCartCode);
modelOrder.setPaymentModuleCode(order.getPaymentModule());
payment.setModuleName(order.getPaymentModule());
if (transaction != null) {
orderService.processOrder(modelOrder, customer, order.getShoppingCartItems(), summary, payment, store);
} else {
orderService.processOrder(modelOrder, customer, order.getShoppingCartItems(), summary, payment, transaction, store);
}
return modelOrder;
} catch (ServiceException se) {
// may be invalid credit card
throw se;
} catch (Exception e) {
throw new ServiceException(e);
}
}
use of com.salesmanager.core.model.order.orderstatus.OrderStatusHistory in project shopizer by shopizer-ecommerce.
the class OrderFacadeImpl method getReadableOrderHistory.
@Override
public List<ReadableOrderStatusHistory> getReadableOrderHistory(Long orderId, MerchantStore store, Language language) {
Order order = orderService.getOrder(orderId, store);
if (order == null) {
throw new ResourceNotFoundException("Order id [" + orderId + "] not found for merchand [" + store.getId() + "]");
}
Set<OrderStatusHistory> historyList = order.getOrderHistory();
List<ReadableOrderStatusHistory> returnList = historyList.stream().map(f -> mapToReadbleOrderStatusHistory(f)).collect(Collectors.toList());
return returnList;
}
Aggregations