@phala/pay 0.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.
Files changed (69) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE +202 -0
  3. package/README.md +142 -0
  4. package/dist/chains.d.ts +7 -0
  5. package/dist/chains.d.ts.map +1 -0
  6. package/dist/chains.js +16 -0
  7. package/dist/chains.js.map +1 -0
  8. package/dist/checkout.d.ts +63 -0
  9. package/dist/checkout.d.ts.map +1 -0
  10. package/dist/checkout.js +146 -0
  11. package/dist/checkout.js.map +1 -0
  12. package/dist/client.d.ts +26 -0
  13. package/dist/client.d.ts.map +1 -0
  14. package/dist/client.js +35 -0
  15. package/dist/client.js.map +1 -0
  16. package/dist/format.d.ts +13 -0
  17. package/dist/format.d.ts.map +1 -0
  18. package/dist/format.js +37 -0
  19. package/dist/format.js.map +1 -0
  20. package/dist/index.d.ts +8 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +8 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/payment.d.ts +19 -0
  25. package/dist/payment.d.ts.map +1 -0
  26. package/dist/payment.js +34 -0
  27. package/dist/payment.js.map +1 -0
  28. package/dist/quote.d.ts +36 -0
  29. package/dist/quote.d.ts.map +1 -0
  30. package/dist/quote.js +59 -0
  31. package/dist/quote.js.map +1 -0
  32. package/dist/react/Checkout.d.ts +21 -0
  33. package/dist/react/Checkout.d.ts.map +1 -0
  34. package/dist/react/Checkout.js +168 -0
  35. package/dist/react/Checkout.js.map +1 -0
  36. package/dist/react/QrCode.d.ts +8 -0
  37. package/dist/react/QrCode.d.ts.map +1 -0
  38. package/dist/react/QrCode.js +20 -0
  39. package/dist/react/QrCode.js.map +1 -0
  40. package/dist/react/appearance.d.ts +24 -0
  41. package/dist/react/appearance.d.ts.map +1 -0
  42. package/dist/react/appearance.js +95 -0
  43. package/dist/react/appearance.js.map +1 -0
  44. package/dist/react/index.d.ts +4 -0
  45. package/dist/react/index.d.ts.map +1 -0
  46. package/dist/react/index.js +4 -0
  47. package/dist/react/index.js.map +1 -0
  48. package/dist/react/useCheckout.d.ts +13 -0
  49. package/dist/react/useCheckout.d.ts.map +1 -0
  50. package/dist/react/useCheckout.js +29 -0
  51. package/dist/react/useCheckout.js.map +1 -0
  52. package/dist/wallet.d.ts +42 -0
  53. package/dist/wallet.d.ts.map +1 -0
  54. package/dist/wallet.js +135 -0
  55. package/dist/wallet.js.map +1 -0
  56. package/package.json +93 -0
  57. package/src/chains.ts +20 -0
  58. package/src/checkout.ts +225 -0
  59. package/src/client.ts +54 -0
  60. package/src/format.ts +41 -0
  61. package/src/index.ts +27 -0
  62. package/src/payment.ts +52 -0
  63. package/src/quote.ts +96 -0
  64. package/src/react/Checkout.tsx +409 -0
  65. package/src/react/QrCode.tsx +39 -0
  66. package/src/react/appearance.ts +114 -0
  67. package/src/react/index.ts +5 -0
  68. package/src/react/useCheckout.ts +48 -0
  69. package/src/wallet.ts +175 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,25 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@phala/pay` are recorded here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); versions follow
5
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-09-27
10
+
11
+ ### Added
12
+
13
+ - `PhalaPay({ apiBase })`: `retrieveQuote(clientSecret)` reads a quote's public view
14
+ (`GET /v1/quotes/{id}?client_secret=…`) and `checkout(clientSecret)` follows it, exposing the
15
+ payer-facing status (also `createCheckout` and `retrieveQuote`).
16
+ - `watchWallets` (EIP-6963, `window.ethereum` fallback) and `payWithWallet`: pay a quote's EIP-681
17
+ ERC-20 transfer from a browser wallet, switching or adding the chain.
18
+ - `@phala/pay/react`: `<Checkout>` with wallet ("Pay with crypto", `buttonText`), QR code, and
19
+ manual payment, live status, and Stripe-style `appearance`; `useCheckout`.
20
+ - `formatTokenAmount(quote, locale?)` shows the exact token amount grouped for the locale and
21
+ without trailing zeros (`1,273.9185`); `tokenAmount(quote)` is the plain decimal a wallet
22
+ accepts, which `<Checkout>`'s "Exact amount" copies.
23
+
24
+ [unreleased]: https://github.com/Phala-Network/phala-pay/compare/sdk-js-v0.1.0...HEAD
25
+ [0.1.0]: https://github.com/Phala-Network/phala-pay/releases/tag/sdk-js-v0.1.0
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # @phala/pay
2
+
3
+ Browser checkout for Phala Pay: a framework-agnostic client for a quote's public
4
+ view, and a React component that lets the payer pay from a browser wallet, by QR code, or manually,
5
+ with live status. It is the browser half of the flow; your backend creates the quote with the
6
+ Python SDK (`phala-pay`) and fulfils from the signed `deposit.credited` webhook.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ npm install @phala/pay viem
12
+ ```
13
+
14
+ Peer dependencies: `viem` 2, and `react` 18 or 19 for `@phala/pay/react`. Nothing else:
15
+ wallets are found with EIP-6963 (with a `window.ethereum` fallback), not wagmi or WalletConnect.
16
+
17
+ Until the first npm release, install from the repository with pnpm. The package builds itself
18
+ on install, which pnpm allows only for listed git dependencies: the first `pnpm add` stops and
19
+ prints an `allowBuilds` entry naming the exact commit; add it to `pnpm-workspace.yaml` and run the
20
+ same command again.
21
+
22
+ ```sh
23
+ pnpm add "github:Phala-Network/phala-pay#main&path:/sdk/js"
24
+ ```
25
+
26
+ ```yaml
27
+ # pnpm-workspace.yaml: the entry pnpm printed
28
+ allowBuilds:
29
+ "@phala/pay@https://codeload.github.com/Phala-Network/phala-pay/tar.gz/<commit>#path:/sdk/js": true
30
+ ```
31
+
32
+ ## Quickstart
33
+
34
+ 1. Your backend creates a quote (`POST /v1/quotes`, signed with the product key) and returns only its
35
+ `client_secret` to the signed-in user's browser.
36
+ 2. Render the checkout with it:
37
+
38
+ ```tsx
39
+ "use client";
40
+ import { Checkout } from "@phala/pay/react";
41
+
42
+ export function TopUp(props: { clientSecret: string; onPaid: () => void; onRetry: () => void }) {
43
+ return (
44
+ <Checkout
45
+ clientSecret={props.clientSecret}
46
+ apiBase="https://pay.example.com"
47
+ onSuccess={props.onPaid}
48
+ onExpire={props.onRetry}
49
+ buttonText="Pay with crypto"
50
+ />
51
+ );
52
+ }
53
+ ```
54
+
55
+ 3. Credit the account when your webhook endpoint receives `deposit.credited`. `onSuccess` is for the
56
+ UI only: the browser is not a trusted source of payment.
57
+
58
+ The component reads `GET /v1/quotes/{id}?client_secret=…` every three seconds. That endpoint is
59
+ public (`Access-Control-Allow-Origin: *`) and shows only what the payer needs: the amount, the
60
+ token, the chain, the deposit address, the EIP-681 payment request, the expiry, and the payment
61
+ status. Keep the client secret out of logs and URLs you share; anyone holding it can see that view.
62
+
63
+ ### Statuses
64
+
65
+ | `status` | Shown |
66
+ |---|---|
67
+ | `loading` | Loading payment details |
68
+ | `waiting` | Payment options, the exact amount, and the time left |
69
+ | `seen` | Payment in a block, with its confirmations (a reorg can still remove it) |
70
+ | `confirming` | Payment final, being valued and screened |
71
+ | `credited` | Credited; `onSuccess` is called once |
72
+ | `rejected` | Final but will not be credited; the payer contacts support |
73
+ | `expired`, `canceled` | The address is hidden; `onExpire` is called once |
74
+ | `error` | The client secret is not valid |
75
+
76
+ The payment options disappear once a payment is seen, and at `expires_at`. A payment of a different
77
+ amount, or after expiry, is still credited, at the market price instead of the quote's.
78
+
79
+ ## Appearance
80
+
81
+ ```tsx
82
+ <Checkout
83
+ clientSecret={clientSecret}
84
+ apiBase={apiBase}
85
+ appearance={{
86
+ theme: "dark",
87
+ variables: { colorPrimary: "#cdfa50", borderRadius: "12px", fontFamily: "Inter, sans-serif" },
88
+ }}
89
+ />
90
+ ```
91
+
92
+ Variables: `colorPrimary`, `colorBackground`, `colorText`, `colorTextSecondary`, `colorBorder`,
93
+ `colorDanger`, `colorSuccess`, `fontFamily`, `borderRadius`. Each is also a CSS custom property on
94
+ `.pp-root` (`--pp-color-primary`, …), so a stylesheet can set them too.
95
+
96
+ ## Without React
97
+
98
+ ```ts
99
+ import { PhalaPay, payWithWallet, watchWallets, type Wallet } from "@phala/pay";
100
+
101
+ const pay = new PhalaPay({ apiBase: "https://pay.example.com" });
102
+ const quote = await pay.retrieveQuote(clientSecret); // one read of the public view
103
+ const checkout = pay.checkout(clientSecret); // or follow it until it settles
104
+ const unsubscribe = checkout.subscribe(({ status, quote, error }) => render(status, quote, error));
105
+
106
+ const stop = watchWallets((wallets) => showWalletButtons(wallets));
107
+
108
+ async function onWalletClick(wallet: Wallet) {
109
+ const { quote } = checkout.getState();
110
+ if (quote !== null) {
111
+ // Resolves with the transaction hash once the wallet broadcast the transfer.
112
+ showTransaction(await payWithWallet(wallet.provider, quote));
113
+ await checkout.refresh();
114
+ }
115
+ }
116
+
117
+ // When leaving the page:
118
+ stop();
119
+ unsubscribe();
120
+ checkout.destroy();
121
+ ```
122
+
123
+ `payWithWallet` connects, switches the wallet to the quote's chain (adding Ethereum, Sepolia,
124
+ Base, or Base Sepolia when the wallet lacks it), and sends the ERC-20 `transfer` stated by the
125
+ quote's `payment_uri`, after checking that it pays exactly `amount_atomic` to `address`.
126
+
127
+ ## Development
128
+
129
+ ```sh
130
+ pnpm install
131
+ pnpm run check # typecheck, lint, unit tests, build
132
+ pnpm run e2e # Playwright against Anvil; needs Foundry (anvil, forge) on PATH
133
+ pnpm run e2e:docker # the same, with Chromium from Playwright's image (as CI runs it)
134
+ ```
135
+
136
+ The end-to-end tests start Anvil as Sepolia, deploy a test token, and pay a quote from a mocked
137
+ EIP-6963 wallet backed by the node, then decode the QR code and exercise the manual details.
138
+
139
+ ## Releases
140
+
141
+ A tag `sdk-js-v<version>` matching `package.json` publishes to npm from the `release` workflow
142
+ with trusted publishing and provenance. See [CHANGELOG.md](CHANGELOG.md).
@@ -0,0 +1,7 @@
1
+ import type { Chain } from "viem";
2
+ export declare function knownChain(chainId: number): Chain | undefined;
3
+ /** A human name for the network, for example `Sepolia`. */
4
+ export declare function networkName(chainId: number): string;
5
+ /** The block explorer link for a transaction, when the chain is known. */
6
+ export declare function transactionUrl(chainId: number, hash: string): string | undefined;
7
+ //# sourceMappingURL=chains.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chains.d.ts","sourceRoot":"","sources":["../src/chains.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAMlC,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,CAE7D;AAED,2DAA2D;AAC3D,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,0EAA0E;AAC1E,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAGhF"}
package/dist/chains.js ADDED
@@ -0,0 +1,16 @@
1
+ import { base, baseSepolia, mainnet, sepolia } from "viem/chains";
2
+ /** Chains a wallet can be asked to add; any other chain must already be in the wallet. */
3
+ const CHAINS = [mainnet, sepolia, base, baseSepolia];
4
+ export function knownChain(chainId) {
5
+ return CHAINS.find((chain) => chain.id === chainId);
6
+ }
7
+ /** A human name for the network, for example `Sepolia`. */
8
+ export function networkName(chainId) {
9
+ return knownChain(chainId)?.name ?? `Chain ${chainId}`;
10
+ }
11
+ /** The block explorer link for a transaction, when the chain is known. */
12
+ export function transactionUrl(chainId, hash) {
13
+ const explorer = knownChain(chainId)?.blockExplorers?.default.url;
14
+ return explorer === undefined ? undefined : `${explorer}/tx/${hash}`;
15
+ }
16
+ //# sourceMappingURL=chains.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chains.js","sourceRoot":"","sources":["../src/chains.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAElE,0FAA0F;AAC1F,MAAM,MAAM,GAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;AAEvE,MAAM,UAAU,UAAU,CAAC,OAAe;IACxC,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,OAAO,CAAC,CAAC;AACtD,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,WAAW,CAAC,OAAe;IACzC,OAAO,UAAU,CAAC,OAAO,CAAC,EAAE,IAAI,IAAI,SAAS,OAAO,EAAE,CAAC;AACzD,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,cAAc,CAAC,OAAe,EAAE,IAAY;IAC1D,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,EAAE,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC;IAClE,OAAO,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,QAAQ,OAAO,IAAI,EAAE,CAAC;AACvE,CAAC"}
@@ -0,0 +1,63 @@
1
+ import { type ClientQuote } from "./quote.js";
2
+ /**
3
+ * What the payer sees:
4
+ * - `loading` until the quote is first read;
5
+ * - `waiting` for a payment, until `expires_at`;
6
+ * - `seen` once a transfer is in a block, `confirming` once it is final and being valued;
7
+ * - `credited`, or `rejected` for a final payment that will not be credited;
8
+ * - `expired` or `canceled` without a payment;
9
+ * - `error` when the client secret is not valid.
10
+ */
11
+ export type CheckoutStatus = "loading" | "waiting" | "seen" | "confirming" | "credited" | "rejected" | "expired" | "canceled" | "error";
12
+ export type CheckoutErrorCode = "invalid_client_secret" | "rate_limited" | "network_error" | "invalid_response" | "api_error";
13
+ export declare class CheckoutError extends Error {
14
+ readonly code: CheckoutErrorCode;
15
+ readonly name = "CheckoutError";
16
+ constructor(code: CheckoutErrorCode, message: string, options?: ErrorOptions);
17
+ }
18
+ export interface CheckoutState {
19
+ status: CheckoutStatus;
20
+ quote: ClientQuote | null;
21
+ /** The last failed refresh; polling continues after every error but `invalid_client_secret`. */
22
+ error: CheckoutError | null;
23
+ }
24
+ export interface CheckoutOptions {
25
+ /** The quote's `client_secret`, from your backend's `POST /v1/quotes`. */
26
+ clientSecret: string;
27
+ /** The service origin, for example `https://topup.example.com`. */
28
+ apiBase: string;
29
+ /** Milliseconds between status reads; default 3000. */
30
+ pollInterval?: number;
31
+ fetch?: typeof globalThis.fetch;
32
+ /** Current time in milliseconds; for tests. */
33
+ now?: () => number;
34
+ }
35
+ export interface CheckoutSession {
36
+ getState(): CheckoutState;
37
+ /** Calls `listener` on every state change; returns the unsubscribe function. */
38
+ subscribe(listener: (state: CheckoutState) => void): () => void;
39
+ /** Reads the quote now instead of at the next poll. */
40
+ refresh(): Promise<void>;
41
+ /** Stops polling. */
42
+ destroy(): void;
43
+ }
44
+ export interface RetrieveQuoteOptions {
45
+ clientSecret: string;
46
+ apiBase: string;
47
+ fetch?: typeof globalThis.fetch;
48
+ }
49
+ /**
50
+ * Reads a quote's public view once, as Stripe.js's `retrievePaymentIntent(clientSecret)` does.
51
+ * Rejects with a `CheckoutError`: `invalid_client_secret` for an unknown quote or secret.
52
+ */
53
+ export declare function retrieveQuote(options: RetrieveQuoteOptions): Promise<ClientQuote>;
54
+ /** The payer-facing status of a quote at `nowSeconds`. */
55
+ export declare function checkoutStatus(quote: ClientQuote, nowSeconds: number): CheckoutStatus;
56
+ /**
57
+ * Follows a quote from the browser by polling its public view until it is credited, rejected,
58
+ * canceled, or expired by the service. The status turns `expired` at `expires_at` even while the
59
+ * service, which expires quotes by chain time, still reports the quote open; polling continues
60
+ * until then, so a payment sent just before expiry still shows.
61
+ */
62
+ export declare function createCheckout(options: CheckoutOptions): CheckoutSession;
63
+ //# sourceMappingURL=checkout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkout.d.ts","sourceRoot":"","sources":["../src/checkout.ts"],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,MAAM,WAAW,oBAAoB;IACnC,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC;AAED;;;GAGG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,WAAW,CAAC,CAuBvF;AAED,0DAA0D;AAC1D,wBAAgB,cAAc,CAAC,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,GAAG,cAAc,CAWrF;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,eAAe,GAAG,eAAe,CAgGxE"}
@@ -0,0 +1,146 @@
1
+ import { parseClientQuote, quoteIdFromClientSecret } from "./quote.js";
2
+ export class CheckoutError extends Error {
3
+ code;
4
+ name = "CheckoutError";
5
+ constructor(code, message, options) {
6
+ super(message, options);
7
+ this.code = code;
8
+ }
9
+ }
10
+ const DEFAULT_POLL_INTERVAL = 3000;
11
+ const MAX_BACKOFF = 30_000;
12
+ /**
13
+ * Reads a quote's public view once, as Stripe.js's `retrievePaymentIntent(clientSecret)` does.
14
+ * Rejects with a `CheckoutError`: `invalid_client_secret` for an unknown quote or secret.
15
+ */
16
+ export async function retrieveQuote(options) {
17
+ const quoteId = quoteIdFromClientSecret(options.clientSecret);
18
+ const base = options.apiBase.replace(/\/+$/, "");
19
+ const url = `${base}/v1/quotes/${quoteId}?client_secret=${encodeURIComponent(options.clientSecret)}`;
20
+ const fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
21
+ // A simple GET with no custom headers, so the browser sends no CORS preflight.
22
+ const response = await fetchImpl(url, { cache: "no-store", credentials: "omit" });
23
+ if (response.status === 404) {
24
+ throw new CheckoutError("invalid_client_secret", "the quote or its client secret is unknown");
25
+ }
26
+ if (response.status === 429) {
27
+ throw new CheckoutError("rate_limited", "too many status requests");
28
+ }
29
+ if (!response.ok) {
30
+ throw new CheckoutError("api_error", `the payment service answered ${response.status}`);
31
+ }
32
+ try {
33
+ return parseClientQuote(await response.json());
34
+ }
35
+ catch (cause) {
36
+ throw new CheckoutError("invalid_response", "unexpected response from the payment service", {
37
+ cause,
38
+ });
39
+ }
40
+ }
41
+ /** The payer-facing status of a quote at `nowSeconds`. */
42
+ export function checkoutStatus(quote, nowSeconds) {
43
+ if (quote.payment_status !== "none") {
44
+ return quote.payment_status;
45
+ }
46
+ if (quote.status === "canceled") {
47
+ return "canceled";
48
+ }
49
+ if (quote.status === "expired" || nowSeconds >= quote.expires_at) {
50
+ return "expired";
51
+ }
52
+ return quote.status === "open" ? "waiting" : "confirming";
53
+ }
54
+ /**
55
+ * Follows a quote from the browser by polling its public view until it is credited, rejected,
56
+ * canceled, or expired by the service. The status turns `expired` at `expires_at` even while the
57
+ * service, which expires quotes by chain time, still reports the quote open; polling continues
58
+ * until then, so a payment sent just before expiry still shows.
59
+ */
60
+ export function createCheckout(options) {
61
+ quoteIdFromClientSecret(options.clientSecret);
62
+ const now = options.now ?? Date.now;
63
+ const interval = options.pollInterval ?? DEFAULT_POLL_INTERVAL;
64
+ const listeners = new Set();
65
+ let state = { status: "loading", quote: null, error: null };
66
+ let timer;
67
+ let failures = 0;
68
+ let destroyed = false;
69
+ let inFlight;
70
+ function setState(next) {
71
+ if (next.status === state.status &&
72
+ next.error === state.error &&
73
+ JSON.stringify(next.quote) === JSON.stringify(state.quote)) {
74
+ return;
75
+ }
76
+ state = next;
77
+ for (const listener of listeners) {
78
+ listener(state);
79
+ }
80
+ }
81
+ function finished() {
82
+ const { status, quote } = state;
83
+ return (destroyed ||
84
+ status === "error" ||
85
+ status === "credited" ||
86
+ status === "rejected" ||
87
+ status === "canceled" ||
88
+ (status === "expired" && quote?.status === "expired"));
89
+ }
90
+ async function load() {
91
+ try {
92
+ const quote = await retrieveQuote(options);
93
+ failures = 0;
94
+ setState({ status: checkoutStatus(quote, now() / 1000), quote, error: null });
95
+ }
96
+ catch (cause) {
97
+ const error = cause instanceof CheckoutError
98
+ ? cause
99
+ : new CheckoutError("network_error", "could not reach the payment service", { cause });
100
+ failures += 1;
101
+ const quote = state.quote;
102
+ if (error.code === "invalid_client_secret") {
103
+ setState({ status: "error", quote, error });
104
+ }
105
+ else {
106
+ setState({
107
+ status: quote === null ? state.status : checkoutStatus(quote, now() / 1000),
108
+ quote,
109
+ error,
110
+ });
111
+ }
112
+ }
113
+ }
114
+ function refresh() {
115
+ inFlight ??= load().finally(() => {
116
+ inFlight = undefined;
117
+ });
118
+ return inFlight;
119
+ }
120
+ function schedule() {
121
+ if (finished()) {
122
+ return;
123
+ }
124
+ const delay = failures === 0 ? interval : Math.min(interval * 2 ** failures, MAX_BACKOFF);
125
+ timer = setTimeout(() => {
126
+ void refresh().then(schedule);
127
+ }, delay);
128
+ }
129
+ void refresh().then(schedule);
130
+ return {
131
+ getState: () => state,
132
+ subscribe(listener) {
133
+ listeners.add(listener);
134
+ return () => {
135
+ listeners.delete(listener);
136
+ };
137
+ },
138
+ refresh,
139
+ destroy() {
140
+ destroyed = true;
141
+ clearTimeout(timer);
142
+ listeners.clear();
143
+ },
144
+ };
145
+ }
146
+ //# sourceMappingURL=checkout.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkout.js","sourceRoot":"","sources":["../src/checkout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,uBAAuB,EAAoB,MAAM,YAAY,CAAC;AA6BzF,MAAM,OAAO,aAAc,SAAQ,KAAK;IAI3B;IAHO,IAAI,GAAG,eAAe,CAAC;IAEzC,YACW,IAAuB,EAChC,OAAe,EACf,OAAsB;QAEtB,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAJf,SAAI,GAAJ,IAAI,CAAmB;IAKlC,CAAC;CACF;AA+BD,MAAM,qBAAqB,GAAG,IAAI,CAAC;AACnC,MAAM,WAAW,GAAG,MAAM,CAAC;AAQ3B;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAA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} from "./checkout.js";
2
+ import type { ClientQuote } from "./quote.js";
3
+ export interface PhalaPayOptions {
4
+ /** The service origin, for example `https://pay.example.com`. */
5
+ apiBase: string;
6
+ fetch?: typeof globalThis.fetch;
7
+ }
8
+ /**
9
+ * The browser client, in the shape of Stripe.js: it holds no key, only the service origin, and
10
+ * works with the `client_secret` your backend got from `POST /v1/quotes`.
11
+ *
12
+ * const pay = new PhalaPay({ apiBase: "https://pay.example.com" });
13
+ * const quote = await pay.retrieveQuote(clientSecret);
14
+ * const session = pay.checkout(clientSecret);
15
+ * session.subscribe(({ status }) => render(status));
16
+ */
17
+ export declare class PhalaPay {
18
+ #private;
19
+ readonly apiBase: string;
20
+ constructor(options: PhalaPayOptions);
21
+ /** Reads the quote's public view once. */
22
+ retrieveQuote(clientSecret: string): Promise<ClientQuote>;
23
+ /** Follows the quote until it is credited, rejected, canceled, or expired. */
24
+ checkout(clientSecret: string, options?: Pick<CheckoutOptions, "pollInterval">): CheckoutSession;
25
+ }
26
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,eAAe,EACpB,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,WAAW,eAAe;IAC9B,iEAAiE;IACjE,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC;AAED;;;;;;;;GAQG;AACH,qBAAa,QAAQ;;IACnB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAGb,OAAO,EAAE,eAAe;IAKpC,0CAA0C;IAC1C,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAIzD,8EAA8E;IAC9E,QAAQ,CACN,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,IAAI,CAAC,eAAe,EAAE,cAAc,CAAM,GAClD,eAAe;CAYnB"}