-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Polar integration #461
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
Draft
Genyus
wants to merge
60
commits into
wasp-lang:main
Choose a base branch
from
Genyus:polar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Polar integration #461
Changes from 16 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
728bcd0
refactor: create explicit type for processor IDs
Genyus 61f5e73
refactor: move revenue calculation to processors
Genyus d3f3ed6
docs: improve comments
Genyus 51d05ca
fix: update imports after previous refactoring
Genyus f07fa91
refactor: convert processor IDs to enum values
Genyus 2b42cc6
feat: add validation of server env vars
Genyus a0276a3
feat: add initial Polar integration
Genyus 0ebd0a3
feat: implement total revenue calculation
Genyus e84670d
feat: implement customer portal URL retrieval
Genyus 54292a7
feat: implement checkout session creation
Genyus 156823c
feat: implement webhook handling
Genyus 2841121
feat: add dynamic payment processor selection
Genyus 825eb66
fix: implement webhook handling
Genyus 6ad783b
chore: rename file for consistency
Genyus a97b79f
Merge branch 'main' into polar
Genyus eb68932
fix: address typing errors
Genyus fca11f9
fix: resolve type assignment error
Genyus c71f29a
refactor: streamline payment processor integration
Genyus 732b9ed
style: remove redundant JSDoc comments
Genyus 2410e01
refactor: simplify error handling
Genyus f65cc67
refactor: fix customer portal implementation
Genyus df213c9
chore: update webhook response status codes
Genyus 5f71cac
refactor: remove iteration for single customer lookup
Genyus 882df67
refactor: simplify webhook error handling
Genyus 7d0eb4e
refactor: rename polar client
Genyus 3639c0d
refactor: refactor client configuration
Genyus 2f5748c
refactor: remove standalone client config file
Genyus e5a63de
style: remove extraneous JSDoc comments
Genyus fda6a57
refactor: remove standalone type declarations file
Genyus add2038
docs: remove unnecessary README
Genyus 254aae4
refactor: remodel webhook to align with current integrations
Genyus 780d24c
fix: resolves issues found in testing
Genyus 568c38b
chore: mention Polar as an available payment platform
Genyus 721fd6f
chore: remove env validation
Genyus 31ad46c
chore: remove redundant guard
Genyus f5892aa
chore: remove unsupported event type
Genyus 32c8934
fix: update order completion handler
Genyus 2b3195c
fix: throw error when credits can't be parsed from order
Genyus 2a64914
revert: revert deletion of validation.ts
Genyus 9658034
Merge pull request #1 from wasp-lang/main
Genyus 033bdfe
chore: rename file for consistency
Genyus dbcfa08
style: reorder imports
Genyus e81b2db
fix: correct webhook event support
Genyus 20e95f8
refactor: simplify webhook handlers
Genyus f31f176
style: restore missing line break
Genyus 3003595
chore: add gitignore rules
Genyus 6cce3fe
refactor: restore centralised payment stats
Genyus ce97f73
revert: restore original payment processor files
Genyus 27b8ea7
revert: restore paymentProcessor comments
Genyus 6471324
refactor: improve switch condition checking
Genyus 1b72606
refactor: remove unused code
Genyus b0eaf88
refactor: rename function
Genyus 3defeb9
refactor: simplify user updating
Genyus 2741ea2
refactor: simplify webhook handling
Genyus 543fa92
fix: remove default portal URL
Genyus ee65871
fix: improve type-safety
Genyus 31a3b0b
fix: restore available processors
Genyus 42cbf32
docs: remove JSDoc comment
Genyus adde113
refactor: remove redundant file
Genyus 29ec825
refactor: streamline session management
Genyus 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
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
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
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 |
---|---|---|
|
@@ -4,6 +4,9 @@ import type { MiddlewareConfigFn } from 'wasp/server'; | |
import { PrismaClient } from '@prisma/client'; | ||
import { stripePaymentProcessor } from './stripe/paymentProcessor'; | ||
import { lemonSqueezyPaymentProcessor } from './lemonSqueezy/paymentProcessor'; | ||
import { polarPaymentProcessor } from './polar/paymentProcessor'; | ||
import { PaymentProcessorId, PaymentProcessors } from './types'; | ||
import { getActivePaymentProcessor } from './validation'; | ||
|
||
export interface CreateCheckoutSessionArgs { | ||
userId: string; | ||
|
@@ -16,17 +19,91 @@ export interface FetchCustomerPortalUrlArgs { | |
prismaUserDelegate: PrismaClient['user']; | ||
}; | ||
|
||
/** | ||
* Standard interface for all payment processors | ||
* Provides a consistent API for payment operations across different providers | ||
*/ | ||
export interface PaymentProcessor { | ||
id: 'stripe' | 'lemonsqueezy'; | ||
id: PaymentProcessorId; | ||
/** | ||
* Creates a checkout session for payment processing | ||
* Handles both subscription and one-time payment flows based on the payment plan configuration | ||
* @param args Checkout session creation arguments | ||
* @param args.userId Internal user ID for tracking and database updates | ||
* @param args.userEmail Customer email address for payment processor customer creation/lookup | ||
* @param args.paymentPlan Payment plan configuration containing pricing and payment type information | ||
* @param args.prismaUserDelegate Prisma user delegate for database operations | ||
* @returns Promise resolving to checkout session with session ID and redirect URL | ||
* @throws {Error} When payment processor API calls fail or required configuration is missing | ||
* @example | ||
* ```typescript | ||
* const { session } = await paymentProcessor.createCheckoutSession({ | ||
* userId: 'user_123', | ||
* userEmail: '[email protected]', | ||
* paymentPlan: hobbyPlan, | ||
* prismaUserDelegate: context.entities.User | ||
* }); | ||
* // Redirect user to session.url for payment | ||
* ``` | ||
*/ | ||
Genyus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
createCheckoutSession: (args: CreateCheckoutSessionArgs) => Promise<{ session: { id: string; url: string }; }>; | ||
/** | ||
* Retrieves the customer portal URL for subscription and billing management | ||
* Allows customers to view billing history, update payment methods, and manage subscriptions | ||
* @param args Customer portal URL retrieval arguments | ||
* @param args.userId Internal user ID to lookup customer information | ||
* @param args.prismaUserDelegate Prisma user delegate for database operations | ||
* @returns Promise resolving to customer portal URL or null if not available | ||
* @throws {Error} When user lookup fails or payment processor API calls fail | ||
* @example | ||
* ```typescript | ||
* const portalUrl = await paymentProcessor.fetchCustomerPortalUrl({ | ||
* userId: 'user_123', | ||
* prismaUserDelegate: context.entities.User | ||
* }); | ||
* if (portalUrl) { | ||
* // Redirect user to portal for billing management | ||
* return { redirectUrl: portalUrl }; | ||
* } | ||
* ``` | ||
*/ | ||
fetchCustomerPortalUrl: (args: FetchCustomerPortalUrlArgs) => Promise<string | null>; | ||
/** | ||
* Calculates the total revenue from this payment processor | ||
* @returns Promise resolving to total revenue in dollars | ||
*/ | ||
getTotalRevenue: () => Promise<number>; | ||
webhook: PaymentsWebhook; | ||
webhookMiddlewareConfigFn: MiddlewareConfigFn; | ||
} | ||
|
||
/** | ||
* Choose which payment processor you'd like to use, then delete the | ||
* other payment processor code that you're not using from `/src/payment` | ||
* All available payment processors | ||
*/ | ||
const paymentProcessorMap: Record<PaymentProcessors, PaymentProcessor> = { | ||
Genyus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[PaymentProcessors.Stripe]: stripePaymentProcessor, | ||
[PaymentProcessors.LemonSqueezy]: lemonSqueezyPaymentProcessor, | ||
[PaymentProcessors.Polar]: polarPaymentProcessor, | ||
}; | ||
|
||
/** | ||
* Get the payment processor instance based on environment configuration or override | ||
* @param override Optional processor override for testing scenarios | ||
* @returns The configured payment processor instance | ||
* @throws {Error} When the specified processor is not found in the processor map | ||
*/ | ||
export function getPaymentProcessor(override?: PaymentProcessorId): PaymentProcessor { | ||
const processorId = getActivePaymentProcessor(override); | ||
const processor = paymentProcessorMap[processorId]; | ||
|
||
if (!processor) { | ||
throw new Error(`Payment processor '${processorId}' not found. Available processors: ${Object.keys(paymentProcessorMap).join(', ')}`); | ||
} | ||
|
||
return processor; | ||
} | ||
|
||
/** | ||
* The currently configured payment processor. | ||
*/ | ||
// export const paymentProcessor: PaymentProcessor = lemonSqueezyPaymentProcessor; | ||
export const paymentProcessor: PaymentProcessor = stripePaymentProcessor; | ||
export const paymentProcessor: PaymentProcessor = getPaymentProcessor(); | ||
Genyus marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
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.
Hey, it's great to see that you're trying to improve
open-saas
further above the Polar integration, but I wouldn't do this as part of this PR.Paddle integration PR had the same problem here:
https://github.com/wasp-lang/open-saas/pull/486/files#r2266908329
Doing in this PR will distract us from the main point (Polar), and will prolong the process to get the feature we want.
I've explained in the Paddle PR (linked above) why we don't believe this approach is right for us.
I would kindly ask you to remove non-Polar parts of the PR.
Thanks for the effort.
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.
@FranjoMindek Thanks for the feedback. Happy to proceed, but one of the main reasons I adopted this approach was because I wanted to add e2e tests for Polar and the current architecture makes it impossible to run tests for more than one provider without modifying the source. Do you have any suggestions for how that limitation could be addressed?
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.
Hey @Genyus.
Truthfully,
e2e-tests
are in a bad state and outdated.We will want to improve them but that is a separate issue.
However we did start opening up issues about them:
#469
I wouldn't be concerned with improving them in this PR.
I think it's okay for now to require people to change the
app
to test different payment providers ine2e-tests
.What we want of
e2e-tests
is that we have separatepackage.json
scripts to run for different providers.So if we have the correct payment provider in
app
and run the correct script, it works.So I would rename the current scripts to be more explicit about stripe and then add scripts for
Polar
.This is kinda harder to do fully correct because we would have to change a lot of script names.
I would most likely suggest to do minimal changes for now, and we will refactor all of the names then in a separate PR dedicated to
e2e-tests
.So just change the script name for running Stripe e2e tests, and add yours to be similarly named.
Uh oh!
There was an error while loading. Please reload this page.
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.
One thing in code part of
e2e-tests
you would need to change is to make all "Stripe specific" functions be changed with "payment provider abstract" ones.Now to know which one to run, you could do the environment variable trick in
e2e-tests
, but we would set them throughnpm
scripts instead.e.g.
"local:e2e:polar:start": "PAYMENT_PROVIDER=polar ..."
Then those abstract functions would use
PAYMENT_PROVIDER
to know what to do under the hood.Uh oh!
There was an error while loading. Please reload this page.
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.
There is probably a better way to do this (but it would require too much changes).
This will allows us to do this PR reasonably fast and without larger changes, and later on we can improve it.
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.
OK, understood. So I had already created a separate e2e-tests branch to work on these specifically and I use a
PAYMENT_PROVIDER_ID
env var to handle provider-specific scripts, as you suggested. But I'll leave this alone until I've completed the requested changes to the application code.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.
Okay, btw ping me here and/or on discord when you are ready for re-review.