Search in sources :

Example 76 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class PaymentServiceImpl method initTransaction.

@Override
public Transaction initTransaction(Order order, Customer customer, Payment payment, MerchantStore store) throws ServiceException {
    Validate.notNull(store);
    Validate.notNull(payment);
    Validate.notNull(order);
    Validate.notNull(order.getTotal());
    payment.setCurrency(store.getCurrency());
    BigDecimal amount = order.getTotal();
    // must have a shipping module configured
    Map<String, IntegrationConfiguration> modules = this.getPaymentModulesConfigured(store);
    if (modules == null) {
        throw new ServiceException("No payment module configured");
    }
    IntegrationConfiguration configuration = modules.get(payment.getModuleName());
    if (configuration == null) {
        throw new ServiceException("Payment module " + payment.getModuleName() + " is not configured");
    }
    if (!configuration.isActive()) {
        throw new ServiceException("Payment module " + payment.getModuleName() + " is not active");
    }
    PaymentModule module = this.paymentModules.get(order.getPaymentModuleCode());
    if (module == null) {
        throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " does not exist");
    }
    IntegrationModule integrationModule = getPaymentMethodByCode(store, payment.getModuleName());
    return module.initTransaction(store, customer, amount, payment, configuration, integrationModule);
}
Also used : PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) ServiceException(com.salesmanager.core.business.exception.ServiceException) IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) BigDecimal(java.math.BigDecimal) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule)

Example 77 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException 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;
}
Also used : IntegrationConfiguration(com.salesmanager.core.model.system.IntegrationConfiguration) BigDecimal(java.math.BigDecimal) Date(java.util.Date) PaymentModule(com.salesmanager.core.modules.integration.payment.model.PaymentModule) ServiceException(com.salesmanager.core.business.exception.ServiceException) Transaction(com.salesmanager.core.model.payments.Transaction) OrderTotal(com.salesmanager.core.model.order.OrderTotal) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) IntegrationModule(com.salesmanager.core.model.system.IntegrationModule)

Example 78 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class TransactionServiceImpl method getCapturableTransaction.

@Override
public Transaction getCapturableTransaction(Order order) throws ServiceException {
    List<Transaction> transactions = transactionRepository.findByOrder(order.getId());
    ObjectMapper mapper = new ObjectMapper();
    Transaction capturable = null;
    for (Transaction transaction : transactions) {
        if (transaction.getTransactionType().name().equals(TransactionType.AUTHORIZE.name())) {
            if (!StringUtils.isBlank(transaction.getDetails())) {
                try {
                    @SuppressWarnings("unchecked") Map<String, String> objects = mapper.readValue(transaction.getDetails(), Map.class);
                    transaction.setTransactionDetails(objects);
                    capturable = transaction;
                } catch (Exception e) {
                    throw new ServiceException(e);
                }
            }
        }
        if (transaction.getTransactionType().name().equals(TransactionType.CAPTURE.name())) {
            break;
        }
        if (transaction.getTransactionType().name().equals(TransactionType.REFUND.name())) {
            break;
        }
    }
    return capturable;
}
Also used : Transaction(com.salesmanager.core.model.payments.Transaction) ServiceException(com.salesmanager.core.business.exception.ServiceException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServiceException(com.salesmanager.core.business.exception.ServiceException)

Example 79 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class ZoneServiceImpl method getZones.

@SuppressWarnings("unchecked")
@Override
public List<Zone> getZones(String countryCode, Language language) throws ServiceException {
    Validate.notNull(countryCode, "countryCode cannot be null");
    Validate.notNull(language, "Language cannot be null");
    List<Zone> zones = null;
    try {
        String cacheKey = ZONE_CACHE_PREFIX + countryCode + Constants.UNDERSCORE + language.getCode();
        zones = (List<Zone>) cache.getFromCache(cacheKey);
        if (zones == null) {
            zones = zoneRepository.listByLanguageAndCountry(countryCode, language.getId());
            // set names
            for (Zone zone : zones) {
                ZoneDescription description = zone.getDescriptions().get(0);
                zone.setName(description.getName());
            }
            cache.putInCache(zones, cacheKey);
        }
    } catch (Exception e) {
        LOGGER.error("getZones()", e);
    }
    return zones;
}
Also used : Zone(com.salesmanager.core.model.reference.zone.Zone) ServiceException(com.salesmanager.core.business.exception.ServiceException) ZoneDescription(com.salesmanager.core.model.reference.zone.ZoneDescription)

Example 80 with ServiceException

use of com.salesmanager.core.business.exception.ServiceException in project shopizer by shopizer-ecommerce.

the class SearchServiceImpl method index.

@Async
@SuppressWarnings("rawtypes")
public void index(MerchantStore store, Product product) throws ServiceException {
    if (configuration.getProperty(INDEX_PRODUCTS) == null || configuration.getProperty(INDEX_PRODUCTS).equals(Constants.FALSE)) {
        return;
    }
    FinalPrice price = pricingService.calculateProductPrice(product);
    Set<ProductDescription> descriptions = product.getDescriptions();
    for (ProductDescription description : descriptions) {
        StringBuilder collectionName = new StringBuilder();
        collectionName.append(PRODUCT_INDEX_NAME).append(UNDERSCORE).append(description.getLanguage().getCode()).append(UNDERSCORE).append(store.getCode().toLowerCase());
        IndexProduct index = new IndexProduct();
        index.setId(String.valueOf(product.getId()));
        index.setStore(store.getCode().toLowerCase());
        index.setLang(description.getLanguage().getCode());
        index.setAvailable(product.isAvailable());
        index.setDescription(description.getDescription());
        index.setName(description.getName());
        if (product.getManufacturer() != null) {
            index.setManufacturer(String.valueOf(product.getManufacturer().getId()));
        }
        if (price != null) {
            index.setPrice(price.getFinalPrice().doubleValue());
        }
        index.setHighlight(description.getProductHighlight());
        if (!StringUtils.isBlank(description.getMetatagKeywords())) {
            String[] tags = description.getMetatagKeywords().split(",");
            @SuppressWarnings("unchecked") List<String> tagsList = new ArrayList(Arrays.asList(tags));
            index.setTags(tagsList);
        }
        Set<Category> categories = product.getCategories();
        if (!CollectionUtils.isEmpty(categories)) {
            List<String> categoryList = new ArrayList<String>();
            for (Category category : categories) {
                categoryList.add(category.getCode());
            }
            index.setCategories(categoryList);
        }
        String jsonString = index.toJSONString();
        try {
            searchService.index(jsonString, collectionName.toString());
        } catch (Exception e) {
            throw new ServiceException("Cannot index product id [" + product.getId() + "], " + e.getMessage(), e);
        }
    }
}
Also used : Category(com.salesmanager.core.model.catalog.category.Category) ArrayList(java.util.ArrayList) IndexProduct(com.salesmanager.core.model.search.IndexProduct) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductDescription(com.salesmanager.core.model.catalog.product.description.ProductDescription) FinalPrice(com.salesmanager.core.model.catalog.product.price.FinalPrice) Async(org.springframework.scheduling.annotation.Async)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)230 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)88 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)54 ArrayList (java.util.ArrayList)45 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)44 Language (com.salesmanager.core.model.reference.language.Language)38 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)27 IntegrationConfiguration (com.salesmanager.core.model.system.IntegrationConfiguration)22 Autowired (org.springframework.beans.factory.annotation.Autowired)21 OutputContentFile (com.salesmanager.core.model.content.OutputContentFile)20 Product (com.salesmanager.core.model.catalog.product.Product)19 HashMap (java.util.HashMap)19 Map (java.util.Map)18 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)17 InputContentFile (com.salesmanager.core.model.content.InputContentFile)17 Optional (java.util.Optional)17 IntegrationModule (com.salesmanager.core.model.system.IntegrationModule)16