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:
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:
react
package to handle UI rendering and the js-cookie
package to handle session cookies. Run the following commands:
For NPM:
The next step is to set up your code file to run the SDK functions. To set up the code file, follow these steps:
clientId
with your OAuth app's client ID. You can find the ID in your project's Headless Settings menu.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.
The logic for our pricing plans flow is contained in a React component called Subscriptions
. To create the component, follow these steps:
Subscriptions
component.planList
variable stores the list of pricing plans from your Wix Pricing Plans.
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.
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.
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.
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.
You can use the following full code example as a starting point for developing your own site: