/**
* @uses jQuery
* @uses ep
*/
define('de_epages/paypalplus/ui/paypalpluspaymentwall', [
'jquery',
'ep',
'$dict!../dictionary',
'$ready!',
'ep/ajax',
'ep/fn/busy'
], function($, ep, epDict) {
'use strict';
var PaymentWallError = epDict.translate('{PaymentWallError}');
var isMobileGlobal = undefined;
var isClassicalGlobal = undefined;
var isErrorShown = false;
return function(isMobile, isClassical, BasketOrOrderID, PaymentLineItemID) {
isMobileGlobal = isMobile;
isClassicalGlobal = isClassical;
ep('.SubmitButton').click(function( event ) {
event.stopImmediatePropagation();
event.preventDefault();
ep('.SubmitButton').prop('disabled', true);
ep.ajax({
data : {
'ObjectID' : BasketOrOrderID,
'ChangeAction' : 'PatchPaymentPayPalPlus'
},
cache : false,
type : "POST",
dataType : "json"
}).done(function(jsonData) {
if (jsonData.WasPatchPaymentSuccessful) {
ep('#BasketForm').submit();
} else {
showErrorOnStoreFront();
}
}).fail(function(jsonData) {
showErrorOnStoreFront();
});
});
showBusyBox();
ep.ajax({
data : {
'ObjectID' : BasketOrOrderID,
'ChangeAction' : 'CreatePaymentPayPalPlus'
},
cache : false,
type : "POST",
dataType : "json"
}).done(function(jsonData) {
if (jsonData.WasCreatePaymentSuccessful) {
buildPaymentWall(PaymentLineItemID);
} else {
showErrorOnStoreFront();
}
}).fail(function(jsonData) {
showErrorOnStoreFront();
});
};
function showBusyBox() {
if (isMobileGlobal) {
ep('#CheckOut').prop('disabled', true);
ep('#CheckOutTop').prop('disabled', true);
}
else if(isClassicalGlobal) {
ep('#ppplus').addClass('ep-busy');
ep('.ep-uiInput-button').prop('disabled', true);
}
else {
ep('#ppplus').addClass('ep-busy');
ep('.SubmitButton').prop('disabled', true);
}
};
function OnPaymentWallLoaded() {
if (isMobileGlobal) {
ep('#CheckOut').prop('disabled', isErrorShown);
ep('#CheckOutTop').prop('disabled', isErrorShown);
}
else if(isClassicalGlobal) {
ep('#ppplus').removeClass('ep-busy');
ep('.ep-uiInput-button').prop('disabled', isErrorShown);
}
else {
ep('#ppplus').removeClass('ep-busy');
ep('.SubmitButton').prop('disabled', isErrorShown);
}
appendPaypalplusCookieToForm();
};
function appendPaypalplusCookieToForm() {
var cookieName = 'paypalplus_session';
var input = $('<input>').attr({
id: 'paypalplus-cookie',
type: 'hidden',
name: cookieName,
value: $.cookie(cookieName)
});
$('#paypalplus-cookie').remove();
ep('#BasketForm').append(input);
}
function showErrorOnStoreFront() {
isErrorShown = true;
OnPaymentWallLoaded();
// TODO: we can use "#PaymentWallErrorMessage" now
ep("#" + "PaymentWallErrorMessage").css({
"display" : "block"
}).html(PaymentWallError);
};
function buildPaymentWall(PaymentLineItemID) {
ep.ajax({
data : {
'ObjectID' : PaymentLineItemID,
'ChangeAction' : 'GetPaymentWallDataPayPalPlus'
},
cache : false,
type : "POST",
dataType : "json"
}).done(function(jsonData) {
if(jsonData.wasSuccessful) {
showPaymentWall(jsonData);
} else {
showErrorOnStoreFront(jsonData.errorCode, jsonData.errorMessage);
}
}).fail(function(jsonData) {
showErrorOnStoreFront(jsonData.status, jsonData.statusText);
});
};
function showPaymentWall(jsonData) {
PAYPAL.apps.PPP({
"showPuiOnSandbox": "true",
"approvalUrl" : jsonData.approvalUrl,
"placeholder" : "ppplus",
"mode" : jsonData.mode,
"buttonLocation" : jsonData.buttonLocation,
"language" : jsonData.language,
"country" : jsonData.country,
"enableContinue" : OnPaymentWallLoaded,
});
};
});