Skip to content

Add Slack revenue notifications to Stripe webhook (invoice.payment_succeeded)#359

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/setup-stripe-qonto-slack-integration
Open

Add Slack revenue notifications to Stripe webhook (invoice.payment_succeeded)#359
Copilot wants to merge 3 commits into
mainfrom
copilot/setup-stripe-qonto-slack-integration

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 3, 2026

This updates the Stripe webhook flow so confirmed invoice payments are surfaced in Slack in real time, using server-side signature validation and environment-scoped secrets. The webhook now emits structured payment notifications only for invoice.payment_succeeded events.

  • Webhook event handling

    • Extends /api/webhook to process invoice.payment_succeeded (instead of a no-op path).
    • Extracts amount_paid, currency, and invoice description from the Stripe payload.
    • Adds defensive amount parsing to avoid runtime failures on malformed values.
  • Slack integration (server-mediated)

    • Adds notify_slack(amount, currency, message) in api/index.py.
    • Sends a structured Slack Incoming Webhook payload (status, amount, description).
    • Skips delivery if SLACK_WEBHOOK_URL is unset and handles request failures safely.
  • Runtime configuration and secrets

    • Reads and applies STRIPE_SECRET_KEY, STRIPE_ENDPOINT_SECRET, and SLACK_WEBHOOK_URL from environment variables.
    • Keeps credentials out of source while preserving strict Stripe signature verification.
if event_type == "invoice.payment_succeeded":
    invoice = event.get("data", {}).get("object", {})
    amount_paid = int(invoice.get("amount_paid") or 0)
    currency = str(invoice.get("currency", "")).upper()
    description = str(invoice.get("description") or "Pago de cliente confirmado")
    notify_slack(amount_paid / 100, currency, description)

@LVT-ENG
Copy link
Copy Markdown
Member

LVT-ENG commented Jun 3, 2026

Supercommit_max.sh

Copy link
Copy Markdown
Member

@LVT-ENG LVT-ENG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duoecommmit

@LVT-ENG
Copy link
Copy Markdown
Member

LVT-ENG commented Jun 3, 2026

Supercommit_maxsh

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the existing Flask /api/webhook Stripe webhook handler to emit a Slack Incoming Webhook notification when an invoice.payment_succeeded event is received, and adds unit tests to validate the behavior and error handling.

Changes:

  • Handle invoice.payment_succeeded in /api/webhook by extracting amount_paid, currency, and description, then calling a Slack notifier.
  • Add notify_slack(amount, currency, message) to send a structured Slack webhook payload (and no-op when SLACK_WEBHOOK_URL is unset).
  • Add tests/test_stripe_webhook.py to cover Slack-notification behavior, ignored event types, and invalid payload handling.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
api/index.py Adds Slack notification helper, reads SLACK_WEBHOOK_URL, and processes invoice.payment_succeeded events in the Stripe webhook handler.
tests/test_stripe_webhook.py Adds unit tests for the Stripe webhook route and Slack notification behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread api/index.py
amount_paid = 0
currency = str(invoice.get("currency", "")).upper()
description = str(invoice.get("description") or "Pago de cliente confirmado")
notify_slack(amount_paid / 100, currency, description)
Comment thread api/index.py
Comment on lines +363 to +367
response = requests.post(SLACK_WEBHOOK_URL, json=payload, timeout=5)
return response.status_code == 200
except requests.RequestException as e:
print(f"[tryonyou] slack webhook error: {e}", file=sys.stderr)
return False
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.

3 participants