Skip to content

Run a script when Stripe calls

A webhook trigger starts a script when another service calls its URL. Say your team accepts small disputes rather than fight them, and you want that done the moment a customer disputes a charge. Here’s the script, the trigger Stripe calls, and how a person approves each one before the script closes the dispute.

When a customer disputes a charge, Stripe sends a charge.dispute.created event. The script reads that event and accepts the dispute through Stripe’s API, which closes it in the customer’s favour. It refuses anything over $50.

dispute.py
import json, os, sys, requests
STRIPE = "https://api.stripe.com/v1"
CAP = 50_00 # cents; larger disputes need a person to look
with open(os.environ["REMINIX_INPUTS_FILE"]) as f:
event = json.load(f)["request"]["body"]
if event.get("type") != "charge.dispute.created":
print(json.dumps({"skipped": event.get("type")}))
sys.exit()
dispute = event["data"]["object"]
if dispute["amount"] > CAP:
sys.exit(f"Dispute {dispute['id']} is over $50: look at it in Stripe.")
# No key here: Reminix adds it to requests to api.stripe.com.
closed = requests.post(f"{STRIPE}/disputes/{dispute['id']}/close").json()
print(json.dumps({"dispute": closed["id"], "status": closed["status"],
"amount": f"${dispute['amount'] / 100:.2f}"}))

The folder’s reminix.json declares one input, request, which a webhook trigger fills with Stripe’s call. It also names the host the script calls and where the Stripe key goes:

{
"command": "python dispute.py",
"description": "Accepts a Stripe dispute of up to $50.",
"inputSchema": {
"type": "object",
"required": ["request"],
"properties": { "request": { "type": "object" } }
},
"hosts": ["api.stripe.com"],
"secrets": {
"STRIPE_KEY": {
"host": "api.stripe.com",
"header": "Authorization",
"format": "Bearer {secret}"
}
}
}

Save the Stripe key once, then publish the folder. Here it’s named stripe-dispute, which becomes the script’s slug:

Terminal window
printf %s "$STRIPE_KEY" | reminix secrets set STRIPE_KEY --hosts api.stripe.com
reminix publish

Accepting a dispute gives the money up, so have a person approve each one. Open the script’s Settings and, under Approval, choose Agents need approval. A webhook trigger’s runs wait wherever an agent’s would. Through the API:

Terminal window
curl -X PATCH https://api.reminix.com/v1/scripts/stripe-dispute \
-H "Authorization: Bearer $REMINIX_TOKEN" -H "X-Workspace: acme" \
-H "Content-Type: application/json" \
-d '{ "approvalPolicy": "agents" }'

On the script’s page, choose Triggers → Add trigger, then When something calls its webhook URL. Name it, choose The whole request under Inputs, and leave No signature for now: Stripe gives you its signing secret only after you add the URL. Reminix shows the URL once. Copy it.

In Stripe’s dashboard, open Developers → Webhooks, add an endpoint with that URL, and choose the charge.dispute.created event. Stripe shows the endpoint’s signing secret, whsec_….

Then have the trigger check Stripe’s signature. Its id is in the URL, after /webhooks/:

Terminal window
curl -X PATCH https://api.reminix.com/v1/scripts/stripe-dispute/triggers/$TRIGGER_ID \
-H "Authorization: Bearer $REMINIX_TOKEN" -H "X-Workspace: acme" \
-H "Content-Type: application/json" \
-d '{ "signature": "stripe", "signingSecret": "whsec_…" }'

From then on, Reminix refuses a call without Stripe’s valid signature.

When a customer disputes a $40 charge:

  1. Stripe calls the trigger’s URL with the event. Reminix checks the secret and Stripe’s signature, and answers 202.
  2. Reminix records the run as Awaiting approval, with Stripe’s event as its request input. Workspace owners and admins get a notification.
  3. If someone approves it, the script accepts the dispute. If someone denies it, nothing changes in Stripe.

The trigger’s Recent calls lists each call: started, a repeat, or refused, and why. Stripe sends a call again when it doesn’t hear back, so Reminix starts one run per event, by the event’s id.

If the trigger refuses 50 calls in an hour for a wrong secret or signature, Reminix turns it off and emails you. Rotate its secret, then update the URL in Stripe.