Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

wip

Kiran K2b89ca4cf7ad
diff --git a/apps/web/app/api/campaigns/webhooks/stripe/route.ts b/apps/web/app/api/campaigns/webhooks/stripe/route.ts
index d7fe9f2414..abd259040a 100644
--- a/apps/web/app/api/campaigns/webhooks/stripe/route.ts
+++ b/apps/web/app/api/campaigns/webhooks/stripe/route.ts
@@ -1,3 +1,4 @@
+import { getCustomer } from "@/lib/planetscale";
import { stripe } from "@/lib/stripe";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { nanoid } from "@dub/utils";
@@ -6,6 +7,7 @@ import type Stripe from "stripe";
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;
const relevantEvents = new Set([
+ "customer.created",
"charge.succeeded",
"payment_intent.succeeded",
"checkout.session.completed",
@@ -21,36 +23,32 @@ export const POST = async (req: Request) => {
});
}
- try {
- const event = stripe.webhooks.constructEvent(buf, sig, webhookSecret);
-
- // Ignore unsupported events
- if (!relevantEvents.has(event.type)) {
- return new Response("OK", {
- status: 200,
- });
- }
-
- switch (event.type) {
- // case "charge.succeeded":
- // case "payment_intent.succeeded":
- case "checkout.session.completed":
- await checkoutSessionCompleted(event);
- break;
- }
+ const event = stripe.webhooks.constructEvent(buf, sig, webhookSecret);
+ // Ignore unsupported events
+ if (!relevantEvents.has(event.type)) {
return new Response("OK", {
status: 200,
});
- } catch (error: any) {
- console.log("Stripe webhook error:", error);
+ }
- return new Response("Error", {
- status: 400,
- });
+ switch (event.type) {
+ // case "charge.succeeded":
+ // case "payment_intent.succeeded":
+ case "checkout.session.completed":
+ await checkoutSessionCompleted(event);
+ break;
+ case "customer.created":
+ await customerCreated(event);
+ break;
}
+
+ return new Response("OK", {
+ status: 200,
+ });
};
+// Handle event "checkout.session.completed"
async function checkoutSessionCompleted(event: Stripe.Event) {
const charge = event.data.object as Stripe.Checkout.Session;
const customerId = charge.metadata?.dubCustomerId || null;
@@ -83,3 +81,26 @@ async function checkoutSessionCompleted(event: Stripe.Event) {
}),
});
}
+
+// Handle event "customer.created"
+async function customerCreated(event: Stripe.Event) {
+ const stripeCustomer = event.data.object as Stripe.Customer;
+ const externalId = stripeCustomer.metadata?.dubCustomerId || null;
+ const projectConnectId = event.account;
+
+ console.log("Customer created", stripeCustomer);
+
+ if (!externalId || !projectConnectId) {
+ return;
+ }
+
+ const customer = await getCustomer({ externalId, projectConnectId });
+
+ if (!customer) {
+ console.error("No customer found for `dubCustomerId`", externalId);
+ return;
+ }
+
+ // TODO:
+ // Create a customer in Tinybird + MySQL
+}
diff --git a/apps/web/lib/planetscale.ts b/apps/web/lib/planetscale.ts
index 446d550a81..275addf53d 100644
--- a/apps/web/lib/planetscale.ts
+++ b/apps/web/lib/planetscale.ts
@@ -12,6 +12,16 @@ export const pscale_config = {
export const conn = connect(pscale_config);
+type GetCustomerParams =
+ | {
+ externalId: string;
+ workspaceId: string;
+ }
+ | {
+ externalId: string;
+ projectConnectId: string;
+ };
+
export const getWorkspaceViaEdge = async (workspaceId: string) => {
if (!DATABASE_URL) return null;
@@ -197,17 +207,26 @@ export const createCustomer = async ({
);
};
-export const getCustomer = async ({
- externalId,
- workspaceId,
-}: {
- externalId: string;
- workspaceId: string;
-}) => {
- const { rows } = await conn.execute<Customer>(
- "SELECT * FROM Customer WHERE externalId = ? AND projectId = ? LIMIT 1",
- [externalId, workspaceId],
- );
+export const getCustomer = async (params: GetCustomerParams) => {
+ const { externalId } = params;
- return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
+ // By workspaceId
+ if ("workspaceId" in params) {
+ const { rows } = await conn.execute<Customer>(
+ "SELECT * FROM Customer WHERE externalId = ? AND projectId = ? LIMIT 1",
+ [externalId, params.workspaceId],
+ );
+
+ return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
+ }
+
+ // By projectConnectId
+ if ("projectConnectId" in params) {
+ const { rows } = await conn.execute<Customer>(
+ "SELECT * FROM Customer WHERE externalId = ? AND projectConnectId = ? LIMIT 1",
+ [externalId, params.projectConnectId],
+ );
+
+ return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
+ }
};
diff --git a/apps/web/scripts/stripe.ts b/apps/web/scripts/stripe.ts
new file mode 100644
index 0000000000..f1a9c14741
--- /dev/null
+++ b/apps/web/scripts/stripe.ts
@@ -0,0 +1,45 @@
+// Just for testing
+
+import "dotenv-flow/config";
+
+import Stripe from "stripe";
+
+export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || "", {
+ apiVersion: "2022-11-15",
+ appInfo: {
+ name: "Dub.co",
+ version: "0.1.0",
+ },
+});
+
+async function main() {
+ // const customer = await stripe.customers.create({
+ // email: "[email protected]",
+ // metadata: {
+ // dubCustomerId: "[email protected]",
+ // },
+ // });
+
+ // console.log(customer);
+
+ // Create checkout session for this customer
+ const { url } = await stripe.checkout.sessions.create({
+ customer_email: "[email protected]",
+ success_url: "https://dub.co?session_id={CHECKOUT_SESSION_ID}",
+ cancel_url: "https://dub.co",
+ line_items: [
+ {
+ price: "price_1PE8VYSIvbPMbbGzw4JsqFye",
+ quantity: 1,
+ },
+ ],
+ mode: "subscription",
+ metadata: {
+ dubCustomerId: "[email protected]",
+ },
+ });
+
+ console.log(url);
+}
+
+main();