Skip to content

feat: team wallet #238

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

Merged
merged 57 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
0837342
wip
viktormarinho May 14, 2025
6bebad5
something
viktormarinho May 15, 2025
deccb7d
wip migrating wallet apis
viktormarinho May 15, 2025
8cfb516
wip
viktormarinho May 15, 2025
4d41377
wip
viktormarinho May 16, 2025
3e22546
wip
viktormarinho May 16, 2025
818e112
better
viktormarinho May 16, 2025
7b3c4b9
wip
viktormarinho May 17, 2025
30a1f35
bringing the webhook here
viktormarinho May 18, 2025
055d273
stripe webhook stuff works
viktormarinho May 18, 2025
42d514a
huh kv being versioned
viktormarinho May 18, 2025
1eed49a
workspace
viktormarinho May 19, 2025
0ea7901
fix
viktormarinho May 19, 2025
ed1b357
statements
viktormarinho May 19, 2025
ec53647
statements
viktormarinho May 19, 2025
ec33863
fix + fmt
viktormarinho May 19, 2025
8c53c87
nice chart
viktormarinho May 19, 2025
b08a700
better
viktormarinho May 19, 2025
860d00e
remove old wallet button
viktormarinho May 19, 2025
2e40be2
changes + wip voucher
viktormarinho May 20, 2025
0af69c3
format
viktormarinho May 20, 2025
2df7be0
Merge branch 'main' into team-wallet
viktormarinho May 20, 2025
5a8cceb
fix errors
viktormarinho May 20, 2025
e60a4f0
Merge branch 'main' into team-wallet
viktormarinho May 20, 2025
716e591
fi
viktormarinho May 20, 2025
f71fc62
ok
viktormarinho May 20, 2025
6e5c135
separate hooks, voucher works
viktormarinho May 21, 2025
5890936
lint
viktormarinho May 21, 2025
48555d4
format
viktormarinho May 21, 2025
763142d
optional port env var
viktormarinho May 21, 2025
44a0273
move things to remove a wallet folder
viktormarinho May 21, 2025
cdd20a1
remove logs
viktormarinho May 21, 2025
f41d4a6
fix imports + add migration
viktormarinho May 21, 2025
5d3836e
add env vars
viktormarinho May 21, 2025
4daa569
rename
viktormarinho May 21, 2025
0edfe75
Merge branch 'main' into team-wallet
viktormarinho May 23, 2025
5dcba25
fix
viktormarinho May 23, 2025
f1c66ce
fix wallet crud
viktormarinho May 23, 2025
417f724
Merge branch 'main' into team-wallet
viktormarinho May 23, 2025
1cb264a
Merge branch 'main' into team-wallet
viktormarinho May 26, 2025
2d40135
format
viktormarinho May 26, 2025
dd737d0
fetch migrations + add mine
viktormarinho May 26, 2025
d3bd83e
require balance for all gen operation
viktormarinho May 26, 2025
dd504e8
fix
viktormarinho May 26, 2025
aa9864a
plan
viktormarinho May 27, 2025
e123db2
feature wall
viktormarinho May 27, 2025
87d1ee5
better
viktormarinho May 27, 2025
a372948
fix workspace free credits thing
viktormarinho May 27, 2025
24bbc34
contact us
viktormarinho May 27, 2025
c68d6a1
comment
viktormarinho May 27, 2025
bdffd01
Merge branch 'main' into team-wallet
viktormarinho May 27, 2025
b66f902
fix
viktormarinho May 27, 2025
5463600
Merge branch 'main' into team-wallet
viktormarinho May 27, 2025
f7836ad
trial
viktormarinho May 27, 2025
388f5ac
format
viktormarinho May 27, 2025
c29b56e
use stripe event id to make transaction on webhook idempotent
viktormarinho May 27, 2025
acea2dd
ui changes
viktormarinho May 27, 2025
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
6 changes: 6 additions & 0 deletions .github/workflows/deploy-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
AWS_SECRET_ACCESS_KEY
AWS_REGION
DECO_CHAT_DATA_BUCKET_NAME
STRIPE_SECRET_KEY
STRIPE_WEBHOOK_SECRET
CURRENCY_API_KEY
ISSUER_JWT_SECRET
env:
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
Expand All @@ -73,4 +76,7 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
DECO_CHAT_DATA_BUCKET_NAME: ${{ secrets.DECO_CHAT_DATA_BUCKET_NAME }}
STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}
STRIPE_WEBHOOK_SECRET: ${{ secrets.STRIPE_WEBHOOK_SECRET }}
CURRENCY_API_KEY: ${{ secrets.CURRENCY_API_KEY }}
ISSUER_JWT_SECRET: ${{ secrets.ISSUER_JWT_SECRET }}
7 changes: 5 additions & 2 deletions apps/api/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { default as app } from "./src/app.ts";
const instrumentedApp = getRuntimeKey() === "deno" ? app : instrument(app);

// Domains we consider "self"
const SELF_DOMAINS: string[] = [Hosts.API, "localhost"];
const SELF_DOMAINS: string[] = [
Hosts.API,
`localhost:${Deno.env.get("PORT") || 3001}`,
];

// Patch fetch globally
const originalFetch = globalThis.fetch;
Expand Down Expand Up @@ -43,7 +46,7 @@ globalThis.fetch = async function patchedFetch(

const context = contextStorage.getStore();

if (SELF_DOMAINS.includes(url.hostname)) {
if (SELF_DOMAINS.includes(url.host)) {
if (!context) {
throw new Error("Missing context for internal self-invocation");
}
Expand Down
3 changes: 2 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "module",
"scripts": {
"dev": "deno serve -A --port 3001 --unstable-hmr --unstable-kv --env-file=../web/.env main.ts",
"dev:wrangler": "wrangler dev --port 3001"
"dev:wrangler": "wrangler dev --port 3001",
"stripe:test": "stripe fixtures testing/stripe/fixtures/workspace-wallet-deposit.json"
},
"dependencies": {
"@deco/actors": "npm:@jsr/[email protected]",
Expand Down
4 changes: 4 additions & 0 deletions apps/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { withActorsStubMiddleware } from "./middlewares/actorsStub.ts";
import { withContextMiddleware } from "./middlewares/context.ts";
import { setUserMiddleware } from "./middlewares/user.ts";
import { AppContext, AppEnv, State } from "./utils/context.ts";
import { handleStripeWebhook } from "./webhooks/stripe.ts";

export const app = new Hono<AppEnv>();
export const honoCtxToAppCtx = (c: Context<AppEnv>): AppContext => {
Expand Down Expand Up @@ -227,6 +228,9 @@ Object.entries(loginRoutes).forEach(([route, honoApp]) => {
app.route(route, honoApp);
});

// External webhooks
app.post("/webhooks/stripe", handleStripeWebhook);

// Health check endpoint
app.get("/health", (c: Context) => c.json({ status: "ok" }));

Expand Down
67 changes: 67 additions & 0 deletions apps/api/src/webhooks/stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { Context } from "hono";
import {
createTransactionFromStripeEvent,
serializeError,
verifyAndParseStripeEvent,
WebhookEventIgnoredError,
} from "@deco/sdk/mcp";
import { honoCtxToAppCtx } from "../api.ts";
import { createWalletClient } from "@deco/sdk/mcp/wallet";

export const handleStripeWebhook = async (c: Context) => {
try {
const signature = c.req.header("stripe-signature");
if (!signature) {
throw new Error("Stripe signature header not found");
}

const appContext = honoCtxToAppCtx(c);

const payload = await c.req.text();
const event = await verifyAndParseStripeEvent(
payload,
signature,
appContext,
);
const { transaction, idempotentId } =
await createTransactionFromStripeEvent(
appContext,
event,
);

if (!appContext.envVars.WALLET_API_KEY) {
throw new Error("WALLET_API_KEY is not set");
}

const wallet = createWalletClient(
appContext.envVars.WALLET_API_KEY,
appContext.walletBinding,
);

const response = await wallet["PUT /transactions/:id"]({
id: idempotentId,
}, {
body: transaction,
});

if (!response.ok) {
const error = await response.text();
throw new Error(`Failed to create transaction: ${error}`);
}

return c.json({
message: "Transaction created",
}, 200);
} catch (error) {
if (error instanceof WebhookEventIgnoredError) {
return c.json({
message: error.message,
}, 400);
}

console.error("[Stripe Webhook] Error", serializeError(error));
return c.json({
message: "Internal server error",
}, 500);
}
};
47 changes: 47 additions & 0 deletions apps/api/testing/stripe/fixtures/workspace-wallet-deposit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"_meta": {
"template_version": 0
},
"fixtures": [
{
"name": "customer",
"path": "/v1/customers",
"method": "post",
"params": {
"name": "Viktor Marinho",
"email": "[email protected]",
"metadata": {
"product": "deco.chat",
"workspace": "/users/c40e818c-9a67-4dc6-ac3b-2bb6361663c2"
}
}
},
{
"name": "payment_intent",
"path": "/v1/payment_intents",
"method": "post",
"params": {
"amount": 1418,
"confirm": "true",
"metadata": {
"test": "123"
},
"customer": "${customer:id}",
"currency": "brl",
"description": "(created by Stripe CLI)",
"payment_method": "pm_card_visa",
"payment_method_types": ["card"],
"shipping": {
"name": "Jenny Rosen",
"address": {
"line1": "510 Townsend St",
"postal_code": "94103",
"city": "San Francisco",
"state": "CA",
"country": "US"
}
}
}
}
]
}
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"remark-gfm": "^4.0.1",
"tiptap-markdown": "^0.8.10",
"prismjs": "1.30.0",
"recharts": "^2.15.1",
"react-syntax-highlighter": "15.6.1",
"sonner": "^2.0.3",
"zod": "3.24.3",
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/components/about/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export function Hero({
};

const handleSecondaryButtonClick = () => {
console.log("secondary button clicked");
trackEvent("deco_chat_landing_learn_more_click", {
buttonText: secondaryButtonText,
buttonLink: secondaryButtonLink,
Expand Down
60 changes: 49 additions & 11 deletions apps/web/src/components/chat/ChatError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ import {
TooltipTrigger,
} from "@deco/ui/components/tooltip.tsx";
import { useState } from "react";
import { useWorkspaceLink } from "../../hooks/useNavigateWorkspace.ts";
import { Protect } from "../wallet/plan.tsx";
import { useContactUsUrl } from "../../hooks/useContactUs.ts";

const WELL_KNOWN_ERROR_MESSAGES = {
InsufficientFunds: "Insufficient funds",
};

export function ChatError() {
const workspaceLink = useWorkspaceLink();
const { chat: { error }, retry, correlationIdRef } = useChatContext();
const insufficientFunds = error?.message.includes(
WELL_KNOWN_ERROR_MESSAGES.InsufficientFunds,
);
const contactUsUrl = useContactUsUrl();

useEffect(() => {
if (insufficientFunds) {
Expand All @@ -41,20 +46,53 @@ export function ChatError() {
<div className="flex w-full justify-between p-4 bg-destructive/5 text-destructive rounded-xl text-sm">
<div className="flex items-center gap-4">
<Icon name="info" />
<p>Insufficient funds</p>
<div className="flex flex-col">
<p>Insufficient funds</p>
<Protect
feature="ai-wallet-deposit"
fallback={
<p className="text-xs text-slate-700 pr-2">
This workspace has reached its usage limit. Upgrade your
plan to continue using AI.
</p>
}
>
<p className="text-xs text-slate-700 pr-2">
Your workspace wallet has reached its limit. Add more funds to
continue using AI.
</p>
</Protect>
</div>
</div>
<div className="flex items-center justify-end">
<Button
size="sm"
variant="secondary"
className="bg-background hover:bg-background/80 shadow-none border border-input py-3 px-4 h-10"
asChild
<Protect
feature="ai-wallet-deposit"
fallback={
<Button
size="sm"
variant="secondary"
className="bg-background hover:bg-background/80 shadow-none border border-input py-3 px-4 h-10"
asChild
>
<Link to={contactUsUrl}>
<Icon name="mail" className="mr-2" />
Contact Us
</Link>
</Button>
}
>
<Link to="/wallet">
<Icon name="wallet" className="mr-2" />
Add funds to your wallet
</Link>
</Button>
<Button
size="sm"
variant="secondary"
className="bg-background hover:bg-background/80 shadow-none border border-input py-3 px-4 h-10"
asChild
>
<Link to={workspaceLink("/settings")}>
<Icon name="wallet" className="mr-2" />
Add credits
</Link>
</Button>
</Protect>
</div>
</div>
</div>
Expand Down
Loading