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',
'package/quiqqer/payments/bin/Handler',
], function (QUIControl, QUIButton, QUIControlUtils, PaymentsHandler, 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');
// set random id for AuthBtn to enable re-rending of Amazon Btn Widget
this.$AuthBtnElm.set('id', this.$AuthBtnElm.get('id') + '-' + Math.floor((Math.random() * 1000000) + 1));
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.getAttribute('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') {
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');
},
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 && !AmazonBtnImg.getSize().y) {
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')
);
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');
},
317
318
319
320
321
322
323
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
/**
* 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>'
);
},
/**
* 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 PaymentsHandler.logPaymentsError(Error.getErrorMessage(), Error.getErrorCode());