-
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
Changes from 1 commit
aa9542f
ad0e6da
99e0e27
a68afff
def9ea1
abff006
1d001e3
1c8580c
b3d588a
6afed14
c9be519
00c6001
cf81c0c
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 |
|---|---|---|
|
|
@@ -1934,6 +1934,7 @@ async function handleEndUserTopUpSucceeded( | |
| const netCredited = Number(md.netCredited); | ||
| const developerMargin = Number(md.developerMargin ?? "0"); | ||
| const platformFee = Number(md.platformFee ?? "0"); | ||
| const bonusCredited = Number(md.bonusCredited ?? "0"); | ||
| const grossPaid = paymentIntent.amount / 100; | ||
|
|
||
| if (!walletId || !Number.isFinite(netCredited) || netCredited <= 0) { | ||
|
|
@@ -2026,7 +2027,67 @@ async function handleEndUserTopUpSucceeded( | |
| }); | ||
| } | ||
|
|
||
| return updated.balance; | ||
| // Developer-funded bonus: credit the wallet extra spend power on top of | ||
| // the paid amount, debited from the developer org's credit balance. Only | ||
| // for live wallets, and capped at the org's available credits so the | ||
| // balance can never go negative. The org row is locked (SELECT … FOR | ||
| // UPDATE) so a concurrent debit can't oversell the balance. | ||
|
|
||
| let finalBalance = updated.balance; | ||
| if (bonusCredited > 0 && wallet.mode !== "test") { | ||
| const [org] = await tx | ||
| .select({ credits: tables.organization.credits }) | ||
| .from(tables.organization) | ||
| .where(eq(tables.organization.id, wallet.organizationId)) | ||
| .for("update") | ||
|
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.
When Useful? React with 👍 / 👎. |
||
| .limit(1); | ||
|
|
||
| const availableCredits = Math.max(0, Number(org?.credits ?? "0")); | ||
| const bonusToApply = | ||
| Math.round(Math.min(bonusCredited, availableCredits) * 1e6) / 1e6; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (bonusToApply > 0) { | ||
| const [bonusUpdated] = await tx | ||
| .update(tables.wallet) | ||
| .set({ balance: sql`${tables.wallet.balance} + ${bonusToApply}` }) | ||
| .where(eq(tables.wallet.id, walletId)) | ||
| .returning(); | ||
| finalBalance = bonusUpdated.balance; | ||
|
|
||
| await tx.insert(tables.walletLedger).values({ | ||
| walletId, | ||
| endCustomerId: wallet.endCustomerId, | ||
| organizationId: wallet.organizationId, | ||
| type: "bonus", | ||
| amount: String(bonusToApply), | ||
| balanceAfter: bonusUpdated.balance, | ||
| stripePaymentIntentId: paymentIntent.id, | ||
| description: "End-user top-up bonus", | ||
| }); | ||
|
|
||
| await tx | ||
| .update(tables.organization) | ||
| .set({ | ||
| credits: sql`${tables.organization.credits} - ${bonusToApply}`, | ||
| }) | ||
| .where(eq(tables.organization.id, wallet.organizationId)); | ||
|
|
||
| await tx.insert(tables.transaction).values({ | ||
| organizationId: wallet.organizationId, | ||
| type: "end_user_bonus", | ||
| amount: String(bonusToApply), | ||
| creditAmount: String(-bonusToApply), | ||
| status: "completed", | ||
| stripePaymentIntentId: paymentIntent.id, | ||
| description: `End-user top-up bonus (wallet ${walletId})`, | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| logger.warn( | ||
| `Skipping end-user top-up bonus for wallet ${walletId}: developer org ${wallet.organizationId} has insufficient credits (${availableCredits} available, ${bonusCredited} needed)`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return finalBalance; | ||
| }); | ||
| } catch (err) { | ||
| const code = | ||
|
|
@@ -2044,7 +2105,7 @@ async function handleEndUserTopUpSucceeded( | |
| const updated = { balance: newBalance }; | ||
|
|
||
| logger.info( | ||
| `Credited ${netCredited} to end-user wallet ${walletId} (margin ${developerMargin}, platform fee ${platformFee})`, | ||
| `Credited ${netCredited} to end-user wallet ${walletId} (margin ${developerMargin}, platform fee ${platformFee}, bonus quote ${bonusCredited}, balance now ${newBalance})`, | ||
| ); | ||
|
Comment on lines
2135
to
2137
|
||
|
|
||
| // Notify the developer's webhook endpoints (best-effort). Skip for test-mode | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ALTER TABLE "project" ADD COLUMN "end_user_top_up_bonus_percent" numeric DEFAULT '0' NOT NULL;--> statement-breakpoint | ||
| ALTER TABLE "wallet" ADD COLUMN "bonus_percent_override" numeric; |
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 👍 / 👎.