stripe-create-payment-intent

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

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_...)"); sys.exit(1)

stripe.api_key = STRIPE_SECRET_KEY

# FOOTGUN: amount is in smallest currency unit (cents for USD) — NOT dollars
# $19.99 = 1999 cents, NOT 19.99
AMOUNT_CENTS = 1999
CURRENCY     = "usd"

intent = stripe.PaymentIntent.create(
    amount=AMOUNT_CENTS,
    currency=CURRENCY,
    # FOOTGUN: automatic_payment_methods required in newer Stripe API versions
    automatic_payment_methods={"enabled": True},
    metadata={"source": "checklist.day"},
    idempotency_key=f"checklist-pi-{int(time.time())}",
)

payment_intent_id = intent.id
status            = intent.status
client_secret_ok  = intent.client_secret is not None and "_secret_" in intent.client_secret

print(f"  created: {payment_intent_id}")
print(f"  status:  {status}")
print(f"  amount:  {AMOUNT_CENTS} {CURRENCY.upper()} ({AMOUNT_CENTS/100:.2f})")
print(f"  client_secret: {'present' if client_secret_ok else 'MISSING'}")

# Cleanup — cancel the intent
stripe.PaymentIntent.cancel(payment_intent_id)
print(f"  cancelled: {payment_intent_id}")

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

assert payment_intent_id.startswith("pi_"), f"FAIL: expected pi_... ID, got {payment_intent_id}"
assert status == "requires_payment_method", f"FAIL: unexpected status '{status}'"
assert client_secret_ok, "FAIL: client_secret missing or malformed"

result = {
    "payment_intent_id": payment_intent_id,
    "status":            status,
    "client_secret_ok":  client_secret_ok,
    "amount_cents":      AMOUNT_CENTS,
}
print(json.dumps(result, indent=2))
print("PASS")