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