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..."