follow webhook redirect: parse response and open checkout URL

This commit is contained in:
Oliver
2026-06-07 18:56:24 -03:00
parent 594f17801d
commit 97a48238b8
+22 -1
View File
@@ -2417,7 +2417,7 @@
} }
try { try {
await fetch(ORDER_WEBHOOK, { const res = await fetch(ORDER_WEBHOOK, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
@@ -2434,6 +2434,27 @@
utm_content: document.getElementById('utm_content').value || null, utm_content: document.getElementById('utm_content').value || null,
}), }),
}); });
if (!res.ok) { console.error('Webhook error:', res.status); return; }
// Try to parse the response for a redirect URL
const ct = res.headers.get('content-type') || '';
let data;
if (ct.indexOf('json') !== -1) {
data = await res.json();
} else {
data = await res.text();
}
const checkoutUrl = (data && typeof data === 'object')
? (data.url || data.checkout_url || data.checkoutUrl || data.redirect || data.redirect_url)
: null;
if (checkoutUrl && checkoutUrl.startsWith('http')) {
window.open(checkoutUrl, '_blank');
} else if (typeof data === 'string' && data.startsWith('http')) {
window.open(data, '_blank');
}
} catch (err) { } catch (err) {
console.error('Order webhook failed:', err); console.error('Order webhook failed:', err);
} }