Skip to content

payment chargeback notifications#1109

Merged
N2D4 merged 3 commits into
payment-email-templatesfrom
payment-chargeback-notifications
Jan 20, 2026
Merged

payment chargeback notifications#1109
N2D4 merged 3 commits into
payment-email-templatesfrom
payment-chargeback-notifications

Conversation

@BilalG1
Copy link
Copy Markdown
Collaborator

@BilalG1 BilalG1 commented Jan 15, 2026


Note

Adds invoice visibility and chargeback alerts, and persists invoice data from Stripe.

  • New API: GET /payments/invoices/{customer_type}/{customer_id} with cursor pagination; returns status, amount_total, hosted_invoice_url, created_at_millis
  • Client SDK/UI: listInvoices (+ React hook) and shared invoices types; integrates into template client app caches
  • DB/Model: SubscriptionInvoice gains status, amountTotal, hostedInvoiceUrl (migration + Prisma schema)
  • Stripe webhooks: Track subscription changes incl. invoice.*; upsertStripeInvoice persists invoices; handle charge.dispute.created and send Telegram notifications; stricter handling of unknown events; safer invoice line access
  • Telegram util: New lib/telegram with getTelegramConfig and sendTelegramMessage; init-script-callback migrated to use it
  • Tests: E2E for invoices listing, webhook behaviors (unknown type rejection, chargeback acceptance, receipt/failed emails)

Written by Cursor Bugbot for commit 706ba96. This will update automatically on new commits. Configure here.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jan 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
stack-backend Ready Ready Preview, Comment Jan 20, 2026 0:37am
stack-dashboard Ready Ready Preview, Comment Jan 20, 2026 0:37am
stack-demo Ready Ready Preview, Comment Jan 20, 2026 0:37am
stack-docs Ready Ready Preview, Comment Jan 20, 2026 0:37am

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 15, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jan 15, 2026

Greptile Summary

This PR adds Telegram notifications for payment chargebacks by implementing a handler for Stripe's charge.dispute.created webhook event. The implementation follows clean architecture principles by extracting shared Telegram functionality into a reusable utility module.

Key Changes:

  • Created apps/backend/src/lib/telegram.tsx with getTelegramConfig() and sendTelegramMessage() utilities to handle Telegram API interactions
  • Added charge.dispute.created event handler in the Stripe webhook route that sends formatted chargeback notifications to a dedicated Telegram channel
  • Refactored init-script-callback route to use the new shared Telegram utilities, eliminating code duplication
  • Added comprehensive test coverage for the chargeback webhook with proper mock data handling
  • Introduced support for multiple Telegram chat channels via environment variable configuration (STACK_TELEGRAM_CHAT_ID_CHARGEBACKS for chargebacks, STACK_TELEGRAM_CHAT_ID for init-stack)

The notification message includes all relevant dispute details: project info, tenancy ID, Stripe account, dispute ID, amount, reason, status, associated charge/payment intent IDs, timestamp, and live mode indicator.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is well-structured with proper error handling, follows existing patterns in the codebase, includes test coverage, and makes sensible refactoring improvements. The Telegram notification feature is optional (gracefully returns early if not configured) and doesn't affect existing functionality.
  • No files require special attention

Important Files Changed

Filename Overview
apps/backend/src/lib/telegram.tsx New utility module for Telegram notifications with config management and message sending
apps/backend/src/app/api/latest/integrations/stripe/webhooks/route.tsx Added charge.dispute.created webhook handler to send Telegram notifications for chargebacks
apps/backend/src/app/api/latest/internal/init-script-callback/route.tsx Refactored to use shared Telegram utilities, reducing code duplication
apps/e2e/tests/backend/endpoints/api/v1/stripe-webhooks.test.ts Added test coverage for chargeback webhook handling with proper mock data

Sequence Diagram

sequenceDiagram
    participant Stripe
    participant WebhookRoute as Stripe Webhook Handler
    participant TelegramLib as Telegram Library
    participant StripeAPI as Stripe API
    participant Database
    participant TelegramAPI as Telegram API

    Stripe->>WebhookRoute: charge.dispute.created event
    WebhookRoute->>WebhookRoute: Extract mockData from event
    WebhookRoute->>WebhookRoute: Check event.type === "charge.dispute.created"
    WebhookRoute->>TelegramLib: getTelegramConfig("chargebacks")
    TelegramLib->>TelegramLib: Read STACK_TELEGRAM_BOT_TOKEN
    TelegramLib->>TelegramLib: Read STACK_TELEGRAM_CHAT_ID_CHARGEBACKS
    alt Config not available
        TelegramLib-->>WebhookRoute: null
        WebhookRoute-->>Stripe: Return early (200 OK)
    else Config available
        TelegramLib-->>WebhookRoute: {botToken, chatId}
        WebhookRoute->>WebhookRoute: Validate event.account exists
        WebhookRoute->>StripeAPI: getTenancyForStripeAccountId(accountId, mockData)
        StripeAPI->>StripeAPI: Retrieve account metadata
        StripeAPI-->>WebhookRoute: Tenancy object
        WebhookRoute->>WebhookRoute: buildChargebackMessage()
        WebhookRoute->>WebhookRoute: Format dispute details
        WebhookRoute->>TelegramLib: sendTelegramMessage({botToken, chatId, message})
        TelegramLib->>TelegramAPI: POST to api.telegram.org/sendMessage
        TelegramAPI-->>TelegramLib: Response
        alt Telegram request failed
            TelegramLib-->>WebhookRoute: Throw StackAssertionError
        else Success
            TelegramLib-->>WebhookRoute: Success
            WebhookRoute-->>Stripe: 200 OK {received: true}
        end
    end
Loading

Comment thread apps/backend/src/lib/telegram.tsx
@BilalG1 BilalG1 requested a review from N2D4 January 15, 2026 02:11
@BilalG1 BilalG1 assigned N2D4 and unassigned BilalG1 Jan 15, 2026
@N2D4 N2D4 merged commit 2f3e4cb into payment-email-templates Jan 20, 2026
16 of 17 checks passed
@N2D4 N2D4 deleted the payment-chargeback-notifications branch January 20, 2026 00:30
@promptless
Copy link
Copy Markdown
Contributor

promptless Bot commented Jan 21, 2026

📝 Documentation updates detected!

New suggestion: Document invoice listing feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants