@bify/sdk 0.1.0-rc.1
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/LICENSE +202 -0
- package/README.md +123 -0
- package/dist/client.d.ts +54 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +256 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +37 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +17 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +148 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +367 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/webhooks.d.ts +4 -0
- package/dist/webhooks.d.ts.map +1 -0
- package/dist/webhooks.js +71 -0
- package/dist/webhooks.js.map +1 -0
- package/package.json +52 -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 2026 BIFY
|
|
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,123 @@
|
|
|
1
|
+
# BIFY Server SDK
|
|
2
|
+
|
|
3
|
+
`@bify/sdk` is the trusted integration package for partner-owned commerce. A
|
|
4
|
+
partner keeps its own catalogue, prices, inventory, cart, checkout, and
|
|
5
|
+
fulfilment systems. BIFY receives an immutable order snapshot and provides
|
|
6
|
+
settlement, on-chain purchase records, certificates, rewards, and customer
|
|
7
|
+
access infrastructure.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @bify/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { BifyClient } from "@bify/sdk";
|
|
15
|
+
|
|
16
|
+
const bify = new BifyClient({ apiKey: process.env.BIFY_API_KEY });
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The API key is a server-side credential. It must never reach the browser.
|
|
20
|
+
|
|
21
|
+
## Checkout
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
const session = await bify.checkout.createOrderSession({
|
|
25
|
+
externalProductId: order.lineItem.externalProductId,
|
|
26
|
+
productName: order.lineItem.name,
|
|
27
|
+
productCategory: order.lineItem.category,
|
|
28
|
+
totalPriceUsdc: order.totalUsdcBaseUnits,
|
|
29
|
+
quantity: order.quantity,
|
|
30
|
+
externalOrderId: order.id,
|
|
31
|
+
shippingRequired: true,
|
|
32
|
+
idempotencyKey: `checkout:${order.id}`,
|
|
33
|
+
// Customer details are optional here; the BIFY widget can collect them.
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Read the price from your own catalogue on the server, never from the request
|
|
38
|
+
body — otherwise a tampered client can mint a cheap session.
|
|
39
|
+
|
|
40
|
+
Pass only `session.id` and `session.clientToken` to the browser. The widget
|
|
41
|
+
takes it from there.
|
|
42
|
+
|
|
43
|
+
## Settlement rails
|
|
44
|
+
|
|
45
|
+
A session is always **denominated in USDC**. Which rail settles it is the
|
|
46
|
+
buyer's choice, made in the widget from `session.paymentMethods`:
|
|
47
|
+
|
|
48
|
+
- **`usdc`** — the buyer connects a wallet, approves USDC to the payment
|
|
49
|
+
router, and submits the contract-backed `payHostedOrder` transaction.
|
|
50
|
+
- **`card`** — the buyer pays with a card through the payment processor. The
|
|
51
|
+
charge settles off-chain and is recorded on-chain with no value transfer, so
|
|
52
|
+
a buyer with no funded wallet can still complete a purchase and receive a
|
|
53
|
+
certificate.
|
|
54
|
+
|
|
55
|
+
`paymentMethods` contains `card` only when the deployment has a payment
|
|
56
|
+
processor configured; the backend omits it otherwise, so the widget never
|
|
57
|
+
offers a rail that cannot settle.
|
|
58
|
+
|
|
59
|
+
`createOrderSession` therefore accepts no rail selection — passing
|
|
60
|
+
`paymentMethod: "card"` is rejected. There is nothing for a partner server to
|
|
61
|
+
choose: the order total is the same either way.
|
|
62
|
+
|
|
63
|
+
After settlement, on either rail, the widget fetches a signed certificate
|
|
64
|
+
voucher, the buyer submits `mintCertificate` from their own wallet, and the
|
|
65
|
+
widget confirms that mint with the backend.
|
|
66
|
+
|
|
67
|
+
## Webhooks
|
|
68
|
+
|
|
69
|
+
Events are signed with HMAC-SHA256 over `{timestamp}.{raw body}` and delivered
|
|
70
|
+
in the `BIFY-Signature` header as `t=<unix>,v1=<hex>`.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { parseVerifiedWebhook } from "@bify/sdk";
|
|
74
|
+
|
|
75
|
+
const event = parseVerifiedWebhook({
|
|
76
|
+
payload: rawRequestBody, // the untouched bytes, not a re-serialized object
|
|
77
|
+
signature: req.headers["bify-signature"],
|
|
78
|
+
secret: process.env.BIFY_WEBHOOK_SECRET,
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Verify against the **raw** body. Parsing and re-serializing JSON first changes
|
|
83
|
+
the bytes and invalidates the signature.
|
|
84
|
+
|
|
85
|
+
`secret` also accepts an array, so a rotation can be performed with no downtime
|
|
86
|
+
by accepting the previous and current secrets during the overlap window.
|
|
87
|
+
|
|
88
|
+
Event names are typed as `BifyEventName`.
|
|
89
|
+
|
|
90
|
+
## What the SDK covers
|
|
91
|
+
|
|
92
|
+
- hosted order sessions and status retrieval;
|
|
93
|
+
- signed certificate voucher retrieval and on-chain mint confirmation;
|
|
94
|
+
- partner-scoped order, fulfilment, certificate, reward, access, voting, and
|
|
95
|
+
identity APIs;
|
|
96
|
+
- idempotency keys, timeout handling, typed errors, and webhook verification.
|
|
97
|
+
|
|
98
|
+
The SDK does not submit wallet transactions. Use
|
|
99
|
+
`checkout.getCertificateVoucher()` when a partner server needs to coordinate a
|
|
100
|
+
custom browser experience; the browser widget or a voucher widget must submit
|
|
101
|
+
the buyer-signed certificate mint.
|
|
102
|
+
|
|
103
|
+
## Boundaries
|
|
104
|
+
|
|
105
|
+
BIFY does not provide product registration, inventory, price authority, quote
|
|
106
|
+
calculation, marketplace/resale, or managed wallets. Those decisions remain
|
|
107
|
+
with the partner. RWA flows remain in the separate RWA frontend/backend.
|
|
108
|
+
|
|
109
|
+
## Development
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
npm install
|
|
113
|
+
npm run typecheck
|
|
114
|
+
npm test
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The browser widget is published as
|
|
118
|
+
[`@bify/commerce-widget`](https://www.npmjs.com/package/@bify/commerce-widget).
|
|
119
|
+
Canonical documentation is maintained in GitBook.
|
|
120
|
+
|
|
121
|
+
## Licence
|
|
122
|
+
|
|
123
|
+
Apache-2.0. See [LICENSE](./LICENSE).
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { BifyClientOptions, BifyCapabilities, BifyEvent, CampaignStatus, CreateCampaignInput, CertificateListInput, CertificatePage, CertificateStatus, CertificateMintConfirmation, CheckoutCertificateVoucher, CheckoutSession, CheckoutSessionStatus, ClaimStatus, ConfirmRewardClaimInput, CreateOrderCheckoutSessionInput, GetCertificateVoucherInput, ConfirmCertificateMintInput, FulfilmentInput, OrderLookup, OrderStatus, RewardClaimInput, VerifyWebhookInput, AccessPassStatus, AccessGrantStatus, CreateAccessPassInput, CreatePollInput, IdentityAttestationInput, IdentityAttestationResult, PollStatus } from "./types.js";
|
|
2
|
+
export declare class BifyClient {
|
|
3
|
+
private readonly http;
|
|
4
|
+
readonly checkout: {
|
|
5
|
+
createOrderSession: (input: CreateOrderCheckoutSessionInput) => Promise<CheckoutSession>;
|
|
6
|
+
retrieve: (sessionId: string) => Promise<CheckoutSessionStatus>;
|
|
7
|
+
getCertificateVoucher: (input: GetCertificateVoucherInput) => Promise<CheckoutCertificateVoucher>;
|
|
8
|
+
};
|
|
9
|
+
readonly capabilities: {
|
|
10
|
+
/**
|
|
11
|
+
* Deployment-level capability report. Describes the deployment as a whole;
|
|
12
|
+
* the rails available for a specific order come from
|
|
13
|
+
* `CheckoutSession.paymentMethods`, which is resolved per session.
|
|
14
|
+
*/
|
|
15
|
+
retrieve: () => Promise<BifyCapabilities>;
|
|
16
|
+
};
|
|
17
|
+
readonly orders: {
|
|
18
|
+
retrieve: (input: OrderLookup) => Promise<OrderStatus>;
|
|
19
|
+
markFulfilled: (input: FulfilmentInput) => Promise<OrderStatus>;
|
|
20
|
+
};
|
|
21
|
+
readonly certificates: {
|
|
22
|
+
retrieve: (certificateId: string) => Promise<CertificateStatus>;
|
|
23
|
+
verify: (certificateId: string) => Promise<CertificateStatus>;
|
|
24
|
+
list: (input?: CertificateListInput) => Promise<CertificatePage>;
|
|
25
|
+
confirmMint: (input: ConfirmCertificateMintInput) => Promise<CertificateMintConfirmation>;
|
|
26
|
+
};
|
|
27
|
+
readonly rewards: {
|
|
28
|
+
create: (input: CreateCampaignInput) => Promise<CampaignStatus>;
|
|
29
|
+
retrieve: (campaignId: string) => Promise<CampaignStatus>;
|
|
30
|
+
claim: (input: RewardClaimInput) => Promise<ClaimStatus>;
|
|
31
|
+
confirmClaim: (input: ConfirmRewardClaimInput) => Promise<ClaimStatus>;
|
|
32
|
+
retrieveClaim: (claimId: string) => Promise<ClaimStatus>;
|
|
33
|
+
};
|
|
34
|
+
readonly access: {
|
|
35
|
+
create: (input: CreateAccessPassInput) => Promise<AccessPassStatus>;
|
|
36
|
+
retrieve: (accessId: string) => Promise<AccessPassStatus>;
|
|
37
|
+
setActive: (accessId: string, active: boolean) => Promise<AccessPassStatus>;
|
|
38
|
+
grant: (accessId: string, walletAddress: `0x${string}`) => Promise<AccessGrantStatus>;
|
|
39
|
+
revoke: (accessId: string, walletAddress: `0x${string}`) => Promise<AccessGrantStatus>;
|
|
40
|
+
};
|
|
41
|
+
readonly voting: {
|
|
42
|
+
createPoll: (input: CreatePollInput) => Promise<PollStatus>;
|
|
43
|
+
retrievePoll: (pollId: string) => Promise<PollStatus>;
|
|
44
|
+
setPollActive: (pollId: string, active: boolean) => Promise<PollStatus>;
|
|
45
|
+
};
|
|
46
|
+
readonly identity: {
|
|
47
|
+
attest: (input: IdentityAttestationInput) => Promise<IdentityAttestationResult>;
|
|
48
|
+
};
|
|
49
|
+
readonly webhooks: {
|
|
50
|
+
verify: <T = Record<string, unknown>>(input: VerifyWebhookInput) => BifyEvent<T>;
|
|
51
|
+
};
|
|
52
|
+
constructor(options: BifyClientOptions);
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACT,cAAc,EACd,mBAAmB,EACnB,oBAAoB,EACpB,eAAe,EACf,iBAAiB,EACjB,2BAA2B,EAC3B,0BAA0B,EAC1B,eAAe,EACf,qBAAqB,EACrB,WAAW,EACX,uBAAuB,EACvB,+BAA+B,EAC/B,0BAA0B,EAC1B,2BAA2B,EAC3B,eAAe,EACf,WAAW,EACX,WAAW,EACX,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,qBAAqB,EACrB,eAAe,EACf,wBAAwB,EACxB,yBAAyB,EACzB,UAAU,EACX,MAAM,YAAY,CAAC;AA2EpB,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAiB;IAEtC,QAAQ,CAAC,QAAQ;oCACa,+BAA+B;8BAQrC,MAAM;uCACG,0BAA0B;MAQzD;IAEF,QAAQ,CAAC,YAAY;QACnB;;;;WAIG;;MAEH;IAEF,QAAQ,CAAC,MAAM;0BACK,WAAW;+BAQN,eAAe;MAWtC;IAEF,QAAQ,CAAC,YAAY;kCACO,MAAM;gCACR,MAAM;uBAChB,oBAAoB;6BAYb,2BAA2B;MAchD;IAEF,QAAQ,CAAC,OAAO;wBACE,mBAAmB;+BAKZ,MAAM;uBACd,gBAAgB;8BAQT,uBAAuB;iCAYpB,MAAM;MAC/B;IAEF,QAAQ,CAAC,MAAM;wBACG,qBAAqB;6BAKhB,MAAM;8BACL,MAAM,UAAU,OAAO;0BAK3B,MAAM,iBAAiB,KAAK,MAAM,EAAE;2BAKnC,MAAM,iBAAiB,KAAK,MAAM,EAAE;MAKvD;IAEF,QAAQ,CAAC,MAAM;4BACO,eAAe;+BAKZ,MAAM;gCACL,MAAM,UAAU,OAAO;MAK/C;IAEF,QAAQ,CAAC,QAAQ;wBACC,wBAAwB;MAexC;IAEF,QAAQ,CAAC,QAAQ;iBACN,CAAC,mCAAmC,kBAAkB,KAAG,SAAS,CAAC,CAAC,CAAC;MAC9E;gBAEU,OAAO,EAAE,iBAAiB;CAKvC"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { BifyHttpClient } from "./http.js";
|
|
2
|
+
import { parseVerifiedWebhook } from "./webhooks.js";
|
|
3
|
+
import { BifyValidationError } from "./errors.js";
|
|
4
|
+
function validateIdentifier(value, label, maxLength = 255) {
|
|
5
|
+
if (typeof value !== "string" || !value.trim())
|
|
6
|
+
throw new BifyValidationError(`${label} is required.`);
|
|
7
|
+
const normalized = value.trim();
|
|
8
|
+
if (normalized.length > maxLength)
|
|
9
|
+
throw new BifyValidationError(`${label} must be ${maxLength} characters or fewer.`);
|
|
10
|
+
return normalized;
|
|
11
|
+
}
|
|
12
|
+
function validateQuantity(value) {
|
|
13
|
+
if (!Number.isSafeInteger(value) || value < 1)
|
|
14
|
+
throw new BifyValidationError("Quantity must be a positive safe integer.");
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function validateIdempotencyKey(value) {
|
|
18
|
+
const key = validateIdentifier(value, "Idempotency key");
|
|
19
|
+
if (key.length > 255)
|
|
20
|
+
throw new BifyValidationError("Idempotency key must be 255 characters or fewer.");
|
|
21
|
+
return key;
|
|
22
|
+
}
|
|
23
|
+
function validateCustomer(customer) {
|
|
24
|
+
if (!customer || typeof customer !== "object")
|
|
25
|
+
throw new BifyValidationError("Customer details are required.");
|
|
26
|
+
if (typeof customer.name !== "string" || !customer.name.trim() || customer.name.trim().length > 200)
|
|
27
|
+
throw new BifyValidationError("Customer name is required and must be 200 characters or fewer.");
|
|
28
|
+
if (typeof customer.email !== "string" || !customer.email.trim() || customer.email.trim().length > 320 || !/^\S+@\S+\.\S+$/.test(customer.email.trim()))
|
|
29
|
+
throw new BifyValidationError("A valid customer email is required.");
|
|
30
|
+
if (customer.phone !== undefined && (typeof customer.phone !== "string" || customer.phone.trim().length > 32))
|
|
31
|
+
throw new BifyValidationError("Customer phone number is invalid.");
|
|
32
|
+
for (const [label, address] of [["shipping address", customer.shippingAddress], ["billing address", customer.billingAddress]]) {
|
|
33
|
+
if (!address)
|
|
34
|
+
continue;
|
|
35
|
+
if (typeof address !== "object" || typeof address.line1 !== "string" || (address.line2 !== undefined && typeof address.line2 !== "string") || typeof address.city !== "string" || typeof address.region !== "string" || typeof address.postalCode !== "string" || typeof address.country !== "string" || !address.line1.trim() || address.line1.trim().length > 200 || (typeof address.line2 === "string" && address.line2.trim().length > 200) || !address.city.trim() || address.city.trim().length > 100 || !address.region.trim() || address.region.trim().length > 100 || !address.postalCode.trim() || address.postalCode.trim().length > 32 || !/^[A-Za-z]{2}$/.test(address.country.trim()))
|
|
36
|
+
throw new BifyValidationError(`${label} is incomplete.`);
|
|
37
|
+
}
|
|
38
|
+
if (customer.billingSameAsShipping && !customer.shippingAddress)
|
|
39
|
+
throw new BifyValidationError("Shipping address is required when billing uses the same address.");
|
|
40
|
+
return customer;
|
|
41
|
+
}
|
|
42
|
+
function validateWalletAddress(value, label) {
|
|
43
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]{40}$/.test(value)) {
|
|
44
|
+
throw new BifyValidationError(`${label} must be a valid EVM wallet address.`);
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
function externalOrderSessionInput(input) {
|
|
49
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
50
|
+
throw new BifyValidationError("Checkout session input is required.");
|
|
51
|
+
const paymentMethod = input.paymentMethod ?? "usdc";
|
|
52
|
+
// A checkout session is always denominated in USDC. The settlement rail is
|
|
53
|
+
// selected by the buyer in the widget from `session.paymentMethods`, which the
|
|
54
|
+
// backend populates with the rails a deployment can actually settle, so a
|
|
55
|
+
// partner server has no rail to choose at creation time.
|
|
56
|
+
if (paymentMethod !== "usdc") {
|
|
57
|
+
throw new BifyValidationError("Checkout sessions are always created in USDC. The buyer picks the settlement rail in the widget from session.paymentMethods, which includes 'card' when the deployment has a payment processor configured.");
|
|
58
|
+
}
|
|
59
|
+
const checkoutMode = "hosted_transfer";
|
|
60
|
+
const externalProductId = validateIdentifier(input.externalProductId, "External product ID");
|
|
61
|
+
const productName = validateIdentifier(input.productName, "Product name");
|
|
62
|
+
const productCategory = validateIdentifier(input.productCategory, "Product category");
|
|
63
|
+
const unitPriceUsdc = input.unitPriceUsdc === undefined ? undefined : validateIdentifier(input.unitPriceUsdc, "Unit USDC amount");
|
|
64
|
+
if (unitPriceUsdc !== undefined && (!/^[0-9]+$/.test(unitPriceUsdc) || BigInt(unitPriceUsdc) <= 0n))
|
|
65
|
+
throw new BifyValidationError("Unit USDC amount must be a positive integer in base units.");
|
|
66
|
+
const totalPriceUsdc = validateIdentifier(input.totalPriceUsdc, "Total USDC amount");
|
|
67
|
+
if (!/^[0-9]+$/.test(totalPriceUsdc) || BigInt(totalPriceUsdc) <= 0n)
|
|
68
|
+
throw new BifyValidationError("Total USDC amount must be a positive integer in base units.");
|
|
69
|
+
return {
|
|
70
|
+
externalProductId, productName, productCategory, unitPriceUsdc, totalPriceUsdc,
|
|
71
|
+
shippingRequired: input.shippingRequired ?? false,
|
|
72
|
+
quantity: validateQuantity(input.quantity),
|
|
73
|
+
externalOrderId: validateIdentifier(input.externalOrderId, "External order ID"),
|
|
74
|
+
// The session is created denominated in USDC. Which rail the buyer actually uses is chosen in the
|
|
75
|
+
// widget from the rails the deployment can settle (session.paymentMethods), so a card buyer needs no
|
|
76
|
+
// change here — the order total is the same either way.
|
|
77
|
+
paymentMethod: "usdc", checkoutMode, certificate: input.certificate,
|
|
78
|
+
customer: input.customer ? validateCustomer(input.customer) : undefined, idempotencyKey: validateIdempotencyKey(input.idempotencyKey),
|
|
79
|
+
walletAddress: input.walletAddress === undefined ? undefined : validateWalletAddress(input.walletAddress, "Wallet address"),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export class BifyClient {
|
|
83
|
+
http;
|
|
84
|
+
checkout = {
|
|
85
|
+
createOrderSession: (input) => {
|
|
86
|
+
const body = externalOrderSessionInput(input);
|
|
87
|
+
return this.http.request("/v1/checkout/sessions", {
|
|
88
|
+
method: "POST",
|
|
89
|
+
body,
|
|
90
|
+
idempotencyKey: body.idempotencyKey,
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
retrieve: (sessionId) => this.http.request(`/v1/checkout/sessions/${encodeURIComponent(validateIdentifier(sessionId, "Session ID"))}`),
|
|
94
|
+
getCertificateVoucher: (input) => {
|
|
95
|
+
if (!input || typeof input !== "object")
|
|
96
|
+
throw new BifyValidationError("Certificate voucher input is required.");
|
|
97
|
+
const sessionId = validateIdentifier(input.sessionId, "Session ID");
|
|
98
|
+
const clientToken = validateIdentifier(input.clientToken, "Checkout client token");
|
|
99
|
+
return this.http.request(`/v1/public/checkout/sessions/${encodeURIComponent(sessionId)}/certificate/voucher`, {
|
|
100
|
+
method: "POST", body: { clientToken }, auth: false,
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
capabilities = {
|
|
105
|
+
/**
|
|
106
|
+
* Deployment-level capability report. Describes the deployment as a whole;
|
|
107
|
+
* the rails available for a specific order come from
|
|
108
|
+
* `CheckoutSession.paymentMethods`, which is resolved per session.
|
|
109
|
+
*/
|
|
110
|
+
retrieve: () => this.http.request("/v1/capabilities", { auth: false }),
|
|
111
|
+
};
|
|
112
|
+
orders = {
|
|
113
|
+
retrieve: (input) => {
|
|
114
|
+
const value = input.orderId?.trim() || input.externalOrderId?.trim();
|
|
115
|
+
if (!value)
|
|
116
|
+
throw new BifyValidationError("Order ID or external order ID is required.");
|
|
117
|
+
const query = input.orderId?.trim()
|
|
118
|
+
? `orderId=${encodeURIComponent(input.orderId.trim())}`
|
|
119
|
+
: `externalOrderId=${encodeURIComponent(input.externalOrderId.trim())}`;
|
|
120
|
+
return this.http.request(`/v1/orders?${query}`);
|
|
121
|
+
},
|
|
122
|
+
markFulfilled: (input) => this.http.request(`/v1/orders/${encodeURIComponent(validateIdentifier(input.orderId, "Order ID"))}/fulfilment`, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: {
|
|
125
|
+
fulfilledAt: input.fulfilledAt,
|
|
126
|
+
fulfilmentReference: input.fulfilmentReference,
|
|
127
|
+
},
|
|
128
|
+
idempotencyKey: validateIdempotencyKey(input.idempotencyKey),
|
|
129
|
+
}),
|
|
130
|
+
};
|
|
131
|
+
certificates = {
|
|
132
|
+
retrieve: (certificateId) => this.http.request(`/v1/certificates/${encodeURIComponent(validateIdentifier(certificateId, "Certificate ID"))}`),
|
|
133
|
+
verify: (certificateId) => this.http.request(`/v1/public/certificates/${encodeURIComponent(validateIdentifier(certificateId, "Certificate ID"))}`, { auth: false }),
|
|
134
|
+
list: (input = {}) => {
|
|
135
|
+
const query = new URLSearchParams();
|
|
136
|
+
if (input.status)
|
|
137
|
+
query.set("status", input.status);
|
|
138
|
+
if (input.limit !== undefined) {
|
|
139
|
+
if (!Number.isSafeInteger(input.limit) || input.limit < 1 || input.limit > 100) {
|
|
140
|
+
throw new BifyValidationError("Certificate page size must be between 1 and 100.");
|
|
141
|
+
}
|
|
142
|
+
query.set("limit", String(input.limit));
|
|
143
|
+
}
|
|
144
|
+
if (input.cursor)
|
|
145
|
+
query.set("cursor", input.cursor);
|
|
146
|
+
return this.http.request(`/v1/certificates${query.size ? `?${query}` : ""}`);
|
|
147
|
+
},
|
|
148
|
+
confirmMint: (input) => {
|
|
149
|
+
if (!input || typeof input !== "object")
|
|
150
|
+
throw new BifyValidationError("Certificate confirmation input is required.");
|
|
151
|
+
const sessionId = validateIdentifier(input.sessionId, "Session ID");
|
|
152
|
+
const token = validateIdentifier(input.clientToken, "Checkout client token");
|
|
153
|
+
const certificateId = validateIdentifier(input.certificateId, "Certificate ID");
|
|
154
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(certificateId) || !/^0x[0-9a-fA-F]{64}$/.test(input.transactionHash)) {
|
|
155
|
+
throw new BifyValidationError("Certificate ID and transaction hash must be 32-byte hex values.");
|
|
156
|
+
}
|
|
157
|
+
return this.http.request(`/v1/public/checkout/sessions/${encodeURIComponent(sessionId)}/certificate/confirm`, {
|
|
158
|
+
method: "POST",
|
|
159
|
+
body: { clientToken: token, certificateId, transactionHash: input.transactionHash },
|
|
160
|
+
auth: false,
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
rewards = {
|
|
165
|
+
create: (input) => this.http.request("/v1/rewards/campaigns", {
|
|
166
|
+
method: "POST",
|
|
167
|
+
body: { ...input },
|
|
168
|
+
idempotencyKey: input.id ?? `campaign:${input.rewardType}`,
|
|
169
|
+
}),
|
|
170
|
+
retrieve: (campaignId) => this.http.request(`/v1/rewards/campaigns/${encodeURIComponent(validateIdentifier(campaignId, "Campaign ID"))}`),
|
|
171
|
+
claim: (input) => this.http.request(`/v1/rewards/campaigns/${encodeURIComponent(validateIdentifier(input.campaignId, "Campaign ID"))}/claims`, {
|
|
172
|
+
method: "POST",
|
|
173
|
+
body: { purchaseId: input.purchaseId, claimant: input.claimant },
|
|
174
|
+
idempotencyKey: validateIdempotencyKey(input.idempotencyKey),
|
|
175
|
+
}),
|
|
176
|
+
confirmClaim: (input) => {
|
|
177
|
+
if (!input || typeof input !== "object")
|
|
178
|
+
throw new BifyValidationError("Reward confirmation input is required.");
|
|
179
|
+
const claimId = validateIdentifier(input.claimId, "Claim ID");
|
|
180
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(claimId) || !/^0x[0-9a-fA-F]{64}$/.test(input.transactionHash)) {
|
|
181
|
+
throw new BifyValidationError("Claim ID and transaction hash must be 32-byte hex values.");
|
|
182
|
+
}
|
|
183
|
+
return this.http.request(`/v1/public/rewards/claims/${encodeURIComponent(claimId)}/confirm`, {
|
|
184
|
+
method: "POST",
|
|
185
|
+
body: { transactionHash: input.transactionHash },
|
|
186
|
+
auth: false,
|
|
187
|
+
});
|
|
188
|
+
},
|
|
189
|
+
retrieveClaim: (claimId) => this.http.request(`/v1/rewards/claims/${encodeURIComponent(validateIdentifier(claimId, "Claim ID"))}`),
|
|
190
|
+
};
|
|
191
|
+
access = {
|
|
192
|
+
create: (input) => this.http.request("/v1/access-passes", {
|
|
193
|
+
method: "POST",
|
|
194
|
+
body: input,
|
|
195
|
+
idempotencyKey: input.accessId,
|
|
196
|
+
}),
|
|
197
|
+
retrieve: (accessId) => this.http.request(`/v1/access-passes/${encodeURIComponent(validateIdentifier(accessId, "Access pass ID"))}`),
|
|
198
|
+
setActive: (accessId, active) => this.http.request(`/v1/access-passes/${encodeURIComponent(validateIdentifier(accessId, "Access pass ID"))}/status`, {
|
|
199
|
+
method: "POST",
|
|
200
|
+
body: { active },
|
|
201
|
+
idempotencyKey: `access-status:${accessId}:${active ? "active" : "paused"}`,
|
|
202
|
+
}),
|
|
203
|
+
grant: (accessId, walletAddress) => this.http.request(`/v1/access-passes/${encodeURIComponent(validateIdentifier(accessId, "Access pass ID"))}/grants`, {
|
|
204
|
+
method: "POST",
|
|
205
|
+
body: { walletAddress },
|
|
206
|
+
idempotencyKey: `access-grant:${accessId}:${walletAddress.toLowerCase()}`,
|
|
207
|
+
}),
|
|
208
|
+
revoke: (accessId, walletAddress) => this.http.request(`/v1/access-passes/${encodeURIComponent(validateIdentifier(accessId, "Access pass ID"))}/revoke`, {
|
|
209
|
+
method: "POST",
|
|
210
|
+
body: { walletAddress },
|
|
211
|
+
idempotencyKey: `access-revoke:${accessId}:${walletAddress.toLowerCase()}`,
|
|
212
|
+
}),
|
|
213
|
+
};
|
|
214
|
+
voting = {
|
|
215
|
+
createPoll: (input) => this.http.request("/v1/voting/polls", {
|
|
216
|
+
method: "POST",
|
|
217
|
+
body: input,
|
|
218
|
+
idempotencyKey: input.pollId,
|
|
219
|
+
}),
|
|
220
|
+
retrievePoll: (pollId) => this.http.request(`/v1/voting/polls/${encodeURIComponent(validateIdentifier(pollId, "Poll ID"))}`),
|
|
221
|
+
setPollActive: (pollId, active) => this.http.request(`/v1/voting/polls/${encodeURIComponent(validateIdentifier(pollId, "Poll ID"))}/status`, {
|
|
222
|
+
method: "POST",
|
|
223
|
+
body: { active },
|
|
224
|
+
idempotencyKey: `poll-status:${pollId}:${active ? "active" : "paused"}`,
|
|
225
|
+
}),
|
|
226
|
+
};
|
|
227
|
+
identity = {
|
|
228
|
+
attest: (input) => {
|
|
229
|
+
const signature = validateIdentifier(input.signature, "Identity attestation signature");
|
|
230
|
+
if (!/^t=\d+,v1=[0-9a-fA-F]{64}$/.test(signature)) {
|
|
231
|
+
throw new BifyValidationError("Identity attestation signature is invalid.");
|
|
232
|
+
}
|
|
233
|
+
if (!Number.isSafeInteger(input.occurredAt) || input.occurredAt < 1)
|
|
234
|
+
throw new BifyValidationError("Identity event time is invalid.");
|
|
235
|
+
if (!Number.isSafeInteger(input.kycExpiresAt) || input.kycExpiresAt < 0)
|
|
236
|
+
throw new BifyValidationError("Identity expiry is invalid.");
|
|
237
|
+
if (!Number.isInteger(input.kycStatus) || input.kycStatus < 0 || input.kycStatus > 5)
|
|
238
|
+
throw new BifyValidationError("KYC status is invalid.");
|
|
239
|
+
return this.http.request("/v1/identity/attest", {
|
|
240
|
+
method: "POST",
|
|
241
|
+
auth: false,
|
|
242
|
+
body: { ...input, signature: undefined },
|
|
243
|
+
headers: { "BIFY-KYC-Signature": signature },
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
webhooks = {
|
|
248
|
+
verify: (input) => parseVerifiedWebhook(input),
|
|
249
|
+
};
|
|
250
|
+
constructor(options) {
|
|
251
|
+
// @dev: This client is intended for partner backend code. Browser code
|
|
252
|
+
// should use the public widget/session-token flow instead of this client.
|
|
253
|
+
this.http = new BifyHttpClient(options);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
//# sourceMappingURL=client.js.map
|