Ecommerce Plugin
Basic Usage
In the plugins array of your Payload Config, call the plugin with:
Options
Option | Type | Description |
|---|---|---|
| | Configuration to override the default access control, use this when checking for roles or multi tenancy. More |
| | Configuration for addresses collection and supported fields. More |
| | Configuration for carts collection. More |
| | Supported currencies by the store. More |
| | Used to provide the customers slug. More |
| | Enable inventory tracking within Payload. Defaults to |
| | Configuring payments and supported payment methods. More |
| | Configuration for products, variants collections and more. More |
| | Configuration for orders collection. More |
| | Configuration for transactions collection. More |
Note that the fields in overrides take a function that receives the default fields and returns an array of fields. This allows you to add fields to the collection.
Access
The plugin requires access control functions in order to restrict permissions to certain collections or fields. You can override these functions by providing your own in the access option.
Option | Type | Description |
|---|---|---|
| | Authenticated access only, provided by default. |
| | Public access, provided by default. |
| | Limited to only admin users. |
| | Limited to only admin users, specifically for Field level access control. |
| | Is the owner of the document via the |
| | The document is published or user is admin. |
| | Limited to customers only, specifically for Field level access control. |
The default access control functions are:
authenticatedOnly
Access control to check if the user is authenticated. By default the following is provided:
publicAccess
Access control to allow public access. By default the following is provided:
adminOnly
Access control to check if the user has admin permissions.
Example:
adminOnlyFieldAccess
Field level access control to check if the user has admin permissions.
Example:
adminOrCustomerOwner
Access control to check if the user has admin permissions or is the owner of the document via the customer field.
Example:
adminOrPublishedStatus
Access control to check if the user has admin permissions or if the document is published.
Example:
customerOnlyFieldAccess
Field level access control to check if the user has customer permissions.
Example:
Addresses
The addresses option is used to configure the addresses collection and supported fields. Defaults to true which will create an addresses collection with default fields. It also takes an object:
Option | Type | Description |
|---|---|---|
| | A function that is given the |
| | Allows you to override the collection for |
| | An array of supported countries in ISO 3166-1 alpha-2 format. Defaults to all countries. |
You can add your own fields or modify the structure of the existing on in the collection. Example for overriding the default fields:
supportedCountries
The supportedCountries option is an array of country codes in ISO 3166-1 alpha-2 format. This is used to limit the countries that can be selected when creating or updating an address. If not provided, all countries will be supported. Currently used for storing addresses only.
You can import the default list of countries from the plugin:
Carts
The carts option is used to configure the carts collection. Defaults to true which will create a carts collection with default fields. It also takes an object:
Option | Type | Description |
|---|---|---|
| | Allows you to override the collection for |
You can add your own fields or modify the structure of the existing on in the collection. Example for overriding the default fields:
Carts are created when a customer adds their first item to the cart. The cart is then updated as they add or remove items. The cart is linked to a Customer via the customer field. If the user is authenticated, this will be set to their user ID. If the user is not authenticated, this will be null. If the user is not authenticated, the cart ID is stored in local storage and used to fetch the cart on subsequent requests. Access control by default works so that if the user is not authenticated then they can only access carts that have no customer linked to them.
Customers
The customers option is required and is used to provide the customers collection slug. This collection is used to link orders, carts, and addresses to a customer.
Option | Type | Description |
|---|---|---|
| | The slug of the customers collection. |
While it's recommended to use just one collection for customers and your editors, you can use any collection you want for your customers. Just make sure that your access control is checking for the correct collections as well.
Currencies
The currencies option is used to configure the supported currencies by the store. Defaults to true which will support USD. It also takes an object:
Option | Type | Description |
|---|---|---|
| | An array of supported currencies by the store. Defaults to |
| | The default currency code to use for the store. Defaults to the first currency. Must be one of the |
The Currency type is as follows:
For example, to support JYP in addition to USD:
Note that adding a new currency could generate a new schema migration as it adds new prices fields in your products.
We currently support the following currencies out of the box:
USDEURGBP
You can import these from the plugin:
Inventory
The inventory option is used to enable or disable inventory tracking within Payload. It defaults to true. It also takes an object:
Option | Type | Description |
|---|---|---|
| | Override the field name used to track inventory. Defaults to |
For now it's quite rudimentary tracking with no integrations to 3rd party services. It will simply add an inventory field to the variants collection and decrement the inventory when an order is placed.
Payments
The payments option is used to configure payments and supported payment methods.
Option | Type | Description |
|---|---|---|
| | An array of payment method adapters. Currently, only Stripe is supported. More |
Payment adapters
The plugin supports payment adapters to integrate with different payment gateways. Currently, only the Stripe adapter is available. Adapters will provide a client side version as well with slightly different arguments.
Every adapter supports the following arguments in addition to their own:
Argument | Type | Description |
|---|---|---|
| | Human readabale label for this payment adapter. |
| | Use this to override the available fields for the payment adapter type. |
Client side base arguments are the following:
Argument | Type | Description |
|---|---|---|
| | Human readabale label for this payment adapter. |
See the Stripe adapter for an example of client side arguments and the React section for usage.
groupOverrides
The groupOverrides option allows you to customize the fields that are available for a specific payment adapter. It takes a GroupField object with a fields function that receives the default fields and returns an array of fields. These fields are stored in transactions and can be used to collect additional information for the payment method. Stripe, for example, will track the paymentIntentID.
Example for overriding the default fields:
Stripe Adapter
The Stripe adapter is used to integrate with the Stripe payment gateway. It requires a secret key, publishable key, and optionally webhook secret.
Argument | Type | Description |
|---|---|---|
| | Required for communicating with the Stripe API in the backend. |
| | Required for communicating with the Stripe API in the client side. |
| | The webhook secret used to verify incoming webhook requests from Stripe. |
| | An array of webhook handlers to register within Payload's REST API for Stripe to callback. |
| | The Stripe API version to use. See docs. This will be deprecated soon by Stripe's SDK, configure the API version in your Stripe Dashboard. |
| | The application info to pass to Stripe. See docs. |
Stripe webhooks
The webhooks option allows you to register custom webhook handlers for Stripe events. This is useful if you want to handle specific events that are not covered by the default handlers provided by the plugin.
Stripe client side
On the client side, you can use the publishableKey to initialize Stripe and handle payments. The client side version of the adapter only requires the label and publishableKey arguments. Never expose the secretKey or webhookSecret keys on the client side.
Products
The products option is used to configure the products and variants collections. Defaults to true which will create products and variants collections with default fields. It also takes an object:
Option | Type | Description |
|---|---|---|
| | Allows you to override the collection for |
| | Configuration for the variants collection. Defaults to true. More |
| | Customise the validation used for checking products or variants before a transaction is created or a payment can be confirmed. More |
You can add your own fields or modify the structure of the existing on in the collections. Example for overriding the default fields:
Variants
The variants option is used to configure the variants collection. It takes an object:
Option | Type | Description |
|---|---|---|
| | Allows you to override the collection for |
| | Allows you to override the collection for |
| | Allows you to override the collection for |
You can add your own fields or modify the structure of the existing on in the collection. Example for overriding the default fields:
The key differences between these collections:
variantTypesare the types of variants that a product can have, e.g. Size, Color.variantOptionsare the options for each variant type, e.g. Small, Medium, Large for Size.variantsare the actual variants of a product, e.g. a T-Shirt in Size Small and Color Red.
Products validation
We use an addition validation step when creating transactions or confirming payments to ensure that the products and variants being purchased are valid. This is to prevent issues such as purchasing a product that is out of stock or has been deleted.
You can customise this validation by providing your own validation function via the validation option which receives the following arguments:
Option | Type | Description |
|---|---|---|
| | The full currencies configuration provided in the plugin options. |
| | The product being purchased. |
| | The variant being purchased, if a variant was selected for the product otherwise it will be |
| | The quantity being purchased. |
| | The currency code being used for the purchase. |
The function should throw an error if the product or variant is not valid. If the function does not throw an error, the product or variant is considered valid.
The default validation function checks for the following:
- A currency is provided.
- The product or variant has a price in the selected currency.
- The product or variant has enough inventory for the requested quantity.
Orders
The orders option is used to configure the orders collection. Defaults to true which will create an orders collection with default fields. It also takes an object:
Option | Type | Description |
|---|---|---|
| | Allows you to override the collection for |
You can add your own fields or modify the structure of the existing on in the collection. Example for overriding the default fields:
Transactions
The transactions option is used to configure the transactions collection. Defaults to true which will create a transactions collection with default fields. It also takes an object:
Option | Type | Description |
|---|---|---|
| | Allows you to override the collection for |
You can add your own fields or modify the structure of the existing on in the collection. Example for overriding the default fields: