button.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define(
  6. [
  7. 'rjsResolver',
  8. 'uiRegistry',
  9. 'uiComponent',
  10. 'underscore',
  11. 'jquery',
  12. 'braintree',
  13. 'Magento_Braintree/js/paypal/form-builder',
  14. 'domReady!'
  15. ],
  16. function (
  17. resolver,
  18. registry,
  19. Component,
  20. _,
  21. $,
  22. braintree,
  23. formBuilder
  24. ) {
  25. 'use strict';
  26. return Component.extend({
  27. defaults: {
  28. integrationName: 'braintreePaypal.currentIntegration',
  29. /**
  30. * {String}
  31. */
  32. displayName: null,
  33. /**
  34. * {String}
  35. */
  36. clientToken: null,
  37. /**
  38. * {Object}
  39. */
  40. clientConfig: {
  41. /**
  42. * @param {Object} integration
  43. */
  44. onReady: function (integration) {
  45. resolver(function () {
  46. registry.set(this.integrationName, integration);
  47. $('#' + this.id).removeAttr('disabled');
  48. }, this);
  49. },
  50. /**
  51. * @param {Object} payload
  52. */
  53. onPaymentMethodReceived: function (payload) {
  54. $('body').trigger('processStart');
  55. formBuilder.build(
  56. {
  57. action: this.actionSuccess,
  58. fields: {
  59. result: JSON.stringify(payload)
  60. }
  61. }
  62. ).submit();
  63. }
  64. }
  65. },
  66. /**
  67. * @returns {Object}
  68. */
  69. initialize: function () {
  70. this._super()
  71. .initComponent();
  72. return this;
  73. },
  74. /**
  75. * @returns {Object}
  76. */
  77. initComponent: function () {
  78. var currentIntegration = registry.get(this.integrationName),
  79. $this = $('#' + this.id),
  80. self = this,
  81. data = {
  82. amount: $this.data('amount'),
  83. locale: $this.data('locale'),
  84. currency: $this.data('currency')
  85. },
  86. initCallback = function () {
  87. $this.attr('disabled', 'disabled');
  88. registry.remove(this.integrationName);
  89. braintree.setup(this.clientToken, 'custom', this.getClientConfig(data));
  90. $this.off('click')
  91. .on('click', function (event) {
  92. event.preventDefault();
  93. registry.get(self.integrationName, function (integration) {
  94. try {
  95. integration.paypal.initAuthFlow();
  96. } catch (e) {
  97. $this.attr('disabled', 'disabled');
  98. }
  99. });
  100. });
  101. }.bind(this);
  102. currentIntegration ?
  103. currentIntegration.teardown(initCallback) :
  104. initCallback();
  105. return this;
  106. },
  107. /**
  108. * @returns {Object}
  109. */
  110. getClientConfig: function (data) {
  111. this.clientConfig.paypal = {
  112. singleUse: true,
  113. amount: data.amount,
  114. currency: data.currency,
  115. locale: data.locale,
  116. enableShippingAddress: true,
  117. headless: true
  118. };
  119. if (this.displayName) {
  120. this.clientConfig.paypal.displayName = this.displayName;
  121. }
  122. _.each(this.clientConfig, function (fn, name) {
  123. if (typeof fn === 'function') {
  124. this.clientConfig[name] = fn.bind(this);
  125. }
  126. }, this);
  127. return this.clientConfig;
  128. }
  129. });
  130. }
  131. );