Pricing Plans Quick Start

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
    For Yarn:
    Copy
  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
    For Yarn:
    Copy

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
  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

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. 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

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

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

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

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

Complete code example

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

Copy
Was this helpful?
Yes
No