PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.4.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.4.0
2.4.0 2.3.0 2.2.0 2.1.0 2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 All 65 releases
subscription / assets / js / integration_settings.js

integration_settings.js in Subscriptions for WooCommerce with Stripe Recurring Payments 2.4.0, at assets/js/integration_settings.js

143 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global subscrptIntegrations */
2
3 var SUBSCRPT_SPINNER_SVG =
4 '<svg xmlns="http://www.w3.org/2000/svg"' +
5 ' style="animation:subscrptSpin 0.75s linear infinite;vertical-align:-3px;margin-right:5px;display:inline-block;"' +
6 ' width="13" height="13" viewBox="0 0 13 13" fill="none">' +
7 '<circle cx="6.5" cy="6.5" r="5" stroke="currentColor" stroke-opacity="0.25" stroke-width="2"/>' +
8 '<path d="M6.5 1.5A5 5 0 0 1 11.5 6.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>' +
9 "</svg>";
10
11 (function () {
12 var style = document.createElement("style");
13 style.textContent = "@keyframes subscrptSpin{to{transform:rotate(360deg)}}";
14 document.head.appendChild(style);
15 })();
16
17 /**
18 * One-click install and activate a payment gateway plugin from the integrations page.
19 *
20 * @param {HTMLButtonElement} btn The clicked button element.
21 * @param {string} slug The wp.org plugin slug.
22 */
23 function subscrptInstallPlugin(btn, slug) {
24 var originalHTML = btn.innerHTML;
25 var originalCursor = btn.style.cursor;
26
27 btn.disabled = true;
28 btn.style.cursor = "wait";
29 btn.innerHTML = SUBSCRPT_SPINNER_SVG + (btn.dataset.busyLabel || wp.i18n.__("Installing…", "subscription"));
30
31 var data = new FormData();
32 data.append("action", "subscrpt_install_integration_plugin");
33 data.append("plugin_slug", slug);
34 data.append("nonce", subscrptIntegrations.nonce);
35
36 fetch(subscrptIntegrations.ajaxUrl, { method: "POST", body: data })
37 .then(function (response) {
38 return response.json();
39 })
40 .then(function (result) {
41 if (result.success) {
42 var url = new URL(window.location.href);
43 url.searchParams.set("subscrpt_installed", "1");
44 window.location.href = url.toString();
45 } else {
46 btn.disabled = false;
47 btn.style.cursor = originalCursor;
48 btn.innerHTML = originalHTML;
49 // eslint-disable-next-line no-alert
50 window.alert(
51 (result.data && result.data.message) || wp.i18n.__("Installation failed. Please try again.", "subscription"),
52 );
53 }
54 })
55 .catch(function () {
56 btn.disabled = false;
57 btn.style.cursor = originalCursor;
58 btn.innerHTML = originalHTML;
59 // eslint-disable-next-line no-alert
60 window.alert(wp.i18n.__("Installation failed. Please try again.", "subscription"));
61 });
62 }
63
64 /**
65 * Links and buttons that cannot carry an inline handler — e.g. inside a settings field
66 * description, which is passed through wp_kses_post() — declare the slug in
67 * data-subscrpt-install-plugin instead.
68 */
69 document.addEventListener("click", function (event) {
70 var btn = event.target.closest("[data-subscrpt-install-plugin]");
71 if (!btn || btn.disabled) {
72 return;
73 }
74 event.preventDefault();
75 subscrptInstallPlugin(btn, btn.dataset.subscrptInstallPlugin);
76 });
77
78 /**
79 * Activate an already installed integration plugin from the integrations page.
80 *
81 * @param {HTMLButtonElement} btn The clicked button element.
82 * @param {string} pluginFile The plugin basename, e.g. "tutor/tutor.php".
83 */
84 function subscrptActivatePlugin(btn, pluginFile) {
85 var originalHTML = btn.innerHTML;
86 var originalCursor = btn.style.cursor;
87
88 btn.disabled = true;
89 btn.style.cursor = "wait";
90 btn.innerHTML = SUBSCRPT_SPINNER_SVG + "Activating…";
91
92 var data = new FormData();
93 data.append("action", "subscrpt_activate_integration_plugin");
94 data.append("plugin_file", pluginFile);
95 data.append("nonce", subscrptIntegrations.nonce);
96
97 fetch(subscrptIntegrations.ajaxUrl, { method: "POST", body: data })
98 .then(function (response) {
99 return response.json();
100 })
101 .then(function (result) {
102 if (result.success) {
103 var url = new URL(window.location.href);
104 url.searchParams.delete("subscrpt_installed");
105 url.searchParams.set("subscrpt_activated", "1");
106 window.location.href = url.toString();
107 } else {
108 btn.disabled = false;
109 btn.style.cursor = originalCursor;
110 btn.innerHTML = originalHTML;
111 // eslint-disable-next-line no-alert
112 window.alert((result.data && result.data.message) || "Activation failed. Please try again.");
113 }
114 })
115 .catch(function () {
116 btn.disabled = false;
117 btn.style.cursor = originalCursor;
118 btn.innerHTML = originalHTML;
119 // eslint-disable-next-line no-alert
120 window.alert("Activation failed. Please try again.");
121 });
122 }
123
124 function wpSubsInstallPaypalIntegration() {
125 jQuery
126 .post(wpSubsIntegrations.ajax_url, {
127 action_callback: "wp_subs_install_paypal_integration",
128 nonce: wpSubsIntegrations.nonce,
129 })
130 .done(function (response) {
131 console.log(response);
132
133 if (response.success) {
134 console.log("Option updated:", response.data);
135 } else {
136 console.error("Failed to update:", response.data);
137 }
138 })
139 .fail(function () {
140 console.error("AJAX request failed");
141 });
142 }
143