express-checkout-wrapper.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'mage/translate',
  8. 'Magento_Customer/js/customer-data',
  9. 'Magento_Paypal/js/in-context/express-checkout-smart-buttons',
  10. 'mage/cookies'
  11. ], function ($, $t, customerData, checkoutSmartButtons) {
  12. 'use strict';
  13. return {
  14. defaults: {
  15. paymentActionError: $t('Something went wrong with your request. Please try again later.'),
  16. signInMessage: $t('To check out, please sign in with your email address.')
  17. },
  18. /**
  19. * Render PayPal buttons using checkout.js
  20. */
  21. renderPayPalButtons: function (element) {
  22. checkoutSmartButtons(this.prepareClientConfig(), element);
  23. },
  24. /**
  25. * Validate payment method
  26. *
  27. * @param {Object} actions
  28. */
  29. validate: function (actions) {
  30. this.actions = actions || this.actions;
  31. },
  32. /**
  33. * Execute logic on Paypal button click
  34. */
  35. onClick: function () {},
  36. /**
  37. * Before payment execute
  38. *
  39. * @param {Function} resolve
  40. * @param {Function} reject
  41. * @return {*}
  42. */
  43. beforePayment: function (resolve, reject) { //eslint-disable-line no-unused-vars
  44. return $.Deferred().resolve();
  45. },
  46. /**
  47. * After payment execute
  48. *
  49. * @param {Object} res
  50. * @param {Function} resolve
  51. * @param {Function} reject
  52. *
  53. * @return {*}
  54. */
  55. afterPayment: function (res, resolve, reject) {
  56. if (res.success) {
  57. return resolve(res.token);
  58. }
  59. this.addError(res['error_message']);
  60. return reject(new Error(res['error_message']));
  61. },
  62. /**
  63. * Catch payment
  64. *
  65. * @param {Error} err
  66. * @param {Function} resolve
  67. * @param {Function} reject
  68. */
  69. catchPayment: function (err, resolve, reject) {
  70. this.addError(this.paymentActionError);
  71. reject(err);
  72. },
  73. /**
  74. * Before onAuthorize execute
  75. *
  76. * @param {Function} resolve
  77. * @param {Function} reject
  78. * @param {Object} actions
  79. *
  80. * @return {jQuery.Deferred}
  81. */
  82. beforeOnAuthorize: function (resolve, reject, actions) { //eslint-disable-line no-unused-vars
  83. return $.Deferred().resolve();
  84. },
  85. /**
  86. * After onAuthorize execute
  87. *
  88. * @param {Object} res
  89. * @param {Function} resolve
  90. * @param {Function} reject
  91. * @param {Object} actions
  92. *
  93. * @return {*}
  94. */
  95. afterOnAuthorize: function (res, resolve, reject, actions) {
  96. if (res.success) {
  97. resolve();
  98. return actions.redirect(window, res.redirectUrl);
  99. }
  100. this.addError(res['error_message']);
  101. return reject(new Error(res['error_message']));
  102. },
  103. /**
  104. * Catch payment
  105. *
  106. * @param {Error} err
  107. * @param {Function} resolve
  108. * @param {Function} reject
  109. */
  110. catchOnAuthorize: function (err, resolve, reject) {
  111. this.addError(this.paymentActionError);
  112. reject(err);
  113. },
  114. /**
  115. * Process cancel action
  116. *
  117. * @param {Object} data
  118. * @param {Object} actions
  119. */
  120. onCancel: function (data, actions) {
  121. actions.redirect(window, this.clientConfig.onCancelUrl);
  122. },
  123. /**
  124. * Process errors
  125. *
  126. * @param {Error} err
  127. */
  128. onError: function (err) { //eslint-disable-line no-unused-vars
  129. // Uncaught error isn't displayed in the console
  130. },
  131. /**
  132. * Adds error message
  133. *
  134. * @param {String} message
  135. * @param {String} [type]
  136. */
  137. addError: function (message, type) {
  138. type = type || 'error';
  139. customerData.set('messages', {
  140. messages: [{
  141. type: type,
  142. text: message
  143. }],
  144. 'data_id': Math.floor(Date.now() / 1000)
  145. });
  146. },
  147. /**
  148. * @returns {String}
  149. */
  150. getButtonId: function () {
  151. return this.inContextId;
  152. },
  153. /**
  154. * Populate client config with all required data
  155. *
  156. * @return {Object}
  157. */
  158. prepareClientConfig: function () {
  159. this.clientConfig.client = {};
  160. this.clientConfig.client[this.clientConfig.environment] = this.clientConfig.merchantId;
  161. this.clientConfig.rendererComponent = this;
  162. this.clientConfig.formKey = $.mage.cookies.get('form_key');
  163. return this.clientConfig;
  164. }
  165. };
  166. });