Correlate
cor·re·late, verb | ˈkôrəˌlāt | to have a mutual relationship or connection, in which one thing affects or depends on another.
Correlate is a service that listens for webhook events and, in response to those events, sends notification emails about orders. It can optionally store order information in a database, allowing you to query and make connections between the data later on (or, one could say correlations). e.g. Which product variant has the most sales? Which day typically has the most sales?
Features
- Webhook Handling: Processes Stripe checkout session events
- Checkout Session Creation: Creates Stripe checkout sessions for payment processing
- Email Notifications: Sends confirmation emails for successful orders
- Optional Database Integration: Stores order information using a MySQL database (can be disabled)
- Containerized Deployment: Easy deployment with Docker
Installation
Prerequisites
- MSRV is Rust 1.82
- Stripe account with webhook configuration
- MariaDB/MySQL database (optional, only if database integration is enabled)
Using Cargo
# Clone the repository
# Build the project (with database support enabled by default)
# Build without database support
# Run the application
Using Docker
# Build the Docker image (with database support by default)
# Build the Docker image without database support
# Run the container with database support
# Run the container without database support
Dependencies
The project uses the following key dependencies:
- rocket: Web framework for handling HTTP requests and responses
- lettre: Email sending library
- tera: Template engine for generating email content
- validator: Data validation library
- ureq: HTTP client for making API calls to Stripe
- thiserror: Error handling utilities
Optional Dependencies
These dependencies are only required if database integration is enabled:
- diesel: ORM and query builder
- diesel_migrations: Database migration tools
- rocket_db_pools: Database connection pooling
- mysqlclient-sys: MySQL client library
Configuration
Feature Flags
| Feature Flag | Description | Default |
|---|---|---|
database |
Enables database integration | Enabled |
To build without database support:
To explicitly enable database support:
Environment Variables
| Variable | Description | Default | Required |
|---|---|---|---|
CORRELATE_STRIPE_SECRET |
Your Stripe API secret key | - | Yes |
CORRELATE_EMAIL_SENDER |
Email address to send notifications from | - | Yes |
CORRELATE_EMAIL_RECIPIENT |
Email address to send notifications to | - | Yes |
CORRELATE_EMAIL_SMTP_RELAY_HOST |
SMTP server hostname | - | No |
CORRELATE_EMAIL_SMTP_RELAY_PORT |
SMTP server port | 25 | No |
CORRELATE_EMAIL_SMTP_USERNAME |
SMTP username (if authentication required) | - | No |
CORRELATE_EMAIL_SMTP_PASSWORD |
SMTP password (if authentication required) | - | No |
CORRELATE_PRODUCTS |
TOML-formatted array of product variants for checkout (see example below) | - | Yes |
CORRELATE_DATABASES |
MySQL connection string (only needed when database feature is enabled) | - | Only with database feature |
Rocket.toml Configuration
You can also configure the application using a Rocket.toml file:
[]
= "sk_test_your_test_key"
[]
= "noreply@example.com"
= "orders@example.com"
= "smtp.example.com"
= 2525
= "username"
= "password"
# Product configuration for checkout session creation
= [
{ = "Product Name", = "product-sku", = "price_12345" },
{ = "Another Product", = "another-sku", = "price_67890" }
]
# Only needed when database feature is enabled
[]
= { = "mysql://user:password@hostname:3306/database_name" }
Usage
Setting Up Stripe Webhooks
- Go to the Stripe Dashboard
- Create a new webhook endpoint with your server URL (e.g.,
https://your-domain.com/) - Select the
checkout.session.completedevent to listen for - Copy the webhook signing secret to use with the application
API Endpoints
GET /: Returns application informationPOST /: Webhook endpoint for Stripe eventsPOST /checkout: Endpoint to create a new Stripe checkout session
Example: Creating a Checkout Session
To create a new checkout session for payment processing:
- Configure products in your
Rocket.tomlfile or environment variables (see Configuration section) - Send a POST request to the
/checkoutendpoint with the following JSON payload:
- The API will validate the request, look up the Stripe price ID for each product, and create a checkout session via Stripe's API
- If successful, it will return a status 200 with the Stripe checkout URL
Note: The name and sku in your request must match entries in your product configuration. The service will use these to find the corresponding Stripe price ID.
Example: Processing a Webhook
When a successful checkout is completed in Stripe, the webhook will:
- Receive the event data
- Validate the payment was successful
- Retrieve order information
- Send a confirmation email to the customer
- Return a success status
Database Schema
When the database feature is enabled, the application uses the following tables:
stripe_customers: Stores customer information from Stripe eventsusers: Basic user account information (if user accounts are enabled)
Note: The database integration is optional. If you don't want to store order information, you can build the application without database support using
--no-default-features.
Development
Setting Up Development Environment
With Database Support (Default)
To create a local mysql/mariadb database named correlate you would use diesel to run the commands below:
# Install development dependencies
# Set up the database
# Set up the database without TLS support
# Run in development mode with database support
Without Database Support
If you don't need database integration:
# Run in development mode without database support
Running Tests
License
This project is licensed under the MIT License - see the LICENSE file for details.