use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.
the class ShoppingOrderPaymentController method paymentAction.
/**
* Recalculates shipping and tax following a change in country or province
*
* @param order
* @param request
* @param response
* @param locale
* @return
* @throws Exception
*/
@RequestMapping(value = { "/order/payment/{action}/{paymentmethod}.html" }, method = RequestMethod.POST)
@ResponseBody
public String paymentAction(@Valid @ModelAttribute(value = "order") ShopOrder order, @PathVariable String action, @PathVariable String paymentmethod, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
Language language = (Language) request.getAttribute("LANGUAGE");
MerchantStore store = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
String shoppingCartCode = getSessionAttribute(Constants.SHOPPING_CART, request);
Validate.notNull(shoppingCartCode, "shoppingCartCode does not exist in the session");
AjaxResponse ajaxResponse = new AjaxResponse();
try {
com.salesmanager.core.model.shoppingcart.ShoppingCart cart = shoppingCartFacade.getShoppingCartModel(shoppingCartCode, store);
Set<ShoppingCartItem> items = cart.getLineItems();
List<ShoppingCartItem> cartItems = new ArrayList<ShoppingCartItem>(items);
order.setShoppingCartItems(cartItems);
// validate order first
Map<String, String> messages = new TreeMap<String, String>();
orderFacade.validateOrder(order, new BeanPropertyBindingResult(order, "order"), messages, store, locale);
if (CollectionUtils.isNotEmpty(messages.values())) {
for (String key : messages.keySet()) {
String value = messages.get(key);
ajaxResponse.addValidationMessage(key, value);
}
ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_VALIDATION_FAILED);
return ajaxResponse.toJSONString();
}
IntegrationConfiguration config = paymentService.getPaymentConfiguration(order.getPaymentModule(), store);
IntegrationModule integrationModule = paymentService.getPaymentMethodByCode(store, order.getPaymentModule());
// OrderTotalSummary orderTotalSummary =
// orderFacade.calculateOrderTotal(store, order, language);
OrderTotalSummary orderTotalSummary = super.getSessionAttribute(Constants.ORDER_SUMMARY, request);
if (orderTotalSummary == null) {
orderTotalSummary = orderFacade.calculateOrderTotal(store, order, language);
super.setSessionAttribute(Constants.ORDER_SUMMARY, orderTotalSummary, request);
}
ShippingSummary summary = (ShippingSummary) request.getSession().getAttribute("SHIPPING_SUMMARY");
if (summary != null) {
order.setShippingSummary(summary);
}
if (action.equals(INIT_ACTION)) {
if (paymentmethod.equals("PAYPAL")) {
try {
PaymentModule module = paymentService.getPaymentModule("paypal-express-checkout");
PayPalExpressCheckoutPayment p = (PayPalExpressCheckoutPayment) module;
PaypalPayment payment = new PaypalPayment();
payment.setCurrency(store.getCurrency());
Transaction transaction = p.initPaypalTransaction(store, cartItems, orderTotalSummary, payment, config, integrationModule);
transactionService.create(transaction);
super.setSessionAttribute(Constants.INIT_TRANSACTION_KEY, transaction, request);
StringBuilder urlAppender = new StringBuilder();
urlAppender.append(coreConfiguration.getProperty("PAYPAL_EXPRESSCHECKOUT_REGULAR"));
urlAppender.append(transaction.getTransactionDetails().get("TOKEN"));
if (config.getEnvironment().equals(com.salesmanager.core.business.constants.Constants.PRODUCTION_ENVIRONMENT)) {
StringBuilder url = new StringBuilder().append(coreConfiguration.getProperty("PAYPAL_EXPRESSCHECKOUT_PRODUCTION")).append(urlAppender.toString());
ajaxResponse.addEntry("url", url.toString());
} else {
StringBuilder url = new StringBuilder().append(coreConfiguration.getProperty("PAYPAL_EXPRESSCHECKOUT_SANDBOX")).append(urlAppender.toString());
ajaxResponse.addEntry("url", url.toString());
}
// keep order in session when user comes back from pp
super.setSessionAttribute(Constants.ORDER, order, request);
ajaxResponse.setStatus(AjaxResponse.RESPONSE_OPERATION_COMPLETED);
} catch (Exception e) {
ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
}
} else if (paymentmethod.equals("stripe3")) {
try {
PaymentModule module = paymentService.getPaymentModule(paymentmethod);
Stripe3Payment p = (Stripe3Payment) module;
PaypalPayment payment = new PaypalPayment();
payment.setCurrency(store.getCurrency());
Transaction transaction = p.initTransaction(store, null, orderTotalSummary.getTotal(), null, config, integrationModule);
transactionService.create(transaction);
super.setSessionAttribute(Constants.INIT_TRANSACTION_KEY, transaction, request);
// keep order in session when user comes back from pp
super.setSessionAttribute(Constants.ORDER, order, request);
ajaxResponse.setStatus(AjaxResponse.RESPONSE_OPERATION_COMPLETED);
ajaxResponse.setDataMap(transaction.getTransactionDetails());
} catch (Exception e) {
ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
}
}
}
} catch (Exception e) {
LOGGER.error("Error while performing payment action " + action + " for payment method " + paymentmethod, e);
ajaxResponse.setErrorMessage(e);
ajaxResponse.setStatus(AjaxResponse.RESPONSE_STATUS_FAIURE);
}
return ajaxResponse.toJSONString();
}
use of com.salesmanager.core.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.
the class PaymentServiceImpl method savePaymentModuleConfiguration.
@Override
public void savePaymentModuleConfiguration(IntegrationConfiguration configuration, MerchantStore store) throws ServiceException {
// validate entries
try {
String moduleCode = configuration.getModuleCode();
PaymentModule module = paymentModules.get(moduleCode);
if (module == null) {
throw new ServiceException("Payment module " + moduleCode + " does not exist");
}
module.validateModuleConfiguration(configuration, store);
} catch (IntegrationException ie) {
throw ie;
}
try {
Map<String, IntegrationConfiguration> modules = new HashMap<String, IntegrationConfiguration>();
MerchantConfiguration merchantConfiguration = merchantConfigurationService.getMerchantConfiguration(Constants.PAYMENT_MODULES, store);
if (merchantConfiguration != null) {
if (!StringUtils.isBlank(merchantConfiguration.getValue())) {
String decrypted = encryption.decrypt(merchantConfiguration.getValue());
modules = ConfigurationModulesLoader.loadIntegrationConfigurations(decrypted);
}
} else {
merchantConfiguration = new MerchantConfiguration();
merchantConfiguration.setMerchantStore(store);
merchantConfiguration.setKey(Constants.PAYMENT_MODULES);
}
modules.put(configuration.getModuleCode(), configuration);
String configs = ConfigurationModulesLoader.toJSONString(modules);
String encrypted = encryption.encrypt(configs);
merchantConfiguration.setValue(encrypted);
merchantConfigurationService.saveOrUpdate(merchantConfiguration);
} catch (Exception e) {
throw new ServiceException(e);
}
}
use of com.salesmanager.core.modules.integration.payment.model.PaymentModule 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);
}
use of com.salesmanager.core.modules.integration.payment.model.PaymentModule 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.modules.integration.payment.model.PaymentModule in project shopizer by shopizer-ecommerce.
the class PaymentServiceImpl method processCapturePayment.
@Override
public Transaction processCapturePayment(Order order, Customer customer, MerchantStore store) throws ServiceException {
Validate.notNull(customer);
Validate.notNull(store);
Validate.notNull(order);
// 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(order.getPaymentModuleCode());
if (configuration == null) {
throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " is not configured");
}
if (!configuration.isActive()) {
throw new ServiceException("Payment module " + order.getPaymentModuleCode() + " 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, order.getPaymentModuleCode());
// TransactionType transactionType = payment.getTransactionType();
// get the previous transaction
Transaction trx = transactionService.getCapturableTransaction(order);
if (trx == null) {
throw new ServiceException("No capturable transaction for order id " + order.getId());
}
Transaction transaction = module.capture(store, customer, order, trx, configuration, integrationModule);
transaction.setOrder(order);
transactionService.create(transaction);
OrderStatusHistory orderHistory = new OrderStatusHistory();
orderHistory.setOrder(order);
orderHistory.setStatus(OrderStatus.PROCESSED);
orderHistory.setDateAdded(new Date());
orderService.addOrderStatusHistory(order, orderHistory);
order.setStatus(OrderStatus.PROCESSED);
orderService.saveOrUpdate(order);
return transaction;
}
Aggregations