const PAYMENT_INSTRUCTION_METHODS = new Set([
"va",
"qris",
"card",
"invoice",
"alfamart",
"ovo",
"dana",
"shopeepay",
"linkaja",
"gopay"
]);
const SCALEV_QUERY_FORWARD_HOSTS = new Set([
"scalev.com",
"scalev.id",
"app.scalev.id",
"app.scalev.com"
]);
function paymentMethodForRedirect(paymentMethod) {
if (typeof paymentMethod !== "string") return paymentMethod;
if (paymentMethod.toLowerCase().startsWith("va:")) return "va";
if (paymentMethod.toLowerCase().startsWith("bt:")) return "bank_transfer";
return paymentMethod;
}
function publicOrderUrl(order) {
if (order.publicOrderUrl) return order.publicOrderUrl;
return new URL(`/o/${order.secretSlug}`, window.location.origin).toString();
}
function paymentInstructionUrl(order) {
if (order.status === "draft") return publicOrderUrl(order);
if (order.paymentUrl) return order.paymentUrl;
const url = new URL(publicOrderUrl(order));
url.pathname = `${url.pathname.replace(/\/$/, "")}/success`;
return url.toString();
}
function whatsappUrl(phone, order, fallbackMessage = "") {
const cleanPhone = String(phone || "").replace(/[^\d]/g, "");
const message = order.chatMessage || encodeURIComponent(fallbackMessage);
return `https://api.whatsapp.com/send/?phone=${cleanPhone}&text=${message}`;
}
function withCurrentQuery(url) {
const nextUrl = new URL(url, window.location.href);
const currentParams = new URLSearchParams(window.location.search);
currentParams.forEach((value, key) => {
nextUrl.searchParams.set(key, value);
});
return nextUrl.toString();
}
function maybeForwardCurrentQuery(url) {
const nextUrl = new URL(url, window.location.href);
const shouldForward =
nextUrl.host === window.location.host ||
SCALEV_QUERY_FORWARD_HOSTS.has(nextUrl.host);
return shouldForward
? withCurrentQuery(nextUrl.toString())
: nextUrl.toString();
}
function navigateAfterOrder(url) {
if (window.self !== window.top) {
window.parent.postMessage(url, "*");
return;
}
window.location.assign(url);
}
function redirectAfterOrder({
type,
order,
paymentMethod,
customWhatsappPhone,
otherPagePath,
customUrl
}) {
const redirectPaymentMethod = paymentMethodForRedirect(
paymentMethod || order.paymentMethod
);
const finalType = PAYMENT_INSTRUCTION_METHODS.has(redirectPaymentMethod)
? "success_page"
: type;
if (finalType === "success_page") {
return navigateAfterOrder(paymentInstructionUrl(order));
}
if (finalType === "direct_to_whatsapp") {
const phone = order.handlerPhone;
return navigateAfterOrder(
phone ? whatsappUrl(phone, order) : publicOrderUrl(order)
);
}
if (finalType === "direct_to_custom_whatsapp") {
return navigateAfterOrder(whatsappUrl(customWhatsappPhone, order));
}
if (finalType === "other_page") {
return navigateAfterOrder(withCurrentQuery(otherPagePath));
}
if (finalType === "order_page") {
return navigateAfterOrder(publicOrderUrl(order));
}
if (finalType === "custom_url") {
return navigateAfterOrder(maybeForwardCurrentQuery(customUrl));
}
return navigateAfterOrder(paymentInstructionUrl(order));
}