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
This commit is contained in:
parent
d3aa602ba4
commit
3e721b9395
1 changed files with 33 additions and 18 deletions
|
|
@ -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..."
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue