id: stripe-payments-reconciliation
namespace: company.team
description: |
Reconcile Stripe charges against internal paid orders every day: match on
metadata order id, flag missing and amount-mismatched records, open a GitHub
issue and alert Slack when the books do not balance.
inputs:
- id: lookback_hours
type: INT
displayName: Lookback window (hours)
description: How far back to fetch Stripe charges and internal orders (default
24 for a daily run).
defaults: 24
- id: repository
type: STRING
displayName: GitHub repository
description: owner/repo where the discrepancy issue is opened when the books do
not balance.
defaults: your-org/your-repo
- id: amount_tolerance_cents
type: INT
displayName: Amount tolerance (cents)
description: Absolute difference in cents that is still considered a match.
defaults: 0
tasks:
- id: fetch_stripe_charges
type: io.kestra.plugin.core.http.Request
description: Pull recent Stripe charges - each one carries the internal order id
in metadata.order_id.
uri: "https://api.stripe.com/v1/charges?limit=100&created[gte]={{ (now() |
dateAdd(-inputs.lookback_hours, 'HOURS') | date('X')) }}"
method: GET
headers:
Authorization: "Bearer {{ secret('STRIPE_SECRET_KEY') }}"
- id: fetch_internal_orders
type: io.kestra.plugin.jdbc.postgresql.Query
description: Pull the internal orders marked paid in the same window.
url: "{{ secret('POSTGRES_JDBC_URL') }}"
username: "{{ secret('POSTGRES_USERNAME') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
fetchType: FETCH
sql: |
SELECT
order_id,
amount_cents,
currency,
created_at
FROM finance.orders
WHERE status = 'paid'
AND created_at >= now() - make_interval(hours => {{ inputs.lookback_hours }})
ORDER BY created_at
- id: reconcile
type: io.kestra.plugin.scripts.python.Script
description: Match both sides on order id and classify every discrepancy.
inputFiles:
charges.json: "{{ outputs.fetch_stripe_charges.body }}"
orders.json: "{{ outputs.fetch_internal_orders.rows | json }}"
script: |
import json
from kestra import Kestra
with open("charges.json") as f:
charges = json.load(f).get("data", [])
with open("orders.json") as f:
orders = json.load(f)
tolerance = int({{ inputs.amount_tolerance_cents }})
internal = {o["order_id"]: o for o in orders}
discrepancies = []
matched = 0
untracked = 0
seen_orders = set()
for charge in charges:
if not charge.get("paid") or charge.get("status") != "succeeded":
continue
order_id = (charge.get("metadata") or {}).get("order_id")
if not order_id:
untracked += 1
continue
seen_orders.add(order_id)
internal_order = internal.get(order_id)
if internal_order is None:
discrepancies.append({
"kind": "missing_internal_order",
"order_id": order_id,
"stripe_cents": charge.get("amount", 0),
"internal_cents": None,
})
continue
diff = abs(charge.get("amount", 0) - internal_order.get("amount_cents", 0))
if diff > tolerance:
discrepancies.append({
"kind": "amount_mismatch",
"order_id": order_id,
"stripe_cents": charge.get("amount", 0),
"internal_cents": internal_order.get("amount_cents", 0),
})
else:
matched += 1
for order_id, order in internal.items():
if order_id not in seen_orders:
discrepancies.append({
"kind": "missing_stripe_charge",
"order_id": order_id,
"stripe_cents": None,
"internal_cents": order.get("amount_cents", 0),
})
Kestra.outputs({
"discrepancy_count": len(discrepancies),
"matched_count": matched,
"stripe_charge_count": len(charges),
"internal_order_count": len(orders),
"untracked_charges": untracked,
"discrepancies": discrepancies[:20],
})
- id: recon_gate
type: io.kestra.plugin.core.flow.If
description: Open the discrepancy issue and alert Slack when anything failed to
match, log a clean close otherwise.
condition: "{{ outputs.reconcile.vars.discrepancy_count > 0 }}"
then:
- id: create_issue
type: io.kestra.plugin.github.issues.Create
description: Open a labeled GitHub issue listing every unmatched or mismatched
order.
jwtToken: "{{ secret('GITHUB_TOKEN') }}"
repository: "{{ inputs.repository }}"
title: "Payment reconciliation: {{ outputs.reconcile.vars.discrepancy_count }}
discrepancy(ies) in the last {{ inputs.lookback_hours }}h"
body: |
## Stripe vs internal orders
**{{ outputs.reconcile.vars.discrepancy_count }} discrepancy(ies)** in the last {{ inputs.lookback_hours }} hours:
{{ outputs.reconcile.vars.matched_count }} matched, {{ outputs.reconcile.vars.stripe_charge_count }} Stripe charge(s), {{ outputs.reconcile.vars.internal_order_count }} internal order(s), {{ outputs.reconcile.vars.untracked_charges }} charge(s) without order metadata.
| Kind | Order | Stripe (cents) | Internal (cents) |
| --- | --- | --- | --- |
{% for d in outputs.reconcile.vars.discrepancies %}| {{ d.kind }} | {{ d.order_id }} | {{ d.stripe_cents }} | {{ d.internal_cents }} |
{% endfor %}
labels:
- reconciliation
- id: alert_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
:warning: Payment reconciliation found {{ outputs.reconcile.vars.discrepancy_count }} discrepancy(ies) in the last {{ inputs.lookback_hours }}h ({{ outputs.reconcile.vars.matched_count }} matched).
{% for d in outputs.reconcile.vars.discrepancies %}- {{ d.kind }}: order {{ d.order_id }} (stripe {{ d.stripe_cents }} vs internal {{ d.internal_cents }} cents)
{% endfor %}
Details in the linked GitHub issue.
else:
- id: log_clean
type: io.kestra.plugin.core.log.Log
message: "Reconciliation clean: {{ outputs.reconcile.vars.matched_count }}
order(s) matched, 0 discrepancies over the last {{
inputs.lookback_hours }} hours."
outputs:
- id: discrepancy_count
type: INT
value: "{{ outputs.reconcile.vars.discrepancy_count }}"
- id: matched_count
type: INT
value: "{{ outputs.reconcile.vars.matched_count }}"
- id: untracked_charges
type: INT
value: "{{ outputs.reconcile.vars.untracked_charges }}"
errors:
- id: alert_on_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: ":rotating_light: Payment reconciliation {{ flow.id }} FAILED
(execution {{ execution.id }}). The books were not checked this run.
Error: {{ error.message }}"
triggers:
- id: daily_close
type: io.kestra.plugin.core.trigger.Schedule
description: Run after the daily order batch lands. Enable when ready.
cron: "0 7 * * *"
disabled: true
- id: on_demand
type: io.kestra.plugin.core.trigger.Webhook
description: Reconcile on demand, for example from a finance runbook. Replace
the key with your own value.
key: replace-with-a-random-key