1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- define([
- 'underscore',
- 'jquery',
- 'uiComponent',
- 'paypalInContextExpressCheckout',
- 'Magento_Customer/js/customer-data',
- 'domReady!'
- ], function (_, $, Component, paypalExpressCheckout, customerData) {
- 'use strict';
- return Component.extend({
- defaults: {
- clientConfig: {
- checkoutInited: false,
- /**
- * @param {Object} event
- */
- click: function (event) {
- $('body').trigger('processStart');
- event.preventDefault();
- if (!this.clientConfig.checkoutInited) {
- paypalExpressCheckout.checkout.initXO();
- this.clientConfig.checkoutInited = true;
- } else {
- paypalExpressCheckout.checkout.closeFlow();
- }
- $.getJSON(this.path, {
- button: 1
- }).done(function (response) {
- var message = response && response.message;
- if (message) {
- customerData.set('messages', {
- messages: [message]
- });
- }
- if (response && response.url) {
- paypalExpressCheckout.checkout.startFlow(response.url);
- return;
- }
- paypalExpressCheckout.checkout.closeFlow();
- }).fail(function () {
- paypalExpressCheckout.checkout.closeFlow();
- }).always(function () {
- $('body').trigger('processStop');
- customerData.invalidate(['cart']);
- });
- }
- }
- },
- /**
- * @returns {Object}
- */
- initialize: function () {
- this._super();
- return this.initClient();
- },
- /**
- * @returns {Object}
- */
- initClient: function () {
- _.each(this.clientConfig, function (fn, name) {
- if (typeof fn === 'function') {
- this.clientConfig[name] = fn.bind(this);
- }
- }, this);
- paypalExpressCheckout.checkout.setup(this.merchantId, this.clientConfig);
- return this;
- }
- });
- });
|