bitsnap-react 1.0.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/README.md ADDED
@@ -0,0 +1,261 @@
1
+ # Bitsnap Checkout
2
+
3
+ The library is designed to provide a complete e-commerce checkout solution that can be easily integrated into web applications, with focus on:
4
+
5
+ - User experience
6
+ - Payment processing
7
+ - Cart management
8
+ - Product management
9
+ - Webhook support
10
+
11
+ It uses modern React patterns and libraries like:
12
+
13
+ - React Query for data fetching
14
+ - Zod for schema validation
15
+ - Protocol Buffers for data serialization
16
+ - Tailwind CSS for styling
17
+
18
+ ## Capabilities
19
+
20
+ ### Cart Management:
21
+
22
+ - Allows adding products to cart
23
+ - Manages cart state (show/hide)
24
+ - Handles cart items quantity updates
25
+ - Provides cart total calculations
26
+ - Supports removing items from cart
27
+
28
+ ### Checkout Flow:
29
+
30
+ - Handles checkout process
31
+ - Supports different payment gateways
32
+ - Manages shipping/billing addresses
33
+ - Handles coupon codes
34
+ - Supports different delivery methods
35
+
36
+ ### Product Management:
37
+
38
+ - Fetches product details from Bitsnap
39
+ - Handles product variants
40
+ - Manages product inventory/stock
41
+ - Supports product metadata
42
+
43
+ ### Features:
44
+
45
+ - Supports multiple currencies
46
+ - Internationalization support
47
+ - Webhook handling
48
+ - Error handling
49
+ - Loading states
50
+ - Responsive design
51
+ - Protocol buffer integration
52
+
53
+ ### Integration:
54
+
55
+ - Can be integrated into existing websites
56
+ - Configurable project ID
57
+ - Customizable host URL
58
+ - Supports test/production environments
59
+
60
+ # Examples
61
+
62
+ 1. `BitsnapCheckout` Component:
63
+
64
+ ```tsx
65
+ import { BitsnapCheckout } from "bitsnap-checkout";
66
+
67
+ function MyComponent() {
68
+ return (
69
+ <BitsnapCheckout
70
+ projectID="your-project-id"
71
+ onVisibleChange={(isVisible) =>
72
+ console.log("Cart visibility:", isVisible)
73
+ }
74
+ className="custom-class"
75
+ >
76
+ {/* Optional custom cart trigger button content */}
77
+ <span>My Custom Cart Button</span>
78
+ </BitsnapCheckout>
79
+ );
80
+ }
81
+ ```
82
+
83
+ 2. `setProjectID`:
84
+
85
+ ```tsx
86
+ import { setProjectID } from "bitsnap-checkout";
87
+
88
+ // Set the project ID globally
89
+ setProjectID("your-project-id");
90
+ ```
91
+
92
+ 3. Cart Methods:
93
+
94
+ ```tsx
95
+ import {
96
+ Bitsnap,
97
+ addProductToCart,
98
+ showCart,
99
+ hideCart,
100
+ } from "bitsnap-checkout";
101
+
102
+ // Modern approach using namespace
103
+ async function handleCart() {
104
+ // Add product to cart
105
+ await Bitsnap.addProductToCart("product-id", 2, {
106
+ customField: "value",
107
+ });
108
+
109
+ // Show cart
110
+ Bitsnap.showCart();
111
+
112
+ // Hide cart
113
+ Bitsnap.hideCart();
114
+ }
115
+
116
+ // Legacy approach (deprecated)
117
+ async function legacyHandleCart() {
118
+ await addProductToCart("product-id", 1);
119
+ showCart();
120
+ hideCart();
121
+ }
122
+ ```
123
+
124
+ 5. Creating Checkout/Payment:
125
+
126
+ ```tsx
127
+ import { createCheckout, LinkRequest } from "bitsnap-checkout";
128
+
129
+ async function handleCheckout() {
130
+ const request: LinkRequest = {
131
+ items: [
132
+ {
133
+ id: "product-id",
134
+ quantity: 1,
135
+ },
136
+ ],
137
+ details: {
138
+ email: "customer@example.com",
139
+ name: "John Doe",
140
+ address: {
141
+ name: "John Doe",
142
+ line1: "123 Street",
143
+ city: "City",
144
+ country: "US",
145
+ },
146
+ },
147
+ askForAddress: true,
148
+ askForPhone: true,
149
+ };
150
+
151
+ const result = await createCheckout({
152
+ ...request,
153
+ apiKey: "your-api-key",
154
+ testMode: true,
155
+ });
156
+
157
+ if (result.status === "ok") {
158
+ window.location.href = result.redirectURL;
159
+ }
160
+ }
161
+ ```
162
+
163
+ 6. Webhook Handler:
164
+
165
+ ```tsx
166
+ import { handleWebhook } from "bitsnap-checkout";
167
+
168
+ async function processWebhook(req: Request) {
169
+ const payload = await req.text();
170
+ const url = req.url;
171
+ const headers = Object.fromEntries(req.headers);
172
+ const webhookSecret = "your-webhook-secret";
173
+
174
+ const result = await handleWebhook(payload, url, headers, webhookSecret);
175
+
176
+ if (result.isErr) {
177
+ console.error("Webhook error:", result.error);
178
+ return;
179
+ }
180
+
181
+ // Process the webhook event
182
+ const { projectId, environment, eventData } = result;
183
+
184
+ switch (eventData?.event) {
185
+ case "TRANSACTION_SUCCESS":
186
+ // Handle successful transaction
187
+ break;
188
+ case "TRANSACTION_FAILURE":
189
+ // Handle failed transaction
190
+ break;
191
+ // Handle other events...
192
+ }
193
+ }
194
+ ```
195
+
196
+ Complete Example:
197
+
198
+ ```tsx
199
+ import {
200
+ BitsnapCheckout,
201
+ setProjectID,
202
+ setCustomHost,
203
+ Bitsnap,
204
+ createCheckout,
205
+ handleWebhook,
206
+ type LinkRequest,
207
+ } from "bitsnap-checkout";
208
+
209
+ // Configure globally
210
+ setProjectID("your-project-id");
211
+ setCustomHost("https://your-custom-host.com");
212
+
213
+ function Store() {
214
+ async function handleBuyNow(productId: string) {
215
+ // Add to cart
216
+ await Bitsnap.addProductToCart(productId, 1);
217
+
218
+ // Show cart
219
+ Bitsnap.showCart();
220
+ }
221
+
222
+ async function processCheckout(
223
+ items: Array<{ id: string; quantity: number }>,
224
+ ) {
225
+ const checkoutRequest: LinkRequest = {
226
+ items,
227
+ askForAddress: true,
228
+ askForPhone: true,
229
+ details: {
230
+ email: "customer@example.com",
231
+ },
232
+ redirect: {
233
+ successURL: "/success",
234
+ cancelURL: "/cancel",
235
+ },
236
+ };
237
+
238
+ const result = await createCheckout({
239
+ ...checkoutRequest,
240
+ testMode: true,
241
+ });
242
+
243
+ if (result.status === "ok") {
244
+ window.location.href = result.redirectURL;
245
+ }
246
+ }
247
+
248
+ return (
249
+ <div>
250
+ <button onClick={() => handleBuyNow("product-123")}>Buy Now</button>
251
+
252
+ <BitsnapCheckout
253
+ projectID="your-project-id"
254
+ onVisibleChange={(isVisible) => {
255
+ console.log("Cart visibility changed:", isVisible);
256
+ }}
257
+ />
258
+ </div>
259
+ );
260
+ }
261
+ ```
@@ -0,0 +1,9 @@
1
+ var __getOwnPropNames = Object.getOwnPropertyNames;
2
+ var __commonJS = (cb, mod) => function __require() {
3
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
4
+ };
5
+
6
+ export {
7
+ __commonJS
8
+ };
9
+ //# sourceMappingURL=chunk-ZD7AOCMD.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
package/dist/errors.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,11 @@
1
+ import {
2
+ __commonJS
3
+ } from "./chunk-ZD7AOCMD.mjs";
4
+
5
+ // src/errors.ts
6
+ var require_errors = __commonJS({
7
+ "src/errors.ts"() {
8
+ }
9
+ });
10
+ export default require_errors();
11
+ //# sourceMappingURL=errors.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/errors.ts"],"sourcesContent":[""],"mappings":";;;;;AAAA;AAAA;AAAA;AAAA;","names":[]}