@wix/headless-stores 0.0.6 → 0.0.7

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 (46) hide show
  1. package/astro/actions/package.json +4 -0
  2. package/cjs/dist/astro/actions/custom-checkout.d.ts +48 -0
  3. package/cjs/dist/astro/actions/custom-checkout.js +57 -0
  4. package/cjs/dist/astro/actions/index.d.ts +1 -0
  5. package/cjs/dist/astro/actions/index.js +17 -0
  6. package/cjs/dist/react/BuyNow.d.ts +57 -0
  7. package/cjs/dist/react/BuyNow.js +46 -0
  8. package/cjs/dist/react/PayNow.d.ts +43 -0
  9. package/cjs/dist/react/PayNow.js +37 -0
  10. package/cjs/dist/react/index.d.ts +2 -0
  11. package/cjs/dist/react/index.js +18 -0
  12. package/cjs/dist/server-actions/custom-checkout-action.d.ts +49 -0
  13. package/cjs/dist/server-actions/custom-checkout-action.js +63 -0
  14. package/cjs/dist/server-actions/index.d.ts +1 -0
  15. package/cjs/dist/server-actions/index.js +17 -0
  16. package/cjs/dist/services/buy-now-service.d.ts +89 -0
  17. package/cjs/dist/services/buy-now-service.js +68 -0
  18. package/cjs/dist/services/index.d.ts +2 -0
  19. package/cjs/dist/services/index.js +9 -0
  20. package/cjs/dist/services/pay-now-service.d.ts +72 -0
  21. package/cjs/dist/services/pay-now-service.js +50 -0
  22. package/cjs/dist/utils/index.d.ts +1 -0
  23. package/cjs/dist/utils/index.js +33 -0
  24. package/cjs/package.json +3 -0
  25. package/dist/astro/actions/custom-checkout.d.ts +1 -1
  26. package/dist/astro/actions/custom-checkout.js +4 -3
  27. package/dist/react/BuyNow.d.ts +7 -1
  28. package/dist/react/BuyNow.js +6 -2
  29. package/dist/react/CurrentCartServiceProvider.d.ts +5 -0
  30. package/dist/react/CurrentCartServiceProvider.js +12 -0
  31. package/dist/react/PayNow.d.ts +25 -0
  32. package/dist/react/PayNow.js +22 -0
  33. package/dist/react/VariantSelectorServiceProvider.d.ts +7 -0
  34. package/dist/react/VariantSelectorServiceProvider.js +22 -0
  35. package/dist/server-actions/custom-checkout-action.d.ts +5 -1
  36. package/dist/server-actions/custom-checkout-action.js +5 -0
  37. package/dist/services/CurrentCartService.d.ts +18 -0
  38. package/dist/services/CurrentCartService.js +9 -0
  39. package/dist/services/VariantSelectorServices.d.ts +8 -0
  40. package/dist/services/VariantSelectorServices.js +20 -0
  41. package/dist/services/buy-now-service.d.ts +9 -1
  42. package/dist/services/buy-now-service.js +11 -9
  43. package/package.json +31 -7
  44. package/react/package.json +4 -0
  45. package/server-actions/package.json +4 -0
  46. package/services/package.json +4 -0
@@ -0,0 +1,4 @@
1
+ {
2
+ "main": "../../cjs/dist/astro/actions/index.js",
3
+ "types": "../../dist/astro/actions/index.d.ts"
4
+ }
@@ -0,0 +1,48 @@
1
+ import { type CustomLineItemCheckoutOptions } from "@wix/headless-stores/server-actions";
2
+ /**
3
+ * Creates an Astro action factory for custom checkout functionality with line items.
4
+ *
5
+ * This factory function generates an Astro action that can be used to create custom
6
+ * checkout URLs with specific line items. It wraps the Wix headless stores server
7
+ * action functionality in an Astro-compatible action format.
8
+ *
9
+ * @param {CustomLineItemCheckoutOptions} factoryOpts - Configuration options for the custom checkout
10
+ * @param {string} factoryOpts.productName - The name of the product for the custom line item
11
+ * @param {string} [factoryOpts.priceDescription] - A description for the price, which will be displayed to the customer
12
+ * @param {Array<{content: string, title: string}>} [factoryOpts.policies] - An array of policies related to this custom item
13
+ * @param {string} [factoryOpts.postFlowUrl] - The URL to redirect the user to after the checkout is successfully completed
14
+ * @param {number} [factoryOpts.quantity=1] - The quantity of the product
15
+ * @param {string} factoryOpts.price - The price of the product
16
+ *
17
+ * @returns {ReturnType<typeof defineAction>} An Astro action that when invoked returns checkout URL
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * // Define the action with your configuration
22
+ * const customCheckoutAction = customCheckoutActionFactory({
23
+ * productName: "Premium Subscription",
24
+ * price: "29.99",
25
+ * priceDescription: "per month",
26
+ * quantity: 1,
27
+ * postFlowUrl: "https://yoursite.com/thank-you",
28
+ * policies: [
29
+ * {
30
+ * title: "Refund Policy",
31
+ * content: "30-day money back guarantee"
32
+ * }
33
+ * ]
34
+ * });
35
+ *
36
+ * export const server = {
37
+ * checkout: customCheckoutAction
38
+ * };
39
+ *
40
+ * // Use in your Astro component or API route
41
+ * import { actions } from "astro:actions";
42
+ * const checkoutUrl = await actions.customCheckoutAction();
43
+ * ```
44
+ *
45
+ * @see {@link https://docs.astro.build/en/guides/actions/} Astro Actions Documentation
46
+ * @see {@link https://dev.wix.com/docs/sdk/headless/api-reference/stores/checkout} Wix Stores Checkout API
47
+ */
48
+ export declare const customCheckoutActionFactory: (factoryOpts: CustomLineItemCheckoutOptions) => any;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.customCheckoutActionFactory = void 0;
4
+ /// <reference types="astro/env" />
5
+ // @ts-ignore
6
+ const astro_actions_1 = require("astro:actions");
7
+ const server_actions_1 = require("@wix/headless-stores/server-actions");
8
+ /**
9
+ * Creates an Astro action factory for custom checkout functionality with line items.
10
+ *
11
+ * This factory function generates an Astro action that can be used to create custom
12
+ * checkout URLs with specific line items. It wraps the Wix headless stores server
13
+ * action functionality in an Astro-compatible action format.
14
+ *
15
+ * @param {CustomLineItemCheckoutOptions} factoryOpts - Configuration options for the custom checkout
16
+ * @param {string} factoryOpts.productName - The name of the product for the custom line item
17
+ * @param {string} [factoryOpts.priceDescription] - A description for the price, which will be displayed to the customer
18
+ * @param {Array<{content: string, title: string}>} [factoryOpts.policies] - An array of policies related to this custom item
19
+ * @param {string} [factoryOpts.postFlowUrl] - The URL to redirect the user to after the checkout is successfully completed
20
+ * @param {number} [factoryOpts.quantity=1] - The quantity of the product
21
+ * @param {string} factoryOpts.price - The price of the product
22
+ *
23
+ * @returns {ReturnType<typeof defineAction>} An Astro action that when invoked returns checkout URL
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * // Define the action with your configuration
28
+ * const customCheckoutAction = customCheckoutActionFactory({
29
+ * productName: "Premium Subscription",
30
+ * price: "29.99",
31
+ * priceDescription: "per month",
32
+ * quantity: 1,
33
+ * postFlowUrl: "https://yoursite.com/thank-you",
34
+ * policies: [
35
+ * {
36
+ * title: "Refund Policy",
37
+ * content: "30-day money back guarantee"
38
+ * }
39
+ * ]
40
+ * });
41
+ *
42
+ * export const server = {
43
+ * checkout: customCheckoutAction
44
+ * };
45
+ *
46
+ * // Use in your Astro component or API route
47
+ * import { actions } from "astro:actions";
48
+ * const checkoutUrl = await actions.customCheckoutAction();
49
+ * ```
50
+ *
51
+ * @see {@link https://docs.astro.build/en/guides/actions/} Astro Actions Documentation
52
+ * @see {@link https://dev.wix.com/docs/sdk/headless/api-reference/stores/checkout} Wix Stores Checkout API
53
+ */
54
+ const customCheckoutActionFactory = (factoryOpts) => (0, astro_actions_1.defineAction)({
55
+ handler: () => (0, server_actions_1.getCustomLineItemCheckoutURLFactory)(factoryOpts)(),
56
+ });
57
+ exports.customCheckoutActionFactory = customCheckoutActionFactory;
@@ -0,0 +1 @@
1
+ export * from "./custom-checkout";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./custom-checkout"), exports);
@@ -0,0 +1,57 @@
1
+ export type RedirectToCheckout = () => void;
2
+ /**
3
+ * Props passed to the render function of the BuyNow component
4
+ */
5
+ export interface BuyNowRenderProps {
6
+ /** Whether the buy now operation is currently loading */
7
+ isLoading: boolean;
8
+ /** The name of the product being purchased */
9
+ productName: string;
10
+ /** Function to redirect the user to the checkout page */
11
+ redirectToCheckout: RedirectToCheckout;
12
+ /** The error message if the buy now operation fails */
13
+ error: string | null;
14
+ /** The price of the product being purchased */
15
+ price: string;
16
+ /** The currency of the product being purchased */
17
+ currency: string;
18
+ /** Whether the product is in stock */
19
+ inStock: boolean;
20
+ /** Whether the product is available for pre-order */
21
+ preOrderAvailable: boolean;
22
+ }
23
+ export type BuyNowChildren = (props: BuyNowRenderProps) => React.ReactNode;
24
+ /**
25
+ * Props for the BuyNow component
26
+ */
27
+ export interface BuyNowProps {
28
+ /** Render function that receives buy now state and actions */
29
+ children: BuyNowChildren;
30
+ }
31
+ /**
32
+ * A headless component that provides buy now functionality using the render props pattern.
33
+ *
34
+ * This component manages the state and actions for a "buy now" flow, allowing consumers
35
+ * to render their own UI while accessing the underlying buy now functionality.
36
+ * @example
37
+ * ```tsx
38
+ * <BuyNow>
39
+ * {({ isLoading, productName, redirectToCheckout, error, price, currency, inStock, preOrderAvailable }) => (
40
+ * <div>
41
+ * <h2>{productName}</h2>
42
+ * <p>{price} {currency}</p>
43
+ * {error && <div className="error">{error}</div>}
44
+ * {inStock && <div>In stock</div>}
45
+ * {preOrderAvailable && <div>Pre-order available</div>}
46
+ * <button
47
+ * onClick={redirectToCheckout}
48
+ * disabled={isLoading}
49
+ * >
50
+ * {isLoading ? 'Processing...' : 'Buy Now'}
51
+ * </button>
52
+ * </div>
53
+ * )}
54
+ * </BuyNow>
55
+ * ```
56
+ */
57
+ export declare function BuyNow(props: BuyNowProps): import("react").ReactNode;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BuyNow = BuyNow;
4
+ const services_manager_react_1 = require("@wix/services-manager-react");
5
+ const buy_now_service_1 = require("../services/buy-now-service");
6
+ ;
7
+ ;
8
+ /**
9
+ * A headless component that provides buy now functionality using the render props pattern.
10
+ *
11
+ * This component manages the state and actions for a "buy now" flow, allowing consumers
12
+ * to render their own UI while accessing the underlying buy now functionality.
13
+ * @example
14
+ * ```tsx
15
+ * <BuyNow>
16
+ * {({ isLoading, productName, redirectToCheckout, error, price, currency, inStock, preOrderAvailable }) => (
17
+ * <div>
18
+ * <h2>{productName}</h2>
19
+ * <p>{price} {currency}</p>
20
+ * {error && <div className="error">{error}</div>}
21
+ * {inStock && <div>In stock</div>}
22
+ * {preOrderAvailable && <div>Pre-order available</div>}
23
+ * <button
24
+ * onClick={redirectToCheckout}
25
+ * disabled={isLoading}
26
+ * >
27
+ * {isLoading ? 'Processing...' : 'Buy Now'}
28
+ * </button>
29
+ * </div>
30
+ * )}
31
+ * </BuyNow>
32
+ * ```
33
+ */
34
+ function BuyNow(props) {
35
+ const { redirectToCheckout, loadingSignal, productName, errorSignal, price, currency, inStock, preOrderAvailable, } = (0, services_manager_react_1.useService)(buy_now_service_1.BuyNowServiceDefinition);
36
+ return props.children({
37
+ isLoading: loadingSignal.get(),
38
+ error: errorSignal.get(),
39
+ productName: productName,
40
+ redirectToCheckout,
41
+ price,
42
+ currency,
43
+ inStock,
44
+ preOrderAvailable,
45
+ });
46
+ }
@@ -0,0 +1,43 @@
1
+ export type PayNowRedirectToCheckout = () => void;
2
+ /**
3
+ * Props passed to the render function of the PayNow component
4
+ */
5
+ export interface PayNowRenderProps {
6
+ /** Whether the buy now operation is currently loading */
7
+ isLoading: boolean;
8
+ /** Function to redirect the user to the checkout page */
9
+ redirectToCheckout: PayNowRedirectToCheckout;
10
+ /** The error message if the buy now operation fails */
11
+ error: string | null;
12
+ }
13
+ export type PayNowChildren = (props: PayNowRenderProps) => React.ReactNode;
14
+ /**
15
+ * Props for the PayNow component
16
+ */
17
+ export interface PayNowProps {
18
+ /** Render function that receives buy now state and actions */
19
+ children: PayNowChildren;
20
+ }
21
+ /**
22
+ * A headless component that provides pay now functionality using the render props pattern.
23
+ *
24
+ * This component manages the state and actions for a "pay now" flow, allowing consumers
25
+ * to render their own UI while accessing the underlying payment functionality.
26
+ * @example
27
+ * ```tsx
28
+ * <PayNow>
29
+ * {({ isLoading, redirectToCheckout, error }) => (
30
+ * <div>
31
+ * {error && <div className="error">{error}</div>}
32
+ * <button
33
+ * onClick={redirectToCheckout}
34
+ * disabled={isLoading}
35
+ * >
36
+ * {isLoading ? 'Processing...' : 'Pay Now'}
37
+ * </button>
38
+ * </div>
39
+ * )}
40
+ * </PayNow>
41
+ * ```
42
+ */
43
+ export declare function PayNow(props: PayNowProps): import("react").ReactNode;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PayNow = PayNow;
4
+ const services_manager_react_1 = require("@wix/services-manager-react");
5
+ const pay_now_service_1 = require("../services/pay-now-service");
6
+ ;
7
+ ;
8
+ /**
9
+ * A headless component that provides pay now functionality using the render props pattern.
10
+ *
11
+ * This component manages the state and actions for a "pay now" flow, allowing consumers
12
+ * to render their own UI while accessing the underlying payment functionality.
13
+ * @example
14
+ * ```tsx
15
+ * <PayNow>
16
+ * {({ isLoading, redirectToCheckout, error }) => (
17
+ * <div>
18
+ * {error && <div className="error">{error}</div>}
19
+ * <button
20
+ * onClick={redirectToCheckout}
21
+ * disabled={isLoading}
22
+ * >
23
+ * {isLoading ? 'Processing...' : 'Pay Now'}
24
+ * </button>
25
+ * </div>
26
+ * )}
27
+ * </PayNow>
28
+ * ```
29
+ */
30
+ function PayNow(props) {
31
+ const { redirectToCheckout, loadingSignal, errorSignal, } = (0, services_manager_react_1.useService)(pay_now_service_1.PayNowServiceDefinition);
32
+ return props.children({
33
+ isLoading: loadingSignal.get(),
34
+ error: errorSignal.get(),
35
+ redirectToCheckout,
36
+ });
37
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./BuyNow";
2
+ export * from "./PayNow";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./BuyNow"), exports);
18
+ __exportStar(require("./PayNow"), exports);
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Options for creating a checkout with a custom line item.
3
+ */
4
+ export interface CustomLineItemCheckoutOptions {
5
+ /**
6
+ * The name of the product for the custom line item.
7
+ * @example "My Custom Product"
8
+ */
9
+ productName: string;
10
+ /**
11
+ * A description for the price, which will be displayed to the customer.
12
+ * @example "per month"
13
+ */
14
+ priceDescription: string;
15
+ /**
16
+ * An array of policies related to this custom item.
17
+ * Each policy should have a title and content.
18
+ */
19
+ policies?: {
20
+ content: string;
21
+ title: string;
22
+ }[];
23
+ /**
24
+ * The URL to redirect the user to after the checkout is successfully completed.
25
+ */
26
+ postFlowUrl?: string;
27
+ /**
28
+ * The quantity of the product.
29
+ * @default 1
30
+ */
31
+ quantity?: number;
32
+ /**
33
+ * The price of the product.
34
+ */
35
+ price: string;
36
+ /**
37
+ * The currency of the product. It will only take effect after configuring the payment provider to accept this currency.
38
+ */
39
+ currency: string;
40
+ }
41
+ /**
42
+ * Creates a factory function to generate checkout URLs for custom line items with a fixed price.
43
+ * This is useful when you have a single product or service with a known price,
44
+ * and you want to dynamically create checkout sessions for it.
45
+ *
46
+ * @param factoryOpts - The options for the factory, including the price.
47
+ * @returns A function that takes `CustomLineItemCheckoutOptions` and returns a checkout URL.
48
+ */
49
+ export declare function getCustomLineItemCheckoutURLFactory(factoryOpts: CustomLineItemCheckoutOptions): () => Promise<string>;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCustomLineItemCheckoutURLFactory = getCustomLineItemCheckoutURLFactory;
4
+ const ecom_1 = require("@wix/ecom");
5
+ const redirects_1 = require("@wix/redirects");
6
+ const essentials_1 = require("@wix/essentials");
7
+ /**
8
+ * Creates a factory function to generate checkout URLs for custom line items with a fixed price.
9
+ * This is useful when you have a single product or service with a known price,
10
+ * and you want to dynamically create checkout sessions for it.
11
+ *
12
+ * @param factoryOpts - The options for the factory, including the price.
13
+ * @returns A function that takes `CustomLineItemCheckoutOptions` and returns a checkout URL.
14
+ */
15
+ function getCustomLineItemCheckoutURLFactory(factoryOpts) {
16
+ /**
17
+ * Generates a checkout URL for a custom line item.
18
+ * @param opts - The options for the custom line item checkout.
19
+ * @returns A promise that resolves to the full URL for the redirect session to the checkout.
20
+ * @throws Will throw an error if the checkout creation or redirect session fails.
21
+ */
22
+ return async function getCustomLineItemCheckoutURL() {
23
+ try {
24
+ const checkoutResult = await essentials_1.auth.elevate(ecom_1.checkout.createCheckout)({
25
+ customLineItems: [
26
+ {
27
+ productName: {
28
+ original: factoryOpts.productName,
29
+ },
30
+ price: factoryOpts.price,
31
+ quantity: factoryOpts.quantity || 1,
32
+ itemType: {
33
+ preset: ecom_1.checkout.ItemTypeItemType.PHYSICAL,
34
+ },
35
+ priceDescription: {
36
+ original: factoryOpts.priceDescription
37
+ },
38
+ policies: factoryOpts.policies || [],
39
+ }
40
+ ],
41
+ channelType: ecom_1.checkout.ChannelType.WEB,
42
+ ...(factoryOpts.currency ? {
43
+ checkoutInfo: {
44
+ currency: factoryOpts.currency,
45
+ }
46
+ } : {})
47
+ });
48
+ if (!checkoutResult._id) {
49
+ throw new Error(`Failed to create checkout for custom line item ${factoryOpts.productName}`);
50
+ }
51
+ const { redirectSession } = await redirects_1.redirects.createRedirectSession({
52
+ ecomCheckout: { checkoutId: checkoutResult._id },
53
+ callbacks: {
54
+ ...(factoryOpts.postFlowUrl ? { postFlowUrl: factoryOpts.postFlowUrl } : {})
55
+ },
56
+ });
57
+ return redirectSession?.fullUrl;
58
+ }
59
+ catch (error) {
60
+ throw error;
61
+ }
62
+ };
63
+ }
@@ -0,0 +1 @@
1
+ export * from "./custom-checkout-action";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./custom-checkout-action"), exports);
@@ -0,0 +1,89 @@
1
+ import { ServiceFactoryConfig, Signal } from "@wix/services-definitions";
2
+ export declare const BuyNowServiceDefinition: string & {
3
+ __api: {
4
+ redirectToCheckout: () => Promise<void>;
5
+ loadingSignal: Signal<boolean>;
6
+ errorSignal: Signal<string | null>;
7
+ };
8
+ __config: {};
9
+ isServiceDefinition?: boolean;
10
+ } & {
11
+ redirectToCheckout: () => Promise<void>;
12
+ loadingSignal: Signal<boolean>;
13
+ errorSignal: Signal<string | null>;
14
+ };
15
+ export declare const BuyNowServiceImplementation: import("@wix/services-definitions").ServiceFactory<string & {
16
+ __api: {
17
+ redirectToCheckout: () => Promise<void>;
18
+ loadingSignal: Signal<boolean>;
19
+ errorSignal: Signal<string | null>;
20
+ };
21
+ __config: {};
22
+ isServiceDefinition?: boolean;
23
+ } & {
24
+ redirectToCheckout: () => Promise<void>;
25
+ loadingSignal: Signal<boolean>;
26
+ errorSignal: Signal<string | null>;
27
+ }, {
28
+ productId: string;
29
+ variantId?: string;
30
+ productName: string;
31
+ price: string;
32
+ currency: string;
33
+ inStock: boolean;
34
+ preOrderAvailable: boolean;
35
+ }, import("@wix/services-definitions").ThreadMode.MAIN>;
36
+ export declare const loadBuyNowServiceInitialData: (productSlug: string, variantId?: string) => Promise<{
37
+ [BuyNowServiceDefinition]: {
38
+ productId: string;
39
+ productName: string;
40
+ price: string;
41
+ currency: string;
42
+ variantId: string | null | undefined;
43
+ inStock: boolean | undefined;
44
+ preOrderAvailable: boolean | undefined;
45
+ };
46
+ }>;
47
+ export declare const buyNowServiceBinding: <T extends {
48
+ [key: string]: Awaited<ReturnType<typeof loadBuyNowServiceInitialData>>[typeof BuyNowServiceDefinition];
49
+ }>(servicesConfigs: T, additionalConfig?: Partial<ServiceFactoryConfig<typeof BuyNowServiceImplementation>>) => readonly [string & {
50
+ __api: {
51
+ redirectToCheckout: () => Promise<void>;
52
+ loadingSignal: Signal<boolean>;
53
+ errorSignal: Signal<string | null>;
54
+ };
55
+ __config: {};
56
+ isServiceDefinition?: boolean;
57
+ } & {
58
+ redirectToCheckout: () => Promise<void>;
59
+ loadingSignal: Signal<boolean>;
60
+ errorSignal: Signal<string | null>;
61
+ }, import("@wix/services-definitions").ServiceFactory<string & {
62
+ __api: {
63
+ redirectToCheckout: () => Promise<void>;
64
+ loadingSignal: Signal<boolean>;
65
+ errorSignal: Signal<string | null>;
66
+ };
67
+ __config: {};
68
+ isServiceDefinition?: boolean;
69
+ } & {
70
+ redirectToCheckout: () => Promise<void>;
71
+ loadingSignal: Signal<boolean>;
72
+ errorSignal: Signal<string | null>;
73
+ }, {
74
+ productId: string;
75
+ variantId?: string;
76
+ productName: string;
77
+ price: string;
78
+ currency: string;
79
+ inStock: boolean;
80
+ preOrderAvailable: boolean;
81
+ }, import("@wix/services-definitions").ThreadMode.MAIN>, {
82
+ productId: string;
83
+ variantId?: string;
84
+ productName: string;
85
+ price: string;
86
+ currency: string;
87
+ inStock: boolean;
88
+ preOrderAvailable: boolean;
89
+ }];
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buyNowServiceBinding = exports.loadBuyNowServiceInitialData = exports.BuyNowServiceImplementation = exports.BuyNowServiceDefinition = void 0;
4
+ const services_definitions_1 = require("@wix/services-definitions");
5
+ const signals_1 = require("@wix/services-definitions/core-services/signals");
6
+ const stores_1 = require("@wix/stores");
7
+ const utils_1 = require("../utils");
8
+ exports.BuyNowServiceDefinition = (0, services_definitions_1.defineService)("BuyNow");
9
+ exports.BuyNowServiceImplementation = services_definitions_1.implementService.withConfig()(exports.BuyNowServiceDefinition, ({ getService, config }) => {
10
+ const signalsService = getService(signals_1.SignalsServiceDefinition);
11
+ const loadingSignal = signalsService.signal(false);
12
+ const errorSignal = signalsService.signal(null);
13
+ const inStockSignal = signalsService.signal(config.inStock);
14
+ const preOrderAvailableSignal = signalsService.signal(config.preOrderAvailable);
15
+ return {
16
+ redirectToCheckout: async () => {
17
+ loadingSignal.set(true);
18
+ try {
19
+ const checkoutUrl = await (0, utils_1.getCheckoutUrlForProduct)(config.productId, config.variantId);
20
+ window.location.href = checkoutUrl;
21
+ }
22
+ catch (error) {
23
+ errorSignal.set(error.toString());
24
+ loadingSignal.set(false);
25
+ }
26
+ },
27
+ loadingSignal,
28
+ errorSignal,
29
+ inStockSignal,
30
+ preOrderAvailableSignal,
31
+ productName: config.productName,
32
+ price: config.price,
33
+ currency: config.currency,
34
+ };
35
+ });
36
+ const loadBuyNowServiceInitialData = async (productSlug, variantId) => {
37
+ const res = await stores_1.productsV3.getProductBySlug(productSlug, {
38
+ fields: ["CURRENCY"],
39
+ });
40
+ const product = res.product;
41
+ const selectedVariant = variantId ? product.variantsInfo?.variants?.find((v) => v._id === variantId) : product.variantsInfo?.variants?.[0];
42
+ const price = selectedVariant?.price?.actualPrice?.amount ?? product.actualPriceRange?.minValue?.amount;
43
+ const inStock = selectedVariant?.inventoryStatus?.inStock;
44
+ const preOrderAvailable = selectedVariant?.inventoryStatus?.preorderEnabled;
45
+ return {
46
+ [exports.BuyNowServiceDefinition]: {
47
+ productId: product._id,
48
+ productName: product.name,
49
+ price: price,
50
+ currency: product.currency,
51
+ variantId: selectedVariant?._id,
52
+ inStock,
53
+ preOrderAvailable,
54
+ },
55
+ };
56
+ };
57
+ exports.loadBuyNowServiceInitialData = loadBuyNowServiceInitialData;
58
+ const buyNowServiceBinding = (servicesConfigs, additionalConfig = {}) => {
59
+ return [
60
+ exports.BuyNowServiceDefinition,
61
+ exports.BuyNowServiceImplementation,
62
+ {
63
+ ...servicesConfigs[exports.BuyNowServiceDefinition],
64
+ ...additionalConfig,
65
+ },
66
+ ];
67
+ };
68
+ exports.buyNowServiceBinding = buyNowServiceBinding;
@@ -0,0 +1,2 @@
1
+ export { buyNowServiceBinding, loadBuyNowServiceInitialData, } from "./buy-now-service";
2
+ export { payNowServiceBinding, loadPayNowServiceInitialData, } from "./pay-now-service";
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadPayNowServiceInitialData = exports.payNowServiceBinding = exports.loadBuyNowServiceInitialData = exports.buyNowServiceBinding = void 0;
4
+ var buy_now_service_1 = require("./buy-now-service");
5
+ Object.defineProperty(exports, "buyNowServiceBinding", { enumerable: true, get: function () { return buy_now_service_1.buyNowServiceBinding; } });
6
+ Object.defineProperty(exports, "loadBuyNowServiceInitialData", { enumerable: true, get: function () { return buy_now_service_1.loadBuyNowServiceInitialData; } });
7
+ var pay_now_service_1 = require("./pay-now-service");
8
+ Object.defineProperty(exports, "payNowServiceBinding", { enumerable: true, get: function () { return pay_now_service_1.payNowServiceBinding; } });
9
+ Object.defineProperty(exports, "loadPayNowServiceInitialData", { enumerable: true, get: function () { return pay_now_service_1.loadPayNowServiceInitialData; } });