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:
- Set up the Wix Headless environment.
- Import the SDK modules and create an SDK client.
- Query your Wix Pricing Plans items.
- Map the items to your UI.
- 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:
- If you haven't already, create a project.
When prompted to add functionalities to your new project, select Pricing Plans. - Set up authorization for your site by creating and configuring an OAuth app.
- Set a domain to be used by Wix-managed pages.
- Set a domain that Wix can redirect to after completing a Wix-managed process.
- Install the API client and relevant SDK module packages by running the following commands:
For NPM:
For Yarn:Copy1npm install @wix/sdk2npm install @wix/pricing-plans3npm install @wix/redirectsCopy1yarn add @wix/sdk2yarn add @wix/pricing-plans3yarn add @wix/redirects
- Install the
react
package to handle UI rendering and thejs-cookie
package to handle session cookies. Run the following commands: For NPM:For Yarn:Copy1npm install react2npm install js-cookieCopy1yarn add react2yarn 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:
- Add the following import statements to the top of your code file:
Copy1import Cookies from 'js-cookie';2import { useEffect, useState } from 'react';3import { createClient, OAuthStrategy } from '@wix/sdk';4import { plans } from '@wix/pricing-plans';5import { redirects } from '@wix/redirects';
- 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 fortokens
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.Copy1const myWixClient = createClient({2modules: { plans, redirects }, // <-- {{ This is an example. Add the modules needed for your tutorial (and remove this comment :) ).}}3auth: OAuthStrategy({ clientId: `<YOUR-CLIENT-ID>` }),4tokens: 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:
- Add the following function component to your code file:
1export default function Subscriptions() {
- Define the state variable for the component by adding the following code to the
Subscriptions
component.
TheplanList
variable stores the list of pricing plans from your Wix Pricing Plans.
1const [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.
1async 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.
1useEffect(() => {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.
1return (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.
1async 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:
1import Cookies from 'js-cookie';2import { useEffect, useState } from 'react';3import styles from '@/styles/pages.module.css';4
5import { createClient, OAuthStrategy } from '@wix/sdk';6import { plans } from '@wix/pricing-plans';7import { redirects } from '@wix/redirects';8
9const 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
17export 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 <div44 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}