-
Notifications
You must be signed in to change notification settings - Fork 157
feat(sdk): configurable top-up bonus multiplier #2894
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
+27,609
−23
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa9542f
feat(sdk): configurable top-up bonus multiplier
steebchen ad0e6da
fix(sdk): address top-up bonus review feedback
steebchen 99e0e27
chore(db): reset migrations before merging main
steebchen a68afff
Merge remote-tracking branch 'origin/main' into top-up-bonus-multiplier
steebchen def9ea1
chore(db): regenerate migration after merging main
steebchen abff006
fix(admin): exclude SDK end-user rows from revenue, track bonus
steebchen 1d001e3
test(sdk): e2e bonus flow + pre-seeded POC project
steebchen 1c8580c
fix(admin): count end-user top-ups as revenue, not the bonus
steebchen b3d588a
feat(admin): show combined free credits given (gift + bonus)
steebchen 6afed14
chore(db): reset migrations before merging main
steebchen c9be519
Merge remote-tracking branch 'origin/main' into top-up-bonus-multiplier
steebchen 00c6001
chore(db): regenerate migration after merging main
steebchen cf81c0c
Merge remote-tracking branch 'origin/main' into top-up-bonus-multiplier
steebchen 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "vitest"; | ||
|
|
||
| import { db, eq, tables } from "@llmgateway/db"; | ||
|
|
||
| import { | ||
| handleEndUserTopUpRefunded, | ||
| handleEndUserTopUpSucceeded, | ||
| } from "./stripe.js"; | ||
| import { deleteAll } from "./testing.js"; | ||
|
|
||
| import type Stripe from "stripe"; | ||
|
|
||
| const ORG_ID = "bonus-org-id"; | ||
| const PROJECT_ID = "bonus-project-id"; | ||
|
|
||
| interface TopUpMetadata { | ||
| walletId: string; | ||
| netCredited: string; | ||
| developerMargin?: string; | ||
| platformFee?: string; | ||
| bonusCredited?: string; | ||
| } | ||
|
|
||
| function makeTopUpIntent( | ||
| id: string, | ||
| amount: number, | ||
| metadata: TopUpMetadata, | ||
| ): Stripe.PaymentIntent { | ||
| return { | ||
| id, | ||
| amount, | ||
| metadata: { | ||
| kind: "end_user_topup", | ||
| ...metadata, | ||
| }, | ||
| } as unknown as Stripe.PaymentIntent; | ||
| } | ||
|
|
||
| async function seedWallet(opts: { | ||
| walletId: string; | ||
| customerId: string; | ||
| externalId: string; | ||
| mode: "live" | "test"; | ||
| }) { | ||
| await db.insert(tables.endCustomer).values({ | ||
| id: opts.customerId, | ||
| organizationId: ORG_ID, | ||
| projectId: PROJECT_ID, | ||
| externalId: opts.externalId, | ||
| mode: opts.mode, | ||
| }); | ||
|
|
||
| await db.insert(tables.wallet).values({ | ||
| id: opts.walletId, | ||
| endCustomerId: opts.customerId, | ||
| projectId: PROJECT_ID, | ||
| organizationId: ORG_ID, | ||
| mode: opts.mode, | ||
| balance: "0", | ||
| }); | ||
| } | ||
|
|
||
| async function getOrgCredits(): Promise<number> { | ||
| const org = await db.query.organization.findFirst({ | ||
| where: { id: { eq: ORG_ID } }, | ||
| }); | ||
| return Number(org?.credits ?? "0"); | ||
| } | ||
|
|
||
| async function getWalletBalance(walletId: string): Promise<number> { | ||
| const wallet = await db.query.wallet.findFirst({ | ||
| where: { id: { eq: walletId } }, | ||
| }); | ||
| return Number(wallet?.balance ?? "0"); | ||
| } | ||
|
|
||
| describe("end-user top-up bonus", () => { | ||
| beforeEach(async () => { | ||
| await db.insert(tables.organization).values({ | ||
| id: ORG_ID, | ||
| name: "Bonus Org", | ||
| billingEmail: "bonus@example.com", | ||
| credits: "100", | ||
| }); | ||
|
|
||
| await db.insert(tables.project).values({ | ||
| id: PROJECT_ID, | ||
| name: "Bonus Project", | ||
| organizationId: ORG_ID, | ||
| endUserEnabled: true, | ||
| endUserTopUpBonusPercent: "50", | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await deleteAll(); | ||
| }); | ||
|
|
||
| test("credits the bonus and debits the developer org credits (live wallet)", async () => { | ||
| await seedWallet({ | ||
| walletId: "wallet-live", | ||
| customerId: "cust-live", | ||
| externalId: "ext-live", | ||
| mode: "live", | ||
| }); | ||
|
|
||
| await handleEndUserTopUpSucceeded( | ||
| makeTopUpIntent("pi_bonus_live", 1000, { | ||
| walletId: "wallet-live", | ||
| netCredited: "10", | ||
| bonusCredited: "5", | ||
| }), | ||
| ); | ||
|
|
||
| // $10 paid + $5 bonus of spend power. | ||
| expect(await getWalletBalance("wallet-live")).toBe(15); | ||
| // The $5 bonus is funded from the org's credit balance. | ||
| expect(await getOrgCredits()).toBe(95); | ||
|
|
||
| const ledger = await db.query.walletLedger.findMany({ | ||
| where: { walletId: { eq: "wallet-live" } }, | ||
| }); | ||
| const topup = ledger.find((r) => r.type === "topup"); | ||
| const bonus = ledger.find((r) => r.type === "bonus"); | ||
| expect(Number(topup?.amount)).toBe(10); | ||
| expect(Number(bonus?.amount)).toBe(5); | ||
|
|
||
| const bonusTxn = await db.query.transaction.findFirst({ | ||
| where: { | ||
| organizationId: { eq: ORG_ID }, | ||
| type: { eq: "end_user_bonus" }, | ||
| }, | ||
| }); | ||
| expect(Number(bonusTxn?.creditAmount)).toBe(-5); | ||
| }); | ||
|
|
||
| test("caps the bonus at the org's available credits", async () => { | ||
| await db | ||
| .update(tables.organization) | ||
| .set({ credits: "3" }) | ||
| .where(eq(tables.organization.id, ORG_ID)); | ||
|
|
||
| await seedWallet({ | ||
| walletId: "wallet-cap", | ||
| customerId: "cust-cap", | ||
| externalId: "ext-cap", | ||
| mode: "live", | ||
| }); | ||
|
|
||
| await handleEndUserTopUpSucceeded( | ||
| makeTopUpIntent("pi_bonus_cap", 1000, { | ||
| walletId: "wallet-cap", | ||
| netCredited: "10", | ||
| bonusCredited: "5", | ||
| }), | ||
| ); | ||
|
|
||
| // Only $3 of bonus could be funded, so the wallet gets $10 + $3. | ||
| expect(await getWalletBalance("wallet-cap")).toBe(13); | ||
| expect(await getOrgCredits()).toBe(0); | ||
| }); | ||
|
|
||
| test("never applies a bonus to a test-mode wallet", async () => { | ||
| await seedWallet({ | ||
| walletId: "wallet-test", | ||
| customerId: "cust-test", | ||
| externalId: "ext-test", | ||
| mode: "test", | ||
| }); | ||
|
|
||
| await handleEndUserTopUpSucceeded( | ||
| makeTopUpIntent("pi_bonus_test", 1000, { | ||
| walletId: "wallet-test", | ||
| netCredited: "10", | ||
| bonusCredited: "5", | ||
| }), | ||
| ); | ||
|
|
||
| expect(await getWalletBalance("wallet-test")).toBe(10); | ||
| // Org credits are untouched for sandbox top-ups. | ||
| expect(await getOrgCredits()).toBe(100); | ||
|
|
||
| const bonus = await db.query.walletLedger.findFirst({ | ||
| where: { walletId: { eq: "wallet-test" }, type: { eq: "bonus" } }, | ||
| }); | ||
| expect(bonus).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("claws back the bonus and restores org credits on refund", async () => { | ||
| await seedWallet({ | ||
| walletId: "wallet-refund", | ||
| customerId: "cust-refund", | ||
| externalId: "ext-refund", | ||
| mode: "live", | ||
| }); | ||
|
|
||
| await handleEndUserTopUpSucceeded( | ||
| makeTopUpIntent("pi_bonus_refund", 1000, { | ||
| walletId: "wallet-refund", | ||
| netCredited: "10", | ||
| bonusCredited: "5", | ||
| }), | ||
| ); | ||
| expect(await getWalletBalance("wallet-refund")).toBe(15); | ||
| expect(await getOrgCredits()).toBe(95); | ||
|
|
||
| const topUpRow = await db.query.walletLedger.findFirst({ | ||
| where: { | ||
| stripePaymentIntentId: { eq: "pi_bonus_refund" }, | ||
| type: { eq: "topup" }, | ||
| }, | ||
| }); | ||
| expect(topUpRow).toBeDefined(); | ||
|
|
||
| await handleEndUserTopUpRefunded(topUpRow!); | ||
|
|
||
| // Both the paid top-up and the developer-funded bonus are pulled back. | ||
| expect(await getWalletBalance("wallet-refund")).toBe(0); | ||
| // The $5 bonus is returned to the org's credit balance. | ||
| expect(await getOrgCredits()).toBe(100); | ||
| }); | ||
| }); |
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.
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.
When the developer org already has fewer credits than the configured bonus, this returns and stores the full
bonusCreditedamount even though the success handler later caps the actual bonus to the org's available balance. In that scenario the payment UI can show (for example) a $15 credit total for a $10 top-up with a 50% bonus, but after the user pays the wallet may receive only $10 because the org had no credits left. Cap the quoted bonus against the current org balance, or make the response explicitly an estimate so the checkout does not promise spend power that fulfillment will not apply.Useful? React with 👍 / 👎.