-
Notifications
You must be signed in to change notification settings - Fork 18
refactor(subscribe): integrate Privy authentication for checkout sessions #1724
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
base: test
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import createClientCheckoutSession from "../createClientCheckoutSession"; | ||
| import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; | ||
|
|
||
| vi.mock("@/lib/api/getClientApiBaseUrl", () => ({ | ||
| getClientApiBaseUrl: vi.fn(), | ||
| })); | ||
|
|
||
| describe("createClientCheckoutSession", () => { | ||
| const mockOpen = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.spyOn(console, "error").mockImplementation(() => {}); | ||
| vi.mocked(getClientApiBaseUrl).mockReturnValue("https://api.recoupable.com"); | ||
| vi.stubGlobal("window", { open: mockOpen } as unknown as Window); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("POSTs only successUrl to recoup-api with Bearer auth and opens checkout url", async () => { | ||
| global.fetch = vi.fn().mockResolvedValue({ | ||
| ok: true, | ||
| json: vi.fn().mockResolvedValue({ | ||
| id: "cs_test_1", | ||
| url: "https://checkout.stripe.com/test", | ||
| }), | ||
| }) as unknown as typeof fetch; | ||
|
|
||
| await createClientCheckoutSession( | ||
| "privy-token", | ||
| "https://chat.recoupable.com/billing", | ||
| ); | ||
|
|
||
| expect(fetch).toHaveBeenCalledWith( | ||
| "https://api.recoupable.com/api/subscriptions/sessions", | ||
| expect.objectContaining({ | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: "Bearer privy-token", | ||
| }, | ||
| }), | ||
| ); | ||
| const init = vi.mocked(fetch).mock.calls[0][1] as RequestInit; | ||
| expect(JSON.parse(String(init.body))).toEqual({ | ||
| successUrl: "https://chat.recoupable.com/billing", | ||
| }); | ||
| expect(mockOpen).toHaveBeenCalledWith( | ||
| "https://checkout.stripe.com/test", | ||
| "_blank", | ||
| "noopener,noreferrer", | ||
| ); | ||
| }); | ||
|
|
||
| it("does not open a window and returns error when response is not ok", async () => { | ||
| global.fetch = vi.fn().mockResolvedValue({ | ||
| ok: false, | ||
| status: 401, | ||
| json: vi.fn().mockResolvedValue({ error: "Unauthorized" }), | ||
| }) as unknown as typeof fetch; | ||
|
|
||
| const result = await createClientCheckoutSession("bad-token", "https://ex.com"); | ||
|
|
||
| expect(mockOpen).not.toHaveBeenCalled(); | ||
| expect(result).toEqual({ error: expect.any(Error) }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,35 @@ | ||
| const createClientCheckoutSession = async (accountId: string) => { | ||
| import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl"; | ||
|
|
||
| const createClientCheckoutSession = async ( | ||
| accessToken: string, | ||
| successUrl: string, | ||
| ) => { | ||
| try { | ||
| const response = await fetch(`/api/stripe/session/create`, { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| const response = await fetch( | ||
| `${getClientApiBaseUrl()}/api/subscriptions/sessions`, | ||
| { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| Authorization: `Bearer ${accessToken}`, | ||
| }, | ||
| body: JSON.stringify({ successUrl }), | ||
| }, | ||
| body: JSON.stringify({ | ||
| accountId, | ||
| successUrl: `${window.location.href}`, | ||
| }), | ||
| }); | ||
| ); | ||
|
|
||
| const data = (await response.json()) as { | ||
| id?: string; | ||
| url?: string; | ||
| error?: string; | ||
| }; | ||
|
|
||
| if (!response.ok || typeof data.url !== "string") { | ||
| throw new Error(data.error || "Failed to create checkout session"); | ||
|
Comment on lines
+8
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checkout requests are pointed at the wrong API contract.
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| const data = await response.json(); | ||
| window.open(data.data.url, "__blank"); | ||
| window.open(data.url, "_blank", "noopener,noreferrer"); | ||
| } catch (error) { | ||
| console.error("Error creating checkout session:", error); | ||
| return { error }; | ||
| } | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't gate checkout on
account_id.The new checkout flow uses a Privy access token, so the early
returnonuserData?.account_idblocks the non-subscribed path entirely for users who no longer have an account ID. Move that guard inside theisSubscribedbranch so only the portal flow depends onaccount_id.Suggested fix
const handleClick = async () => { - if (!userData?.account_id) return; - if (isSubscribed) { + if (!userData?.account_id) return; void createClientPortalSession(userData.account_id); return; }📝 Committable suggestion
🤖 Prompt for AI Agents