express-checkout-smart-buttons.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore',
  7. 'paypalInContextExpressCheckout'
  8. ], function (_, paypal) {
  9. 'use strict';
  10. /**
  11. * Returns array of allowed funding
  12. *
  13. * @param {Object} config
  14. * @return {Array}
  15. */
  16. function getFunding(config) {
  17. return _.map(config, function (name) {
  18. return paypal.FUNDING[name];
  19. });
  20. }
  21. return function (clientConfig, element) {
  22. paypal.Button.render({
  23. env: clientConfig.environment,
  24. client: clientConfig.client,
  25. locale: clientConfig.locale,
  26. funding: {
  27. allowed: getFunding(clientConfig.allowedFunding),
  28. disallowed: getFunding(clientConfig.disallowedFunding)
  29. },
  30. style: clientConfig.styles,
  31. // Enable Pay Now checkout flow (optional)
  32. commit: clientConfig.commit,
  33. /**
  34. * Validate payment method
  35. *
  36. * @param {Object} actions
  37. */
  38. validate: function (actions) {
  39. clientConfig.rendererComponent.validate(actions);
  40. },
  41. /**
  42. * Execute logic on Paypal button click
  43. */
  44. onClick: function () {
  45. clientConfig.rendererComponent.onClick();
  46. },
  47. /**
  48. * Set up a payment
  49. *
  50. * @return {*}
  51. */
  52. payment: function () {
  53. var params = {
  54. 'quote_id': clientConfig.quoteId,
  55. 'customer_id': clientConfig.customerId || '',
  56. 'form_key': clientConfig.formKey,
  57. button: clientConfig.button
  58. };
  59. return new paypal.Promise(function (resolve, reject) {
  60. clientConfig.rendererComponent.beforePayment(resolve, reject).then(function () {
  61. paypal.request.post(clientConfig.getTokenUrl, params).then(function (res) {
  62. return clientConfig.rendererComponent.afterPayment(res, resolve, reject);
  63. }).catch(function (err) {
  64. return clientConfig.rendererComponent.catchPayment(err, resolve, reject);
  65. });
  66. });
  67. });
  68. },
  69. /**
  70. * Execute the payment
  71. *
  72. * @param {Object} data
  73. * @param {Object} actions
  74. * @return {*}
  75. */
  76. onAuthorize: function (data, actions) {
  77. var params = {
  78. paymentToken: data.paymentToken,
  79. payerId: data.payerID,
  80. quoteId: clientConfig.quoteId || '',
  81. customerId: clientConfig.customerId || '',
  82. 'form_key': clientConfig.formKey
  83. };
  84. return new paypal.Promise(function (resolve, reject) {
  85. clientConfig.rendererComponent.beforeOnAuthorize(resolve, reject, actions).then(function () {
  86. paypal.request.post(clientConfig.onAuthorizeUrl, params).then(function (res) {
  87. clientConfig.rendererComponent.afterOnAuthorize(res, resolve, reject, actions);
  88. }).catch(function (err) {
  89. return clientConfig.rendererComponent.catchOnAuthorize(err, resolve, reject);
  90. });
  91. });
  92. });
  93. },
  94. /**
  95. * Process cancel action
  96. *
  97. * @param {Object} data
  98. * @param {Object} actions
  99. */
  100. onCancel: function (data, actions) {
  101. clientConfig.rendererComponent.onCancel(data, actions);
  102. },
  103. /**
  104. * Process errors
  105. */
  106. onError: function (err) {
  107. clientConfig.rendererComponent.onError(err);
  108. }
  109. }, element);
  110. };
  111. });