follow webhook redirect: parse response and open checkout URL
This commit is contained in:
+22
-1
@@ -2417,7 +2417,7 @@
|
||||
}
|
||||
|
||||
try {
|
||||
await fetch(ORDER_WEBHOOK, {
|
||||
const res = await fetch(ORDER_WEBHOOK, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -2434,6 +2434,27 @@
|
||||
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) {
|
||||
console.error('Order webhook failed:', err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user