With this plugin you can easily integrate Stripe into Payload. Simply provide your Stripe credentials and this plugin will open up a two-way communication channel between the two platforms. This enables you to easily sync data back and forth, as well as proxy the Stripe REST API through Payload's Access Control. Use this plugin to completely offload billing to Stripe and retain full control over your application's data.
For example, you might be building an e-commerce or SaaS application, where you have a products or a plans collection that requires either a one-time payment or a subscription. You can to tie each of these products to Stripe, then easily subscribe to billing-related events to perform your application's business logic, such as active purchases or subscription cancellations.
To build a checkout flow on your front-end you can either use Stripe Checkout, or you can also build a completely custom checkout experience from scratch using Stripe Web Elements. Then to build fully custom, secure customer dashboards, you can leverage Payload's Access Control to restrict access to your Stripe resources so your users never have to leave your site to manage their accounts.
The beauty of this plugin is the entirety of your application's content and business logic can be handled in Payload while Stripe handles solely the billing and payment processing. You can build a completely proprietary application that is endlessly customizable and extendable, on APIs and databases that you own. Hosted services like Shopify or BigCommerce might fracture your application's content then charge you for access.
Install the plugin using any JavaScript package manager like pnpm, npm, or Yarn:
In the plugins array of your Payload Config, call the plugin with options:
Option | Type | Default | Description |
|---|---|---|---|
| string | | Your Stripe secret key |
| string | | Your Stripe webhook endpoint secret |
| boolean | | When |
| object or function | | Either a function to handle all webhooks events, or an object of Stripe webhook handlers, keyed to the name of the event |
| array | | An array of sync configs |
| boolean | | When |
* An asterisk denotes that a property is required.
The following custom endpoints are automatically opened for you:
Endpoint | Method | Description |
|---|---|---|
| | Proxies the Stripe REST API behind Payload access control and returns the result. See the REST Proxy section for more details. |
| | Handles all Stripe webhook events |
If rest is true, proxies the Stripe REST API behind Payload access control and returns the result. This flag should only be used for local development, see the security note below for more information.
If you need to proxy the API server-side, use the stripeProxy function.
Stripe webhooks are used to sync from Stripe to Payload. Webhooks listen for events on your Stripe account so you can trigger reactions to them. Follow the steps below to enable webhooks.
Development:
stripe loginstripe listen --forward-to localhost:3000/api/stripe/webhooks.env file as STRIPE_WEBHOOKS_ENDPOINT_SECRETProduction:
YOUR_DOMAIN_NAME/api/stripe/webhooks as the "Webhook Endpoint URL".env file as STRIPE_WEBHOOKS_ENDPOINT_SECRETwebhooks portion of this plugin's config:For a full list of available webhooks, see here.
On the server you should interface with Stripe directly using the stripe npm module. That might look something like this:
Alternatively, you can interface with the Stripe using the stripeProxy, which is exactly what the /api/stripe/rest endpoint does behind-the-scenes. Here's the same example as above, but piped through the proxy:
This option will setup a basic sync between Payload collections and Stripe resources for you automatically. It will create all the necessary hooks and webhooks handlers, so the only thing you have to do is map your Payload fields to their corresponding Stripe properties. As documents are created, updated, and deleted from either Stripe or Payload, the changes are reflected on either side.
Using sync will do the following:
stripeID read-only field on each collection, this is a field generated by Stripe and used as a cross-referenceskipSync read-only flag on each collection to prevent infinite syncs when hooks trigger webhooksbeforeValidate: createNewInStripebeforeChange: syncExistingWithStripeafterDelete: deleteFromStripeSTRIPE_TYPE.created: handleCreatedOrUpdatedSTRIPE_TYPE.updated: handleCreatedOrUpdatedSTRIPE_TYPE.deleted: handleDeletedAll types can be directly imported: