Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Environment, Paddle } from "npm:@paddle/paddle-node-sdk@1.9.1";
- import { createClient } from "npm:@supabase/supabase-js";
- const paddle = new Paddle(Deno.env.get("PADDLE_API_KEY")!, {
- environment: Environment.sandbox,
- });
- const supabase = createClient(Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!);
- Deno.serve(async (req: Request) => {
- const payload = await req.json();
- const { data } = payload;
- const subscription = await paddle.subscriptions.get(data.id);
- if (subscription.status === "active") {
- const userId = subscription.customData?.userId;
- console.log("Syncing user with paddle..", userId, subscription.customerId);
- if (!userId) {
- console.log("No userId found in subscription custom data");
- return new Response("OK", { status: 200 });
- }
- const { data: userData, error } = await supabase.from("user_info").select().eq("id", userId)
- if (error) {
- console.error(error);
- return new Response("OK", { status: 200 });
- }
- if (userData?.length === 0 || userData === null) return new Response("OK", { status: 200 });
- if (userData[0].paddle_customer_id === (subscription.customerId)) {
- const customer = await paddle.customers.get(subscription.customerId);
- if (!customer.customData?.userId === userId) {
- await paddle.customers.update(subscription.customerId, {
- customData: {
- userId
- }
- })
- }
- }
- await supabase.from("user_info").update({
- paddle_customer_id: subscription.customerId
- }).eq("id", userId)
- await paddle.customers.update(subscription.customerId, {
- customData: {
- userId
- }
- })
- console.log("User synced with paddle", userId, subscription.customerId);
- }
- return new Response("OK", { status: 200 });
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement