Newer
Older
/**
* PaymentDisplay for Amazon Pay
*
* @author Patrick Müller (www.pcsg.de)
*/
define('package/quiqqer/payment-amazon/bin/controls/PaymentDisplay', [
'qui/controls/Control',
'qui/controls/buttons/Button',
], function (QUIControl, QUIButton, QUIControlUtils, QUIAjax, QUILocale) {
"use strict";
var pkg = 'quiqqer/payment-amazon';
return new Class({
Extends: QUIControl,
Type : 'package/quiqqer/payment-amazon/bin/controls/PaymentDisplay',
Binds: [
'$onImport',
sandbox : true,
sellerid : '',
clientid : '',
orderhash : '',
successful: false
},
initialize: function (options) {
this.parent(options);
this.$orderReferenceId = false;
this.$AuthBtnElm = null;
this.$WalletElm = null;
this.$PayBtn = null;
this.$MsgElm = null;
this.addEvents({
onImport: this.$onImport
});
},
/**
* Event: onImport
*/
$onImport: function () {
if (Elm.getElement('.message-error')) {
(function () {
self.fireEvent('processingError', [self]);
}).delay(2000);
return;
}
if (Elm.getElement('.message-error')) {
return;
}
this.$MsgElm = Elm.getElement('.quiqqer-payment-amazon-message');
this.$AuthBtnElm = Elm.getElement('#quiqqer-payment-amazon-btn');
this.$WalletElm = Elm.getElement('#quiqqer-payment-amazon-wallet');
this.$showMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.info')
);
QUIControlUtils.getControlByElement(
Elm.getParent('[data-qui="package/quiqqer/order/bin/frontend/controls/OrderProcess"]')
).then(function (OrderProcess) {
self.$OrderProcess = OrderProcess;
if (self.getAttribute('successful')) {
OrderProcess.next();
return;
}
self.$loadAmazonWidgets();
});
},
/**
* Load Amazon Pay widgets
*/
$loadAmazonWidgets: function () {
var widgetUrl = "https://static-eu.payments-amazon.com/OffAmazonPayments/eur/sandbox/lpa/js/Widgets.js";
if (!this.getAttributes('sandbox')) {
widgetUrl = 'https://static-eu.payments-amazon.com/OffAmazonPayments/eur/lpa/js/Widgets.js';
window.onAmazonPaymentsReady = this.$showAmazonPayBtn;
window.onAmazonLoginReady = this.$onAmazonLoginReady;
if (typeof amazon !== 'undefined') {
var ScriptElm = document.getElement('script[src="' + widgetUrl + '"]');
if (ScriptElm) {
amazon = null;
ScriptElm.destroy();
}
new Element('script', {
async: "async",
src : widgetUrl
}).inject(document.body);
},
/**
* Execute if Amazon Login has loaded
*/
$onAmazonLoginReady: function () {
amazon.Login.setClientId(this.getAttribute('clientid'));
},
/**
// re-display if button was previously rendered and hidden
this.$AuthBtnElm.removeClass('quiqqer-payment-amazon__hidden');
color: this.$AuthBtnElm.get('data-color'),
size : this.$AuthBtnElm.get('data-size'),
popup: true,
scope: 'payments:widget'
}, function (Response) {
if (Response.error) {
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.login_error')
);
self.fireEvent('processingError', [self]);
return;
}
self.$accessToken = Response.access_token;
self.$AuthBtnElm.addClass('quiqqer-payment-amazon__hidden');
onFinish: function() {
console.log("Amazon onFinish");
},
onSuccess: function() {
console.log("Amazon onSuccess");
},
onError: function (Error) {
switch (Error.getErrorCode()) {
// handle errors on the shop side (most likely misconfiguration)
case 'InvalidAccountStatus':
case 'InvalidSellerId':
case 'InvalidParameterValue':
case 'MissingParameter':
case 'UnknownError':
self.$AuthBtnElm.addClass('quiqqer-payment-amazon__hidden');
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.configuration_error')
);
self.$logError(Error);
break;
default:
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.login_error')
);
}
self.fireEvent('processingError', [self]);
this.$OrderProcess.Loader.show();
var waitForBtnElm = setInterval(function() {
var AmazonBtnImg = self.$AuthBtnElm.getElement('img');
if (!AmazonBtnImg) {
return;
}
if (!AmazonBtnImg.complete) {
return;
}
clearInterval(waitForBtnElm);
self.$OrderProcess.resize();
self.$OrderProcess.Loader.hide();
}, 200);
*
* @param {Boolean} [showInfoMessage] - Show info message
if (showInfoMessage) {
this.$showMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.wallet_info')
);
}
this.$WalletElm.set('html', '');
this.$WalletElm.removeClass('quiqqer-payment-amazon__hidden');
var Options = {
sellerId : this.getAttribute('sellerid'),
design : {
designMode: 'responsive'
},
onPaymentSelect: function () {
onError : function (Error) {
switch (Error.getErrorCode()) {
// handle errors on the shop side (most likely misconfiguration)
case 'InvalidAccountStatus':
case 'InvalidSellerId':
case 'InvalidParameterValue':
case 'MissingParameter':
case 'UnknownError':
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.configuration_error')
);
self.$logError(Error);
break;
case 'AddressNotModifiable':
case 'BuyerNotAssociated':
case 'BuyerSessionExpired':
case 'PaymentMethodNotModifiable':
case 'StaleOrderReference':
self.$AuthBtnElm.removeClass('quiqqer-payment-amazon__hidden');
self.$showErrorMsg(Error.getErrorMessage());
break;
default:
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.wallet_error')
);
}
self.fireEvent('processingError', [self]);
}
};
if (!this.$orderReferenceId) {
Options.onOrderReferenceCreate = function (orderReference) {
self.$orderReferenceId = orderReference.getAmazonOrderReferenceId();
}
} else {
Options.amazonOrderReferenceId = this.$orderReferenceId;
}
if (!this.$PayBtn) {
var PayBtnElm = this.getElm().getElement('#quiqqer-payment-amazon-btn-pay');
'class' : 'btn-primary',
disabled : true,
text : QUILocale.get(pkg, 'controls.PaymentDisplay.btn_pay.text', {
display_price: PayBtnElm.get('data-price')
}),
alt : QUILocale.get(pkg, 'controls.PaymentDisplay.btn_pay.title', {
display_price: PayBtnElm.get('data-price')
}),
title : QUILocale.get(pkg, 'controls.PaymentDisplay.btn_pay.title', {
display_price: PayBtnElm.get('data-price')
}),
textimage: 'fa fa-amazon',
events : {
new OffAmazonPayments.Widgets.Wallet(Options).bind('quiqqer-payment-amazon-wallet');
},
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* Start payment process
*
* @param {Object} Btn
*/
$onPayBtnClick: function (Btn) {
var self = this;
Btn.disable();
Btn.setAttribute('texticon', 'fa fa-spinner fa-spin');
self.$WalletElm.addClass('quiqqer-payment-amazon__hidden');
self.$OrderProcess.Loader.show(
QUILocale.get(pkg, 'controls.PaymentDisplay.authorize_payment')
);
self.$authorizePayment().then(function (success) {
if (success) {
self.$OrderProcess.next();
return;
}
self.$OrderProcess.Loader.hide();
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.processing_error')
);
self.$showAmazonWallet(false);
Btn.enable();
Btn.setAttribute('textimage', 'fa fa-amazon');
}, function (error) {
self.$OrderProcess.Loader.hide();
self.$showErrorMsg(error.getMessage());
if (error.getAttribute('orderCancelled')) {
self.$orderReferenceId = false;
}
if (error.getAttribute('reRenderWallet')) {
self.$WalletElm.removeClass('quiqqer-payment-amazon__hidden');
self.$showAmazonWallet(false);
Btn.enable();
Btn.setAttribute('textimage', 'fa fa-amazon');
self.fireEvent('processingError', [self]);
return;
}
// sign out
amazon.Login.logout();
Btn.destroy();
self.$showErrorMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.fatal_error')
);
new QUIButton({
text : QUILocale.get(pkg, 'controls.PaymentDisplay.btn_reselect_payment.text'),
texticon: 'fa fa-credit-card',
events : {
onClick: function () {
window.location.reload();
}
}
}).inject(self.getElm().getElement('#quiqqer-payment-amazon-btn-pay'))
/**
* Start the payment process
*
* @return {Promise}
*/
var self = this;
return new Promise(function (resolve, reject) {
QUIAjax.post('package_quiqqer_payment-amazon_ajax_authorizePayment', resolve, {
'package' : pkg,
orderHash : self.getAttribute('orderhash'),
orderReferenceId: self.$orderReferenceId,
onError : reject
})
});
},
/**
* Show error msg
*
*/
$showErrorMsg: function (msg) {
this.$MsgElm.set(
'html',
'<p class="message-error">' + msg + '</p>'
);
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
},
/**
* Show normal msg
*
* @param {String} msg
*/
$showMsg: function (msg) {
this.$MsgElm.set(
'html',
'<p>' + msg + '</p>'
);
},
/**
* Log an Amazon Pay widget/processing error
*
* @param {Object} Error - Amazon Pay widget error
* @return {Promise}
*/
$logError: function (Error) {
return new Promise(function (resolve, reject) {
QUIAjax.post('package_quiqqer_payment-amazon_ajax_logFrontendError', resolve, {
'package': pkg,
errorCode: Error.getErrorCode(),
errorMsg : Error.getErrorMessage(),
onError : reject
});
});