@three-ws/x402-payment-modal 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/LICENSE +180 -0
- package/README.md +347 -0
- package/TUTORIAL.md +188 -0
- package/dist/index.d.ts +141 -0
- package/dist/x402.js +1777 -0
- package/dist/x402.min.js +375 -0
- package/docs/api-reference.md +227 -0
- package/docs/architecture.md +171 -0
- package/docs/server-setup.md +239 -0
- package/docs/siwx.md +116 -0
- package/docs/spending-caps.md +92 -0
- package/docs/theming.md +124 -0
- package/examples/README.md +22 -0
- package/examples/plain-html/index.html +229 -0
- package/examples/react/README.md +69 -0
- package/examples/react/X402Button.jsx +84 -0
- package/examples/server-express/package.json +16 -0
- package/examples/server-express/public/index.html +89 -0
- package/examples/server-express/server.js +89 -0
- package/package.json +113 -0
- package/server/README.md +68 -0
- package/server/checkout.js +392 -0
- package/server/express.js +44 -0
- package/server/vercel.js +54 -0
- package/src/index.js +1776 -0
- package/types/index.d.ts +141 -0
- package/types/server.d.ts +109 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@three-ws/x402-payment-modal` are documented here. This
|
|
4
|
+
project adheres to [Semantic Versioning](https://semver.org).
|
|
5
|
+
|
|
6
|
+
## 1.1.0
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- **Pay in USDC _or_ THREE on Solana.** When a 402 challenge offers more than one
|
|
11
|
+
Solana token, the modal renders a token picker so the buyer chooses which to
|
|
12
|
+
pay in; the headline price and the built transaction follow the choice. USDC
|
|
13
|
+
and [$THREE](https://three.ws/three-token) (`FeMb…pump`) are recognized by mint
|
|
14
|
+
— correct symbol, decimals, and branding even when the `accept` omits
|
|
15
|
+
`extra.name`/`extra.decimals`.
|
|
16
|
+
- **`solanaAccept()` server helper** — build a spec-shaped Solana `accept` from
|
|
17
|
+
`token: 'usdc' | 'three'` (or an explicit `mint`) with the price as `uiAmount`
|
|
18
|
+
(human) or `amount` (atomic). Exports `THREE_MINT`, `USDC_MINT_SOLANA`, and
|
|
19
|
+
`WELL_KNOWN_SOLANA_TOKENS`.
|
|
20
|
+
- **`window.X402.tokens`** + client exports `THREE_MINT`, `USDC_MINT_SOLANA`,
|
|
21
|
+
`KNOWN_SOLANA_TOKENS` for inline merchants composing challenges in the browser.
|
|
22
|
+
|
|
23
|
+
### Notes
|
|
24
|
+
|
|
25
|
+
- THREE is a utility token, not a stablecoin: the browser can't dollar-denominate
|
|
26
|
+
it, so client-side spending caps apply to USDC only — enforce THREE caps
|
|
27
|
+
server-side. Settlement is unchanged — the checkout endpoint already transfers
|
|
28
|
+
any SPL mint named by the chosen `accept`.
|
|
29
|
+
|
|
30
|
+
## 1.0.0
|
|
31
|
+
|
|
32
|
+
Initial public release. Extracted from the three.ws platform as a standalone,
|
|
33
|
+
dependency-free package.
|
|
34
|
+
|
|
35
|
+
### Added
|
|
36
|
+
|
|
37
|
+
- **Drop-in payment modal** for any x402 paid endpoint — declarative
|
|
38
|
+
(`data-x402-endpoint` auto-binding) and programmatic (`pay()` / `window.X402`).
|
|
39
|
+
- **Solana payments** via Phantom (USDC), backed by the bundled checkout server
|
|
40
|
+
helpers (`prepare` / `encode`).
|
|
41
|
+
- **EVM payments** via any injected wallet (Base USDC, EIP-3009
|
|
42
|
+
`transferWithAuthorization`) — signed entirely in the browser, no server call.
|
|
43
|
+
- **SIWX (Sign-In-With-X / CAIP-122) re-entry** — wallets that already paid can
|
|
44
|
+
sign in instead of paying again, with automatic fallback to the pay flow.
|
|
45
|
+
- **Client-side spending caps** — per-call / hourly / daily limits enforced in
|
|
46
|
+
`localStorage`, with rollback on failure.
|
|
47
|
+
- **`configure()` + script-tag `data-*` config** for checkout origin, branding,
|
|
48
|
+
builder-code attribution, and esm.sh CDN URLs.
|
|
49
|
+
- **Framework-agnostic server module** with Express and Vercel adapters
|
|
50
|
+
(`@three-ws/x402-payment-modal/server`).
|
|
51
|
+
- **Theming** — light + automatic dark mode, all classes overridable, `--x402-z`
|
|
52
|
+
z-index custom property.
|
|
53
|
+
- **Automatic 429 throttle retry** — re-sends the same signed payment up to twice
|
|
54
|
+
while an upstream rate limit resets (no double charge; payment isn't settled
|
|
55
|
+
until the merchant call succeeds).
|
|
56
|
+
- TypeScript definitions, full docs (`docs/`), tutorial, and runnable examples
|
|
57
|
+
(plain HTML, React, Express).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other transformations
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any
|
|
81
|
+
Contribution embodied within the Work constitutes direct or contributory
|
|
82
|
+
patent infringement, then any patent licenses granted to You under
|
|
83
|
+
this License for that Work shall terminate as of the date such
|
|
84
|
+
litigation is filed.
|
|
85
|
+
|
|
86
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
87
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
88
|
+
modifications, and in Source or Object form, provided that You
|
|
89
|
+
meet the following conditions:
|
|
90
|
+
|
|
91
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
92
|
+
Works a copy of this License; and
|
|
93
|
+
|
|
94
|
+
(b) You must cause any modified files to carry prominent notices
|
|
95
|
+
stating that You changed the files; and
|
|
96
|
+
|
|
97
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
98
|
+
that You distribute, all copyright, patent, trademark, and
|
|
99
|
+
attribution notices from the Source form of the Work,
|
|
100
|
+
excluding those notices that do not pertain to any part of
|
|
101
|
+
the Derivative Works; and
|
|
102
|
+
|
|
103
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
104
|
+
distribution, You must include a readable copy of the
|
|
105
|
+
attribution notices contained within such NOTICE file, in
|
|
106
|
+
at least one of the following places: within a NOTICE text
|
|
107
|
+
file distributed as part of the Derivative Works; within
|
|
108
|
+
the Source form or documentation, if provided along with the
|
|
109
|
+
Derivative Works; or, within a display generated by the
|
|
110
|
+
Derivative Works, if and wherever such third-party notices
|
|
111
|
+
normally appear. The contents of the NOTICE file are for
|
|
112
|
+
informational purposes only and do not modify the License.
|
|
113
|
+
You may add Your own attribution notices within Derivative
|
|
114
|
+
Works that You distribute, alongside or in addition to the
|
|
115
|
+
NOTICE text from the Work, provided that such additional
|
|
116
|
+
attribution notices cannot be construed as modifying the License.
|
|
117
|
+
|
|
118
|
+
You may add Your own license statement for Your modifications and
|
|
119
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
120
|
+
publish, distribute, sublicense, and/or sell copies of the
|
|
121
|
+
Derivative Works, and to permit persons to whom the Derivative Works
|
|
122
|
+
are furnished to do so.
|
|
123
|
+
|
|
124
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
125
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
126
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
127
|
+
this License, without any additional terms or conditions.
|
|
128
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
129
|
+
the terms of any separate license agreement you may have executed
|
|
130
|
+
with Licensor regarding such Contributions.
|
|
131
|
+
|
|
132
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
133
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
134
|
+
except as required for reasonable and customary use in describing the
|
|
135
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
136
|
+
|
|
137
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
138
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
139
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
140
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
141
|
+
implied, including, without limitation, any warranties or conditions
|
|
142
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
143
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
144
|
+
appropriateness of using or reproducing the Work and assume any
|
|
145
|
+
risks associated with Your exercise of permissions under this License.
|
|
146
|
+
|
|
147
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
148
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
149
|
+
unless required by applicable law (such as deliberate and grossly
|
|
150
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
151
|
+
liable to You for damages, including any direct, indirect, special,
|
|
152
|
+
incidental, or exemplary damages of any character arising as a
|
|
153
|
+
result of this License or out of the use or inability to use the
|
|
154
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
155
|
+
work stoppage, computer failure or malfunction, or all other
|
|
156
|
+
commercial damages or losses), even if such Contributor has been
|
|
157
|
+
advised of the possibility of such damages.
|
|
158
|
+
|
|
159
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
160
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
161
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
162
|
+
or other liability obligations and/or rights consistent with this
|
|
163
|
+
License. However, in accepting such obligations, You may offer only
|
|
164
|
+
conditions consistent with this License.
|
|
165
|
+
|
|
166
|
+
END OF TERMS AND CONDITIONS
|
|
167
|
+
|
|
168
|
+
Copyright 2026 nirholas
|
|
169
|
+
|
|
170
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
171
|
+
you may not use this file except in compliance with the License.
|
|
172
|
+
You may obtain a copy of the License at
|
|
173
|
+
|
|
174
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
175
|
+
|
|
176
|
+
Unless required by applicable law or agreed to in writing, software
|
|
177
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
178
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
179
|
+
See the License for the specific language governing permissions and
|
|
180
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
# @three-ws/x402-payment-modal
|
|
2
|
+
|
|
3
|
+
**A drop-in payment modal for any [x402](https://x402.org) paid endpoint.** One ES
|
|
4
|
+
module, zero runtime dependencies. It turns an HTTP `402 Payment Required`
|
|
5
|
+
challenge into a polished checkout: wallet connect (Phantom on Solana, MetaMask /
|
|
6
|
+
any EVM wallet on Base via EIP-3009), the `402 → sign → settle` flow, optional
|
|
7
|
+
SIWX re-entry, client-side spending caps, and a receipt — vanilla JS, no bundler
|
|
8
|
+
required.
|
|
9
|
+
|
|
10
|
+
```html
|
|
11
|
+
<script type="module" src="https://unpkg.com/@three-ws/x402-payment-modal"></script>
|
|
12
|
+
|
|
13
|
+
<button
|
|
14
|
+
data-x402-endpoint="https://api.example.com/paid/summarize"
|
|
15
|
+
data-x402-method="POST"
|
|
16
|
+
data-x402-body='{"text":"hello"}'
|
|
17
|
+
data-x402-merchant="Acme"
|
|
18
|
+
data-x402-action="Summarize">
|
|
19
|
+
Pay & Run
|
|
20
|
+
</button>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
That's the whole integration. Clicking the button opens the modal, runs the
|
|
24
|
+
payment, calls your endpoint with the signed `X-PAYMENT` header, and fires an
|
|
25
|
+
`x402:result` event with the response.
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Why
|
|
30
|
+
|
|
31
|
+
x402 lets any HTTP endpoint charge a micropayment per call: the server answers an
|
|
32
|
+
unpaid request with `402` and a list of accepted payments; the client pays and
|
|
33
|
+
retries with an `X-PAYMENT` header. The protocol is simple — the *wallet UX* is
|
|
34
|
+
not. This package is the missing front end: a single, framework-agnostic modal
|
|
35
|
+
that handles wallet detection, chain switching, signing, settlement, errors,
|
|
36
|
+
throttle retries, and the receipt, so you ship a paid endpoint in minutes.
|
|
37
|
+
|
|
38
|
+
- **Zero dependencies, zero build.** Plain ES module. Drop in a `<script>` tag or
|
|
39
|
+
`import` it. The Solana/EVM crypto helpers are loaded lazily from a CDN *only*
|
|
40
|
+
when a payment is actually attempted.
|
|
41
|
+
- **Solana + EVM.** Phantom (Solana) and any injected EVM wallet (Base USDC
|
|
42
|
+
via EIP-3009 `transferWithAuthorization`). The modal picks the right path from
|
|
43
|
+
the 402 challenge.
|
|
44
|
+
- **Pay in USDC _or_ THREE on Solana.** Offer both and the buyer gets a token
|
|
45
|
+
picker — pay in USDC, or in [$THREE](https://three.ws/three-token), the three.ws
|
|
46
|
+
utility token, recognized on sight (symbol, decimals, branding). See
|
|
47
|
+
[Accepting multiple Solana tokens](#accepting-multiple-solana-tokens-usdc--three).
|
|
48
|
+
- **SIWX re-entry.** If a wallet already paid for a resource, it can sign back in
|
|
49
|
+
instead of paying again. See [docs/siwx.md](docs/siwx.md).
|
|
50
|
+
- **Spending caps.** Optional per-call / hourly / daily caps enforced in the
|
|
51
|
+
browser. See [docs/spending-caps.md](docs/spending-caps.md).
|
|
52
|
+
- **Themeable.** Light + automatic dark mode, all classes overridable. See
|
|
53
|
+
[docs/theming.md](docs/theming.md).
|
|
54
|
+
- **Accessible.** Focus management, `aria-modal`, keyboard `Esc` to close.
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Install
|
|
59
|
+
|
|
60
|
+
**Via CDN (no build step):**
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<script type="module" src="https://unpkg.com/@three-ws/x402-payment-modal"></script>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**Via npm (bundler / framework):**
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npm install @three-ws/x402-payment-modal
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
import { pay, configure } from '@three-ws/x402-payment-modal';
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
For Solana payments you also run a tiny server endpoint — install the optional
|
|
77
|
+
peer deps there:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
npm install @solana/web3.js @solana/spl-token
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> EVM-only sites need **nothing** server-side: the wallet signs EIP-3009
|
|
84
|
+
> typed-data entirely in the browser.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Quick start
|
|
89
|
+
|
|
90
|
+
### 1. Declarative (HTML attributes)
|
|
91
|
+
|
|
92
|
+
Any element with `data-x402-endpoint` is auto-bound on load and re-scanned as the
|
|
93
|
+
DOM changes:
|
|
94
|
+
|
|
95
|
+
```html
|
|
96
|
+
<button
|
|
97
|
+
data-x402-endpoint="/api/paid/translate"
|
|
98
|
+
data-x402-method="POST"
|
|
99
|
+
data-x402-body='{"text":"bonjour","to":"en"}'
|
|
100
|
+
data-x402-merchant="Acme Translate"
|
|
101
|
+
data-x402-action="Translate">
|
|
102
|
+
Translate ($0.01)
|
|
103
|
+
</button>
|
|
104
|
+
|
|
105
|
+
<script type="module" src="https://unpkg.com/@three-ws/x402-payment-modal"></script>
|
|
106
|
+
|
|
107
|
+
<script>
|
|
108
|
+
document.querySelector('button').addEventListener('x402:result', (e) => {
|
|
109
|
+
console.log('paid + result:', e.detail.result);
|
|
110
|
+
});
|
|
111
|
+
</script>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### 2. Programmatic
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import { pay } from '@three-ws/x402-payment-modal';
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
const { result, payment } = await pay({
|
|
121
|
+
endpoint: '/api/paid/translate',
|
|
122
|
+
method: 'POST',
|
|
123
|
+
body: { text: 'bonjour', to: 'en' },
|
|
124
|
+
merchant: 'Acme Translate',
|
|
125
|
+
action: 'Translate',
|
|
126
|
+
});
|
|
127
|
+
console.log(result, 'tx:', payment?.transaction);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
if (err.code !== 'cancelled') console.error(err);
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
See the runnable [examples/](examples/) — plain HTML, React, and an Express
|
|
134
|
+
server.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## How it works
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
click ──▶ GET/POST endpoint
|
|
142
|
+
│
|
|
143
|
+
▼ HTTP 402 { accepts: [ {network, amount, asset, payTo, ...} ] }
|
|
144
|
+
┌─────────────┐
|
|
145
|
+
│ discover │ modal shows price + network
|
|
146
|
+
└─────────────┘
|
|
147
|
+
│
|
|
148
|
+
▼
|
|
149
|
+
┌─────────────┐
|
|
150
|
+
│ connect │ Phantom (Solana) or EVM wallet (Base)
|
|
151
|
+
└─────────────┘
|
|
152
|
+
│
|
|
153
|
+
┌────┴───────────────────────────────────────┐
|
|
154
|
+
▼ Solana ▼ EVM
|
|
155
|
+
POST /api/x402-checkout?action=prepare sign EIP-3009
|
|
156
|
+
Phantom signTransaction transferWithAuthorization
|
|
157
|
+
POST /api/x402-checkout?action=encode (browser only — no server)
|
|
158
|
+
└────┬───────────────────────────────────────┘
|
|
159
|
+
▼
|
|
160
|
+
┌─────────────┐
|
|
161
|
+
│ verify │ retry endpoint with `X-PAYMENT: <base64>`
|
|
162
|
+
└─────────────┘ (auto-retries once on a 429 throttle)
|
|
163
|
+
│
|
|
164
|
+
▼ 200 + `X-PAYMENT-RESPONSE` settlement header
|
|
165
|
+
receipt + result
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Full walkthrough in [docs/architecture.md](docs/architecture.md).
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## Client API
|
|
173
|
+
|
|
174
|
+
| Export | Signature | Purpose |
|
|
175
|
+
| --- | --- | --- |
|
|
176
|
+
| `pay` | `pay(opts) => Promise<PayResult>` | Open the modal and run a payment. |
|
|
177
|
+
| `configure` | `configure(opts) => Config` | Set checkout origin, branding, builder-code, CDN URLs. |
|
|
178
|
+
| `init` | `init() => void` | Bind `[data-x402-endpoint]` elements (auto-called). |
|
|
179
|
+
| `version` | `string` | Library version. |
|
|
180
|
+
|
|
181
|
+
Also exposed as `window.X402.{ pay, init, configure, version }` for inline
|
|
182
|
+
scripts.
|
|
183
|
+
|
|
184
|
+
**`pay(opts)`** — `opts`:
|
|
185
|
+
|
|
186
|
+
| Field | Type | Notes |
|
|
187
|
+
| --- | --- | --- |
|
|
188
|
+
| `endpoint` | `string` | **Required.** The paid (x402) URL. |
|
|
189
|
+
| `method` | `string` | Default `GET`, or `POST` when `body` is set. |
|
|
190
|
+
| `body` | `object \| string` | Objects are JSON-stringified. |
|
|
191
|
+
| `headers` | `object` | Extra headers merged into the paid call. |
|
|
192
|
+
| `merchant` | `string` | Shown in the modal header. |
|
|
193
|
+
| `action` | `string` | Shown in the modal header. |
|
|
194
|
+
| `autoConnect` | `boolean` | Skip the picker when exactly one wallet is detected. |
|
|
195
|
+
| `caps` | `SpendingCaps` | `{ maxPerCall, maxPerHour, maxPerDay }` in atomic micro-USD. |
|
|
196
|
+
|
|
197
|
+
Resolves to `PayResult`: `{ ok: true, result, payment?: { network, transaction,
|
|
198
|
+
payer }, siwx?: { address, network }, response: { status, headers } }`. Rejects
|
|
199
|
+
with an `Error` whose `.code === 'cancelled'` when the user closes the modal.
|
|
200
|
+
|
|
201
|
+
**DOM events** (bubbling `CustomEvent`s on the clicked element):
|
|
202
|
+
`x402:result` (`detail` = `PayResult`), `x402:error` (`detail` = `{ error }`),
|
|
203
|
+
`x402:siwx-signed` (`detail` = `{ address, network }`).
|
|
204
|
+
|
|
205
|
+
Full reference: [docs/api-reference.md](docs/api-reference.md).
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Configuration
|
|
210
|
+
|
|
211
|
+
Defaults work out of the box. Override host-specific bits with `configure()`:
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
import { configure } from '@three-ws/x402-payment-modal';
|
|
215
|
+
|
|
216
|
+
configure({
|
|
217
|
+
checkoutOrigin: 'https://pay.acme.com', // where your Solana checkout endpoint lives
|
|
218
|
+
brand: { name: 'Acme', url: 'https://acme.com' },
|
|
219
|
+
footerNote: 'Acme Pay',
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
…or with `data-*` attributes on the script tag (no inline module needed):
|
|
224
|
+
|
|
225
|
+
```html
|
|
226
|
+
<script type="module"
|
|
227
|
+
src="https://unpkg.com/@three-ws/x402-payment-modal"
|
|
228
|
+
data-x402-checkout-origin="https://pay.acme.com"
|
|
229
|
+
data-x402-brand-name="Acme"
|
|
230
|
+
data-x402-brand-url="https://acme.com"></script>
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
| Option | `data-*` attribute | Default |
|
|
234
|
+
| --- | --- | --- |
|
|
235
|
+
| `checkoutOrigin` | `data-x402-checkout-origin` | the script's own origin |
|
|
236
|
+
| `checkoutPath` | `data-x402-checkout-path` | `/api/x402-checkout` |
|
|
237
|
+
| `footerNote` | `data-x402-footer-note` | `x402 · onchain settled` |
|
|
238
|
+
| `brand.name` / `brand.url` | `data-x402-brand-name` / `-brand-url` | `three.ws` |
|
|
239
|
+
| `builderCode.wallet` / `.service` | `data-x402-builder-wallet` / `-builder-service` | three.ws codes |
|
|
240
|
+
| `esm.solanaWeb3` / `esm.nobleHashesSha3` | — | pinned esm.sh URLs |
|
|
241
|
+
|
|
242
|
+
Repoint `esm.*` at a self-hosted mirror if a strict Content-Security-Policy
|
|
243
|
+
blocks `esm.sh`. (The EVM/Base path needs no third-party code at all.)
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Server (Solana only)
|
|
248
|
+
|
|
249
|
+
Phantom signs serialized transactions but doesn't build instructions, so the
|
|
250
|
+
Solana path needs a tiny endpoint to build the SPL transfer and wrap the signed
|
|
251
|
+
tx into the `X-PAYMENT` envelope. The package ships it.
|
|
252
|
+
|
|
253
|
+
**Express:**
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
import express from 'express';
|
|
257
|
+
import { x402CheckoutRouter } from '@three-ws/x402-payment-modal/server/express';
|
|
258
|
+
|
|
259
|
+
const app = express();
|
|
260
|
+
app.use(express.json());
|
|
261
|
+
app.use('/api/x402-checkout', x402CheckoutRouter({ rpcUrl: process.env.SOLANA_RPC_URL }));
|
|
262
|
+
app.listen(3000);
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Vercel / Next.js** — `api/x402-checkout.js`:
|
|
266
|
+
|
|
267
|
+
```js
|
|
268
|
+
export { default } from '@three-ws/x402-payment-modal/server/vercel';
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
Lower-level helpers (`prepareSolanaCheckout`, `encodeX402Payment`,
|
|
272
|
+
`handleCheckout`, `CheckoutError`) are exported from
|
|
273
|
+
`@three-ws/x402-payment-modal/server`. Full guide:
|
|
274
|
+
[docs/server-setup.md](docs/server-setup.md).
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## Accepting multiple Solana tokens (USDC + THREE)
|
|
279
|
+
|
|
280
|
+
An x402 challenge can list more than one accepted payment. List a USDC accept
|
|
281
|
+
**and** a [$THREE](https://three.ws/three-token) accept on Solana, and the modal
|
|
282
|
+
renders a token picker: the buyer chooses which to pay in, the headline price and
|
|
283
|
+
the transaction follow the choice. No client wiring — the checkout endpoint
|
|
284
|
+
already builds an SPL transfer for whatever mint the chosen accept names.
|
|
285
|
+
|
|
286
|
+
Build the accepts with the `solanaAccept` helper (no hardcoded mints):
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
import { solanaAccept } from '@three-ws/x402-payment-modal/server';
|
|
290
|
+
|
|
291
|
+
// feePayer is your facilitator's sponsor account (pays the SOL network fee).
|
|
292
|
+
const accepts = [
|
|
293
|
+
solanaAccept({ token: 'usdc', uiAmount: 0.25, payTo, feePayer }), // $0.25 in USDC
|
|
294
|
+
solanaAccept({ token: 'three', uiAmount: 1000, payTo, feePayer }), // or 1,000 THREE
|
|
295
|
+
];
|
|
296
|
+
|
|
297
|
+
// Return them in your 402 body / PAYMENT-REQUIRED envelope:
|
|
298
|
+
// { x402Version: 2, accepts }
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
`solanaAccept` takes `token: 'usdc' | 'three'` (or an explicit `mint` for any
|
|
302
|
+
SPL token), the price as `uiAmount` (human units) or `amount` (atomic string),
|
|
303
|
+
and emits a spec-shaped `accept`. The constants `THREE_MINT`, `USDC_MINT_SOLANA`,
|
|
304
|
+
and `WELL_KNOWN_SOLANA_TOKENS` are exported too. On the client, the same mints
|
|
305
|
+
are exposed at `window.X402.tokens` for inline merchants.
|
|
306
|
+
|
|
307
|
+
> **THREE is a utility token, not a stablecoin.** Its price floats, so the modal
|
|
308
|
+
> can't dollar-denominate it for browser-side spending caps — enforce caps for
|
|
309
|
+
> THREE server-side. USDC caps work in the browser as usual.
|
|
310
|
+
|
|
311
|
+
---
|
|
312
|
+
|
|
313
|
+
## Documentation
|
|
314
|
+
|
|
315
|
+
- [Architecture](docs/architecture.md) — the full payment lifecycle.
|
|
316
|
+
- [Client API reference](docs/api-reference.md)
|
|
317
|
+
- [Server setup](docs/server-setup.md)
|
|
318
|
+
- [Theming](docs/theming.md)
|
|
319
|
+
- [SIWX re-entry](docs/siwx.md)
|
|
320
|
+
- [Spending caps](docs/spending-caps.md)
|
|
321
|
+
- [Tutorial](TUTORIAL.md) — build a paid endpoint end-to-end.
|
|
322
|
+
|
|
323
|
+
---
|
|
324
|
+
|
|
325
|
+
## Browser support
|
|
326
|
+
|
|
327
|
+
Modern evergreen browsers (ES2020, `BigInt`, dynamic `import()`, `fetch`,
|
|
328
|
+
`crypto.getRandomValues`). The Solana wallet path requires
|
|
329
|
+
[Phantom](https://phantom.app); the EVM path requires an injected wallet
|
|
330
|
+
(`window.ethereum`).
|
|
331
|
+
|
|
332
|
+
## Security
|
|
333
|
+
|
|
334
|
+
- Payments are signed in the user's wallet — this package never sees a private
|
|
335
|
+
key. The Solana checkout server only builds an unsigned transaction and base64-
|
|
336
|
+
wraps the user-signed one; it cannot move funds.
|
|
337
|
+
- Spending caps are a **client-side guardrail**, not a security boundary — a user
|
|
338
|
+
can clear `localStorage`. Enforce real limits server-side.
|
|
339
|
+
- Pin the script to a version or self-host it for production.
|
|
340
|
+
|
|
341
|
+
## License
|
|
342
|
+
|
|
343
|
+
[Apache-2.0](LICENSE) © three.ws
|
|
344
|
+
|
|
345
|
+
> Maintained inside the [three.ws](https://three.ws) monorepo and published as a
|
|
346
|
+
> standalone package. Issues and PRs:
|
|
347
|
+
> <https://github.com/nirholas/three.ws/issues>.
|