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.

add transfer + org transfers (for fees)

Steven Teyd7cc1e0ecc4b
diff --git a/apps/web/app/api/dots/webhook/route.ts b/apps/web/app/api/dots/webhook/route.ts
new file mode 100644
index 0000000000..98ebe0c535
--- /dev/null
+++ b/apps/web/app/api/dots/webhook/route.ts
@@ -0,0 +1,17 @@
+import { NextRequest, NextResponse } from "next/server";
+
+export async function POST(req: NextRequest) {
+ const body = await req.json();
+
+ const { event } = body;
+
+ let response = "OK";
+
+ switch (event) {
+ case "payout.created":
+ // await payoutCreated(body);
+ break;
+ }
+
+ return NextResponse.json({ response });
+}
diff --git a/apps/web/lib/actions/create-dots-transfer.ts b/apps/web/lib/actions/create-dots-transfer.ts
new file mode 100644
index 0000000000..f30512e44d
--- /dev/null
+++ b/apps/web/lib/actions/create-dots-transfer.ts
@@ -0,0 +1,39 @@
+"use server";
+
+import { z } from "zod";
+import { createOrgTransfer } from "../dots/create-org-transfer";
+import { createTransfer } from "../dots/create-transfer";
+import { authActionClient } from "./safe-action";
+
+const schema = z.object({
+ dotsUserId: z.string(),
+ amount: z.number(),
+ workspaceId: z.string(),
+});
+
+export const createDotsTransferAction = authActionClient
+ .schema(schema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace } = ctx;
+ const { dotsUserId, amount } = parsedInput;
+
+ if (!workspace.dotsAppId) {
+ throw new Error("Dots app not found");
+ }
+
+ const res = await Promise.all([
+ createTransfer({
+ amount,
+ dotsAppId: workspace.dotsAppId,
+ dotsUserId,
+ }),
+ // each transfer incurs a fee of $1 + 2% with a cap of $20 (2000 cents)
+ createOrgTransfer({
+ amount: Math.min(amount * 0.02 + 100, 2000),
+ dotsAppId: workspace.dotsAppId,
+ }),
+ ]);
+ console.log("res", res);
+
+ return res;
+ });
diff --git a/apps/web/lib/dots/create-org-transfer.ts b/apps/web/lib/dots/create-org-transfer.ts
new file mode 100644
index 0000000000..14b742cd1e
--- /dev/null
+++ b/apps/web/lib/dots/create-org-transfer.ts
@@ -0,0 +1,27 @@
+import { DOTS_API_URL } from "./env";
+import { dotsHeaders } from "./utils";
+
+export const createOrgTransfer = async ({
+ amount,
+ dotsAppId,
+}: {
+ amount: number;
+ dotsAppId: string;
+}) => {
+ console.log(`Creating an org transfer of ${amount} cents`);
+ const response = await fetch(`${DOTS_API_URL}/organization-transfers`, {
+ method: "POST",
+ headers: dotsHeaders(),
+ body: JSON.stringify({
+ amount,
+ api_app_id: dotsAppId,
+ }),
+ });
+
+ if (!response.ok) {
+ const error = await response.json();
+ throw new Error(`Failed to create Dots org transfer: ${error.message}`);
+ }
+
+ return await response.json();
+};
diff --git a/apps/web/lib/dots/send-payout.ts b/apps/web/lib/dots/create-payout.ts
similarity index 61%
rename from apps/web/lib/dots/send-payout.ts
rename to apps/web/lib/dots/create-payout.ts
index be6e458ee8..8c2eb5b3a7 100644
--- a/apps/web/lib/dots/send-payout.ts
+++ b/apps/web/lib/dots/create-payout.ts
@@ -1,5 +1,6 @@
import z from "../zod";
import { DOTS_API_URL } from "./env";
+import { DotsPayoutPlatform } from "./types";
import { dotsHeaders } from "./utils";
const responseSchema = z.object({
@@ -17,31 +18,29 @@ const responseSchema = z.object({
]),
});
-export const sendPayout = async ({
+export const createPayout = async ({
dotsUserId,
amount,
- dotsAppId,
+ platform,
}: {
dotsUserId: string;
amount: number;
- dotsAppId: string;
+ platform: DotsPayoutPlatform;
}) => {
- const response = await fetch(`${DOTS_API_URL}/payouts/send-payout`, {
+ const response = await fetch(`${DOTS_API_URL}/payouts`, {
method: "POST",
- headers: dotsHeaders({ dotsAppId }),
+ headers: dotsHeaders(),
body: JSON.stringify({
user_id: dotsUserId,
amount,
+ platform,
}),
});
if (!response.ok) {
- console.error(await response.text());
-
- throw new Error(`Failed to create Dots user.`);
+ const error = await response.json();
+ throw new Error(`Failed to create Dots payout: ${error.message}`);
}
- // TODO: [dots] update payout status in our DB? might also need to use webhooks for this
-
return responseSchema.parse(await response.json());
};
diff --git a/apps/web/lib/dots/create-transfer.ts b/apps/web/lib/dots/create-transfer.ts
new file mode 100644
index 0000000000..273430dd21
--- /dev/null
+++ b/apps/web/lib/dots/create-transfer.ts
@@ -0,0 +1,29 @@
+import { DOTS_API_URL } from "./env";
+import { dotsHeaders } from "./utils";
+
+export const createTransfer = async ({
+ amount,
+ dotsAppId,
+ dotsUserId,
+}: {
+ amount: number;
+ dotsAppId: string;
+ dotsUserId: string;
+}) => {
+ console.log(`Creating a transfer of ${amount} cents`);
+ const response = await fetch(`${DOTS_API_URL}/transfers`, {
+ method: "POST",
+ headers: dotsHeaders({ dotsAppId }),
+ body: JSON.stringify({
+ user_id: dotsUserId,
+ amount: -amount, // negative means transfer from Business to Partner
+ }),
+ });
+
+ if (!response.ok) {
+ const error = await response.json();
+ throw new Error(`Failed to create Dots transfer: ${error.message}`);
+ }
+
+ return await response.json();
+};
diff --git a/apps/web/lib/dots/schemas.ts b/apps/web/lib/dots/schemas.ts
index 1d9d766a69..7e51357b1c 100644
--- a/apps/web/lib/dots/schemas.ts
+++ b/apps/web/lib/dots/schemas.ts
@@ -1,5 +1,4 @@
import z from "../zod";
-import { DOTS_PAYOUT_PLATFORMS } from "./platforms";
const dotsTransferSchema = z.object({
id: z.string(),
@@ -57,9 +56,15 @@ export const dotsTransfersSchema = z.object({
has_more: z.boolean(),
});
-export const dotsPayoutPlatforms = z.enum(
- DOTS_PAYOUT_PLATFORMS.map((platform) => platform.id) as [string, ...string[]],
-);
+export const dotsPayoutPlatforms = z.enum([
+ "ach",
+ "paypal",
+ "venmo",
+ "cash_app",
+ "intl_transfer",
+ "airtm",
+ "payoneer",
+]);
export const dotsUserSchema = z.object({
id: z.string(),
diff --git a/apps/web/lib/dots/types.ts b/apps/web/lib/dots/types.ts
index 51c6bce52e..755c35e49a 100644
--- a/apps/web/lib/dots/types.ts
+++ b/apps/web/lib/dots/types.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import {
dotsAppSchema,
dotsFlowStepsSchema,
+ dotsPayoutPlatforms,
dotsTransfersSchema,
dotsUserSchema,
} from "./schemas";
@@ -13,3 +14,5 @@ export type DotsTransfers = z.infer<typeof dotsTransfersSchema>;
export type DotsUser = z.infer<typeof dotsUserSchema>;
export type DotsFlowSteps = z.infer<typeof dotsFlowStepsSchema>;
+
+export type DotsPayoutPlatform = z.infer<typeof dotsPayoutPlatforms>;
diff --git a/apps/web/ui/programs/payout-confirm-sheet.tsx b/apps/web/ui/programs/payout-confirm-sheet.tsx
index d2569116d1..f101be5e29 100644
--- a/apps/web/ui/programs/payout-confirm-sheet.tsx
+++ b/apps/web/ui/programs/payout-confirm-sheet.tsx
@@ -1,3 +1,5 @@
+import { createDotsTransferAction } from "@/lib/actions/create-dots-transfer";
+import useWorkspace from "@/lib/swr/use-workspace";
import { PayoutWithPartnerProps } from "@/lib/types";
import { X } from "@/ui/shared/icons";
import { Button, Sheet } from "@dub/ui";
@@ -8,6 +10,7 @@ import {
DICEBEAR_AVATAR_URL,
formatDate,
} from "@dub/utils";
+import { useAction } from "next-safe-action/hooks";
import { Dispatch, Fragment, SetStateAction, useMemo, useState } from "react";
import { toast } from "sonner";
@@ -57,6 +60,13 @@ function PayoutConfirmSheetContent({
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState("wire");
+ const { id: workspaceId } = useWorkspace();
+ const { executeAsync, isExecuting } = useAction(createDotsTransferAction, {
+ onError({ error }) {
+ toast.error(error.serverError?.serverError);
+ },
+ });
+
return (
<>
<div>
@@ -130,12 +140,24 @@ function PayoutConfirmSheetContent({
<Button
type="button"
variant="primary"
- onClick={() => {
- toast.info("WIP");
+ disabled={!payout.partner.dotsUserId}
+ onClick={async () => {
+ if (!payout.partner.dotsUserId) {
+ toast.error("Partner has no Dots user ID");
+ return;
+ }
+
+ await executeAsync({
+ dotsUserId: payout.partner.dotsUserId,
+ amount: payout.total,
+ workspaceId: workspaceId!,
+ });
+ toast.success("Successfully created payout");
setIsOpen(false);
}}
text="Confirm payout"
className="w-fit"
+ loading={isExecuting}
/>
</div>
</div>
diff --git a/apps/web/ui/programs/payout-details-sheet.tsx b/apps/web/ui/programs/payout-details-sheet.tsx
index 731e4385e0..686c64b941 100644
--- a/apps/web/ui/programs/payout-details-sheet.tsx
+++ b/apps/web/ui/programs/payout-details-sheet.tsx
@@ -181,7 +181,7 @@ function PayoutDetailsSheetContent({
onConfirmPayout();
setIsOpen(false);
}}
- text="Confirm payout"
+ text="Review payout"
className="w-fit"
/>
)}