feat(credits): prepurchase credits #2174
Merged
icecrasher321 merged 15 commits intostagingfrom Dec 7, 2025
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
Greptile OverviewGreptile SummaryImplements prepurchased credit system for Pro and Team plans. Users can purchase $10-$1000 in credits using their payment method on file, which are then applied to reduce overage charges before billing. Key Changes:
Issues Found:
Confidence Score: 3/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant UI as Credit Balance UI
participant API as /api/billing/credits
participant Purchase as purchaseCredits()
participant Stripe
participant DB as Database
participant Webhook as Invoice Webhook
User->>UI: Click "Add Credits"
UI->>UI: Generate requestId (UUID)
User->>UI: Enter amount ($10-$1000)
UI->>API: POST with amount and requestId
API->>API: Validate session and amount
API->>Purchase: purchaseCredits(userId, amount, requestId)
Purchase->>DB: canPurchaseCredits(userId)
DB-->>Purchase: Pro/Team only = true
Purchase->>DB: getHighestPrioritySubscription(userId)
DB-->>Purchase: subscription data
Purchase->>DB: isOrgAdmin(userId, orgId)
DB-->>Purchase: admin check result
Purchase->>Stripe: subscriptions.retrieve(subscriptionId)
Stripe-->>Purchase: subscription with customer and payment method
Purchase->>Stripe: invoices.create(metadata contains credit purchase type)
Note over Purchase,Stripe: Uses idempotency key with requestId
Stripe-->>Purchase: invoice created
Purchase->>Stripe: invoiceItems.create(amount)
Stripe-->>Purchase: line item added
Purchase->>Stripe: invoices.finalizeInvoice(invoiceId)
Stripe-->>Purchase: invoice finalized
Purchase->>Stripe: invoices.pay(invoiceId)
Note over Purchase,Stripe: Initiates payment (async)
Stripe-->>Purchase: payment initiated
Purchase->>DB: addCredits(entityType, entityId, amount)
Note over Purchase,DB: ISSUE: Credits added before payment confirmed
DB-->>Purchase: credits added
Purchase->>DB: getCreditBalance(entityId)
DB-->>Purchase: new balance
Purchase->>DB: setUsageLimitForCredits()
Note over Purchase,DB: Only updates if newLimit > currentLimit
DB-->>Purchase: limit updated
Purchase-->>API: success response
API-->>UI: 200 OK
UI-->>User: Credits added successfully message
Note over Stripe,Webhook: Later (async)
Stripe->>Webhook: invoice.payment_succeeded
Webhook->>DB: getCreditBalance(entityId)
DB-->>Webhook: current balance
Webhook->>Webhook: Send confirmation emails
Webhook-->>Stripe: webhook processed
|
10 tasks
Collaborator
|
@greptile |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Can prepurchase credits with payment method on file.
Type of Change
Testing
Tested manually
Checklist