paypal-checkout.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'Magento_Ui/js/modal/confirm',
  8. 'Magento_Customer/js/customer-data',
  9. 'jquery/ui',
  10. 'mage/mage'
  11. ], function ($, confirm, customerData) {
  12. 'use strict';
  13. $.widget('mage.paypalCheckout', {
  14. options: {
  15. originalForm:
  16. 'form:not(#product_addtocart_form_from_popup):has(input[name="product"][value=%1])',
  17. productId: 'input[type="hidden"][name="product"]',
  18. ppCheckoutSelector: '[data-role=pp-checkout-url]',
  19. ppCheckoutInput: '<input type="hidden" data-role="pp-checkout-url" name="return_url" value=""/>'
  20. },
  21. /**
  22. * Initialize store credit events
  23. * @private
  24. */
  25. _create: function () {
  26. this.element.on('click', '[data-action="checkout-form-submit"]', $.proxy(function (e) {
  27. var $target = $(e.target),
  28. returnUrl = $target.data('checkout-url'),
  29. productId = $target.closest('form').find(this.options.productId).val(),
  30. originalForm = this.options.originalForm.replace('%1', productId),
  31. self = this,
  32. billingAgreement = customerData.get('paypal-billing-agreement');
  33. e.preventDefault();
  34. if (billingAgreement().askToCreate) {
  35. confirm({
  36. content: billingAgreement().confirmMessage,
  37. actions: {
  38. /**
  39. * Confirmation handler
  40. *
  41. */
  42. confirm: function () {
  43. returnUrl = billingAgreement().confirmUrl;
  44. self._redirect(returnUrl, originalForm);
  45. },
  46. /**
  47. * Cancel confirmation handler
  48. *
  49. */
  50. cancel: function (event) {
  51. if (event && !$(event.target).hasClass('action-close')) {
  52. self._redirect(returnUrl);
  53. }
  54. }
  55. }
  56. });
  57. } else {
  58. this._redirect(returnUrl, originalForm);
  59. }
  60. }, this));
  61. },
  62. /**
  63. * Redirect to certain url, with optional form
  64. * @param {String} returnUrl
  65. * @param {HTMLElement} originalForm
  66. *
  67. */
  68. _redirect: function (returnUrl, originalForm) {
  69. var $form,
  70. ppCheckoutInput;
  71. if (this.options.isCatalogProduct) {
  72. // find the form from which the button was clicked
  73. $form = originalForm ? $(originalForm) : $($(this.options.shortcutContainerClass).closest('form'));
  74. ppCheckoutInput = $form.find(this.options.ppCheckoutSelector)[0];
  75. if (!ppCheckoutInput) {
  76. ppCheckoutInput = $(this.options.ppCheckoutInput);
  77. ppCheckoutInput.appendTo($form);
  78. }
  79. $(ppCheckoutInput).val(returnUrl);
  80. $form.submit();
  81. } else {
  82. $.mage.redirect(returnUrl);
  83. }
  84. }
  85. });
  86. return $.mage.paypalCheckout;
  87. });