Payments and the Stripe webhook

How Robert's API creates Stripe payments, verifies webhook signatures and keeps subscriptions up to date.

On this page The path of a payment The webhook Signature verificationEvents handledPrivate messages to subscribersServer slots Configuration Sandbox mode

Robert's payments go through Stripe. This page describes the technical flow, for people who develop or host Robert. How it works from the subscriber's side is explained in Managing your subscription.

Robert never sees bank cards

Payment takes place on a page hosted by Stripe. Robert's API only receives the state of the subscription, never a card number.

The path of a payment

  1. The dashboard calls POST /billing/checkout with the plan, the billing cycle and the two mandatory consents (accept_terms and waive_withdrawal).
  2. The API creates a Stripe Checkout session and returns its address; the browser is redirected there.
  3. After payment, Stripe sends the person back to the dashboard (Subscription & invoices).
  4. In parallel, Stripe calls Robert's webhook. It, and it alone, activates or updates the subscription.

The consents (date the terms of sale were accepted, request for immediate performance) are recorded in the Stripe session's metadata and in the API's audit log. Stripe promotion codes are accepted on the payment page. The free trial is only added if the account has never had a subscription.

Extra servers go through PUT /billing/subscriptions/{id}/extra-servers ({"quantity": n}): the API adds, changes or removes the “extra server” line of the Stripe subscription (prorated), then the customer.subscription.updated webhook confirms the quantity.

The webhook

POST https://robert-bot.com/api/v1/billing/webhook

This route is public (no session, no CSRF token): the signature is what authenticates Stripe.

Signature verification

Stripe sends a Stripe-Signature header of the form t=1790812800,v1=5257a8…. The API:

  1. recomputes the HMAC-SHA256 of t, a full stop and the raw request body, using the STRIPE_WEBHOOK_SECRET secret;
  2. compares the result with the v1 signatures, in constant time;
  3. rejects messages whose t timestamp is more than 5 minutes away from the server's clock.
Situation Response
STRIPE_WEBHOOK_SECRET is not set 503
Signature missing, invalid or too old 400 bad_signature
Event accepted 200 {"received": true}

Events handled

Stripe event Effect
checkout.session.completed in one-off payment mode Activates the Lifetime Premium offer (or credits the AI credit top-up) for the account named in the metadata, only when the session is paid (payment_status = paid). An unpaid session (delayed payment method) grants nothing and waits for the next event. If every lifetime place is taken when the payment completes, nothing is granted and an open error (source billing) asks for a refund
checkout.session.async_payment_succeeded Delayed payment collected: grants what the session paid for, as above
checkout.session.async_payment_failed Delayed payment declined: nothing is granted, the failure is written to the audit log
customer.subscription.created Creates the subscription with its plan (read from the price), cycle and status. One free trial per Discord account: a second subscription in trial has its trial ended at Stripe (charged at once)
customer.subscription.updated Updates the status, the period end, the scheduled cancellation and the number of extra servers
customer.subscription.deleted Sets the subscription to the canceled status
charge.refunded Full refund (refunded = true) of a one-off payment: the lifetime access moves to the canceled status (the server falls back to the free plan), the AI credit top-up is withdrawn. A partial refund withdraws nothing and stays in the audit log. A refund of a recurring subscription is only logged: Stripe sends customer.subscription.* itself
invoice.payment_failed Failed renewal payment: access is left to customer.subscription.updated, the failure is written to the audit log and the subscriber gets a private message on Discord (one per attempt). The first payment of a new subscription is not concerned: it fails in front of the customer, on the payment page
charge.dispute.created Dispute (bank chargeback): same effect as a full refund

Other events are accepted and ignored. Every event received is written to the API's audit log.

Private messages to subscribers

Robert tells the subscriber by Discord private message what an event changed: confirmed order, trial started, scheduled or undone cancellation, plan change, failed payment and payment received again, end of the subscription, refund or dispute. Each message is sent once: an event Stripe sends again with the same identifier changes the data as before but sends nothing. Messages about a failed payment, the end of a subscription, a refund or a dispute are always sent; the others follow the account preference shown on the Subscription page.

The Robert account concerned is found through the subscription's user_id metadata or, failing that, through the Stripe customer ID. A subscription is considered active when its status is active or trialing and its current period has not ended.

If a plan reaches its end date without Stripe having announced a renewal, the API sets the subscription to expired on its own (with an hour's grace, to give the webhook time to arrive). The servers it covered are then flagged to the bot, which reloads their plan: see Bot integration.

Server slots

A subscription's capacity is the number of servers in its plan, plus the quantity of the “Extra server” add-on present in the Stripe subscription. If the capacity drops, the surplus slots are removed, starting with the servers assigned most recently.

Configuration

Environment variable Purpose
STRIPE_SECRET_KEY Secret key of the Stripe API. Without it, payments are unavailable (503 billing_not_configured)
STRIPE_WEBHOOK_SECRET Signing secret of the webhook endpoint (whsec_…)
STRIPE_PRICES JSON object mapping each offer to its Stripe price ID
STRIPE_AUTOMATIC_TAX 1 to let Stripe work out VAT automatically
ROBERT_BILLING_SANDBOX 1 for sandbox mode (see below)

The keys of STRIPE_PRICES follow the plan_cycle pattern:

{
  "premium_month": "price_…",
  "premium_year": "price_…",
  "pro_month": "price_…",
  "pro_year": "price_…",
  "network_month": "price_…",
  "network_year": "price_…",
  "premium_lifetime_once": "price_…",
  "extra_server_month": "price_…",
  "extra_server_year": "price_…"
}

If a price is missing, POST /billing/checkout answers 503 with the price_missing code.

In the Stripe dashboard, create a webhook endpoint pointing to the address above and subscribe it to the four events in the “Events handled” table.

Sandbox mode

With no Stripe key and ROBERT_BILLING_SANDBOX=1, POST /billing/checkout activates the plan immediately, with no payment. This mode is for testing the whole flow (plan, slots, limits) in development. The dashboard then shows “Payments in test mode: nothing is actually charged”.

GET /health reports the current mode in its billing field: "stripe", "sandbox" or null.