From 3e721b9395f8d1552b2412854db04b6fc72192ca Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Fri, 6 Mar 2026 10:58:52 -0600 Subject: [PATCH] fix: check for existing billing meter before creating - First check if a meter with 'devices_monitored' event exists - Reuse existing meter ID if found - Only create new meter if none exists - Fixes error when meter already exists in Stripe account --- setup_stripe_production.sh | 51 ++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/setup_stripe_production.sh b/setup_stripe_production.sh index 06003efe..87048902 100755 --- a/setup_stripe_production.sh +++ b/setup_stripe_production.sh @@ -43,31 +43,46 @@ fi echo "" echo "==============================================" -echo "Step 1: Creating billing meter..." +echo "Step 1: Checking for existing billing meter..." echo "==============================================" -METER_RESPONSE=$(curl -s https://api.stripe.com/v1/billing/meters \ +# 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" \ - -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") + -H "Stripe-Version: 2024-11-20.acacia") -METER_ID=$(echo "$METER_RESPONSE" | grep -o '"id":"mtr_[^"]*"' | head -1 | cut -d'"' -f4) +METER_ID=$(echo "$EXISTING_METERS" | grep -o '"event_name":"devices_monitored"' -B 20 | grep -o '"id":"mtr_[^"]*"' | head -1 | cut -d'"' -f4) -if [[ -z "$METER_ID" ]]; then - echo "Error creating meter:" - echo "$METER_RESPONSE" - exit 1 +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 "✓ Billing meter created successfully!" -echo " Meter ID: $METER_ID" -echo " Display name: Devices Monitored" -echo " Event name: devices_monitored" - echo "" echo "==============================================" echo "Step 2: Creating metered price..."