-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(credits): prepurchase credits #2174
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
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
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
|
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.
27 files reviewed, 5 comments
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.
28 files reviewed, 2 comments
Summary
Can prepurchase credits with payment method on file.
Type of Change
Testing
Tested manually
Checklist