Autopay offered to clients: database 4.3.7 adds settings.config_invoice_offer_autopay and recurring_invoices.recurring_invoice_offer_autopay, an invoice from a recurring invoice with no autopay offers Set up autopay in its email and on its guest and portal pages, guest/guest_autopay.php sets it up with no sign-in through a Stripe Checkout Session in setup mode (name, email and agreement kept in its metadata and the recurring invoice's history), the card is saved and autopay turned on when Stripe sends them back, agents are told and the client gets a confirmation, the portal turns it on with a saved card, Settings > Invoices and each recurring invoice's Autopay card can turn the offer off; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-12-01 14:55:29 -05:00
parent 0b770dcdf0
commit 25dac4cee2
19 changed files with 872 additions and 6 deletions

View File

@@ -0,0 +1,83 @@
/*
* Setting up autopay from an invoice's link (guest/guest_autopay.php). Their
* name, email and agreement go to guest_ajax.php (guest_autopay_checkout),
* which answers with a Checkout Session's client secret; Stripe's card form
* then takes the place of the form, and sends them on to the return address
* once the card is added. Anything that stops it is said on the page.
*/
(function () {
'use strict';
var form = document.querySelector('form[data-autopay-form]');
if (!form) {
return;
}
var holder = document.querySelector('[data-autopay-checkout]');
var errorBox = form.querySelector('[data-autopay-error]');
var submit = form.querySelector('[data-autopay-submit]');
var started = false;
function show(el, on) {
if (el) {
el.hidden = !on;
}
}
function busy(on) {
submit.disabled = on;
show(form.querySelector('[data-autopay-busy]'), on);
show(form.querySelector('[data-autopay-idle]'), !on);
}
function fail(message) {
errorBox.textContent = message;
show(errorBox, true);
busy(false);
}
form.addEventListener('submit', function (e) {
e.preventDefault();
show(errorBox, false);
if (!form.checkValidity()) {
form.classList.add('was-validated');
if (!form.querySelector('[name="agree"]').checked) {
fail('Tick the box to agree to autopay first.');
}
return;
}
if (started) {
return;
}
if (typeof window.Stripe !== 'function') {
show(document.querySelector('[data-autopay-unavailable]'), true);
return;
}
busy(true);
var problem = '';
var stripe = window.Stripe(form.getAttribute('data-stripe-key'));
stripe.initEmbeddedCheckout({
fetchClientSecret: function () {
return fetch('guest_ajax.php?guest_autopay_checkout=1', {method: 'POST', body: new FormData(form), credentials: 'same-origin'})
.then(function (response) {
return response.json().catch(function () {
return {};
});
})
.then(function (data) {
if (!data.clientSecret) {
problem = data.error || '';
throw new Error(problem || 'No checkout');
}
return data.clientSecret;
});
}
}).then(function (checkout) {
started = true;
show(form, false);
show(holder, true);
checkout.mount('#autopay-checkout');
}).catch(function () {
fail(problem || "The card form couldn't start - try again in a minute, or get in touch.");
});
});
})();