Advertisement
Magery183

something something

Nov 17th, 2024 (edited)
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. import { Environment, Paddle } from "npm:@paddle/paddle-node-sdk@1.9.1";
  2. import { createClient } from "npm:@supabase/supabase-js";
  3.  
  4. const paddle = new Paddle(Deno.env.get("PADDLE_API_KEY")!, {
  5. environment: Environment.sandbox,
  6. });
  7.  
  8. const supabase = createClient(Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!);
  9.  
  10. Deno.serve(async (req: Request) => {
  11. const payload = await req.json();
  12.  
  13. const { data } = payload;
  14.  
  15. const subscription = await paddle.subscriptions.get(data.id);
  16.  
  17. if (subscription.status === "active") {
  18. const userId = subscription.customData?.userId;
  19. console.log("Syncing user with paddle..", userId, subscription.customerId);
  20. if (!userId) {
  21. console.log("No userId found in subscription custom data");
  22. return new Response("OK", { status: 200 });
  23. }
  24. const { data: userData, error } = await supabase.from("user_info").select().eq("id", userId)
  25.  
  26. if (error) {
  27. console.error(error);
  28. return new Response("OK", { status: 200 });
  29. }
  30.  
  31. if (userData?.length === 0 || userData === null) return new Response("OK", { status: 200 });
  32.  
  33. if (userData[0].paddle_customer_id === (subscription.customerId)) {
  34. const customer = await paddle.customers.get(subscription.customerId);
  35. if (!customer.customData?.userId === userId) {
  36. await paddle.customers.update(subscription.customerId, {
  37. customData: {
  38. userId
  39. }
  40. })
  41. }
  42. }
  43.  
  44. await supabase.from("user_info").update({
  45. paddle_customer_id: subscription.customerId
  46. }).eq("id", userId)
  47.  
  48. await paddle.customers.update(subscription.customerId, {
  49. customData: {
  50. userId
  51. }
  52. })
  53.  
  54. console.log("User synced with paddle", userId, subscription.customerId);
  55. }
  56.  
  57. return new Response("OK", { status: 200 });
  58. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement