amazon-button.js 6.56 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
/**
 * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
define([
    'jquery',
    'Magento_Customer/js/customer-data',
    'Magento_Customer/js/section-config',
    'Amazon_Payment/js/model/amazonPaymentConfig',
    'amazonCsrf',
    'modernizr/modernizr',
    'amazonCore',
    'jquery/ui',
    'uiRegistry'
], function ($, customerData, sectionConfig, amazonPaymentConfig, amazonCsrf) {
    'use strict';
    var _this;

    if (amazonPaymentConfig.isDefined()) {

        $.widget('amazon.AmazonButton', {
            options: {
                merchantId: null,
                buttonType: 'LwA',
                buttonColor: 'Gold',
                buttonSize: 'medium',
                redirectUrl: null,
                loginPostUrl: null
            },

            /**
             * Create button
             */
            _create: function () {
                _this = this;

                this._verifyAmazonConfig();

                if (typeof OffAmazonPayments === 'undefined') {
                    // async render
                    $(window).on('OffAmazonPayments', $.proxy(function () {
                        this._renderAmazonButton();
                    }, this));
                } else {
                    this._renderAmazonButton();
                }
            },

            /**
             * Verify if checkout config is available
             * @private
             */
            _verifyAmazonConfig: function () {
                if (amazonPaymentConfig.isDefined()) {
                    this.options.merchantId = amazonPaymentConfig.getValue('merchantId');
                    this.options.buttonType = this.options.buttonType === 'LwA' ?
                        amazonPaymentConfig.getValue('buttonTypeLwa') : amazonPaymentConfig.getValue('buttonTypePwa');
                    this.options.buttonColor = amazonPaymentConfig.getValue('buttonColor');
                    this.options.buttonSize = amazonPaymentConfig.getValue('buttonSize');
                    this.options.redirectUrl = amazonPaymentConfig.getValue('redirectUrl');
                    this.options.loginPostUrl = amazonPaymentConfig.getValue('loginPostUrl');
                    this.options.loginScope = amazonPaymentConfig.getValue('loginScope');
                    this.options.buttonLanguage = amazonPaymentConfig.getValue('displayLanguage');
                }
            },

            /**
             * Validate CSRF cookie and redirect to HTTPS
             */
            secureHttpsCallback: function (event) {
                var sections = sectionConfig.getAffectedSections(_this.options.loginPostUrl);

                if (!event.state || !amazonCsrf.isValid(event.state)) {
                    window.location = amazonPaymentConfig.getValue('customerLoginPageUrl');

                    return window.location;
                }

                // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
                if (!event.access_token || !!event.error) {
                    window.location = amazonPaymentConfig.getValue('customerLoginPageUrl');

                    return window.location;
                }

                if (sections) {
                    customerData.invalidate(sections);
                }
                window.location = _this.options.redirectUrl + '?access_token=' + event.access_token;
                // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
            },

            /**
             * Use popup or redirect URI
             *
             * @return {String}
             */
            _popupCallback: function () {
                return _this.usePopUp() ? _this.secureHttpsCallback :
                    amazonPaymentConfig.getValue('oAuthHashRedirectUrl');
            },

            /**
             * Are touch events available
             * (Supports both v2 and v3 Modernizr)
             * @returns {Boolean}
             * @private
             */
            _touchSupported: function () {
                //eslint-disable-next-line no-undef
                return Modernizr.touch !== undefined ? Modernizr.touch : Modernizr.touchevents;
            },

            /**
             * Should we use the pop up login flow?
             *  - are we on an HTTPS page (required for popup)
             *  - confirm we are not on the product detail page (items are added asynchronously to the cart,
             *    hence popups will be blocked)
             *  - confirm we are not using a touch device (redirect provides a better mobile experience)
             * @returns {Boolean}
             * @public
             */
            usePopUp: function () {
                return window.location.protocol === 'https:' && !$('body').hasClass('catalog-product-view') &&
                    !this._touchSupported();
            },

            /**
             * onAmazonPaymentsReady
             * @private
             */
            _renderAmazonButton: function () {
                OffAmazonPayments.Button(this.element[0].id, this.options.merchantId, { //eslint-disable-line no-undef
                    type: this.options.buttonType,
                    color: this.options.buttonColor,
                    size: this.options.buttonSize,
                    language: this.options.buttonLanguage,

                    /**
                     * Authorization callback
                     */
                    authorization: function () {
                        //eslint-disable-next-line no-undef
                        amazon.Login.authorize(_this._getLoginOptions(), _this._popupCallback());
                    }
                });
                $('.amazon-button-container .field-tooltip').fadeIn();
            },

            /**
             * Build login options
             * @returns {{scope: *, popup: *, state: *}}
             * @private
             */
            _getLoginOptions: function () {
                return {
                    scope: this.options.loginScope,
                    popup: this.usePopUp(),
                    state: amazonCsrf.generateNewValue()
                };
            }
        });

        return $.amazon.AmazonButton;
    }
});