#!/bin/bash set -e echo "==============================================" echo "Stripe 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 API key (test or live) read -r -p "Enter your Stripe secret key (sk_test_... or sk_live_...): " STRIPE_KEY if [[ ! $STRIPE_KEY =~ ^sk_(test|live)_ ]]; then echo "Error: API key must start with 'sk_test_' or 'sk_live_'" exit 1 fi # Show warning if using test mode if [[ $STRIPE_KEY =~ ^sk_test_ ]]; then echo "" echo "⚠️ Using TEST mode - this will create test resources" echo "" else echo "" echo "✓ Using LIVE mode - this will create production resources" echo "" 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" # Determine mode for output if [[ $STRIPE_KEY =~ ^sk_test_ ]]; then MODE="TEST" WEBHOOK_URL="http://localhost:4000/api/v1/webhooks/stripe" else MODE="LIVE" WEBHOOK_URL="https://towerops.net/api/v1/webhooks/stripe" fi echo "" echo "==============================================" echo "SETUP COMPLETE! ($MODE MODE)" echo "==============================================" echo "" echo "Next steps:" echo "" echo "1. Set up your webhook endpoint in Stripe Dashboard ($MODE mode):" echo " URL: $WEBHOOK_URL" 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 ""