# Payment Gears (invite-only)

> Build a way to pay at Vorjo's checkout - signed payment sessions, webhook plus verify, test mode, review, refunds. Invite-only.

**Invite-only.** Payment Gears are open to Vorjo's own (house) accounts while we test them. Everything below is the
contract we hold them to, and it is strict on purpose: money moves through them.

A Payment Gear adds a way to pay to a shop's checkout (a local wallet, a bank app, a regional card processor). Like every
hosted Gear it runs on your own server ([Build a Gear](/developers/gears-hosted)). Unlike other Gears, **Vorjo owns the
order and the amount**: you never tell Vorjo what to charge, you only take the payment Vorjo asked for and tell Vorjo it
happened, twice, in two different ways.

## The rules

1. **Vorjo owns the order and the amount.** Vorjo creates the order, prices it, and signs a payment session. Take exactly
   the amount and currency in the session. Never take an amount from the URL, a form or the shopper.
2. **Only Vorjo marks an order paid**, and only after both:
   - your **signed webhook** says the session was paid, and
   - Vorjo's **server-to-server call to your `verifyUrl`** returns the same session, order number, amount and currency.
3. **Any difference is never paid.** If the order number, amount, currency (or your reference) differ between the session,
   your webhook and your verify answer, the order stays unpaid, it is **flagged** for the shop owner ("don't ship"), and
   Vorjo's team is alerted. A flagged session never becomes paid later.
4. **Each webhook event counts once.** Event ids are remembered; a replay is answered `200 {"result":"duplicate"}` and
   ignored. Calls older than 5 minutes are refused.
5. **Test mode first.** A new Payment Gear is in test mode: its orders are test orders (never shipped, never counted as
   sales). It goes live only after **3 successful test payments**, a working test refund, and Vorjo's review.
   Changing the `payment` part of the manifest, your company details or your origins puts it back in test mode until
   it is reviewed again.
6. **Always reviewed**, whatever its visibility. Vorjo can turn a Payment Gear off at every checkout at once (the kill
   switch); payments already made still confirm and refund.

## The flow

```
Shopper            Shop checkout (Vorjo)                    Your Gear                         Vorjo API
   |  Pay with X  ->  |                                         |                                  |
   |                  | order #1042, 99.00 USD (pending)        |                                  |
   |                  | session ps_… signed, 30 min, nonce      |                                  |
   |  <- redirect to sessionUrl?session=<token> ---------------> |                                  |
   |                                                             | check signature + expiry         |
   |  pays on your page  -------------------------------------> | take exactly 99.00 USD           |
   |                                                             | POST webhook (signed) ---------> | check HMAC, time, event id
   |                                                             |                                  | compare order/amount/currency
   |                                                             | <---- POST verifyUrl (signed) -- | ask you again
   |                                                             | {session, order, amount, …} ---> | all match? -> PAID
   |  <- redirect to returnUrl (Vorjo's thank-you page) -------- |                                  |
```

Coming back to `returnUrl` never marks anything paid on its own. If your webhook arrived but your verify endpoint was
down, Vorjo asks verify again when the shopper comes back or the owner opens the order.

## The manifest

A Payment Gear is a hosted Gear with `"type": "payments"` and a `payment` part. It can't add scripts or widgets, and it
has no monthly price.

```json
{
  "id": "acme-pay",
  "type": "payments",
  "name": "Acme Pay",
  "category": "payments",
  "description": "Pay with your Acme wallet. Payments are confirmed with Acme before the order is paid.",
  "developer": { "name": "Acme Payments Ltd", "supportEmail": "support@acme.example", "website": "https://acme.example",
    "privacyUrl": "https://acme.example/privacy", "termsUrl": "https://acme.example/terms" },
  "origins": ["https://pay.acme.example"],
  "payment": {
    "label": "Acme Pay",
    "hint": "You'll confirm in the Acme app.",
    "methods": ["wallet"],
    "countries": ["US", "CA"],
    "currencies": ["USD", "CAD"],
    "sessionUrl": "https://pay.acme.example/vorjo/pay",
    "verifyUrl": "https://pay.acme.example/vorjo/verify",
    "refundUrl": "https://pay.acme.example/vorjo/refund",
    "partialRefunds": true
  }
}
```

| Field | Rule |
| --- | --- |
| `label` | Shown at checkout. Up to 40 characters. |
| `hint` | Optional line under the label at checkout. Up to 120 characters. |
| `methods` | What the shopper pays with, lowercase (`card`, `wallet`, `bank`, `mobile_money`…). At least one. |
| `countries` | Two-letter codes the shopper's billing country (else delivery country) must be in. Empty: every country. |
| `currencies` | Three-letter codes. The method is shown only when the shop's currency is one of them. |
| `sessionUrl`, `verifyUrl`, `refundUrl` | Required, `https://`, public addresses only. |
| `partialRefunds` | `true` when you can refund part of a payment. Otherwise only the full amount can be refunded. |

## Signatures

Your Gear's **signing secret** (Developer dashboard) signs everything, both ways.

**Signed calls** (your webhook to Vorjo; Vorjo's verify and refund calls to you) carry three headers:

```
x-vorjo-gear: acme-pay
x-vorjo-timestamp: 1764633600
x-vorjo-signature: v1=<hex HMAC-SHA256(secret, "<timestamp>.<raw body>")>
```

Check the signature with a constant-time compare **before** reading the body, and refuse a timestamp more than 5 minutes
from now. Answer **401** to anything wrongly signed (the review checks this).

**The session token** in `sessionUrl?session=…` is `base64url(JSON) + "." + hex HMAC-SHA256(secret, base64url part)`.
Refuse it when the signature is wrong or `exp` has passed. Its payload:

```json
{ "v": 1, "session": "ps_…", "gear": "acme-pay", "shop": { "id": "…", "slug": "lamp-house", "name": "Lamp House" },
  "order": 1042, "orderName": "#1042", "amount": "99.00", "currency": "USD", "test": true,
  "returnUrl": "https://lamp-house.vorjo.site/_vorjo/store/return?…", "cancelUrl": "https://lamp-house.vorjo.site/checkout",
  "webhookUrl": "https://api.vorjo.com/v1/payments/acme-pay/webhook", "nonce": "…", "iat": 1764633600, "exp": 1764635400 }
```

`amount` is a decimal string in the currency's units (`"99.00"`, or `"1500"` for JPY). A session lasts **30 minutes**:
don't start a payment on an expired one. `test: true` means no real money may move.

## Your webhook to Vorjo

`POST` the session's `webhookUrl`, signed, once the payment succeeded or failed:

```json
{ "id": "evt_8f2c…", "type": "payment.succeeded", "session": "ps_…", "order": 1042, "amount": "99.00", "currency": "USD", "reference": "acme_tx_123" }
```

- `id`: unique per event. Retrying the same event? Send the same id.
- `type`: `payment.succeeded` or `payment.failed`.
- `reference`: your own payment id. Vorjo stores it and sends it back on verify and refund.

Answers: `200 {"ok":true,"result":"paid" | "verify pending" | "failed" | "mismatch" | "duplicate" | "already paid"}`,
`401` bad signature, `400` stale or malformed, `404` unknown session.

## Your verify endpoint

Vorjo `POST`s `verifyUrl`, signed: `{ "session": "ps_…", "order": 1042, "reference": "acme_tx_123" }`. Look the payment up
in **your own records** and answer `200`:

```json
{ "session": "ps_…", "order": 1042, "amount": "99.00", "currency": "USD", "status": "paid", "reference": "acme_tx_123" }
```

`status` is `paid`, `failed` or `pending`. Unknown session: `404`. Never echo back what the request said; answer from your
ledger.

## Your refund endpoint

From the owner's order page, Vorjo `POST`s `refundUrl`, signed:

```json
{ "refund": "rf_…", "session": "ps_…", "order": 1042, "reference": "acme_tx_123", "amount": "10.00", "currency": "USD", "reason": "Scratched" }
```

Refund it and answer `200 { "refund": "rf_…", "status": "succeeded", "amount": "10.00", "currency": "USD", "reference": "your refund id" }`.
The same `refund` id asked again must give the same answer and refund once. Anything else (`"status": "failed"` with a
`message`, an error, a different amount) is shown to the owner and nothing is recorded as refunded.

## Test mode and going live

1. Install your Gear on a house account's shop (Gear Shop). It shows at checkout marked **Test**.
2. Make 3 test payments that succeed, and one test refund.
3. Vorjo reviews it in Admin › Store › Gears. Every item must pass and be ticked by the reviewer:

| Check | What passes |
| --- | --- |
| HTTPS only | Session, verify, refund, privacy and terms addresses are `https://`. |
| Signature check | `verifyUrl` answers 401 or 403 to a wrongly signed call. |
| Verify endpoint | At least one test payment confirmed (webhook and verify matched). |
| Refund endpoint | At least one test refund succeeded. |
| Privacy and terms | Both linked in `developer`. |
| Company info | Company name, support email and website. The reviewer checks the company is real. |
| 3 test payments | Three successful test payments on the current version. |

Vorjo watches failed payments and mismatches. Too many failures, or any mismatch, opens an alert for Vorjo's team, and a
Payment Gear can be turned off at every checkout at once.

## Security requirements

- Keep the signing secret on your server only. Rotate it from the Developer dashboard if it leaks.
- Verify every signature, constant-time, before parsing. Refuse stale timestamps.
- Take the amount and currency from the signed session only. Show them to the shopper before they pay.
- Keep your own ledger of sessions and payments; verify and refund answer from it.
- Treat the session id as single-use: one payment per session.
- Make refunds idempotent by refund id.
- Never put card numbers or bank passwords in anything you send Vorjo.

## Try it: the sample Payment Gear

`scripts/sample-payment-gear.mjs` in Vorjo's repository implements all of the above in one file (Node, no packages).
Run it with your Gear's id and secret; open `http://127.0.0.1:8790/` for the manifest to paste.

```
GEAR_ID=sample-pay GEAR_SECRET=vgs_… node scripts/sample-payment-gear.mjs
```

Plain `http://127.0.0.1` addresses only work on a local Vorjo (development). Against vorjo.com, give it a public https
address, for example with a tunnel (`cloudflared tunnel --url http://127.0.0.1:8790`), and start it with
`PUBLIC_URL=https://<your-tunnel-address>` so the manifest it prints uses that address.

Its payment page has Pay, Decline and Cancel, plus "Report a wrong amount" and "Report a wrong order" to watch Vorjo
refuse and flag a payment.
