Skip to content

Sync your Stripe data to your Postgres database

License

Notifications You must be signed in to change notification settings

FriendlyCaptcha/friendly-stripe-sync

Repository files navigation

friendly-stripe-sync

Load and periodically synchronize data from Stripe to a Postgres database.

How it works

  1. Creates a schema called stripe in the postgres database with all the needed tables
  2. Loads the initial data from Stripe and populates the tables
  3. Periodically calls the /events endpoint of Stripe to load changes and update the database

Supported Data Types

friendly-stripe-sync currently supports products, prices, customers, subscriptions, subscription items, and coupons.
The data is updated using the following events:

  • customer.created
  • customer.updated
  • customer.deleted
  • product.created
  • product.updated
  • product.deleted
  • customer.subscription.created
  • customer.subscription.updated
  • customer.subscription.paused
  • customer.subscription.deleted
  • price.created
  • price.updated
  • price.deleted

Installation

go install github.com/FriendlyCaptcha/friendly-stripe-sync@latest

Usage

Setup Database

Before running the friendly-stripe-sync service you will want to migrate your database to create the necessary tables.

# Help on all migration-related commands for Postgres
friendly-stripe-sync migrate postgres --help

# List all available migrations
friendly-stripe-sync migrate postgres list

# List the current migration version
friendly-stripe-sync migrate postgres version

# Run all migrations to get up to the latest version
friendly-stripe-sync migrate postgres up

Load the existing Stripe data

To load the initial dataset (required if you have data older than 30 days in your Stripe account). 2

# --purge will delete all existing data before loading the data from Stripe
friendly-stripe-sync load [--purge]

Synchronize once

To load recent changes since the initial load you can synchronize once. This will work for changes that are up to 30 days old.

friendly-stripe-sync sync

Synchronize and watch

If you want to keep your database up to date you should consider running the synchronization periodically.
This will load the initial data if it hasn't been loaded already and will then synchronize periodically (see configuration).

# -i defines how often in seconds it will load changes from Stripe
friendly-stripe-sync watch [-i 60]

Configuration

The app looks for a configuration file called .friendly-stripe-sync.yml in the working directory.

# Sets the log devel to debug and adds more info to the log entires
debug: false
# Makes the log entries human readable instead of JSON
development: false

stripe:
  # You Stripe API key
  api_key: "..."

# Database configuration
postgres:
  host: "localhost"
  port: 5432
  user: "postgres"
  password: ""
  dbname: "friendlystripe"
  sslmode: "disable"

# Exclude certain fields from being stored in the database. By default no fields are excluded.
stripe_sync:
  excluded_fields:
    # Currently only the following fields can be excluded:
    - "customer.address"
    - "customer.phone"

You can also set config fields using environment variables. The app will look for environment variables starting with FSS_. For example FSS_POSTGRES__PASSWORD=1234567890 will overwrite the postgres.password field in the YAML config.