express-checkout-wrapper.js 4.94 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 177 178 179 180 181 182 183 184 185 186 187
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
define([
    'jquery',
    'mage/translate',
    'Magento_Customer/js/customer-data',
    'Magento_Paypal/js/in-context/express-checkout-smart-buttons',
    'mage/cookies'
], function ($, $t, customerData, checkoutSmartButtons) {
    'use strict';

    return {
        defaults: {
            paymentActionError: $t('Something went wrong with your request. Please try again later.'),
            signInMessage: $t('To check out, please sign in with your email address.')
        },

        /**
         * Render PayPal buttons using checkout.js
         */
        renderPayPalButtons: function (element) {
            checkoutSmartButtons(this.prepareClientConfig(), element);
        },

        /**
         * Validate payment method
         *
         * @param {Object} actions
         */
        validate: function (actions) {
            this.actions = actions || this.actions;
        },

        /**
         * Execute logic on Paypal button click
         */
        onClick: function () {},

        /**
         * Before payment execute
         *
         * @param {Function} resolve
         * @param {Function} reject
         * @return {*}
         */
        beforePayment: function (resolve, reject) { //eslint-disable-line no-unused-vars
            return $.Deferred().resolve();
        },

        /**
         * After payment execute
         *
         * @param {Object} res
         * @param {Function} resolve
         * @param {Function} reject
         *
         * @return {*}
         */
        afterPayment: function (res, resolve, reject) {
            if (res.success) {
                return resolve(res.token);
            }

            this.addError(res['error_message']);

            return reject(new Error(res['error_message']));
        },

        /**
         * Catch payment
         *
         * @param {Error} err
         * @param {Function} resolve
         * @param {Function} reject
         */
        catchPayment: function (err, resolve, reject) {
            this.addError(this.paymentActionError);
            reject(err);
        },

        /**
         * Before onAuthorize execute
         *
         * @param {Function} resolve
         * @param {Function} reject
         * @param {Object} actions
         *
         * @return {jQuery.Deferred}
         */
        beforeOnAuthorize: function (resolve, reject, actions) { //eslint-disable-line no-unused-vars
            return $.Deferred().resolve();
        },

        /**
         * After onAuthorize execute
         *
         * @param {Object} res
         * @param {Function} resolve
         * @param {Function} reject
         * @param {Object} actions
         *
         * @return {*}
         */
        afterOnAuthorize: function (res, resolve, reject, actions) {
            if (res.success) {
                resolve();

                return actions.redirect(window, res.redirectUrl);
            }

            this.addError(res['error_message']);

            return reject(new Error(res['error_message']));
        },

        /**
         * Catch payment
         *
         * @param {Error} err
         * @param {Function} resolve
         * @param {Function} reject
         */
        catchOnAuthorize: function (err, resolve, reject) {
            this.addError(this.paymentActionError);
            reject(err);
        },

        /**
         * Process cancel action
         *
         * @param {Object} data
         * @param {Object} actions
         */
        onCancel: function (data, actions) {
            actions.redirect(window, this.clientConfig.onCancelUrl);
        },

        /**
         * Process errors
         *
         * @param {Error} err
         */
        onError: function (err) { //eslint-disable-line no-unused-vars
            // Uncaught error isn't displayed in the console
        },

        /**
         * Adds error message
         *
         * @param {String} message
         * @param {String} [type]
         */
        addError: function (message, type) {
            type = type || 'error';
            customerData.set('messages', {
                messages: [{
                    type: type,
                    text: message
                }],
                'data_id': Math.floor(Date.now() / 1000)
            });
        },

        /**
         * @returns {String}
         */
        getButtonId: function () {
            return this.inContextId;
        },

        /**
         * Populate client config with all required data
         *
         * @return {Object}
         */
        prepareClientConfig: function () {
            this.clientConfig.client = {};
            this.clientConfig.client[this.clientConfig.environment] = this.clientConfig.merchantId;
            this.clientConfig.rendererComponent = this;
            this.clientConfig.formKey = $.mage.cookies.get('form_key');

            return this.clientConfig;
        }
    };
});