@liquidcommerce/cloud-sdk 1.7.0 → 1.8.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/README.md +163 -31
- package/dist/index.cjs +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/liquidcommerce-cloud-sdk.ssr.js +1 -1
- package/dist/types/core/order-authenticated.service.d.ts +1 -10
- package/dist/types/interfaces/checkout.interface.d.ts +12 -1
- package/dist/types/interfaces/liquid-commerce-client.interface.d.ts +11 -0
- package/package.json +1 -1
- package/umd/liquidcommerce-cloud-sdk.min.js +1 -1
package/README.md
CHANGED
|
@@ -21,9 +21,10 @@ The LiquidCommerce Cloud SDK provides an easy way to interact with our APIs thro
|
|
|
21
21
|
- [Catalog](#catalog)
|
|
22
22
|
- [Cart](#cart)
|
|
23
23
|
- [User](#user)
|
|
24
|
-
- [Payment](#payment)
|
|
24
|
+
- [Payment Element](#payment-element)
|
|
25
|
+
- [Legacy Payment](#legacy-payment)
|
|
25
26
|
- [Checkout](#checkout)
|
|
26
|
-
- [
|
|
27
|
+
- [Orders](#orders)
|
|
27
28
|
- [Webhook](#webhook)
|
|
28
29
|
- [Response Types](#response-types)
|
|
29
30
|
- [Error Handling](#error-handling)
|
|
@@ -63,10 +64,10 @@ const client = await LiquidCommerce('YOUR_LIQUIDCOMMERCE_API_KEY', {
|
|
|
63
64
|
|
|
64
65
|
LiquidCommerce provides a separate authentication mechanism for Order API endpoints. The Order client uses Basic Authentication with a username and password.
|
|
65
66
|
|
|
66
|
-
Example using
|
|
67
|
+
Example using LiquidCommerceOrders client:
|
|
67
68
|
|
|
68
69
|
```typescript
|
|
69
|
-
const orderClient = await
|
|
70
|
+
const orderClient = await LiquidCommerceOrders({
|
|
70
71
|
userID: 'YOUR_ORDER_API_USER_ID',
|
|
71
72
|
password: 'YOUR_ORDER_API_PASSWORD',
|
|
72
73
|
env: LIQUID_COMMERCE_ENV.STAGE, // or PROD
|
|
@@ -99,6 +100,21 @@ interface ApiResponse<T> {
|
|
|
99
100
|
|
|
100
101
|
## Services and Usage
|
|
101
102
|
|
|
103
|
+
### Auth
|
|
104
|
+
|
|
105
|
+
While the SDK handles authentication automatically, you can also manually retrieve the authentication details:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
// Manually retrieve authentication details
|
|
109
|
+
try {
|
|
110
|
+
const authDetails = await client.auth();
|
|
111
|
+
console.log('Access Token:', authDetails.token);
|
|
112
|
+
console.log('Expires In:', authDetails.exp);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.error('Failed to get auth details:', error);
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
102
118
|
### Address
|
|
103
119
|
|
|
104
120
|
Services for address validation and lookup:
|
|
@@ -288,13 +304,111 @@ await client.user.purgeAddress('address_id');
|
|
|
288
304
|
await client.user.purgePayment('customer_id', 'payment_id');
|
|
289
305
|
```
|
|
290
306
|
|
|
291
|
-
### Payment
|
|
307
|
+
### Payment Element
|
|
292
308
|
|
|
293
|
-
The
|
|
309
|
+
The Payment Element is a secure and modern UI component for collecting payment details. It simplifies PCI compliance by handling all sensitive information within an iframe.
|
|
294
310
|
|
|
295
311
|
#### Prerequisites
|
|
296
312
|
|
|
297
|
-
|
|
313
|
+
Before mounting the Payment Element, you must create a payment session. This can be tied to a user, cart, or checkout.
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
// Create a payment session to get a client secret
|
|
317
|
+
const paymentSession = await client.user.paymentSession({
|
|
318
|
+
// Optionally link to a cart, checkout, or customer
|
|
319
|
+
// cartId: 'your_cart_id',
|
|
320
|
+
// checkoutToken: 'your_checkout_token',
|
|
321
|
+
// customerId: 'your_customer_id',
|
|
322
|
+
// customerEmail: 'your_customer_id',
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
const { key, secret } = paymentSession.data.session;
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### Integration
|
|
329
|
+
|
|
330
|
+
1. **Initialize and Mount**
|
|
331
|
+
|
|
332
|
+
The `LiquidCommercePaymentElement` function creates and manages the UI component.
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
// Initialize the payment element
|
|
336
|
+
const paymentElement = LiquidCommercePaymentElement({
|
|
337
|
+
session: {
|
|
338
|
+
key, // Public key from payment session
|
|
339
|
+
secret, // Client secret from payment session
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
// Mount the element to a container in your DOM
|
|
344
|
+
await paymentElement.mount({
|
|
345
|
+
elementId: 'payment-element-container',
|
|
346
|
+
appearance: {
|
|
347
|
+
theme: 'night', // 'stripe' | 'night' | 'flat'
|
|
348
|
+
},
|
|
349
|
+
elementOptions: {
|
|
350
|
+
layout: 'tabs', // 'tabs' | 'accordion' | 'auto'
|
|
351
|
+
},
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
2. **Generate a Confirmation Token**
|
|
356
|
+
|
|
357
|
+
Once the user has filled out the payment form, create a confirmation token. This token securely represents the payment details.
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
const result = await paymentElement.createConfirmationToken();
|
|
361
|
+
|
|
362
|
+
if (result.token) {
|
|
363
|
+
// Token successfully created
|
|
364
|
+
const confirmationToken = result.token;
|
|
365
|
+
// Use this token to complete the checkout or save the payment method
|
|
366
|
+
} else {
|
|
367
|
+
// Handle error
|
|
368
|
+
console.error(result.message);
|
|
369
|
+
}
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
3. **Confirm the Payment Session**
|
|
373
|
+
|
|
374
|
+
Use the confirmation token to finalize the payment and retrieve the payment method details.
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
const confirmation = await client.user.confirmPaymentSession(confirmationToken);
|
|
378
|
+
|
|
379
|
+
if (confirmation.data) {
|
|
380
|
+
const paymentMethod = confirmation.data;
|
|
381
|
+
// Now you have the payment method ID, card details, etc.
|
|
382
|
+
// e.g., paymentMethod.id, paymentMethod.card.brand
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
4. **Lifecycle Management**
|
|
387
|
+
|
|
388
|
+
Properly manage the element's lifecycle to ensure a smooth user experience and resource cleanup.
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
// Listen to events
|
|
392
|
+
paymentElement.subscribe('ready', () => {
|
|
393
|
+
console.log('Payment element is ready.');
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
paymentElement.subscribe('change', (event) => {
|
|
397
|
+
// Handle form state changes (e.g., enable/disable submit button)
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
// Clean up when the element is no longer needed
|
|
401
|
+
paymentElement.unmount();
|
|
402
|
+
paymentElement.destroy();
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### Legacy Payment
|
|
406
|
+
|
|
407
|
+
The legacy payment system uses a previous version of our payment integration. For new integrations, we strongly recommend using the [Payment Element](#payment-element) for a more secure and flexible solution.
|
|
408
|
+
|
|
409
|
+
#### Prerequisites
|
|
410
|
+
|
|
411
|
+
1. User Session Creation:
|
|
298
412
|
|
|
299
413
|
```typescript
|
|
300
414
|
// First create or get a user session
|
|
@@ -508,6 +622,12 @@ const preparedCheckout = await client.checkout.prepare({
|
|
|
508
622
|
fulfillmentId: 'fulfillment_id',
|
|
509
623
|
tip: 500, // Amount in cents
|
|
510
624
|
},
|
|
625
|
+
],
|
|
626
|
+
deliveryInstructions: [
|
|
627
|
+
{
|
|
628
|
+
fulfillmentId: 'fulfillment_id',
|
|
629
|
+
instructions: "", // 250 Max characters
|
|
630
|
+
},
|
|
511
631
|
],
|
|
512
632
|
acceptedAccountCreation: true,
|
|
513
633
|
scheduledDelivery: '2024-12-25T14:00:00Z',
|
|
@@ -533,43 +653,55 @@ const preparedCheckout = await client.checkout.prepare({
|
|
|
533
653
|
// ... other checkout details
|
|
534
654
|
});
|
|
535
655
|
|
|
536
|
-
// 2. Initialize payment
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
656
|
+
// 2. Initialize payment element with checkout session
|
|
657
|
+
const paymentElement = LiquidCommercePaymentElement({
|
|
658
|
+
session: {
|
|
659
|
+
key: preparedCheckout.data.payment.publicKey, // From checkout prepare response
|
|
660
|
+
secret: preparedCheckout.data.payment.clientSecret, // From checkout prepare response
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
// 3. Mount the element
|
|
665
|
+
await paymentElement.mount({
|
|
540
666
|
elementId: 'payment-element-container',
|
|
541
667
|
appearance: { theme: 'night' },
|
|
542
668
|
elementOptions: { layout: 'tabs' },
|
|
543
669
|
});
|
|
544
670
|
|
|
545
|
-
//
|
|
546
|
-
|
|
547
|
-
// Monitor payment form state
|
|
548
|
-
const { complete, empty, value } = event;
|
|
549
|
-
});
|
|
671
|
+
// 4. Handle payment element events and create confirmation token
|
|
672
|
+
const result = await paymentElement.createConfirmationToken();
|
|
550
673
|
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
674
|
+
if (result.token) {
|
|
675
|
+
// 5. Confirm the payment collected with the confirmation token
|
|
676
|
+
const confirmation = await client.user.confirmPaymentSession(confirmationToken);
|
|
677
|
+
|
|
678
|
+
if (confirmation?.data?.id) {
|
|
679
|
+
// 6. Complete checkout with the confirmation token
|
|
680
|
+
const completedCheckout = await client.checkout.complete({
|
|
681
|
+
token: preparedCheckout.data.token,
|
|
682
|
+
payment: confirmation?.data?.id,
|
|
683
|
+
});
|
|
684
|
+
}
|
|
559
685
|
}
|
|
560
686
|
|
|
561
|
-
//
|
|
562
|
-
|
|
563
|
-
|
|
687
|
+
// 7. Clean up
|
|
688
|
+
paymentElement.unmount();
|
|
689
|
+
paymentElement.destroy();
|
|
564
690
|
```
|
|
565
691
|
|
|
566
|
-
###
|
|
692
|
+
### Orders
|
|
567
693
|
|
|
568
|
-
|
|
694
|
+
Provides secure access to order data throughout the finalized states of the order lifecycle within the LiquidCommerce ecosystem.
|
|
569
695
|
|
|
570
696
|
```typescript
|
|
697
|
+
const orderClient = await LiquidCommerceOrders({
|
|
698
|
+
userID: 'YOUR_ORDER_API_USER_ID',
|
|
699
|
+
password: 'YOUR_ORDER_API_PASSWORD',
|
|
700
|
+
env: LIQUID_COMMERCE_ENV.STAGE, // or PROD
|
|
701
|
+
});
|
|
702
|
+
|
|
571
703
|
// Fetch order details by ID or number
|
|
572
|
-
const orderResponse = await
|
|
704
|
+
const orderResponse = await orderClient.order.fetch(/* reference id or order number */);
|
|
573
705
|
```
|
|
574
706
|
|
|
575
707
|
[Click here to access the docs for the order response structure](https://docs.liquidcommerce.cloud/types/order)
|
|
@@ -619,4 +751,4 @@ All monetary values in the SDK are handled in cents (the smallest currency unit)
|
|
|
619
751
|
|
|
620
752
|
## Documentation
|
|
621
753
|
|
|
622
|
-
For more detailed information about each method and its parameters, please refer to our [official documentation](https://docs.liquidcommerce.cloud).
|
|
754
|
+
For more detailed information about each method and its parameters, please refer to our [official documentation](https://docs.liquidcommerce.cloud).
|