'use strict';
/**
* Controller for ECM_PPAL.
*
* @module controllers/ECM_PPAL
*/
var app = require('sitegenesis_controllers/cartridge/scripts/app');
var guard = require('sitegenesis_controllers/cartridge/scripts/guard');
/* API Includes */
var PaymentMgr = require('dw/order/PaymentMgr');
var OrderMgr = require('dw/order/OrderMgr');
var Transaction = require('dw/system/Transaction');
var URLUtils = require('dw/web/URLUtils');
var libPayment = require('int_payment_core/cartridge/scripts/lib/libPayment.ds')
var parameterMap = request.httpParameterMap;
var CheckEcommercePayment = require('int_payment_controllers/cartridge/controllers/PAYMENT_ECOMMERCE').CheckEcommercePayment;
var SubmitImpl = app.getModel('Order').submit;
/**
* Accept Ecommerce Return Url
*/
function EcommAccept() {
var res;
var order = OrderMgr.getOrder(parameterMap.orderNo.stringValue);
var ShaCode = libPayment.createShaForUrls(order);
if(ShaCode == parameterMap.tn.toString()) {
Transaction.wrap(function () {
res = libPayment.SendPayPalInfo(order, request.locale, parameterMap);
});
if(res.success) {
if(order.custom.payment_authorizationStatus == "AUTHORISED") {
Transaction.wrap(function () {
order.custom.ecommerceResponseCount = 1;
order.paymentStatus = dw.order.Order.PAYMENT_STATUS_PAID;
libPayment.savePaypalAlias();
});
}
SubmitImpl(order,true);
} else {
var ErrorStatus = res.ErrorStatus;
var res = CheckEcommercePayment();
if(res.EndNode == 'success') {
Transaction.wrap(function () {
res.Order.paymentStatus = dw.order.Order.PAYMENT_STATUS_PAID;
libPayment.savePaypalAlias();
});
SubmitImpl(res.Order, true);
} else if(res.EndNode == 'error' || res.EndNode == 'declined') {
var PlaceOrderError = new dw.system.Status(dw.system.Status.ERROR, "confirm.error.declined")
}
Transaction.wrap(function () {
OrderMgr.failOrder(res.Order);
});
app.getController('COSummary').Start({
Order : res.Order,
PlaceOrderError : PlaceOrderError
});
}
} else {
order = null;
}
app.getController('COSummary').Start({});
}
/**
* Decline Ecommerce Return Url
*/
function EcommDecline() {
EcommCancel(true);
}
/**
* Cancel Ecommerce Return Url
*/
function EcommCancel(showPlaceOrderError) {
var PlaceOrderError = null;
if(showPlaceOrderError){
PlaceOrderError = new dw.system.Status(dw.system.Status.ERROR, "confirm.error.declined")
}
var res = CheckEcommercePayment();
if(res.EndNode == 'success') {
Transaction.wrap(function () {
res.Order.paymentStatus = dw.order.Order.PAYMENT_STATUS_PAID;
});
SubmitImpl(res.Order, true);
} else {
Transaction.wrap(function () {
OrderMgr.failOrder(res.Order);
});
app.getController('COSummary').Start({
Order : res.Order,
PlaceOrderError : PlaceOrderError
});
}
}
/*
* Export the publicly available controller methods
*/
/** Accept Ecommerce Return Url
* @see module:controllers/ECM_PPAL~EcommAccept */
exports.EcommAccept = guard.ensure(['https'], EcommAccept);
/** Decline Ecommerce Return Url
* @see module:controllers/ECM_PPAL~EcommDecline */
exports.EcommDecline = guard.ensure(['https'], EcommDecline);
/** Cancel Ecommerce Return Url
* @see module:controllers/ECM_PPAL~EcommCancel */
exports.EcommCancel = guard.ensure(['https'], EcommCancel);