Skip to content

Instantly share code, notes, and snippets.

@enzoftware
Created April 17, 2025 20:12
Show Gist options
  • Save enzoftware/3789a57661f9ac1af6e9f57a60ca6f04 to your computer and use it in GitHub Desktop.
Save enzoftware/3789a57661f9ac1af6e9f57a60ca6f04 to your computer and use it in GitHub Desktop.
// Follow this setup guide to integrate the Deno language server with your editor:
// https://deno.land/manual/getting_started/setup_your_environment
// This enables autocomplete, go to definition, etc.
// Setup type definitions for built-in Supabase Runtime APIs
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
Deno.serve(async (req)=>{
const productAndroidYearly = 'wordia_pro_1:yearly-autorenew';
const productAndroidMonthly = 'wordia_pro_1:monthly-autorenew';
const productIosYearly = 'wordia_3599_1y_7dtrial';
const productIosMonthly = 'wordia_499_1m';
const isYearly = (productId)=>{
return productId === productAndroidYearly || productId === productIosYearly;
};
const isMonthly = (productId)=>{
return productId === productAndroidMonthly || productId === productIosMonthly;
};
const tdAppId = "c845f2a9c07441cdbc586e04ca295088";
const tdServerUrl = "https://ta-upload.hellotalk8.com";
function isRenewalEvent(event) {
return event.event.type === "RENEWAL";
}
function isCancellationEvent(event) {
return event.event.type === "CANCELLATION";
}
try {
// Parse the JSON body
const body = await req.json();
// Process based on the `type` field
if (isRenewalEvent(body)) {
console.log("Processing RENEWAL event...");
handleRenewalEvent(body);
} else if (isCancellationEvent(body)) {
console.log("Processing CANCELLATION event...");
handleCancellationEvent(body);
} else {
console.error("Unknown event type:", body.event.type);
return new Response(JSON.stringify({
error: "Unknown event type"
}), {
status: 400,
headers: {
"Content-Type": "application/json"
}
});
}
return new Response(JSON.stringify({
message: "Event processed successfully"
}), {
headers: {
"Content-Type": "application/json"
},
status: 200
});
} catch (error) {
console.log("Error on request:", error);
throw new Response(JSON.stringify({
error: "Invalid request",
message: error
}), {
status: 400,
headers: {
"Content-Type": "application/json"
}
});
}
async function handleRenewalEvent(renewalEvent) {
console.log("Renewal Event:", renewalEvent);
const payload = {
appid: tdAppId,
data: {
"#type": "track",
"#event_name": "AutoRenewal",
"#time": new Date(renewalEvent.event.event_timestamp_ms).toISOString(),
"properties": {
"usd_income": renewalEvent.event.price_in_purchased_currency,
"product_id": renewalEvent.event.product_id,
"product_type": isYearly(renewalEvent.event.product_id) ? "Yearly" : "Monthly",
"pay_way": renewalEvent.event.store === "PLAY_STORE" ? "Google Play" : "Apple Store"
}
}
};
const response = await fetch(`${tdServerUrl}/sync_json`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
if (response.ok) {
console.log(" Thinking Data - AutoRenewal event sent successfully.", await response.json());
} else {
console.error("Thinking Data - Failed to send AutoRenewal event:", await response.text());
}
}
async function handleCancellationEvent(cancelEvent) {
console.log("Cancellation Event:", cancelEvent);
const payload = {
appid: tdAppId,
data: {
"#type": "track",
"#event_name": "Refund",
"#time": new Date(cancelEvent.event.event_timestamp_ms).toISOString(),
"properties": {
"usd_income": cancelEvent.event.price_in_purchased_currency,
"product_id": cancelEvent.event.product_id,
"product_type": isYearly(cancelEvent.event.product_id) ? "Yearly" : "Monthly",
"pay_way": cancelEvent.event.store === "PLAY_STORE" ? "Google Play" : "Apple Store",
"cancel_reason": cancelEvent.event.cancel_reason
}
}
};
const response = await fetch(`${tdServerUrl}/sync_json`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
if (response.ok) {
console.log(" Thinking Data - Refund event sent successfully.", await response.json());
} else {
console.error(" Thinking Data - Failed to send Refund event:", await response.text());
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment