Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pluggy #341

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"next-themes": "^0.3.0",
"nuqs": "^1.19.3",
"papaparse": "^5.4.1",
"pluggy-connect-sdk": "^2.9.1",
"react": "18.3.1",
"react-colorful": "^5.6.1",
"react-dom": "18.3.1",
Expand Down
29 changes: 29 additions & 0 deletions apps/dashboard/src/actions/institutions/create-pluggy-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use server";

import { client } from "@midday/engine/client";
import { getSession } from "@midday/supabase/cached-queries";

export const createPluggyLinkTokenAction = async () => {
const {
data: { session },
} = await getSession();

if (!session?.user.id) {
throw new Error("User not found");
}

const pluggyResponse = await client.auth.pluggy.link.$post({
json: {
userId: session.user.id,
environment: "production",
},
});

if (!pluggyResponse.ok) {
throw new Error("Failed to create pluggy link token");
}

const { data } = await pluggyResponse.json();

return data.access_token;
};
Empty file.
15 changes: 14 additions & 1 deletion apps/dashboard/src/components/connect-bank-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type Props = {
provider: string;
availableHistory: number;
openPlaid: () => void;
openPluggy: (props: { institutionId?: string }) => void;
};

export function ConnectBankProvider({
id,
provider,
openPlaid,
openPluggy,
availableHistory,
}: Props) {
const { setParams } = useConnectParams();
Expand Down Expand Up @@ -51,7 +53,7 @@ export function ConnectBankProvider({
/>
);
}
case "plaid":
case "plaid": {
return (
<BankConnectButton
onClick={() => {
Expand All @@ -60,6 +62,17 @@ export function ConnectBankProvider({
}}
/>
);
}
case "pluggy": {
return (
<BankConnectButton
onClick={() => {
updateUsage();
openPluggy({ institutionId: id });
}}
/>
);
}
default:
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/src/components/institution-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function InstitutionInfo({ provider, children }: Props) {
return `With Plaid we can connect to 12,000+ financial institutions across the US, Canada, UK, and Europe are covered by Plaid's network`;
case "teller":
return "With Teller we can connect instantly to more than 5,000 financial institutions in the US.";
case "pluggy":
return "With Pluggy we can connect to all major financial institutions in Brazil.";
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use client";

import { createPlaidLinkTokenAction } from "@/actions/institutions/create-plaid-link";
import { createPluggyLinkTokenAction } from "@/actions/institutions/create-pluggy-link";
import { exchangePublicToken } from "@/actions/institutions/exchange-public-token";
import { getInstitutions } from "@/actions/institutions/get-institutions";
import { useConnectParams } from "@/hooks/use-connect-params";
import { usePluggyLink } from "@/hooks/use-pluggy-link";
import type { Institutions } from "@midday-ai/engine/resources/institutions/institutions";
import { track } from "@midday/events/client";
import { LogEvents } from "@midday/events/events";
Expand All @@ -18,6 +20,7 @@ import {
import { Input } from "@midday/ui/input";
import { Skeleton } from "@midday/ui/skeleton";
import { useDebounce, useScript } from "@uidotdev/usehooks";
import { useTheme } from "next-themes";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { usePlaidLink } from "react-plaid-link";
Expand Down Expand Up @@ -49,6 +52,7 @@ type SearchResultProps = {
provider: string;
availableHistory: number;
openPlaid: () => void;
openPluggy: () => void;
};

function SearchResult({
Expand All @@ -58,6 +62,7 @@ function SearchResult({
provider,
availableHistory,
openPlaid,
openPluggy,
}: SearchResultProps) {
return (
<div className="flex justify-between">
Expand All @@ -79,6 +84,7 @@ function SearchResult({
provider={provider}
openPlaid={openPlaid}
availableHistory={availableHistory}
openPluggy={openPluggy}
/>
</div>
);
Expand All @@ -92,9 +98,11 @@ export function ConnectTransactionsModal({
countryCode: initialCountryCode,
}: ConnectTransactionsModalProps) {
const router = useRouter();
const { theme } = useTheme();
const [loading, setLoading] = useState(true);
const [results, setResults] = useState<Institutions["data"]>([]);
const [plaidToken, setPlaidToken] = useState<string | undefined>();
const [pluggyToken, setPluggyToken] = useState<string | undefined>();

const {
countryCode,
Expand Down Expand Up @@ -144,6 +152,37 @@ export function ConnectTransactionsModal({
},
});

const { open: openPluggy } = usePluggyLink({
token: pluggyToken,
theme: theme === "dark" ? "dark" : "light",
language: "en",
onSuccess: (itemData) => {
// const { access_token, item_id } = await exchangePublicToken(public_token);

// setParams({
// step: "account",
// provider: "pluggy",
// token: access_token,
// ref: item_id,
// institution_id: metadata.institution?.institution_id,
// });
track({
event: LogEvents.ConnectBankAuthorized.name,
channel: LogEvents.ConnectBankAuthorized.channel,
provider: "pluggy",
});
},
onExit: () => {
setParams({ step: "connect" });

track({
event: LogEvents.ConnectBankCanceled.name,
channel: LogEvents.ConnectBankCanceled.channel,
provider: "pluggy",
});
},
});

const handleOnClose = () => {
setParams(
{
Expand Down Expand Up @@ -202,6 +241,21 @@ export function ConnectTransactionsModal({
}
}, [isOpen, countryCode]);

useEffect(() => {
async function createLinkToken() {
const token = await createPluggyLinkTokenAction();

if (token) {
setPluggyToken(token);
}
}

// NOTE: Only run where Pluggy is supported (Brazil)
if (isOpen && countryCode === "BR") {
createLinkToken();
}
}, [isOpen, countryCode]);

return (
<Dialog open={isOpen} onOpenChange={handleOnClose}>
<DialogContent>
Expand Down Expand Up @@ -271,6 +325,10 @@ export function ConnectTransactionsModal({
setParams({ step: null });
openPlaid();
}}
openPluggy={() => {
setParams({ step: null });
openPluggy({ institutionId: institution.id });
}}
/>
);
})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export function SelectBankAccountsModal() {
useEffect(() => {
async function fetchData() {
const { data } = await getAccounts({
provider: provider as "teller" | "plaid" | "gocardless",
provider: provider as "teller" | "plaid" | "gocardless" | "pluggy",
id: ref ?? undefined,
accessToken: token ?? undefined,
institutionId: institution_id ?? undefined,
Expand Down
53 changes: 53 additions & 0 deletions apps/dashboard/src/hooks/use-pluggy-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as PluggyConnect from "pluggy-connect-sdk";
import { useEffect, useState } from "react";

type Props = {
token: string;
institutionId?: string;
onExit: () => void;
onSuccess: (itemData: any) => void;
onError: () => void;
includeSandbox?: boolean;
theme?: "light" | "dark";
language?: string;
};

export function usePluggyLink({
token,
institutionId,
onExit,
onSuccess,
onError,
includeSandbox = false,
language = "pt",
theme,
}: Props) {
const [connectToken, setConnectToken] = useState<string>("");
const [selectedConnectorId, setSelectedConnectorId] = useState<
number | undefined
>(institutionId ? Number(institutionId) : undefined);

useEffect(() => {
setConnectToken(token);
}, [token]);

const pluggyConnect = new PluggyConnect.PluggyConnect({
connectToken,
includeSandbox,
selectedConnectorId,
onClose: onExit,
onSuccess,
onError,
theme,
language,
});

const handleOpen = ({ institutionId }: { institutionId?: string }) => {
setSelectedConnectorId(Number(institutionId));
pluggyConnect.init();
};

return {
open: handleOpen,
};
}
4 changes: 3 additions & 1 deletion apps/engine/.dev.vars-example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ TYPESENSE_API_KEY=
TYPESENSE_ENDPOINT=
TYPESENSE_ENDPOINT_US=
TYPESENSE_ENDPOINT_EU=
TYPESENSE_ENDPOINT_AU=
TYPESENSE_ENDPOINT_AU=
PLUGGY_CLIENT_ID=
PLUGGY_SECRET=
10 changes: 0 additions & 10 deletions apps/engine/.env-example

This file was deleted.

11 changes: 6 additions & 5 deletions apps/engine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@
"dependencies": {
"@hono/swagger-ui": "^0.5.0",
"@hono/zod-openapi": "^0.18.3",
"@hono/zod-validator": "^0.4.1",
"hono": "^4.6.13",
"@hono/zod-validator": "^0.4.2",
"hono": "^4.6.14",
"plaid": "^30.0.0",
"pluggy-sdk": "^0.65.1",
"typesense": "^1.8.2",
"workers-ai-provider": "^0.0.10",
"xior": "^0.6.1",
"zod": "^3.23.8"
"zod": "^3.24.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20241205.0",
"@cloudflare/workers-types": "^4.20241218.0",
"@types/bun": "^1.1.14",
"wrangler": "^3.93.0"
"wrangler": "^3.99.0"
},
"exports": {
"./client": "./src/client/index.ts",
Expand Down
2 changes: 2 additions & 0 deletions apps/engine/src/common/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type Bindings = {
PLAID_CLIENT_ID: string;
PLAID_ENVIRONMENT: string;
PLAID_SECRET: string;
PLUGGY_CLIENT_ID: string;
PLUGGY_SECRET: string;
TYPESENSE_API_KEY: string;
TYPESENSE_ENDPOINT_AU: string;
TYPESENSE_ENDPOINT_EU: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/engine/src/common/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const GeneralErrorSchema = z.object({
}),
});

export const Providers = z.enum(["teller", "plaid", "gocardless"]);
export const Providers = z.enum(["teller", "plaid", "gocardless", "pluggy"]);

export const HeadersSchema = z.object({
authorization: z.string().openapi({
Expand Down
11 changes: 10 additions & 1 deletion apps/engine/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { logger } from "@/utils/logger";
import { GoCardLessProvider } from "./gocardless/gocardless-provider";
import { PlaidProvider } from "./plaid/plaid-provider";
import { PluggyProvider } from "./pluggy/pluggy-provider";
import { TellerProvider } from "./teller/teller-provider";
import type {
DeleteAccountsRequest,
Expand All @@ -17,7 +18,12 @@ import type {
export class Provider {
#name?: string;

#provider: PlaidProvider | TellerProvider | GoCardLessProvider | null = null;
#provider:
| PlaidProvider
| TellerProvider
| GoCardLessProvider
| PluggyProvider
| null = null;

constructor(params?: ProviderParams) {
this.#name = params?.provider;
Expand All @@ -32,6 +38,9 @@ export class Provider {
case "plaid":
this.#provider = new PlaidProvider(params);
break;
case "pluggy":
this.#provider = new PluggyProvider(params);
break;
default:
}
}
Expand Down
Loading
Loading