express-checkout.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore',
  7. 'jquery',
  8. 'uiComponent',
  9. 'paypalInContextExpressCheckout',
  10. 'Magento_Customer/js/customer-data',
  11. 'domReady!'
  12. ], function (_, $, Component, paypalExpressCheckout, customerData) {
  13. 'use strict';
  14. return Component.extend({
  15. defaults: {
  16. clientConfig: {
  17. checkoutInited: false,
  18. /**
  19. * @param {Object} event
  20. */
  21. click: function (event) {
  22. $('body').trigger('processStart');
  23. event.preventDefault();
  24. if (!this.clientConfig.checkoutInited) {
  25. paypalExpressCheckout.checkout.initXO();
  26. this.clientConfig.checkoutInited = true;
  27. } else {
  28. paypalExpressCheckout.checkout.closeFlow();
  29. }
  30. $.getJSON(this.path, {
  31. button: 1
  32. }).done(function (response) {
  33. var message = response && response.message;
  34. if (message) {
  35. customerData.set('messages', {
  36. messages: [message]
  37. });
  38. }
  39. if (response && response.url) {
  40. paypalExpressCheckout.checkout.startFlow(response.url);
  41. return;
  42. }
  43. paypalExpressCheckout.checkout.closeFlow();
  44. }).fail(function () {
  45. paypalExpressCheckout.checkout.closeFlow();
  46. }).always(function () {
  47. $('body').trigger('processStop');
  48. customerData.invalidate(['cart']);
  49. });
  50. }
  51. }
  52. },
  53. /**
  54. * @returns {Object}
  55. */
  56. initialize: function () {
  57. this._super();
  58. return this.initClient();
  59. },
  60. /**
  61. * @returns {Object}
  62. */
  63. initClient: function () {
  64. _.each(this.clientConfig, function (fn, name) {
  65. if (typeof fn === 'function') {
  66. this.clientConfig[name] = fn.bind(this);
  67. }
  68. }, this);
  69. paypalExpressCheckout.checkout.setup(this.merchantId, this.clientConfig);
  70. return this;
  71. }
  72. });
  73. });