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.

Deposit Funds into App Wallet

Kiran K06860558bfb8
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/page.tsx
index 9cca2fd505..5a1a0f8a61 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/page.tsx
@@ -4,11 +4,9 @@ import { MaxWidthWrapper } from "@dub/ui";
import { Suspense } from "react";
import { BankAccount } from "./bank-account";
import { ComplianceFlow } from "./compliance-flow";
-import { Finance } from "./finance";
-
-export default function BankAccounts({ params }: { params: { slug: string } }) {
- const { slug: workspaceSlug } = params;
+import { Wallet } from "./wallet";
+export default function BankAccounts() {
return (
<Suspense fallback={<LayoutLoader />}>
<PageContent title="Bank Accounts">
@@ -16,7 +14,7 @@ export default function BankAccounts({ params }: { params: { slug: string } }) {
<MaxWidthWrapper className="grid gap-5 pb-10 pt-3">
<BankAccount />
<ComplianceFlow />
- <Finance workspaceSlug={workspaceSlug} />
+ <Wallet />
</MaxWidthWrapper>
</div>
</PageContent>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/finance.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/wallet.tsx
similarity index 60%
rename from apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/finance.tsx
rename to apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/wallet.tsx
index f99fdca5c1..77b22ee1db 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/finance.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/bank-accounts/wallet.tsx
@@ -1,8 +1,16 @@
+"use client";
+
+import { useDepositFundsModal } from "@/ui/modals/deposit-funds-modal";
+import { Button } from "@dub/ui";
import { CreditCard } from "lucide-react";
-export const Finance = async ({ workspaceSlug }: { workspaceSlug: string }) => {
+export const Wallet = () => {
+ const { DepositFundsModal, setShowDepositFundsModal } =
+ useDepositFundsModal();
+
return (
<>
+ <DepositFundsModal />
<div className="flex items-center gap-5 rounded-lg border bg-white p-5">
<div className="hidden h-12 w-12 items-center justify-center rounded-full border border-neutral-300 sm:inline-flex">
<CreditCard className="h-5 w-5 text-gray-600" />
@@ -14,6 +22,13 @@ export const Finance = async ({ workspaceSlug }: { workspaceSlug: string }) => {
Wallet balance and transaction history.
</div>
</div>
+
+ <div>
+ <Button
+ text="Deposit funds"
+ onClick={() => setShowDepositFundsModal(true)}
+ />
+ </div>
</div>
</>
);
diff --git a/apps/web/lib/actions/deposit-funds.ts b/apps/web/lib/actions/deposit-funds.ts
new file mode 100644
index 0000000000..7cbb3f3cb7
--- /dev/null
+++ b/apps/web/lib/actions/deposit-funds.ts
@@ -0,0 +1,26 @@
+"use server";
+
+import { z } from "zod";
+import { depositFunds } from "../dots/deposit-funds";
+import { depositFundsSchema } from "../dots/schemas";
+import { authActionClient } from "./safe-action";
+
+const schema = depositFundsSchema.extend({
+ workspaceId: z.string(),
+});
+
+export const depositFundsAction = authActionClient
+ .schema(schema)
+ .action(async ({ parsedInput, ctx }) => {
+ const { workspace } = ctx;
+ const { amount } = parsedInput;
+
+ if (!workspace.dotsAppId) {
+ throw new Error("Dots app ID is not set for this workspace.");
+ }
+
+ return await depositFunds({
+ dotsAppId: workspace.dotsAppId,
+ amount,
+ });
+ });
diff --git a/apps/web/lib/dots/deposit-funds.ts b/apps/web/lib/dots/deposit-funds.ts
new file mode 100644
index 0000000000..5cfa54c5a8
--- /dev/null
+++ b/apps/web/lib/dots/deposit-funds.ts
@@ -0,0 +1,49 @@
+import z from "../zod";
+import { DOTS_API_URL } from "./env";
+import { dotsHeaders } from "./utils";
+
+const statusSchema = z.enum([
+ "created",
+ "pending",
+ "failed",
+ "completed",
+ "reversed",
+ "canceled",
+ "flagged",
+]);
+
+const schema = z.object({
+ id: z.string(),
+ amount: z.number(),
+ status: statusSchema,
+ status_history: z.array(
+ z.object({
+ status: statusSchema,
+ timestamp: z.string(),
+ }),
+ ),
+});
+
+export const depositFunds = async ({
+ dotsAppId,
+ amount,
+}: {
+ dotsAppId: string;
+ amount: string;
+}) => {
+ const response = await fetch(`${DOTS_API_URL}/apps/${dotsAppId}/deposit`, {
+ method: "POST",
+ headers: dotsHeaders(),
+ body: JSON.stringify({
+ amount: Number(amount) * 100, // The amount to deposit in cents
+ idempotency_key: crypto.randomUUID(),
+ }),
+ });
+
+ if (!response.ok) {
+ console.error(await response.text());
+ throw new Error(`Failed to deposit funds into Dots app ${dotsAppId}.`);
+ }
+
+ return schema.parse(await response.json());
+};
diff --git a/apps/web/lib/dots/schemas.ts b/apps/web/lib/dots/schemas.ts
index a916038a05..02c80afc0a 100644
--- a/apps/web/lib/dots/schemas.ts
+++ b/apps/web/lib/dots/schemas.ts
@@ -5,3 +5,7 @@ export const addBankAccountSchema = z.object({
routingNumber: z.string().min(1),
accountType: z.enum(["checking", "savings"]).default("checking"),
});
+
+export const depositFundsSchema = z.object({
+ amount: z.string(),
+});
diff --git a/apps/web/prisma/schema/program.prisma b/apps/web/prisma/schema/program.prisma
index c83de88130..ae4bcdfe5e 100644
--- a/apps/web/prisma/schema/program.prisma
+++ b/apps/web/prisma/schema/program.prisma
@@ -10,6 +10,8 @@ model Program {
slug String @unique
logo String?
type ProgramType @default(affiliate)
+
+
// TODO: add more fields for program management flow
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
diff --git a/apps/web/ui/modals/add-bank-account-modal.tsx b/apps/web/ui/modals/add-bank-account-modal.tsx
index f962a6b92a..72cdf51ddf 100644
--- a/apps/web/ui/modals/add-bank-account-modal.tsx
+++ b/apps/web/ui/modals/add-bank-account-modal.tsx
@@ -9,13 +9,13 @@ import { useCallback, useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
+type BankAccount = z.infer<typeof addBankAccountSchema>;
+
interface AddBankAccountProps {
showModal: boolean;
setShowModal: (showModal: boolean) => void;
}
-type BankAccount = z.infer<typeof addBankAccountSchema>;
-
interface AddBankAccountFormProps {
closeModal: () => void;
}
diff --git a/apps/web/ui/modals/deposit-funds-modal.tsx b/apps/web/ui/modals/deposit-funds-modal.tsx
new file mode 100644
index 0000000000..b0cca01b20
--- /dev/null
+++ b/apps/web/ui/modals/deposit-funds-modal.tsx
@@ -0,0 +1,122 @@
+import { depositFundsAction } from "@/lib/actions/deposit-funds";
+import { depositFundsSchema } from "@/lib/dots/schemas";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { Button, Modal } from "@dub/ui";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { useAction } from "next-safe-action/hooks";
+import { useCallback, useMemo, useState } from "react";
+import { useForm } from "react-hook-form";
+import { toast } from "sonner";
+import { z } from "zod";
+
+const DepositFunds = ({
+ showModal,
+ setShowModal,
+}: {
+ showModal: boolean;
+ setShowModal: (showModal: boolean) => void;
+}) => {
+ return (
+ <Modal
+ showModal={showModal}
+ setShowModal={setShowModal}
+ drawerRootProps={{ repositionInputs: false }}
+ >
+ <h3 className="border-b border-gray-200 px-4 py-4 text-lg font-medium sm:px-6">
+ Deposit funds
+ </h3>
+ <div className="scrollbar-hide mt-6 max-h-[calc(100dvh-200px)] overflow-auto overflow-y-scroll">
+ <DepositFundsForm closeModal={() => setShowModal(false)} />
+ </div>
+ </Modal>
+ );
+};
+
+const DepositFundsForm = ({ closeModal }: { closeModal: () => void }) => {
+ const { id: workspaceId, mutate } = useWorkspace();
+
+ const {
+ register,
+ handleSubmit,
+ formState: { isValid, isSubmitting },
+ } = useForm<z.infer<typeof depositFundsSchema>>({
+ resolver: zodResolver(depositFundsSchema),
+ });
+
+ const { executeAsync, isExecuting } = useAction(depositFundsAction, {
+ async onSuccess() {
+ toast.success("Funds deposited successfully.");
+ mutate();
+ closeModal();
+ },
+ onError({ error }) {
+ toast.error(error.serverError?.serverError);
+ },
+ });
+
+ const onSubmit = async (data: z.infer<typeof depositFundsSchema>) => {
+ await executeAsync({ ...data, workspaceId: workspaceId! });
+ };
+
+ return (
+ <form onSubmit={handleSubmit(onSubmit)}>
+ <div className="flex flex-col gap-y-6 px-4 text-left sm:px-6">
+ <div className="flex flex-col gap-3">
+ <div>
+ <label htmlFor="amount" className="flex items-center space-x-2">
+ <h2 className="text-sm font-medium text-gray-900">Amount</h2>
+ </label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <input
+ {...register("amount")}
+ type="number"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ required
+ autoFocus
+ autoComplete="off"
+ placeholder="100.00"
+ />
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div className="mt-8 flex justify-end gap-2 border-t border-gray-200 px-4 py-4 sm:px-6">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Cancel"
+ className="h-9 w-fit"
+ disabled={isSubmitting || isExecuting}
+ onClick={closeModal}
+ />
+
+ <Button
+ type="submit"
+ text="Deposit funds"
+ className="h-9 w-fit"
+ disabled={!isValid}
+ loading={isSubmitting || isExecuting}
+ />
+ </div>
+ </form>
+ );
+};
+
+export function useDepositFundsModal() {
+ const [showDepositFundsModal, setShowDepositFundsModal] = useState(false);
+
+ const DepositFundsModal = useCallback(() => {
+ return (
+ <DepositFunds
+ showModal={showDepositFundsModal}
+ setShowModal={setShowDepositFundsModal}
+ />
+ );
+ }, [showDepositFundsModal, setShowDepositFundsModal]);
+
+ return useMemo(
+ () => ({ setShowDepositFundsModal, DepositFundsModal }),
+ [setShowDepositFundsModal, DepositFundsModal],
+ );
+}