'use strict';
/**
* Controller for Ingenico.
*
* @module controllers/Ingenico
*/
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;
/**
* Checks if order with given No exist, if yes - clear OrderNo
*/
function CheckIfOrderNoExist() {
if(!parameterMap.OrderNo.empty){
var order = OrderMgr.getOrder(parameterMap.OrderNo.stringValue);
if(order !== null) {
var OrderNo = null;
} else {
var OrderNo = parameterMap.OrderNo.stringValue;
}
} else {
var OrderNo = null;
}
return OrderNo;
}
/**
* Check if Ingenico is enabled
*/
function IsEnabled() {
var IngenicoConfiguration = libPayment.getIngenicoConfiguration();
if(IngenicoConfiguration.enableIngenico){
return true;
} else {
return false;
}
}
/**
* Template for Accept Ecommerce.
*/
function TemplateEcommerce(args) {
if(IsEnabled()){
app.getView({
Order: args.Order
}).render('ecommerceRedirectAccept');
}
}
/**
* Save Klarna Fields
*/
function SaveKlarnaFields() {
var cart = app.getModel('Cart').get();
if(IsEnabled()){
if(parameterMap[session.forms.billing.paymentMethods.selectedPaymentMethodID.htmlName].value == 'Klarna E-commerce' || parameterMap[session.forms.billing.paymentMethods.selectedPaymentMethodID.htmlName].value == 'Klarna Installment E-commerce') {
Transaction.wrap(function () {
libPayment.setKlarnaFields(parameterMap[session.forms.billing.paymentMethods.selectedPaymentMethodID.htmlName].value, parameterMap[session.forms.billing.billingAddress.addressFields.country.htmlName].stringValue, customer, cart.object);
});
}
}
}
/**
* Reset Payment Forms
*/
function ResetPaymentForms() {
var cart = app.getModel('Cart').get();
if(session.forms.billing.paymentMethods.selectedPaymentMethodID.value.equals("PayPal")) {
require('int_payment_controllers/cartridge/controllers/PAYMENT_ECOMMERCE').ResetAllPaymentForms();
require('int_payment_controllers/cartridge/controllers/PAYMENT_DIRECT_DEBITS').ResetAllPaymentForms();
session.forms.billing.paymentMethods.creditCard.clearFormElement();
session.forms.billing.paymentMethods.bml.clearFormElement();
Transaction.wrap(function () {
cart.removePaymentInstruments(cart.getPaymentInstruments(dw.order.PaymentInstrument.METHOD_CREDIT_CARD));
cart.removePaymentInstruments(cart.getPaymentInstruments(dw.order.PaymentInstrument.METHOD_BML));
});
} else if(['DIRECT_DEBITS_AT', 'DIRECT_DEBITS_DE', 'DIRECT_DEBITS_NL'].indexOf(session.forms.billing.paymentMethods.selectedPaymentMethodID.value) === -1) {
session.forms.billing.paymentMethods.bml.clearFormElement();
require('int_payment_controllers/cartridge/controllers/PAYMENT_ECOMMERCE').ResetAllPaymentForms();
require('int_payment_controllers/cartridge/controllers/PAYMENT_DIRECT_DEBITS').ResetAllPaymentForms();
} else if(!session.forms.billing.paymentMethods.selectedPaymentMethodID.value.equals(dw.order.PaymentInstrument.METHOD_CREDIT_CARD)) {
require('int_payment_controllers/cartridge/controllers/PAYMENT_ECOMMERCE').ResetAllPaymentForms();
require('int_payment_controllers/cartridge/controllers/PAYMENT_DIRECT_DEBITS').ResetAllPaymentForms();
Transaction.wrap(function () {
session.forms.billing.paymentMethods.bml.clearFormElement();
cart.removePaymentInstruments(cart.getPaymentInstruments("PayPal"));
cart.removePaymentInstruments(cart.getPaymentInstruments(dw.order.PaymentInstrument.METHOD_BML));
});
} else if(!session.forms.billing.paymentMethods.selectedPaymentMethodID.value.equals(dw.order.PaymentInstrument.METHOD_BML)) {
require('int_payment_controllers/cartridge/controllers/PAYMENT_ECOMMERCE').ResetAllPaymentForms();
session.forms.billing.paymentMethods.creditCard.clearFormElement();
Transaction.wrap(function () {
cart.removePaymentInstruments(cart.getPaymentInstruments(dw.order.PaymentInstrument.METHOD_CREDIT_CARD));
cart.removePaymentInstruments(cart.getPaymentInstruments("PayPal"));
});
} else {
require('int_payment_controllers/cartridge/controllers/PAYMENT_ECOMMERCE').ResetAllPaymentForms();
}
}
/**
* Hosted Tokenization Page
*/
function HostedTokenizationPage() {
var HostedTokenizationURL = libPayment.GetHostedTokenizationURL();
app.getView({
HostedTokenizationURL: HostedTokenizationURL
}).render('checkout/hostedtokenizationpage');
}
/**
* Hosted Tokenization Succes
*/
function HostedTokenizationSucces() {
if(session.custom.hostedTokenizationSucces) {
var template = 'util/successjson';
} else {
var template = 'util/errorjson';
}
app.getView({}).render(template);
}
/**
* This pipeline renders the order confirmation page after the successful order creation , for Ecommerce .
*/
function AcceptEcommerce() {
var order = OrderMgr.getOrder(parameterMap.orderNo.stringValue);
var ShaCode = libPayment.createShaForUrls(order);
if(ShaCode == parameterMap.tn.toString()) {
if (!customer.authenticated) {
// Initializes the account creation form for guest checkouts by populating the first and last name with the
// used billing address.
var customerForm = app.getForm('profile.customer');
customerForm.setValue('firstname', order.billingAddress.firstName);
customerForm.setValue('lastname', order.billingAddress.lastName);
customerForm.setValue('email', order.customerEmail);
}
app.getForm('profile.login.passwordconfirm').clear();
app.getForm('profile.login.password').clear();
} else {
order = null;
}
app.getController('COSummary').AcceptEcommerce({
Order : order
});
}
/**
* This renders the summary page prior to order creation , for Ecommerce , if the order is declined.
*/
function DeclineEcommerce() {
CancelEcommerce(true);
}
/**
* This renders the summary page prior to order creation , for Ecommerce , if the order is cancelled .
*/
function CancelEcommerce(showPlaceOrderError) {
var PlaceOrderError = null;
var order = OrderMgr.getOrder(parameterMap.orderNo.stringValue);
var ShaCode = libPayment.createShaForUrls(order);
if(showPlaceOrderError){
PlaceOrderError = new dw.system.Status(dw.system.Status.ERROR, "confirm.error.declined")
}
if(ShaCode == parameterMap.tn.toString()) {
Transaction.wrap(function () {
OrderMgr.failOrder(order);
if(order.custom.failOrder == true) {
order.custom.failOrder = false;
}
});
} else {
order = null;
}
app.getController('COSummary').Ecommerce({
Order : order,
PlaceOrderError : PlaceOrderError
});
}
/*
* Export the private controller methods
*/
exports.CheckIfOrderNoExist = CheckIfOrderNoExist;
exports.IsEnabled = IsEnabled;
exports.ResetPaymentForms = ResetPaymentForms;
exports.TemplateEcommerce = TemplateEcommerce;
exports.SaveKlarnaFields = SaveKlarnaFields;
/*
* Export the publicly available controller methods
*/
/** Hosted Tokenization Page.
* @see module:controllers/Ingenico~HostedTokenizationPage */
exports.HostedTokenizationPage = guard.ensure(['get'], HostedTokenizationPage);
/** Hosted Tokenization Succes.
* @see module:controllers/Ingenico~HostedTokenizationSucces */
exports.HostedTokenizationSucces = guard.ensure(['get'], HostedTokenizationSucces);
/** This renders the summary page prior to order creation , for Ecommerce , if the order is declined.
* @see module:controllers/Ingenico~DeclineEcommerce */
exports.DeclineEcommerce = guard.ensure(['https'], DeclineEcommerce);
/** This renders the summary page prior to order creation , for Ecommerce , if the order is cancelled .
* @see module:controllers/Ingenico~CancelEcommerce */
exports.CancelEcommerce = guard.ensure(['https'], CancelEcommerce);
/** This pipeline renders the order confirmation page after the successful order creation , for Ecommerce .
* @see module:controllers/Ingenico~AcceptEcommerce */
exports.AcceptEcommerce = guard.ensure(['https'], AcceptEcommerce);