Overview
The Stripe integration is a lightweight webhook server that automatically creates and renews Cryptlex licenses when Stripe payments are processed. It handles invoice payments, checkout sessions, and customer creation events to ensure seamless license management. Key capabilities:- Create licenses when subscriptions start or one-time purchases complete
- Automatically renew licenses on recurring subscription payments
- Sync customer data between Stripe and Cryptlex
- Verify webhook authenticity using Stripe webhook signatures
Supported Webhook Events
The integration handles three Stripe webhook event types:invoice.paid
invoice.paid
Triggered when a Stripe invoice is successfully paid.Actions:
- New subscription (
billing_reason: subscription_create): Creates a new user and license in Cryptlex - Recurring payment (
billing_reason: subscription_cycle): Renews the existing license and extends expiry date
subscription_id to link future renewals.checkout.session.completed
checkout.session.completed
Triggered when a Stripe Checkout session is successfully completed.Actions:
- Creates a new user (or finds existing) based on customer email
- Creates a new license linked to the Stripe subscription
- Stores subscription ID in license metadata for future renewals
customer.created
customer.created
Triggered when a new customer is created in Stripe.Actions:
- Creates or updates a user in Cryptlex with the customer’s email and name
- Returns the Cryptlex user ID
Environment Variables
Set these environment variables in your hosting environment:| Variable | Required | Description |
|---|---|---|
STRIPE_WEBHOOK_SECRET | Yes | Your Stripe webhook signing secret, used to verify webhook authenticity |
CRYPTLEX_PRODUCT_ID | Yes | The Cryptlex Product ID for licenses to create or renew |
CRYPTLEX_ACCESS_TOKEN | Yes | Cryptlex API access token with license:read, license:write, user:read, user:write permissions |
CRYPTLEX_WEB_API_BASE_URL | Yes | Base URL of the Cryptlex Web API |
You can find your Stripe webhook secret in the Stripe Dashboard under Developers > Webhooks. Each webhook endpoint has its own signing secret.
Setup Instructions
1. Deploy the Integration
Choose your deployment method: AWS Lambda: Review the providedaws.yml GitHub Actions workflow for automated deployments.
Node.js / Docker:
Use the provided Dockerfile to run in any containerized environment.
2. Configure Stripe Webhook
- In your Stripe Dashboard, go to Developers > Webhooks
- Click Add endpoint
- Set the endpoint URL to your deployed server:
https://your-domain.com/v1 - Select the following events to listen to:
invoice.paidcheckout.session.completedcustomer.created
- Click Add endpoint
- Copy the Signing secret and set it as
STRIPE_WEBHOOK_SECRETin your environment
3. Set Environment Variables
Configure all required environment variables in your hosting platform:Webhook Signature Verification
The integration uses Stripe’s official webhook verification to ensure requests are authentic:- Stripe signs each webhook with your endpoint’s secret
- The signature is sent in the
stripe-signatureheader - The integration verifies the signature matches the raw request body
- Only verified events are processed
Stripe’s
constructEventAsync will throw an error if signature verification fails, automatically rejecting tampered requests.Example Workflows
New Subscription
When a customer subscribes via Stripe:- Event:
invoice.paidwithbilling_reason: subscription_create - Handler:
handleInvoicePaid - Actions:
- Extract customer email from invoice
- Create or find user in Cryptlex
- Create new license with subscription ID in metadata
- Result: Customer receives license key
Recurring Payment
When a subscription renews:- Event:
invoice.paidwithbilling_reason: subscription_cycle - Handler:
handleInvoicePaid - Actions:
- Find license by subscription ID metadata
- Renew license to extend expiry date
- Result: License remains valid for another billing period
Checkout Session
When a customer completes checkout:- Event:
checkout.session.completed - Handler:
handleCheckoutSessionFlow - Actions:
- Extract customer email and name from session
- Create user and license
- Store subscription ID for future renewals
- Result: New license created and ready to use
FAQ
What happens if a webhook fails?
What happens if a webhook fails?
Stripe automatically retries failed webhooks for up to 3 days. Check your webhook logs in the Stripe Dashboard to see delivery attempts and errors.
Can I use this for one-time purchases?
Can I use this for one-time purchases?
Yes! The
checkout.session.completed event handles both subscription and one-time purchases. For one-time purchases without subscriptions, the subscription ID will be the session ID.How do I test webhooks locally?
How do I test webhooks locally?
Use the Stripe CLI to forward webhooks to localhost:
What if a customer email already exists?
What if a customer email already exists?
The integration uses
insertUser which handles duplicates gracefully. If a user with the same email exists, it returns the existing user ID instead of creating a duplicate.Can I customize which Product ID is used?
Can I customize which Product ID is used?
Currently, the integration uses a single
CRYPTLEX_PRODUCT_ID for all licenses. For multiple products, you can modify the handlers to extract product ID from Stripe metadata.