# paypal

Payments and subscriptions with PayPal, over its REST API: no package, no
PayPal script in the page (the buyer approves on PayPal's own page). The
whole of it is [docs/paypal.md](../../docs/paypal.md).

```toml
# mote.toml
[packages]
paypal = "../mote/extensions/paypal"
```

```text
use "pkg:paypal/paypal.mote"        // the API
use "pkg:paypal/webhooks.mote"      // POST /paypal/webhook, PaypalSubscriptionKept, the hooks

let o = paypalOrder({amount: 4500, currency: "USD", user: me, ref: order, back: "…/paid", cancel: "…/cart", key: `order-${order}`})?
go(o.approve)                          // then, back at /paid?token=…:
paypalCapture(token)?                  // once, however often it is asked
fn paypalPaid(c: PaypalCapture): bool  // the event: give what was bought
```

Settings: `PAYPAL_CLIENT_ID`, `PAYPAL_SECRET` (a secret), `PAYPAL_LIVE=1`
for live (unset: the sandbox), `PAYPAL_WEBHOOK_ID` with webhooks.mote.
Optional: `PAYPAL_CAPTURE_ON_APPROVAL`, `PAYPAL_PLANS`, `PAYPAL_API`.

| File | |
|---|---|
| `paypal.mote` | The token (kept, got again on a 401); orders (create, get, capture once, authorize, take, void), captures, refunds; plans (every page), subscriptions (begin, get, cancel, suspend, activate); payouts; each typed and failing with a sentence and a kind; `paypalStart`, `PaypalButton`. |
| `webhooks.mote` | The route: the event checked by PayPal's verify call, or here by PayPal's certificate (`PAYPAL_VERIFY=cert`, on `crypto.cert`), kept once, answered at once; a job that captures an approved order nobody came back for, reads each subscription again into `PaypalSubscriptionKept`, and calls the hooks. |
| `tests.mote` | Its tests: `use "pkg:paypal/tests.mote"` in a program that takes both files, then `mote test`. |

**Covered**, from what a product does week to week: a one-off payment by
redirect (create, approve at PayPal, capture, a capture the buyer never came
back for, a declined way of paying sent back to PayPal), authorize and
capture later, refunds in part or whole, subscriptions to plans made in
PayPal's dashboard and their states kept by events, payouts to PayPal
addresses, events verified by PayPal's call and applied once.

**Left out on purpose**: PayPal's JavaScript buttons and card fields (the
redirect needs none; a page that wants them can use `extern script`, as
docs/js.md shows for Stripe); making products and plans from code (the
Catalog Products and Plans APIs: plans change rarely, and PayPal plans
cannot change their price once active); disputes (`CUSTOMER.DISPUTE.*`
reach `paypalEvent`, and are answered in PayPal's Resolution Center);
Vault (saved payment methods), invoicing, shipping and tracking, and
PayPal's partner (marketplace) APIs, which need PayPal's approval.

**Not yet run against the real PayPal**: only against a stand-in of its API
(`tests/svc_standin.mjs paypal`, `tests/all.sh real_svc_paypal`). The
endpoints and fields are written from PayPal's REST reference as of 2026;
the steps to check them in the sandbox are in the docs.
