See a live version of this implementation at the subject.network planet store.
This is my project to connect BTCPay Server, SendGrid, and simple database management. The first two versions of this were a shell script and webhook configuration, this is a Python- and Docker-ized version with additional functionality, including automatic listings generated for BTCPay (but you'll still need to manually paste them in).
This script will allow you to collect payment from BTCPay, and automatically trigger an email with a planet code to the email submitted by the customer after payment is confirmed. It will import an L2 planet code CSV from Bridge and deduplicate any existing entries. This configuration assumes you are running the container on the host device running the Docker container, but you don't have to.
You need to have a SendGrid account with a validated email, API key, and dynamic template -- you get 100 emails/day with a free account. You will also need to provide it with a CSV of planets & codes, but a test set is included. Delete db.sq3
to clear the state.
You need docker & compose installed, and a BTCPayServer store installed and configured.
Copy the variables from the compose file into a .env
file and assign them:
API_KEY
-- the BTCPay API keySG_API
-- SendGrid API keyTEMPLATE_ID
-- SendGrid dynamic template IDURL
-- URL for BTCPay that is accessible from inside container (http://172.20.0.1:80
or whatever the docker gateway IP is should work)S3_URL
-- S3 API endpointS3_ACCESS
-- S3 access keyS3_SECRET
-- S3 secret keyS3_BUCKET
-- S3 bucket nameGIFT_AUTH
-- Password used to send gift planets
Note that I've had issues with Litestream behaving badly on S3 providers other than DigitalOcean (referral).
The container will use the ./data
directory to store the DB. Copy the template DB into this directory and name it db.sq3
.
After your variables are set, simply run docker-compose up -d
.
When app runs, it will look for a CSV named ./data/planets.csv
and import it into a DB, then mark it as imported. It will not re-import a CSV if it has 'IMPORTED_DATA' appended to it.
You can easily query the sales stats of the DB by GETing the /
route (root path).
At the bottom of your store's page, expand the 'Notification URL Callbacks' modal and enter http://sales-hook:5000
or http://172.20.0.1:5000
as the URL (or whatever the address is as reachable from BTCPay).
This version (v3) includes functions to automatically generate BTCPay listings in text format and upload SVGs of the sigils to your S3 bucket.
This function is not exposed so you'll need to get a REPL shell and run the following commands:
docker exec -it sales-hook /bin/bash
cd app
python
import app
app.inventory_gen(50)
This will generate 50 individual planet listings and take the planets out of the random pool (which is the default). The listings will be output to ./data/inventory/inv.txt
.
You can paste the content of the text into the store's listings:
This will BCC my email on a sale! It is hardcoded at the moment, edit the function on line 266 in app.py
before you run this.