use of com.salesmanager.core.business.modules.integration.payment.impl.Stripe3Payment 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();
}
Aggregations