Pricing Plans Quick Start

Share your feedback
Reach out to us with feedback and suggestions to improve the Wix Headless experience, and join the Headless channel of the Devs on Wix Discord community to discuss features and connect with our growing community of developers.

The SDK pricing-plans module allows you to take advantage of Wix Pricing Plans business services in a site or app you build on any platform. This means you can create and manage your pricing plans and orders. This tutorial shows you how to create a seamless pricing plans checkout.

The tutorial is based on the Wix Headless example site. You can test out the live example site, or fork the site's code repo to use as a starting point for your own site or app.

This implementation focuses on simplicity and understandability, rather than feature richness, performance or completeness. For details about additional functionality, see Wix Pricing Plans in the API Reference. Looking for a more comprehensive example site integrating Wix Headless APIs for pricing plan management? Check out our starter templates.

Note: The code in this tutorial is written in JSX, but you can use the SDK in any JavaScript environment.

Implementing the Seamless Pricing Plan Checkout includes the following steps:

  1. Set up the Wix Headless environment.
  2. Import the SDK modules and create an SDK client.
  3. Query your Wix Pricing Plans items.
  4. Map the items to your UI.
  5. Create a checkout with a selected plan's details.

Step 1: Set up the Wix Headless environment

Before using the SDK, there are a few things you need to set up on your Wix account and in your external site or app's coding environment.

To set up the Wix Headless environment, follow these steps:

  1. If you haven't already, create a project.
    When prompted to add functionalities to your new project, select Pricing Plans.
  2. Set up authorization for your site by creating and configuring an OAuth app.
  3. Set a domain to be used by Wix-managed pages.
  4. Set a domain that Wix can redirect to after completing a Wix-managed process.
  5. Install the API client and relevant SDK module packages by running the following commands: For NPM:
    Copy
    1
    npm install @wix/sdk
    2
    npm install @wix/pricing-plans
    3
    npm install @wix/redirects
    For Yarn:
    Copy
    1
    yarn add @wix/sdk
    2
    yarn add @wix/pricing-plans
    3
    yarn add @wix/redirects
  6. Install the react package to handle UI rendering and the js-cookie package to handle session cookies. Run the following commands: For NPM:
    Copy
    1
    npm install react
    2
    npm install js-cookie
    For Yarn:
    Copy
    1
    yarn add react
    2
    yarn add js-cookie

Step 2: Import the SDK modules and create an SDK client

The next step is to set up your code file to run the SDK functions. To set up the code file, follow these steps:

  1. Add the following import statements to the top of your code file:
    Copy
    1
    import Cookies from 'js-cookie';
    2
    import { useEffect, useState } from 'react';
    3
    import { createClient, OAuthStrategy } from '@wix/sdk';
    4
    import { plans } from '@wix/pricing-plans';
    5
    import { redirects } from '@wix/redirects';
  2. Create an SDK client by adding the following code to your code file. Replace the value for clientId with your OAuth app's client ID. You can find the ID in your project's Headless Settings menu.
    The value for tokens is the 'session' cookie on the site visitor's browser. It's used to make calls to the Wix API. This way, you can maintain previous visitor sessions. For information about managing session cookies, see Session Token Management.
    Copy
    1
    const myWixClient = createClient({
    2
    modules: { plans, redirects }, // <-- {{ This is an example. Add the modules needed for your tutorial (and remove this comment :) ).}}
    3
    auth: OAuthStrategy({ clientId: `<YOUR-CLIENT-ID>` }),
    4
    tokens: JSON.parse(Cookies.get('session') || '{"accessToken": {}, "refreshToken": {}}'),
    5
    });

Step 3: Create a React component and state variables

The logic for our pricing plans flow is contained in a React component called Subscriptions. To create the component, follow these steps:

  1. Add the following function component to your code file:
Copy
1
export default function Subscriptions() {
  1. Define the state variable for the component by adding the following code to the Subscriptions component.
    The planList variable stores the list of pricing plans from your Wix Pricing Plans.
Copy
1
const [planList, setPlanList] = useState([]);

Step 4: Fetch your Wix Pricing Plans items

Define a function to fetch your pricing plans by adding the following code to the Subscriptions component. This function runs when the component is first rendered. The function uses the queryPublicPlans() function from the SDK's Plans module to query your pricing plans.

Copy
1
async function fetchPlans() {
2
const planList = await myWixClient.plans.queryPublicPlans().find();
3
setPlanList(planList.items);
4
}

Add the following code to the Subscriptions component to run the fetchPlans() function after the component is rendered. This ensures that the data is retrieved when the component mounts.

Copy
1
useEffect(() => {
2
fetchPlans();
3
}, []);

Step 5: Map the items to your UI

Create a list in your UI, and map each plan to a list item. Add an onClick() event handler to each plan. When a visitor clicks a plan, call the createRedirect() function that redirects a visitor to a checkout page with the selected plan's details.

Copy
1
return (
2
<div>
3
<h2>Choose Plan:</h2>
4
{planList.map((plan) => {
5
return (
6
<div key={plan._id} onClick={() => createRedirect(plan)}>
7
{plan.name}
8
</div>
9
);
10
})}
11
</div>
12
);

Step 6: Create a checkout with a selected plan's details

In the createRedirect() function, call the createRedirectSession() function with the selected plan to get a checkout URL. Use this URL to temporarily redirect the visitor to a Wix checkout page. After checkout, the visitor is redirected to the URL defined in the postFlowUrl callback property.

Note: When redirecting from Wix to an external site, Wix validates that the provided URL is registered under an allowed domain for the given client ID. Therefore, you must add your domain to the OAuth app.

Copy
1
async function createRedirect(plan) {
2
const redirect = await myWixClient.redirects.createRedirectSession({
3
paidPlansCheckout: { planId: plan._id },
4
callbacks: { postFlowUrl: window.location.href },
5
});
6
window.location = redirect.redirectSession.fullUrl;
7
}

Complete code example

You can use the following full code example as a starting point for developing your own site:

Copy
1
import Cookies from 'js-cookie';
2
import { useEffect, useState } from 'react';
3
import styles from '@/styles/pages.module.css';
4
5
import { createClient, OAuthStrategy } from '@wix/sdk';
6
import { plans } from '@wix/pricing-plans';
7
import { redirects } from '@wix/redirects';
8
9
const myWixClient = createClient({
10
modules: { plans, redirects },
11
auth: OAuthStrategy({
12
clientId: `10c1663b-2cdf-47c5-a3ef-30c2e8543849`,
13
tokens: JSON.parse(Cookies.get('session') || null),
14
}),
15
});
16
17
export default function Subscriptions() {
18
const [planList, setPlanList] = useState([]);
19
20
async function fetchPlans() {
21
const planList = await myWixClient.plans.queryPublicPlans().find();
22
setPlanList(planList.items);
23
}
24
25
async function createRedirect(plan) {
26
const redirect = await myWixClient.redirects.createRedirectSession({
27
paidPlansCheckout: { planId: plan._id },
28
callbacks: { postFlowUrl: window.location.href },
29
});
30
window.location = redirect.redirectSession.fullUrl;
31
}
32
33
useEffect(() => {
34
fetchPlans();
35
}, []);
36
37
return (
38
<div className={styles.grid}>
39
<div>
40
<h2>Choose Plan:</h2>
41
{planList.map((plan) => {
42
return (
43
<div
44
className={styles.card}
45
key={plan._id}
46
onClick={() => createRedirect(plan)}
47
>
48
{plan.name}
49
</div>
50
);
51
})}
52
</div>
53
</div>
54
);
55
}
Was this helpful?
Yes
No