let attempts = 0;
const maxAttempts = 50;
console.log("Begin TryPushFunc");
setTimeout(function(){
function tryPushOrder() {
attempts++;
console.log("Attempting to fetch Zeffy payment data, attempt #" + attempts);
fetch("/wp-json/zeffy/v1/latest-payment")
.then((payment) => {
console.log("Raw payment response:", payment);
if (!payment) return;
console.log("Fetched payment data:", payment);
if (payment.error || !payment.order_id) {
if (attempts < maxAttempts) {
setTimeout(tryPushOrder, 1000);
} else {
console.error(
"Zeffy payment data never arrived after " +
maxAttempts +
" attempts"
);
}
return;
}
function uuidToInt(uuid) {
let hash = 0;
const str = uuid.replace(/-/g, ""); // remove hyphens
for (let i = 0; i < str.length; i++) {
hash = (Math.imul(31, hash) + str.charCodeAt(i)) | 0;
}
return Math.abs(hash);
}
const orderId = uuidToInt(payment.order_id);
console.log("Converted order ID:", orderId);
try {
_upf.push([
"order",
{
order_id: orderId,
order_name: "#" + payment.order_id,
amount: payment.amount,
currency: payment.currency,
items: [
{
name: payment.description,
amount: payment.amount,
currency: payment.currency,
},
],
customer: {
email: payment.email,
first_name: payment.first_name,
last_name: payment.last_name,
},
},
]);
} catch (error) {
console.error("Error pushing order:", error);
}
})
.catch((err) => {
console.error("Fetch error:", err);
if (attempts < maxAttempts) setTimeout(tryPushOrder, 3000);
});
window.addEventListener("load", tryPushOrder);
}
}, 7000);
Skip to content