# Customers and the portal

## Your customer reference

`customer_ref` is **your** id for a buyer: a user id, an account number, an email hash. Send it on checkout sessions and server-side orders and Regoria ties orders, saved cards and subscriptions to it. A server-side order (`POST /v1/orders`) must name its customer by `customer_ref` or `customer_id`, and the saved card it charges must be that customer's (`422 customer_ref_required` when neither is sent, `422 payment_method_unusable` on a mismatch). It is 1 to 255 characters with no control characters and never changes once set (`422 customer_ref_immutable`).

## The customer record

A customer appears when a session with a `customer_ref` is paid, or when you create it first:

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/customers \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customer_ref": "cus_A1B2", "email": "buyer@example.com", "name": "Jamie Rivera", "metadata": {"crm_ref": "CRM-1001"}}'
```

`customer_ref` is optional here too: a customer created without one has none. `409 customer_exists` when the reference is taken. One email belongs to one customer: a create or an update with an email another of your customers holds, in any letter case, answers `409` `customer_already_exists` with that customer's `customer_id` and writes nothing. A checkout session sent without `customer_ref` gets its customer from the buyer's email at payment: the customer already holding it, or a new one with no `customer_ref`; a session whose `customer_ref` arrives with an email another customer holds belongs to that customer, who takes the reference when it has none. `PATCH /v1/customers/{customer}` updates email, name or metadata (last write wins). `GET /v1/customers` lists them, those with orders first, with `orders_count`, `first_order_at`, `last_order_at` and `refunded_cents`; `customer_ref=` and `buyer_email=` find one.

Every customer has a `cus_` id. Sessions, orders, subscriptions and payment methods return it as `customer_id` beside `customer_ref`, and the `order.paid` and `subscription.*` webhooks carry it as `customer_id` too. The order, subscription, charge and customer lists filter by `customer_id=` as well as `customer_ref=`.

## Saved cards

A card is saved when a buyer pays with `save_payment_method: true`, pays a plan (the renewals need it) or saves one in a `setup` session or the portal. `payment_method.created` names the `pm_` id; `GET /v1/payment-methods?customer_ref=cus_A1B2` (or `?customer_id=` with the customer's `cus_` id; one of the two is required) lists a customer's cards with brand, last 4, expiry, `issuer_country` (the issuing bank's country from the card's BIN, `null` when the BIN is unknown) and `is_default`. Only a summary is ever returned: the card number and security code never leave the processor's vault.

`DELETE /v1/payment-methods/{paymentMethod}` removes a card from the customer; a subscription renewing on it needs another card first (`POST /v1/subscriptions/{subscription}/payment-method`).

## The portal

The hosted portal lets a buyer see their orders and receipts, manage their cards, and cancel or change a subscription, without you building any of it. You mint a link for one customer:

```bash
curl -X POST https://api.sandbox.staging.regoria.com/v1/portal-sessions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"customer_ref": "cus_A1B2"}'
```

Name the customer by `customer_ref` or by `customer_id` (its `cus_` id), one of the two; both answer `422` `validation` on `customer_id`. The answer is `url` and `expires_at`: the link signs the buyer straight in, with no email and no code, **once** and for **10 minutes**, so mint it when the buyer clicks and open it at once. A second open, or an open after it expires, shows the expired-link page. Pass `flow` (`update_card` or `cancel`) with `subscription_id` (a `sub_` id of this customer) to open the portal on that subscription's flow; without `flow` the link opens the portal home. Pass `email` when the customer has none on file yet (it fills an empty one, never overwrites); an email another of your customers already holds answers `409` `customer_already_exists` with that customer's `customer_id`, and nothing is written.

The portal links in Regoria's own emails still open the email-and-code step; for a customer with no `customer_ref` they name it by its `cus_` id.

Put the link behind a "Manage billing" button in your own signed-in area, mint it on click, and redirect. What the buyer does in the portal reaches you as events: `subscription.cancelled`, `subscription.payment_method_changed`, `payment_method.created`, `payment_method.updated`.
