Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 1 addition & 54 deletions src/pages/api/qgiv-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,61 +97,8 @@ export const POST: APIRoute = async ({ request, locals }) => {
});
}

let donationType: "monthly" | "onetime" | null = null;

if (payload.isRecurring === "y" || payload.type === "recurring") {
donationType = "monthly";
} else if (payload.isRecurring === "n" || payload.type === "one time") {
donationType = "onetime";
}

if (donationType) {
const eventName = donationType === "monthly" ? "Monthly-donate" : "One-time-donate";

const donorIp = request.headers.get("cf-connecting-ip") ?? request.headers.get("x-forwarded-for") ?? "";

const plausibleRequest = fetch("https://plausible.io/api/event", {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (compatible; T4P-Webhook/1.0)",
...(donorIp && { "X-Forwarded-For": donorIp }),
},
body: JSON.stringify({
domain: "techforpalestine.org",
name: eventName,
url: "https://techforpalestine.org/donate",
props: {
source: "webhook",
form: String(payload.form?.name || "Unknown"),
amount: String(payload.value ?? payload.donationAmount ?? "0"),
transactionId: String(payload.id ?? payload.transactionId ?? ""),
},
}),
})
.then(async (res) => {
const dropped = res.headers.get("x-plausible-dropped");
if (!res.ok) {
const body = await res.text();
reportError(new Error(`Plausible API error: ${res.status}`), { context: "Plausible API", eventName, body });
} else if (dropped === "1") {
reportError(new Error("Plausible dropped the event"), { context: "Plausible API", eventName, donorIp });
} else {
console.log(`✅ Plausible event sent: ${eventName}`);
}
})
.catch((err) => reportError(err, { context: "Plausible API", eventName }));

ctx?.waitUntil(plausibleRequest);

return new Response(
JSON.stringify({ success: true, donationType, eventName, formId, message: "Donation tracked" }),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
}

return new Response(
JSON.stringify({ success: true, message: "Webhook received but donation type unclear" }),
JSON.stringify({ success: true, message: "Webhook received" }),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
} catch (error) {
Expand Down
18 changes: 16 additions & 2 deletions src/pages/donate.astro
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,23 @@ const isUK = country === "GB";
});
}

// Temporary: log full donationComplete payload to find recurring field.
document.addEventListener("QGIV.donationComplete", function (event) {
console.log("[QGIV donationComplete]", JSON.stringify(event.detail));
var data = (event instanceof CustomEvent && event.detail) ? event.detail : {};
var transaction = (data.QGIV && data.QGIV.transaction) ? data.QGIV.transaction : {};

if (transaction.status !== "Accepted") return;

var isRecurring = transaction.recurring === true ||
(typeof transaction.frequency === "string" &&
transaction.frequency.toLowerCase() !== "one time");

var donationEvent = isRecurring ? "Monthly-donate" : "One-time-donate";

if (typeof window.plausible !== "undefined") {
window.plausible(donationEvent, {
props: { source: "qgiv-embed", amount: String(transaction.total || "") },
});
}
});

// Track form variant (safely)
Expand Down
Loading