fix CORS on webhook redirect: use redirect=manual, extract Location header

This commit is contained in:
Oliver
2026-06-07 19:03:24 -03:00
parent 97a48238b8
commit b07233f1bf
+11 -1
View File
@@ -2419,6 +2419,7 @@
try { try {
const res = await fetch(ORDER_WEBHOOK, { const res = await fetch(ORDER_WEBHOOK, {
method: 'POST', method: 'POST',
redirect: 'manual',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
product: product, product: product,
@@ -2435,9 +2436,18 @@
}), }),
}); });
// 3xx redirect — extract the Location header and open it
if (res.type === 'opaqueredirect' || (res.status >= 300 && res.status < 400)) {
const location = res.headers.get('Location');
if (location && location.startsWith('http')) {
window.open(location, '_blank');
return;
}
}
// Non-redirect: try parsing the body for a URL
if (!res.ok) { console.error('Webhook error:', res.status); return; } 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') || ''; const ct = res.headers.get('content-type') || '';
let data; let data;
if (ct.indexOf('json') !== -1) { if (ct.indexOf('json') !== -1) {