stripe-issue-refund
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"); sys.exit(1)
stripe.api_key = STRIPE_SECRET_KEY
AMOUNT_CENTS = 3000
PARTIAL_REFUND = 1000
# Create and confirm a payment to refund
intent = stripe.PaymentIntent.create(
amount=AMOUNT_CENTS,
currency="usd",
payment_method="pm_card_visa",
payment_method_types=["card"],
confirm=True,
return_url="https://checklist.day",
idempotency_key=f"checklist-refund-pi-{int(time.time())}",
)
payment_intent_id = intent.id
print(f" payment: {payment_intent_id} (status={intent.status})")
# Full refund
# FOOTGUN: refund via payment_intent — not charge ID (charge deprecated as primary key)
refund = stripe.Refund.create(
payment_intent=payment_intent_id,
idempotency_key=f"checklist-refund-{int(time.time())}",
)
refund_id = refund.id
refund_status = refund.status
amount_refunded = refund.amount
print(f" full refund: {refund_id} status={refund_status} amount={amount_refunded}")
# Partial refund test — create new payment
intent2 = stripe.PaymentIntent.create(
amount=AMOUNT_CENTS,
currency="usd",
payment_method="pm_card_visa",
payment_method_types=["card"],
confirm=True,
return_url="https://checklist.day",
idempotency_key=f"checklist-refund2-pi-{int(time.time())}",
)
partial_refund_obj = stripe.Refund.create(
payment_intent=intent2.id,
amount=PARTIAL_REFUND,
)
partial_ok = partial_refund_obj.amount == PARTIAL_REFUND and partial_refund_obj.status == "succeeded"
print(f" partial refund: {partial_refund_obj.amount} cents status={partial_refund_obj.status} ok={partial_ok}")
# ─────────────────────────────────────────
# POST_EXECUTION
# ─────────────────────────────────────────
assert refund_id.startswith("re_"), f"FAIL: expected re_... ID, got {refund_id}"
assert refund_status == "succeeded", f"FAIL: refund status '{refund_status}'"
assert amount_refunded == AMOUNT_CENTS, f"FAIL: expected {AMOUNT_CENTS}, got {amount_refunded}"
assert partial_ok, "FAIL: partial refund failed"
result = {
"refund_id": refund_id,
"refund_status": refund_status,
"amount_refunded": amount_refunded,
"partial_ok": partial_ok,
}
print(json.dumps(result, indent=2))
print("PASS")