| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
set -euo pipefail |
| 38 |
|
| 39 |
DRY_RUN=0 |
| 40 |
ARCHIVE_OLD=0 |
| 41 |
for arg in "$@"; do |
| 42 |
case "$arg" in |
| 43 |
--dry-run) DRY_RUN=1 ;; |
| 44 |
--archive-old-on-tier-products) ARCHIVE_OLD=1 ;; |
| 45 |
*) echo "unknown arg: $arg" >&2; exit 2 ;; |
| 46 |
esac |
| 47 |
done |
| 48 |
|
| 49 |
: "${STRIPE_SECRET_KEY:?STRIPE_SECRET_KEY must be set}" |
| 50 |
|
| 51 |
API="https://api.stripe.com/v1" |
| 52 |
AUTH=(-u "${STRIPE_SECRET_KEY}:") |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
declare -A STANDARD_MONTHLY=( |
| 60 |
[basic]=16 |
| 61 |
[small_files]=24 |
| 62 |
[big_files]=36 |
| 63 |
[everything]=60 |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
declare -A PRODUCT_NAME=( |
| 69 |
[basic]="Creator Tier — Basic" |
| 70 |
[small_files]="Creator Tier — Small Files" |
| 71 |
[big_files]="Creator Tier — Big Files" |
| 72 |
[everything]="Creator Tier — Everything" |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
annual_cents() { |
| 77 |
local monthly_cents=$1 |
| 78 |
|
| 79 |
|
| 80 |
local exact=$(( monthly_cents * 12 * 90 / 100 )) |
| 81 |
|
| 82 |
local remainder=$(( exact % 100 )) |
| 83 |
if (( remainder >= 50 )); then |
| 84 |
echo $(( exact - remainder + 100 )) |
| 85 |
else |
| 86 |
echo $(( exact - remainder )) |
| 87 |
fi |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
stripe_get() { |
| 92 |
local path=$1 |
| 93 |
local query=${2:-} |
| 94 |
curl -sS -G "${AUTH[@]}" "${API}/${path}" ${query:+--data-urlencode "$query"} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
ensure_product() { |
| 100 |
local tier_slug=$1 |
| 101 |
local name=${PRODUCT_NAME[$tier_slug]} |
| 102 |
|
| 103 |
|
| 104 |
local resp |
| 105 |
resp=$(stripe_get "products/search" "query=metadata['mnw_tier']:'${tier_slug}' AND active:'true'") |
| 106 |
local existing |
| 107 |
existing=$(echo "$resp" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d['data'][0]['id'] if d.get('data') else '')") |
| 108 |
if [[ -n "$existing" ]]; then |
| 109 |
echo "$existing" |
| 110 |
return |
| 111 |
fi |
| 112 |
|
| 113 |
if (( DRY_RUN )); then |
| 114 |
echo "prod_DRYRUN_${tier_slug}" |
| 115 |
return |
| 116 |
fi |
| 117 |
|
| 118 |
local created |
| 119 |
created=$(curl -sS "${AUTH[@]}" "${API}/products" \ |
| 120 |
-d "name=${name}" \ |
| 121 |
-d "metadata[mnw_tier]=${tier_slug}") |
| 122 |
echo "$created" | python3 -c "import json,sys; print(json.load(sys.stdin)['id'])" |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
find_price_by_lookup() { |
| 127 |
local key=$1 |
| 128 |
local resp |
| 129 |
resp=$(stripe_get "prices" "lookup_keys[]=${key}") |
| 130 |
echo "$resp" | python3 -c " |
| 131 |
import json,sys |
| 132 |
d = json.load(sys.stdin) |
| 133 |
if d.get('data'): |
| 134 |
p = d['data'][0] |
| 135 |
print(f\"{p['id']}|{p['unit_amount']}|{p.get('active', True)}\") |
| 136 |
" |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
create_price() { |
| 142 |
local product_id=$1 |
| 143 |
local unit_amount_cents=$2 |
| 144 |
local interval=$3 |
| 145 |
local lookup=$4 |
| 146 |
local transfer=$5 |
| 147 |
|
| 148 |
if (( DRY_RUN )); then |
| 149 |
echo "price_DRYRUN_${lookup}" |
| 150 |
return |
| 151 |
fi |
| 152 |
|
| 153 |
local args=( |
| 154 |
-d "currency=usd" |
| 155 |
-d "product=${product_id}" |
| 156 |
-d "unit_amount=${unit_amount_cents}" |
| 157 |
-d "recurring[interval]=${interval}" |
| 158 |
-d "lookup_key=${lookup}" |
| 159 |
) |
| 160 |
if (( transfer )); then |
| 161 |
args+=(-d "transfer_lookup_key=true") |
| 162 |
fi |
| 163 |
|
| 164 |
local resp |
| 165 |
resp=$(curl -sS "${AUTH[@]}" "${API}/prices" "${args[@]}") |
| 166 |
local price_id |
| 167 |
price_id=$(echo "$resp" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('id') or ''); sys.exit(0 if d.get('id') else 1)" || true) |
| 168 |
if [[ -z "$price_id" ]]; then |
| 169 |
echo "FAILED to create price ${lookup}: $resp" >&2 |
| 170 |
exit 1 |
| 171 |
fi |
| 172 |
echo "$price_id" |
| 173 |
} |
| 174 |
|
| 175 |
archive_price() { |
| 176 |
local price_id=$1 |
| 177 |
if (( DRY_RUN )); then |
| 178 |
echo "(dry-run) would archive ${price_id}" >&2 |
| 179 |
return |
| 180 |
fi |
| 181 |
curl -sS "${AUTH[@]}" "${API}/prices/${price_id}" -d "active=false" >/dev/null |
| 182 |
echo "archived ${price_id}" >&2 |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
ensure_price() { |
| 187 |
local product_id=$1 |
| 188 |
local unit_amount_cents=$2 |
| 189 |
local interval=$3 |
| 190 |
local lookup=$4 |
| 191 |
|
| 192 |
local found |
| 193 |
found=$(find_price_by_lookup "$lookup") |
| 194 |
if [[ -n "$found" ]]; then |
| 195 |
local existing_id existing_amount existing_active |
| 196 |
IFS='|' read -r existing_id existing_amount existing_active <<<"$found" |
| 197 |
if [[ "$existing_amount" == "$unit_amount_cents" && "$existing_active" == "True" ]]; then |
| 198 |
echo "$existing_id" |
| 199 |
return |
| 200 |
fi |
| 201 |
|
| 202 |
echo "lookup_key ${lookup}: existing ${existing_id} amount=${existing_amount}, want ${unit_amount_cents}; rotating" >&2 |
| 203 |
local new_id |
| 204 |
new_id=$(create_price "$product_id" "$unit_amount_cents" "$interval" "$lookup" 1) |
| 205 |
archive_price "$existing_id" |
| 206 |
echo "$new_id" |
| 207 |
return |
| 208 |
fi |
| 209 |
|
| 210 |
create_price "$product_id" "$unit_amount_cents" "$interval" "$lookup" 0 |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
upper_slug() { |
| 215 |
echo "$1" | tr '[:lower:]' '[:upper:]' |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
env_var_name() { |
| 220 |
local tier_slug=$1 |
| 221 |
local rate=$2 |
| 222 |
local interval=$3 |
| 223 |
local tier_uc |
| 224 |
tier_uc=$(upper_slug "$tier_slug") |
| 225 |
local prefix="CREATOR_TIER_${tier_uc}" |
| 226 |
case "${rate}_${interval}" in |
| 227 |
standard_monthly) echo "${prefix}_PRICE_ID" ;; |
| 228 |
standard_annual) echo "${prefix}_ANNUAL_PRICE_ID" ;; |
| 229 |
founder_monthly) echo "${prefix}_FOUNDER_PRICE_ID" ;; |
| 230 |
founder_annual) echo "${prefix}_FOUNDER_ANNUAL_PRICE_ID" ;; |
| 231 |
esac |
| 232 |
} |
| 233 |
|
| 234 |
main() { |
| 235 |
local TIERS=(basic small_files big_files everything) |
| 236 |
local env_lines=() |
| 237 |
local wanted_lookup_keys=() |
| 238 |
|
| 239 |
for tier in "${TIERS[@]}"; do |
| 240 |
local std_monthly_cents=$(( STANDARD_MONTHLY[$tier] * 100 )) |
| 241 |
local founder_monthly_cents=$(( std_monthly_cents / 2 )) |
| 242 |
local std_annual_cents |
| 243 |
std_annual_cents=$(annual_cents "$std_monthly_cents") |
| 244 |
local founder_annual_cents |
| 245 |
founder_annual_cents=$(annual_cents "$founder_monthly_cents") |
| 246 |
|
| 247 |
echo "==> ${tier}: standard \$${STANDARD_MONTHLY[$tier]}/mo, founder \$$(( STANDARD_MONTHLY[$tier] / 2 ))/mo" >&2 |
| 248 |
echo " annual cents: standard=${std_annual_cents}, founder=${founder_annual_cents}" >&2 |
| 249 |
|
| 250 |
local product_id |
| 251 |
product_id=$(ensure_product "$tier") |
| 252 |
echo " product: ${product_id}" >&2 |
| 253 |
|
| 254 |
for rate in standard founder; do |
| 255 |
for interval in monthly annual; do |
| 256 |
local cents stripe_interval |
| 257 |
if [[ "$rate" == "standard" && "$interval" == "monthly" ]]; then |
| 258 |
cents=$std_monthly_cents |
| 259 |
elif [[ "$rate" == "standard" && "$interval" == "annual" ]]; then |
| 260 |
cents=$std_annual_cents |
| 261 |
elif [[ "$rate" == "founder" && "$interval" == "monthly" ]]; then |
| 262 |
cents=$founder_monthly_cents |
| 263 |
else |
| 264 |
cents=$founder_annual_cents |
| 265 |
fi |
| 266 |
if [[ "$interval" == "monthly" ]]; then |
| 267 |
stripe_interval=month |
| 268 |
else |
| 269 |
stripe_interval=year |
| 270 |
fi |
| 271 |
|
| 272 |
local lookup="creator_tier_${tier}_${rate}_${interval}" |
| 273 |
wanted_lookup_keys+=("$lookup") |
| 274 |
local price_id |
| 275 |
price_id=$(ensure_price "$product_id" "$cents" "$stripe_interval" "$lookup") |
| 276 |
local var |
| 277 |
var=$(env_var_name "$tier" "$rate" "$interval") |
| 278 |
env_lines+=("${var}=${price_id}") |
| 279 |
echo " ${lookup} -> ${price_id} (\$$(awk "BEGIN { printf \"%.2f\", $cents/100 }"))" >&2 |
| 280 |
done |
| 281 |
done |
| 282 |
|
| 283 |
if (( ARCHIVE_OLD )); then |
| 284 |
echo " archiving stale prices on product ${product_id}..." >&2 |
| 285 |
|
| 286 |
|
| 287 |
local resp |
| 288 |
resp=$(stripe_get "prices" "product=${product_id}&active=true&limit=100") |
| 289 |
|
| 290 |
local tier_wanted_py="[" |
| 291 |
for k in "${wanted_lookup_keys[@]}"; do |
| 292 |
if [[ "$k" == "creator_tier_${tier}_"* ]]; then |
| 293 |
tier_wanted_py+="'$k'," |
| 294 |
fi |
| 295 |
done |
| 296 |
tier_wanted_py+="]" |
| 297 |
local stale_ids |
| 298 |
stale_ids=$(echo "$resp" | python3 -c " |
| 299 |
import json, sys |
| 300 |
d = json.load(sys.stdin) |
| 301 |
wanted = set(${tier_wanted_py}) |
| 302 |
for p in d.get('data', []): |
| 303 |
if p.get('lookup_key') not in wanted: |
| 304 |
print(p['id']) |
| 305 |
") |
| 306 |
for sid in $stale_ids; do |
| 307 |
archive_price "$sid" |
| 308 |
done |
| 309 |
fi |
| 310 |
done |
| 311 |
|
| 312 |
echo |
| 313 |
echo "# Paste into the server environment (and restart):" |
| 314 |
printf '%s\n' "${env_lines[@]}" | sort |
| 315 |
} |
| 316 |
|
| 317 |
main "$@" |
| 318 |
|