| 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 + "Installing…"; |
| 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((result.data && result.data.message) || "Installation failed. Please try again."); |
| 51 |
} |
| 52 |
}) |
| 53 |
.catch(function () { |
| 54 |
btn.disabled = false; |
| 55 |
btn.style.cursor = originalCursor; |
| 56 |
btn.innerHTML = originalHTML; |
| 57 |
// eslint-disable-next-line no-alert |
| 58 |
window.alert("Installation failed. Please try again."); |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
function wpSubsInstallPaypalIntegration() { |
| 63 |
jQuery |
| 64 |
.post(wpSubsIntegrations.ajax_url, { |
| 65 |
action_callback: "wp_subs_install_paypal_integration", |
| 66 |
nonce: wpSubsIntegrations.nonce, |
| 67 |
}) |
| 68 |
.done(function (response) { |
| 69 |
console.log(response); |
| 70 |
|
| 71 |
if (response.success) { |
| 72 |
console.log("Option updated:", response.data); |
| 73 |
} else { |
| 74 |
console.error("Failed to update:", response.data); |
| 75 |
} |
| 76 |
}) |
| 77 |
.fail(function () { |
| 78 |
console.error("AJAX request failed"); |
| 79 |
}); |
| 80 |
} |
| 81 |
|