stripe-create-customer

code_execution · unverified · null · json · download .py

Stripe secret key (sk_test_... for test mode)

import sys
import os
import subprocess
import time
import urllib.request
import json

# ─────────────────────────────────────────
# PRE_EXECUTION
# ─────────────────────────────────────────

for attempt in range(2):
    try:
        req = urllib.request.Request(
            "https://checklist.day/api/registry/stripe",
            headers={"User-Agent": "checklist-agent/1.0"}
        )
        with urllib.request.urlopen(req, timeout=10) as resp:
            registry = json.loads(resp.read())
            break
    except Exception as e:
        if attempt == 1:
            print(f"ABORT: registry unreachable — {e}")
            sys.exit(1)
        time.sleep(2)

warnings = registry.get("warnings", [])
if warnings:
    print("[stripe] WARNINGS:")
    for w in warnings if isinstance(warnings, list) else [warnings]:
        print(f"  ⚠ {w}")

# ─────────────────────────────────────────
# EXECUTION
# ─────────────────────────────────────────

subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "stripe>=7.0.0"])

import stripe
StripeError = stripe.StripeError

STRIPE_SECRET_KEY = os.environ.get("STRIPE_SECRET_KEY")
if not STRIPE_SECRET_KEY:
    print("ABORT: STRIPE_SECRET_KEY not set")
    sys.exit(1)

if not STRIPE_SECRET_KEY.startswith("sk_test_"):
    print("ABORT: use test mode key (sk_test_...) — never run against live keys in tests")
    sys.exit(1)

# FOOTGUN: set api_key before any stripe calls
stripe.api_key = STRIPE_SECRET_KEY

IDEMPOTENCY_KEY = f"checklist-create-customer-{int(time.time())}"
EMAIL           = "checklist-test@example.com"

# Create customer
customer = stripe.Customer.create(
    email=EMAIL,
    name="Checklist Test User",
    metadata={"source": "checklist.day", "env": "test"},
    idempotency_key=IDEMPOTENCY_KEY,
)
customer_id = customer.id
print(f"  created: {customer_id} ({EMAIL})")

# Verify by retrieval
retrieved = stripe.Customer.retrieve(customer_id)
email_ok  = retrieved.email == EMAIL
print(f"  retrieved: email={retrieved.email} (match={email_ok})")

# Idempotency — same key returns same customer
customer2    = stripe.Customer.create(
    email=EMAIL,
    name="Checklist Test User",
    metadata={"source": "checklist.day", "env": "test"},
    idempotency_key=IDEMPOTENCY_KEY,
)
idempotent_ok = customer2.id == customer_id
print(f"  idempotency: same id={idempotent_ok}")

# Cleanup
stripe.Customer.delete(customer_id)
cleanup_done = True
print(f"  deleted: {customer_id}")

# ─────────────────────────────────────────
# POST_EXECUTION
# ─────────────────────────────────────────

assert customer_id.startswith("cus_"), f"FAIL: expected cus_... ID, got {customer_id}"
assert email_ok, f"FAIL: email mismatch on retrieval"
assert idempotent_ok, "FAIL: idempotency key returned different customer"

result = {
    "customer_id":   customer_id,
    "email":         EMAIL,
    "idempotent_ok": idempotent_ok,
    "cleanup_done":  cleanup_done,
}
print(json.dumps(result, indent=2))
print("PASS")