use hidden form POST w/ target=_blank so 302 redirect opens in new tab, no CORS
This commit is contained in:
+26
-42
@@ -2417,54 +2417,38 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(ORDER_WEBHOOK, {
|
// Use a hidden form POST so the 302 redirect opens naturally in the new tab
|
||||||
method: 'POST',
|
const form = document.createElement('form');
|
||||||
redirect: 'manual',
|
form.method = 'POST';
|
||||||
headers: { 'Content-Type': 'application/json' },
|
form.action = ORDER_WEBHOOK;
|
||||||
body: JSON.stringify({
|
form.target = '_blank';
|
||||||
|
form.style.display = 'none';
|
||||||
|
|
||||||
|
const fields = {
|
||||||
product: product,
|
product: product,
|
||||||
agent: agent,
|
agent: agent,
|
||||||
period: period,
|
period: period,
|
||||||
location: location,
|
location: location,
|
||||||
email: email || null,
|
email: email || '',
|
||||||
coupon: coupon || null,
|
coupon: coupon || '',
|
||||||
utm_source: document.getElementById('utm_source').value || null,
|
utm_source: document.getElementById('utm_source').value || '',
|
||||||
utm_medium: document.getElementById('utm_medium').value || null,
|
utm_medium: document.getElementById('utm_medium').value || '',
|
||||||
utm_campaign: document.getElementById('utm_campaign').value || null,
|
utm_campaign: document.getElementById('utm_campaign').value || '',
|
||||||
utm_term: document.getElementById('utm_term').value || null,
|
utm_term: document.getElementById('utm_term').value || '',
|
||||||
utm_content: document.getElementById('utm_content').value || null,
|
utm_content: document.getElementById('utm_content').value || '',
|
||||||
}),
|
};
|
||||||
|
|
||||||
|
Object.keys(fields).forEach(function (key) {
|
||||||
|
const inp = document.createElement('input');
|
||||||
|
inp.type = 'hidden';
|
||||||
|
inp.name = key;
|
||||||
|
inp.value = fields[key];
|
||||||
|
form.appendChild(inp);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3xx redirect — extract the Location header and open it
|
document.body.appendChild(form);
|
||||||
if (res.type === 'opaqueredirect' || (res.status >= 300 && res.status < 400)) {
|
form.submit();
|
||||||
const location = res.headers.get('Location');
|
document.body.removeChild(form);
|
||||||
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; }
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user