towerops/setup_stripe_production.sh
Graham McIntire 13631595a7
fix: handle spaces in JSON when extracting Stripe IDs
- Update grep patterns to match JSON with spaces after colons
- Fixes false 'Error creating meter' message when meter created successfully
- Applies to meter ID and price ID extraction
2026-03-06 11:08:07 -06:00

148 lines
4.6 KiB
Bash
Executable file

#!/bin/bash
set -e
echo "=============================================="
echo "Stripe Production Billing Setup"
echo "=============================================="
echo ""
echo "This script will create:"
echo " 1. A billing meter for device usage tracking"
echo " 2. A metered price ($1/device/month)"
echo ""
# Get Stripe live API key
read -r -p "Enter your Stripe LIVE secret key (sk_live_...): " STRIPE_KEY
if [[ ! $STRIPE_KEY =~ ^sk_live_ ]]; then
echo "Error: API key must start with 'sk_live_' for production"
exit 1
fi
echo ""
echo "Fetching your products..."
PRODUCTS=$(curl -s https://api.stripe.com/v1/products \
-u "${STRIPE_KEY}:" \
-H "Stripe-Version: 2024-11-20.acacia")
echo ""
echo "Available products:"
echo "$PRODUCTS" | python3 -c "
import sys, json
data = json.load(sys.stdin)
for i, p in enumerate(data['data'], 1):
print(f\"{i}. {p['name']} (ID: {p['id']})\")
" 2>/dev/null || echo "$PRODUCTS" | grep -o '"id":"prod_[^"]*"' | cut -d'"' -f4
echo ""
read -r -p "Enter your product ID (prod_...): " PRODUCT_ID
if [[ ! $PRODUCT_ID =~ ^prod_ ]]; then
echo "Error: Product ID must start with 'prod_'"
exit 1
fi
echo ""
echo "=============================================="
echo "Step 1: Checking for existing billing meter..."
echo "=============================================="
# First, check if a meter already exists
EXISTING_METERS=$(curl -s https://api.stripe.com/v1/billing/meters \
-u "${STRIPE_KEY}:" \
-H "Stripe-Version: 2024-11-20.acacia")
METER_ID=$(echo "$EXISTING_METERS" | grep -o '"event_name": "devices_monitored"' -B 20 | grep -o '"id": "mtr_[^"]*"' | head -1 | cut -d'"' -f4)
if [[ -n "$METER_ID" ]]; then
echo "✓ Found existing billing meter!"
echo " Meter ID: $METER_ID"
echo " Event name: devices_monitored"
else
echo "No existing meter found. Creating new one..."
METER_RESPONSE=$(curl -s https://api.stripe.com/v1/billing/meters \
-u "${STRIPE_KEY}:" \
-H "Stripe-Version: 2024-11-20.acacia" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "display_name=Devices Monitored" \
-d "event_name=devices_monitored" \
-d "default_aggregation[formula]=sum" \
-d "value_settings[event_payload_key]=value")
METER_ID=$(echo "$METER_RESPONSE" | grep -o '"id": "mtr_[^"]*"' | head -1 | cut -d'"' -f4)
if [[ -z "$METER_ID" ]]; then
echo "Error creating meter:"
echo "$METER_RESPONSE"
exit 1
fi
echo "✓ Billing meter created successfully!"
echo " Meter ID: $METER_ID"
echo " Display name: Devices Monitored"
echo " Event name: devices_monitored"
fi
echo ""
echo "=============================================="
echo "Step 2: Creating metered price..."
echo "=============================================="
PRICE_RESPONSE=$(curl -s https://api.stripe.com/v1/prices \
-u "${STRIPE_KEY}:" \
-H "Stripe-Version: 2024-11-20.acacia" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "product=${PRODUCT_ID}" \
-d "currency=usd" \
-d "recurring[interval]=month" \
-d "recurring[usage_type]=metered" \
-d "recurring[meter]=${METER_ID}" \
-d "billing_scheme=per_unit" \
-d "unit_amount=100" \
-d "nickname=Per Device Monthly")
PRICE_ID=$(echo "$PRICE_RESPONSE" | grep -o '"id": "price_[^"]*"' | head -1 | cut -d'"' -f4)
if [[ -z "$PRICE_ID" ]]; then
echo "Error creating price:"
echo "$PRICE_RESPONSE"
exit 1
fi
echo "✓ Price created successfully!"
echo " Price ID: $PRICE_ID"
echo " Amount: \$1.00/device/month"
echo ""
echo "=============================================="
echo "SETUP COMPLETE!"
echo "=============================================="
echo ""
echo "Next steps:"
echo ""
echo "1. Set up your webhook endpoint in Stripe Dashboard:"
echo " URL: https://towerops.net/api/v1/webhooks/stripe"
echo " Events to subscribe to:"
echo " - customer.subscription.created"
echo " - customer.subscription.updated"
echo " - customer.subscription.deleted"
echo " - invoice.payment_succeeded"
echo " - invoice.payment_failed"
echo " - customer.updated"
echo ""
echo "2. Get your webhook signing secret (whsec_...) from Stripe"
echo ""
echo "3. Create your Kubernetes secret:"
echo ""
echo " kubectl create secret generic towerops-billing \\"
echo " --from-literal=STRIPE_SECRET_KEY=\"${STRIPE_KEY}\" \\"
echo " --from-literal=STRIPE_WEBHOOK_SECRET=\"whsec_YOUR_WEBHOOK_SECRET\" \\"
echo " --from-literal=STRIPE_PRICE_ID=\"${PRICE_ID}\" \\"
echo " --from-literal=STRIPE_METER_ID=\"${METER_ID}\" \\"
echo " -n towerops"
echo ""
echo "4. Restart your deployment:"
echo " kubectl rollout restart deployment/towerops -n towerops"
echo ""
echo "=============================================="
echo ""