-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Lemon Squeezy Importer #4214
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
Open
pepeladeira
wants to merge
16
commits into
main
Choose a base branch
from
lemonsqueezy-importer
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.
Open
Lemon Squeezy Importer #4214
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aeec6f7
initial lemon squeezy importer structure
pepeladeira 19ce444
code improvements
pepeladeira f458d38
update commission status
pepeladeira f199b91
Merge branch 'main' into lemonsqueezy-importer
pepeladeira 81e83ec
Merge branch 'main' into lemonsqueezy-importer
pepeladeira f602c15
add referral_amount and handle metadata.user_id
pepeladeira 287afc2
change orders over invoices for subscriptions
pepeladeira 0515d04
customer_id null
pepeladeira 8617af2
Merge branch 'main' into lemonsqueezy-importer
pepeladeira 94f5cb8
Merge branch 'main' into lemonsqueezy-importer
pepeladeira 9748846
first time order commission
pepeladeira 2916d66
Merge branch 'main' into lemonsqueezy-importer
pepeladeira 0f018ce
code improvements
pepeladeira 90cc229
Merge branch 'main' into lemonsqueezy-importer
pepeladeira 0cc97ac
code improvements
pepeladeira 11f5271
Merge branch 'main' into lemonsqueezy-importer
pepeladeira 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,26 @@ | ||
| import { withCron } from "@/lib/cron/with-cron"; | ||
| import { importCommissions } from "@/lib/lemonsqueezy/import-commissions"; | ||
| import { importCustomers } from "@/lib/lemonsqueezy/import-customers"; | ||
| import { importPartners } from "@/lib/lemonsqueezy/import-partners"; | ||
| import { lemonSqueezyImportPayloadSchema } from "@/lib/lemonsqueezy/schemas"; | ||
| import { logAndRespond } from "../../utils"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| export const POST = withCron(async ({ rawBody }) => { | ||
| const payload = lemonSqueezyImportPayloadSchema.parse(JSON.parse(rawBody)); | ||
|
|
||
| switch (payload.action) { | ||
| case "import-partners": | ||
| await importPartners(payload); | ||
| break; | ||
| case "import-customers": | ||
| await importCustomers(payload); | ||
| break; | ||
| case "import-commissions": | ||
| await importCommissions(payload); | ||
| break; | ||
| } | ||
|
|
||
| return logAndRespond("OK"); | ||
| }); |
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
21 changes: 21 additions & 0 deletions
21
...p/(ee)/api/stripe/integration/webhook/utils/get-dub-customer-external-id-from-metadata.ts
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,21 @@ | ||
| /** | ||
| * Resolve Dub customer.externalId from Stripe object metadata. | ||
| * Precedence: | ||
| * 1. dubCustomerExternalId | ||
| * 2. dubCustomerId | ||
| * 3. user_id (Lemon Squeezy customer id after LS → Stripe migration) | ||
| */ | ||
| export function getDubCustomerExternalIdFromMetadata( | ||
| metadata?: Record<string, string> | null, | ||
| ): string | undefined { | ||
| if (!metadata) return undefined; | ||
|
|
||
| const value = | ||
| metadata.dubCustomerExternalId || | ||
| metadata.dubCustomerId || | ||
| metadata.user_id; | ||
|
|
||
| if (value == null || value === "") return undefined; | ||
|
|
||
| return String(value); | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| "use server"; | ||
|
|
||
| import { LemonSqueezyApi } from "@/lib/lemonsqueezy/api"; | ||
| import { lemonSqueezyImporter } from "@/lib/lemonsqueezy/importer"; | ||
| import { LemonSqueezyStore } from "@/lib/lemonsqueezy/types"; | ||
| import * as z from "zod/v4"; | ||
| import { authActionClient } from "../safe-action"; | ||
| import { throwIfNoPermission } from "../throw-if-no-permission"; | ||
|
|
||
| const schema = z.object({ | ||
| workspaceId: z.string(), | ||
| apiKey: z.string().trim().min(1), | ||
| }); | ||
|
|
||
| export const setLemonSqueezyTokenAction = authActionClient | ||
| .inputSchema(schema) | ||
| .action(async ({ parsedInput, ctx }) => { | ||
| const { workspace } = ctx; | ||
| const { apiKey } = parsedInput; | ||
|
|
||
| throwIfNoPermission({ | ||
| role: workspace.role, | ||
| requiredRoles: ["owner", "member"], | ||
| }); | ||
|
|
||
| const lemonSqueezyApi = new LemonSqueezyApi({ | ||
| apiKey, | ||
| }); | ||
|
|
||
| let stores: LemonSqueezyStore[]; | ||
|
|
||
| try { | ||
| stores = await lemonSqueezyApi.listStores(); | ||
| } catch (error) { | ||
| console.error(error); | ||
| throw new Error("Invalid Lemon Squeezy API key."); | ||
| } | ||
|
|
||
| if (stores.length === 0) { | ||
| throw new Error("No stores found in your Lemon Squeezy account."); | ||
| } | ||
|
|
||
| await lemonSqueezyImporter.setCredentials(workspace.id, { | ||
| apiKey, | ||
| }); | ||
|
|
||
| return { | ||
| stores, | ||
| }; | ||
| }); |
62 changes: 62 additions & 0 deletions
62
apps/web/lib/actions/partners/start-lemonsqueezy-import.ts
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,62 @@ | ||
| "use server"; | ||
|
|
||
| import { createId } from "@/lib/api/create-id"; | ||
| import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw"; | ||
| import { LemonSqueezyApi } from "@/lib/lemonsqueezy/api"; | ||
| import { lemonSqueezyImporter } from "@/lib/lemonsqueezy/importer"; | ||
| import * as z from "zod/v4"; | ||
| import { getProgramOrThrow } from "../../api/programs/get-program-or-throw"; | ||
| import { authActionClient } from "../safe-action"; | ||
| import { throwIfNoPermission } from "../throw-if-no-permission"; | ||
|
|
||
| const schema = z.object({ | ||
| workspaceId: z.string(), | ||
| storeId: z.string().trim().min(1), | ||
| }); | ||
|
|
||
| export const startLemonSqueezyImportAction = authActionClient | ||
| .inputSchema(schema) | ||
| .action(async ({ ctx, parsedInput }) => { | ||
| const { workspace, user } = ctx; | ||
| const { storeId } = parsedInput; | ||
|
|
||
| throwIfNoPermission({ | ||
| role: workspace.role, | ||
| requiredRoles: ["owner", "member"], | ||
| }); | ||
|
|
||
| const programId = getDefaultProgramIdOrThrow(workspace); | ||
|
|
||
| const program = await getProgramOrThrow({ | ||
| workspaceId: workspace.id, | ||
| programId, | ||
| }); | ||
|
|
||
| if (!program.domain) { | ||
| throw new Error("Program domain is not set."); | ||
| } | ||
|
|
||
| if (!program.url) { | ||
| throw new Error("Program URL is not set."); | ||
| } | ||
|
|
||
| const credentials = await lemonSqueezyImporter.getCredentials(workspace.id); | ||
|
|
||
| const lemonSqueezyApi = new LemonSqueezyApi({ | ||
| apiKey: credentials.apiKey, | ||
| }); | ||
|
|
||
| const stores = await lemonSqueezyApi.listStores(); | ||
|
|
||
| if (!stores.some((store) => store.id === storeId)) { | ||
| throw new Error("Invalid Lemon Squeezy store ID."); | ||
| } | ||
|
|
||
| await lemonSqueezyImporter.queue({ | ||
| importId: createId({ prefix: "import_" }), | ||
| userId: user.id, | ||
| programId: program.id, | ||
| storeId, | ||
| action: "import-partners", | ||
| }); | ||
| }); |
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.
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.