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 () {
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 = ''; // @todo LIVE widget url
}
if (typeof amazon !== 'undefined') {
this.$OrderProcess.Loader.show();
if (typeof window.onAmazonPaymentsReady === 'undefined') {
window.onAmazonPaymentsReady = this.$showAmazonPayBtn;
}
if (typeof window.onAmazonLoginReady === 'undefined') {
window.onAmazonLoginReady = this.$onAmazonLoginReady;
}
new Element('script', {
async: "async",
src : widgetUrl
}).inject(document.body);
},
/**
* Execute if Amazon Login has loaded
*/
$onAmazonLoginReady: function () {
amazon.Login.setClientId(this.getAttribute('clientid'));
},
/**
this.$OrderProcess.Loader.hide();
// re-display if button was previously rendered and hidden
this.$AuthBtnElm.removeClass('quiqqer-payment-amazon__hidden');
OffAmazonPayments.Button(
'quiqqer-payment-amazon-btn',
this.getAttribute('sellerid'),
{
type : 'PwA',
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')
);
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')
);
}
}
}
);
},
/**
* Show Amazon Pay Wallet widget
*
* @param {Boolean} [showInfoMessage] - Show info message
if (showInfoMessage) {
this.$showMsg(
QUILocale.get(pkg, 'controls.PaymentDisplay.wallet_info')
);
}
this.$WalletElm.set('html', '');
var Options = {
sellerId : this.getAttribute('sellerid'),
design : {
designMode: 'responsive'
},
onPaymentSelect: function () {
self.$PayBtn.enable();
},
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')
);
}
}
};
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');
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')
}),
new OffAmazonPayments.Widgets.Wallet(Options).bind('quiqqer-payment-amazon-wallet');
},
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* 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('texticon', '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('texticon', 'fa fa-amazon');
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>'
);
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
},
/**
* 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
});
});