-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/74 stripe donations #77
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5366079
feat: add checkoutDonation function for Stripe donations
Jxl-s 6c3262d
feat: add tests for checkoutDonation function and update Jest configu…
Jxl-s f418c6c
chore: add comment to specify donation product
Jxl-s 07ef591
feat: enhance checkoutDonation function to retrieve and validate dona…
Jxl-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
| import { checkoutDonation } from "./actions"; | ||
|
|
||
| const createSession = jest.fn(); | ||
| const retrieveProduct = jest.fn(); | ||
| const retrievePrice = jest.fn(); | ||
| const getUser = jest.fn(); | ||
| const getOrCreateStripeCustomer = jest.fn(); | ||
| const headersGet = jest.fn(); | ||
|
|
||
| jest.mock("@/lib/stripe", () => ({ | ||
| stripe: { | ||
| checkout: { sessions: { create: (...args: unknown[]) => createSession(...args) } }, | ||
| products: { retrieve: (...args: unknown[]) => retrieveProduct(...args) }, | ||
| prices: { retrieve: (...args: unknown[]) => retrievePrice(...args) }, | ||
| }, | ||
| getOrCreateStripeCustomer: (...args: unknown[]) => | ||
| getOrCreateStripeCustomer(...args), | ||
| })); | ||
|
|
||
| jest.mock("@/utils/supabase/server", () => ({ | ||
| createClient: async () => ({ auth: { getUser } }), | ||
| })); | ||
|
|
||
| jest.mock("next/headers", () => ({ | ||
| headers: async () => ({ get: (...args: unknown[]) => headersGet(...args) }), | ||
| })); | ||
|
|
||
| const authedUser = { | ||
| data: { user: { id: "user-1", email: "donor@test.com" } }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| process.env.STRIPE_DONATION_PRODUCT_ID = "prod_test123"; | ||
| getUser.mockResolvedValue(authedUser); | ||
| getOrCreateStripeCustomer.mockResolvedValue("cus_123"); | ||
| retrieveProduct.mockResolvedValue({ default_price: "price_donation" }); | ||
| retrievePrice.mockResolvedValue({ custom_unit_amount: { preset: 1000 } }); | ||
| createSession.mockResolvedValue({ url: "https://stripe.test/session" }); | ||
| headersGet.mockReturnValue("https://app.test"); | ||
| }); | ||
|
|
||
| describe("checkoutDonation", () => { | ||
| it("requires an authenticated user", async () => { | ||
| getUser.mockResolvedValue({ data: { user: null } }); | ||
| const result = await checkoutDonation(); | ||
| expect(result).toEqual({ error: "Not authenticated" }); | ||
| expect(createSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("returns an error when the donation product env var is not set", async () => { | ||
| delete process.env.STRIPE_DONATION_PRODUCT_ID; | ||
| const result = await checkoutDonation(); | ||
| expect(result).toEqual({ error: "Donation product not configured" }); | ||
| expect(createSession).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("creates a checkout session using the donation product's default price", async () => { | ||
| const result = await checkoutDonation(); | ||
|
|
||
| expect(retrieveProduct).toHaveBeenCalledWith("prod_test123"); | ||
| expect(getOrCreateStripeCustomer).toHaveBeenCalledWith( | ||
| "user-1", | ||
| "donor@test.com", | ||
| ); | ||
| expect(createSession).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| customer: "cus_123", | ||
| mode: "payment", | ||
| metadata: { type: "donation" }, | ||
| line_items: [{ price: "price_donation", quantity: 1 }], | ||
| }), | ||
| ); | ||
| expect(result).toEqual({ url: "https://stripe.test/session" }); | ||
| }); | ||
|
|
||
| it("derives success/cancel URLs from the request origin", async () => { | ||
| await checkoutDonation(); | ||
| expect(createSession).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| success_url: "https://app.test/checkout/success", | ||
| cancel_url: "https://app.test/checkout/cancel", | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it("returns an error when Stripe omits the checkout URL", async () => { | ||
| createSession.mockResolvedValue({ url: null }); | ||
| const result = await checkoutDonation(); | ||
| expect(result).toEqual({ error: "Stripe did not return a checkout URL" }); | ||
| }); | ||
|
|
||
| it("throws when the request origin is missing", async () => { | ||
| headersGet.mockReturnValue(null); | ||
| await expect(checkoutDonation()).rejects.toThrow( | ||
| "Missing request origin", | ||
| ); | ||
| expect(createSession).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.