Spaces:
Paused
Paused
Retry Stripe provider stage on same checkout session
Browse files- README.md +13 -0
- app.py +306 -58
- public/index.html +34 -1
README.md
CHANGED
|
@@ -101,6 +101,19 @@ original `cs_live`, the current attempt count, and the final approved state.
|
|
| 101 |
Server stdout/stderr logs remain redacted, but the authenticated web task log
|
| 102 |
shows original field values for operator debugging.
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
The original synchronous endpoint is still available:
|
| 105 |
|
| 106 |
```http
|
|
|
|
| 101 |
Server stdout/stderr logs remain redacted, but the authenticated web task log
|
| 102 |
shows original field values for operator debugging.
|
| 103 |
|
| 104 |
+
If the first Stripe confirm returns `requires_approval`, the app approves the
|
| 105 |
+
existing ChatGPT checkout session. The provider stage then keeps the same
|
| 106 |
+
`cs_live` but starts a fresh Stripe attempt: rerun Stripe init, create a new
|
| 107 |
+
PayPal payment method, and confirm again. If Stripe reports a terminal failed
|
| 108 |
+
submission attempt, the app does not keep polling that failed attempt; it starts
|
| 109 |
+
another fresh Stripe provider attempt for the same `cs_live` until it gets a
|
| 110 |
+
redirect or reaches the cap:
|
| 111 |
+
|
| 112 |
+
```bash
|
| 113 |
+
OPENAI_PAY_PROVIDER_MAX_ATTEMPTS=5
|
| 114 |
+
OPENAI_PAY_PROVIDER_RETRY_DELAY_SECONDS=1
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
The original synchronous endpoint is still available:
|
| 118 |
|
| 119 |
```http
|
app.py
CHANGED
|
@@ -55,6 +55,8 @@ def env_float(name: str, default: float, minimum: float = 0.0, maximum: float =
|
|
| 55 |
APPROVE_MAX_ATTEMPTS = env_int("OPENAI_PAY_APPROVE_MAX_ATTEMPTS", 30, minimum=1, maximum=100)
|
| 56 |
APPROVE_RETRY_DELAY_SECONDS = env_float("OPENAI_PAY_APPROVE_RETRY_DELAY_SECONDS", 1.0, minimum=0.0, maximum=10.0)
|
| 57 |
APPROVE_WINDOW_SECONDS = env_float("OPENAI_PAY_APPROVE_WINDOW_SECONDS", 20.0, minimum=1.0, maximum=120.0)
|
|
|
|
|
|
|
| 58 |
BASE_DIR = Path(__file__).resolve().parent
|
| 59 |
PUBLIC_DIR = BASE_DIR / "public"
|
| 60 |
PAYPAL_HOST_SUFFIXES = ("paypal.com", "paypalobjects.com")
|
|
@@ -1014,6 +1016,23 @@ def extract_redirect_to_url(payload: Any) -> str:
|
|
| 1014 |
return ""
|
| 1015 |
|
| 1016 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1017 |
def stripe_payment_page_redirect_url(
|
| 1018 |
stripe: Any,
|
| 1019 |
cs_id: str,
|
|
@@ -1268,6 +1287,7 @@ def redirect_url_after_confirm(
|
|
| 1268 |
checkout: dict[str, Any],
|
| 1269 |
req: LongLinkRequest,
|
| 1270 |
stripe_hosted_url: str,
|
|
|
|
| 1271 |
) -> str:
|
| 1272 |
redirect_url = extract_redirect_to_url(confirm_payload)
|
| 1273 |
if redirect_url:
|
|
@@ -1277,24 +1297,59 @@ def redirect_url_after_confirm(
|
|
| 1277 |
if isinstance(submission, dict) and submission.get("state") == "requires_approval":
|
| 1278 |
log_info("provider.redirect.requires_approval", submission_state=submission.get("state"))
|
| 1279 |
chatgpt_approve(chatgpt, cs_id, checkout)
|
| 1280 |
-
|
| 1281 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1282 |
retry_payload = stripe_confirm(
|
| 1283 |
stripe,
|
| 1284 |
cs_id,
|
| 1285 |
pm_id,
|
| 1286 |
stripe_pk,
|
| 1287 |
"paypal",
|
| 1288 |
-
|
| 1289 |
-
|
| 1290 |
checkout,
|
| 1291 |
req,
|
| 1292 |
-
|
| 1293 |
)
|
| 1294 |
retry_redirect_url = extract_redirect_to_url(retry_payload)
|
| 1295 |
retry_submission = retry_payload.get("submission_attempt") if isinstance(retry_payload, dict) else None
|
| 1296 |
log_info(
|
| 1297 |
"stripe.confirm.retry_after_approval.done",
|
|
|
|
|
|
|
|
|
|
| 1298 |
has_redirect=bool(retry_redirect_url),
|
| 1299 |
submission_state=retry_submission.get("state") if isinstance(retry_submission, dict) else "",
|
| 1300 |
response_keys=response_keys(retry_payload),
|
|
@@ -1403,68 +1458,261 @@ def create_provider_link(
|
|
| 1403 |
link_type = normalize_link_type(req.link_type)
|
| 1404 |
stripe_pk = req.stripe_publishable_key.strip() or DEFAULT_STRIPE_PK
|
| 1405 |
provider_proxy = normalize_proxy_url(provider_proxy)
|
|
|
|
|
|
|
| 1406 |
log_info(
|
| 1407 |
"provider.create.start",
|
| 1408 |
link_type=link_type,
|
| 1409 |
-
cs_id=
|
| 1410 |
provider_proxy_configured=bool(provider_proxy),
|
| 1411 |
provider_proxy=provider_proxy,
|
|
|
|
| 1412 |
)
|
| 1413 |
stripe = build_stripe_session(req, proxy_override=provider_proxy)
|
| 1414 |
-
|
| 1415 |
-
|
| 1416 |
-
|
| 1417 |
-
|
| 1418 |
-
|
| 1419 |
-
|
| 1420 |
-
|
| 1421 |
-
|
| 1422 |
-
|
| 1423 |
-
|
| 1424 |
-
|
| 1425 |
-
|
| 1426 |
-
|
| 1427 |
-
|
| 1428 |
-
|
| 1429 |
-
|
| 1430 |
-
|
| 1431 |
-
|
| 1432 |
-
|
| 1433 |
-
|
| 1434 |
-
|
| 1435 |
-
|
| 1436 |
-
|
| 1437 |
-
|
| 1438 |
-
|
| 1439 |
-
|
| 1440 |
-
|
| 1441 |
-
|
| 1442 |
-
|
| 1443 |
-
|
| 1444 |
-
|
| 1445 |
-
|
| 1446 |
-
|
| 1447 |
-
|
| 1448 |
-
|
| 1449 |
-
|
| 1450 |
-
|
| 1451 |
-
|
| 1452 |
-
|
| 1453 |
-
|
| 1454 |
-
|
| 1455 |
-
|
| 1456 |
-
|
| 1457 |
-
|
| 1458 |
-
|
| 1459 |
-
|
| 1460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1461 |
)
|
| 1462 |
-
|
| 1463 |
-
"payment_method_id": pm_id,
|
| 1464 |
-
"stripe_redirect_url": stripe_redirect_url,
|
| 1465 |
-
"provider_redirect_url": provider_url,
|
| 1466 |
-
"long_url": provider_url or stripe_redirect_url,
|
| 1467 |
-
}
|
| 1468 |
|
| 1469 |
|
| 1470 |
app = FastAPI(title="OpenAI Pay Long Link")
|
|
|
|
| 55 |
APPROVE_MAX_ATTEMPTS = env_int("OPENAI_PAY_APPROVE_MAX_ATTEMPTS", 30, minimum=1, maximum=100)
|
| 56 |
APPROVE_RETRY_DELAY_SECONDS = env_float("OPENAI_PAY_APPROVE_RETRY_DELAY_SECONDS", 1.0, minimum=0.0, maximum=10.0)
|
| 57 |
APPROVE_WINDOW_SECONDS = env_float("OPENAI_PAY_APPROVE_WINDOW_SECONDS", 20.0, minimum=1.0, maximum=120.0)
|
| 58 |
+
PROVIDER_MAX_ATTEMPTS = env_int("OPENAI_PAY_PROVIDER_MAX_ATTEMPTS", 5, minimum=1, maximum=20)
|
| 59 |
+
PROVIDER_RETRY_DELAY_SECONDS = env_float("OPENAI_PAY_PROVIDER_RETRY_DELAY_SECONDS", 1.0, minimum=0.0, maximum=30.0)
|
| 60 |
BASE_DIR = Path(__file__).resolve().parent
|
| 61 |
PUBLIC_DIR = BASE_DIR / "public"
|
| 62 |
PAYPAL_HOST_SUFFIXES = ("paypal.com", "paypalobjects.com")
|
|
|
|
| 1016 |
return ""
|
| 1017 |
|
| 1018 |
|
| 1019 |
+
def submission_attempt_state(payload: Any) -> str:
|
| 1020 |
+
if not isinstance(payload, dict):
|
| 1021 |
+
return ""
|
| 1022 |
+
submission = payload.get("submission_attempt")
|
| 1023 |
+
if isinstance(submission, dict):
|
| 1024 |
+
state = str(submission.get("state") or "").strip()
|
| 1025 |
+
if state:
|
| 1026 |
+
return state
|
| 1027 |
+
for key in ("setup_intent", "payment_intent"):
|
| 1028 |
+
nested = payload.get(key)
|
| 1029 |
+
if isinstance(nested, dict):
|
| 1030 |
+
state = submission_attempt_state(nested)
|
| 1031 |
+
if state:
|
| 1032 |
+
return state
|
| 1033 |
+
return ""
|
| 1034 |
+
|
| 1035 |
+
|
| 1036 |
def stripe_payment_page_redirect_url(
|
| 1037 |
stripe: Any,
|
| 1038 |
cs_id: str,
|
|
|
|
| 1287 |
checkout: dict[str, Any],
|
| 1288 |
req: LongLinkRequest,
|
| 1289 |
stripe_hosted_url: str,
|
| 1290 |
+
provider_proxy: str = "",
|
| 1291 |
) -> str:
|
| 1292 |
redirect_url = extract_redirect_to_url(confirm_payload)
|
| 1293 |
if redirect_url:
|
|
|
|
| 1297 |
if isinstance(submission, dict) and submission.get("state") == "requires_approval":
|
| 1298 |
log_info("provider.redirect.requires_approval", submission_state=submission.get("state"))
|
| 1299 |
chatgpt_approve(chatgpt, cs_id, checkout)
|
| 1300 |
+
provider_proxy = normalize_proxy_url(provider_proxy)
|
| 1301 |
try:
|
| 1302 |
+
log_info(
|
| 1303 |
+
"stripe.init.after_approval.start",
|
| 1304 |
+
cs_id=cs_id,
|
| 1305 |
+
provider_proxy_configured=bool(provider_proxy),
|
| 1306 |
+
provider_proxy=provider_proxy,
|
| 1307 |
+
same_cs_id=True,
|
| 1308 |
+
)
|
| 1309 |
+
refreshed_init_payload = stripe_init(cs_id, req, proxy_override=provider_proxy)
|
| 1310 |
+
refreshed_hosted_url = str(refreshed_init_payload.get("stripe_hosted_url") or "").strip() or stripe_hosted_url
|
| 1311 |
+
refreshed_ctx = stripe_context(cs_id, refreshed_init_payload, req)
|
| 1312 |
+
refreshed_init_redirect = extract_redirect_to_url(refreshed_init_payload)
|
| 1313 |
+
log_info(
|
| 1314 |
+
"stripe.init.after_approval.done",
|
| 1315 |
+
cs_id=cs_id,
|
| 1316 |
+
same_cs_id=True,
|
| 1317 |
+
response_keys=response_keys(refreshed_init_payload),
|
| 1318 |
+
interesting_fields=interesting_payload_fields(refreshed_init_payload),
|
| 1319 |
+
has_redirect=bool(refreshed_init_redirect),
|
| 1320 |
+
refreshed_init_checksum_present=bool(refreshed_ctx.get("init_checksum")),
|
| 1321 |
+
refreshed_elements_session_id=refreshed_ctx.get("elements_session_id", ""),
|
| 1322 |
+
)
|
| 1323 |
+
if refreshed_init_redirect:
|
| 1324 |
+
log_info("provider.redirect.after_approval_reinit", redirect_url=refreshed_init_redirect, cs_id=cs_id)
|
| 1325 |
+
return refreshed_init_redirect
|
| 1326 |
+
|
| 1327 |
+
log_info(
|
| 1328 |
+
"stripe.confirm.retry_after_approval.start",
|
| 1329 |
+
cs_id=cs_id,
|
| 1330 |
+
payment_method_id=pm_id,
|
| 1331 |
+
same_cs_id=True,
|
| 1332 |
+
refreshed_init=True,
|
| 1333 |
+
)
|
| 1334 |
retry_payload = stripe_confirm(
|
| 1335 |
stripe,
|
| 1336 |
cs_id,
|
| 1337 |
pm_id,
|
| 1338 |
stripe_pk,
|
| 1339 |
"paypal",
|
| 1340 |
+
refreshed_init_payload,
|
| 1341 |
+
refreshed_ctx,
|
| 1342 |
checkout,
|
| 1343 |
req,
|
| 1344 |
+
refreshed_hosted_url,
|
| 1345 |
)
|
| 1346 |
retry_redirect_url = extract_redirect_to_url(retry_payload)
|
| 1347 |
retry_submission = retry_payload.get("submission_attempt") if isinstance(retry_payload, dict) else None
|
| 1348 |
log_info(
|
| 1349 |
"stripe.confirm.retry_after_approval.done",
|
| 1350 |
+
cs_id=cs_id,
|
| 1351 |
+
same_cs_id=True,
|
| 1352 |
+
refreshed_init=True,
|
| 1353 |
has_redirect=bool(retry_redirect_url),
|
| 1354 |
submission_state=retry_submission.get("state") if isinstance(retry_submission, dict) else "",
|
| 1355 |
response_keys=response_keys(retry_payload),
|
|
|
|
| 1458 |
link_type = normalize_link_type(req.link_type)
|
| 1459 |
stripe_pk = req.stripe_publishable_key.strip() or DEFAULT_STRIPE_PK
|
| 1460 |
provider_proxy = normalize_proxy_url(provider_proxy)
|
| 1461 |
+
cs_id = checkout["cs_id"]
|
| 1462 |
+
max_attempts = PROVIDER_MAX_ATTEMPTS
|
| 1463 |
log_info(
|
| 1464 |
"provider.create.start",
|
| 1465 |
link_type=link_type,
|
| 1466 |
+
cs_id=cs_id,
|
| 1467 |
provider_proxy_configured=bool(provider_proxy),
|
| 1468 |
provider_proxy=provider_proxy,
|
| 1469 |
+
provider_max_attempts=max_attempts,
|
| 1470 |
)
|
| 1471 |
stripe = build_stripe_session(req, proxy_override=provider_proxy)
|
| 1472 |
+
approved_once = False
|
| 1473 |
+
last_error = ""
|
| 1474 |
+
last_pm_id = ""
|
| 1475 |
+
last_stripe_redirect_url = ""
|
| 1476 |
+
current_init_payload = init_payload
|
| 1477 |
+
current_hosted_url = stripe_hosted_url
|
| 1478 |
+
|
| 1479 |
+
for provider_attempt in range(1, max_attempts + 1):
|
| 1480 |
+
refreshed_init = provider_attempt > 1
|
| 1481 |
+
if refreshed_init:
|
| 1482 |
+
try:
|
| 1483 |
+
log_info(
|
| 1484 |
+
"stripe.init.provider_retry.start",
|
| 1485 |
+
cs_id=cs_id,
|
| 1486 |
+
provider_attempt=provider_attempt,
|
| 1487 |
+
provider_max_attempts=max_attempts,
|
| 1488 |
+
same_cs_id=True,
|
| 1489 |
+
provider_proxy_configured=bool(provider_proxy),
|
| 1490 |
+
provider_proxy=provider_proxy,
|
| 1491 |
+
)
|
| 1492 |
+
current_init_payload = stripe_init(cs_id, req, proxy_override=provider_proxy)
|
| 1493 |
+
current_hosted_url = str(current_init_payload.get("stripe_hosted_url") or "").strip() or current_hosted_url
|
| 1494 |
+
log_info(
|
| 1495 |
+
"stripe.init.provider_retry.done",
|
| 1496 |
+
cs_id=cs_id,
|
| 1497 |
+
provider_attempt=provider_attempt,
|
| 1498 |
+
same_cs_id=True,
|
| 1499 |
+
response_keys=response_keys(current_init_payload),
|
| 1500 |
+
interesting_fields=interesting_payload_fields(current_init_payload),
|
| 1501 |
+
has_redirect=bool(extract_redirect_to_url(current_init_payload)),
|
| 1502 |
+
)
|
| 1503 |
+
except HTTPException as exc:
|
| 1504 |
+
last_error = str(exc.detail)
|
| 1505 |
+
log_warning(
|
| 1506 |
+
"stripe.init.provider_retry.http_exception",
|
| 1507 |
+
cs_id=cs_id,
|
| 1508 |
+
provider_attempt=provider_attempt,
|
| 1509 |
+
same_cs_id=True,
|
| 1510 |
+
status_code=exc.status_code,
|
| 1511 |
+
detail=last_error,
|
| 1512 |
+
)
|
| 1513 |
+
if provider_attempt >= max_attempts:
|
| 1514 |
+
raise
|
| 1515 |
+
if PROVIDER_RETRY_DELAY_SECONDS:
|
| 1516 |
+
time.sleep(PROVIDER_RETRY_DELAY_SECONDS)
|
| 1517 |
+
continue
|
| 1518 |
+
|
| 1519 |
+
ctx = stripe_context(cs_id, current_init_payload, req)
|
| 1520 |
+
log_info(
|
| 1521 |
+
"provider.attempt.start",
|
| 1522 |
+
cs_id=cs_id,
|
| 1523 |
+
provider_attempt=provider_attempt,
|
| 1524 |
+
provider_max_attempts=max_attempts,
|
| 1525 |
+
same_cs_id=True,
|
| 1526 |
+
refreshed_init=refreshed_init,
|
| 1527 |
+
approved_once=approved_once,
|
| 1528 |
+
currency=ctx.get("currency", ""),
|
| 1529 |
+
checkout_amount=ctx.get("checkout_amount", ""),
|
| 1530 |
+
locale=ctx.get("locale", ""),
|
| 1531 |
+
init_checksum_present=bool(ctx.get("init_checksum")),
|
| 1532 |
+
)
|
| 1533 |
+
try:
|
| 1534 |
+
init_redirect_url = extract_redirect_to_url(current_init_payload)
|
| 1535 |
+
if init_redirect_url:
|
| 1536 |
+
last_stripe_redirect_url = init_redirect_url
|
| 1537 |
+
provider_url = resolve_external_redirect(stripe, init_redirect_url, preferred_hosts=PAYPAL_HOST_SUFFIXES)
|
| 1538 |
+
log_info(
|
| 1539 |
+
"provider.create.done",
|
| 1540 |
+
cs_id=cs_id,
|
| 1541 |
+
provider_attempt=provider_attempt,
|
| 1542 |
+
same_cs_id=True,
|
| 1543 |
+
source="init_payload",
|
| 1544 |
+
payment_method_id=last_pm_id,
|
| 1545 |
+
stripe_redirect_url=init_redirect_url,
|
| 1546 |
+
provider_redirect_url=provider_url,
|
| 1547 |
+
long_url=provider_url or init_redirect_url,
|
| 1548 |
+
)
|
| 1549 |
+
return {
|
| 1550 |
+
"payment_method_id": last_pm_id,
|
| 1551 |
+
"stripe_redirect_url": init_redirect_url,
|
| 1552 |
+
"provider_redirect_url": provider_url,
|
| 1553 |
+
"long_url": provider_url or init_redirect_url,
|
| 1554 |
+
}
|
| 1555 |
+
|
| 1556 |
+
billing = billing_for_link_type(link_type)
|
| 1557 |
+
pm_id = stripe_create_payment_method(stripe, cs_id, stripe_pk, billing, link_type, ctx)
|
| 1558 |
+
last_pm_id = pm_id
|
| 1559 |
+
confirm_payload = stripe_confirm(
|
| 1560 |
+
stripe,
|
| 1561 |
+
cs_id,
|
| 1562 |
+
pm_id,
|
| 1563 |
+
stripe_pk,
|
| 1564 |
+
link_type,
|
| 1565 |
+
current_init_payload,
|
| 1566 |
+
ctx,
|
| 1567 |
+
checkout,
|
| 1568 |
+
req,
|
| 1569 |
+
current_hosted_url,
|
| 1570 |
+
)
|
| 1571 |
+
except HTTPException as exc:
|
| 1572 |
+
last_error = str(exc.detail)
|
| 1573 |
+
log_warning(
|
| 1574 |
+
"provider.attempt.http_exception",
|
| 1575 |
+
cs_id=cs_id,
|
| 1576 |
+
provider_attempt=provider_attempt,
|
| 1577 |
+
same_cs_id=True,
|
| 1578 |
+
status_code=exc.status_code,
|
| 1579 |
+
detail=last_error,
|
| 1580 |
+
)
|
| 1581 |
+
if provider_attempt >= max_attempts:
|
| 1582 |
+
raise
|
| 1583 |
+
if PROVIDER_RETRY_DELAY_SECONDS:
|
| 1584 |
+
time.sleep(PROVIDER_RETRY_DELAY_SECONDS)
|
| 1585 |
+
continue
|
| 1586 |
+
except Exception as exc:
|
| 1587 |
+
last_error = repr(exc)
|
| 1588 |
+
log_warning(
|
| 1589 |
+
"provider.attempt.exception",
|
| 1590 |
+
cs_id=cs_id,
|
| 1591 |
+
provider_attempt=provider_attempt,
|
| 1592 |
+
same_cs_id=True,
|
| 1593 |
+
error=last_error,
|
| 1594 |
+
)
|
| 1595 |
+
if provider_attempt >= max_attempts:
|
| 1596 |
+
raise
|
| 1597 |
+
if PROVIDER_RETRY_DELAY_SECONDS:
|
| 1598 |
+
time.sleep(PROVIDER_RETRY_DELAY_SECONDS)
|
| 1599 |
+
continue
|
| 1600 |
+
|
| 1601 |
+
stripe_redirect_url = extract_redirect_to_url(confirm_payload)
|
| 1602 |
+
if stripe_redirect_url:
|
| 1603 |
+
last_stripe_redirect_url = stripe_redirect_url
|
| 1604 |
+
provider_url = resolve_external_redirect(stripe, stripe_redirect_url, preferred_hosts=PAYPAL_HOST_SUFFIXES)
|
| 1605 |
+
log_info(
|
| 1606 |
+
"provider.create.done",
|
| 1607 |
+
cs_id=cs_id,
|
| 1608 |
+
provider_attempt=provider_attempt,
|
| 1609 |
+
same_cs_id=True,
|
| 1610 |
+
source="confirm_payload",
|
| 1611 |
+
payment_method_id=pm_id,
|
| 1612 |
+
stripe_redirect_url=stripe_redirect_url,
|
| 1613 |
+
provider_redirect_url=provider_url,
|
| 1614 |
+
long_url=provider_url or stripe_redirect_url,
|
| 1615 |
+
)
|
| 1616 |
+
return {
|
| 1617 |
+
"payment_method_id": pm_id,
|
| 1618 |
+
"stripe_redirect_url": stripe_redirect_url,
|
| 1619 |
+
"provider_redirect_url": provider_url,
|
| 1620 |
+
"long_url": provider_url or stripe_redirect_url,
|
| 1621 |
+
}
|
| 1622 |
+
|
| 1623 |
+
state = submission_attempt_state(confirm_payload)
|
| 1624 |
+
log_info(
|
| 1625 |
+
"provider.attempt.confirm_no_redirect",
|
| 1626 |
+
cs_id=cs_id,
|
| 1627 |
+
provider_attempt=provider_attempt,
|
| 1628 |
+
provider_max_attempts=max_attempts,
|
| 1629 |
+
same_cs_id=True,
|
| 1630 |
+
payment_method_id=pm_id,
|
| 1631 |
+
submission_state=state,
|
| 1632 |
+
response_keys=response_keys(confirm_payload),
|
| 1633 |
+
interesting_fields=interesting_payload_fields(confirm_payload),
|
| 1634 |
+
)
|
| 1635 |
+
|
| 1636 |
+
if state == "requires_approval":
|
| 1637 |
+
log_info(
|
| 1638 |
+
"provider.attempt.requires_approval",
|
| 1639 |
+
cs_id=cs_id,
|
| 1640 |
+
provider_attempt=provider_attempt,
|
| 1641 |
+
same_cs_id=True,
|
| 1642 |
+
approved_once=approved_once,
|
| 1643 |
+
)
|
| 1644 |
+
if not approved_once:
|
| 1645 |
+
chatgpt_approve(chatgpt, cs_id, checkout)
|
| 1646 |
+
approved_once = True
|
| 1647 |
+
last_error = "requires_approval handled; retrying provider stage with refreshed Stripe init"
|
| 1648 |
+
elif state in {"failed", "canceled", "cancelled"}:
|
| 1649 |
+
last_error = f"submission_attempt {state}; retrying provider stage with refreshed Stripe init"
|
| 1650 |
+
log_warning(
|
| 1651 |
+
"provider.attempt.submission_terminal",
|
| 1652 |
+
cs_id=cs_id,
|
| 1653 |
+
provider_attempt=provider_attempt,
|
| 1654 |
+
same_cs_id=True,
|
| 1655 |
+
submission_state=state,
|
| 1656 |
+
detail=last_error,
|
| 1657 |
+
)
|
| 1658 |
+
else:
|
| 1659 |
+
try:
|
| 1660 |
+
polled_redirect_url = stripe_payment_page_redirect_url(
|
| 1661 |
+
stripe,
|
| 1662 |
+
cs_id,
|
| 1663 |
+
stripe_pk,
|
| 1664 |
+
req,
|
| 1665 |
+
timeout_seconds=8,
|
| 1666 |
+
max_attempts=3,
|
| 1667 |
+
)
|
| 1668 |
+
last_stripe_redirect_url = polled_redirect_url
|
| 1669 |
+
provider_url = resolve_external_redirect(
|
| 1670 |
+
stripe,
|
| 1671 |
+
polled_redirect_url,
|
| 1672 |
+
preferred_hosts=PAYPAL_HOST_SUFFIXES,
|
| 1673 |
+
)
|
| 1674 |
+
log_info(
|
| 1675 |
+
"provider.create.done",
|
| 1676 |
+
cs_id=cs_id,
|
| 1677 |
+
provider_attempt=provider_attempt,
|
| 1678 |
+
same_cs_id=True,
|
| 1679 |
+
source="payment_page_poll",
|
| 1680 |
+
payment_method_id=pm_id,
|
| 1681 |
+
stripe_redirect_url=polled_redirect_url,
|
| 1682 |
+
provider_redirect_url=provider_url,
|
| 1683 |
+
long_url=provider_url or polled_redirect_url,
|
| 1684 |
+
)
|
| 1685 |
+
return {
|
| 1686 |
+
"payment_method_id": pm_id,
|
| 1687 |
+
"stripe_redirect_url": polled_redirect_url,
|
| 1688 |
+
"provider_redirect_url": provider_url,
|
| 1689 |
+
"long_url": provider_url or polled_redirect_url,
|
| 1690 |
+
}
|
| 1691 |
+
except HTTPException as exc:
|
| 1692 |
+
last_error = str(exc.detail)
|
| 1693 |
+
log_warning(
|
| 1694 |
+
"provider.attempt.poll_no_redirect",
|
| 1695 |
+
cs_id=cs_id,
|
| 1696 |
+
provider_attempt=provider_attempt,
|
| 1697 |
+
same_cs_id=True,
|
| 1698 |
+
status_code=exc.status_code,
|
| 1699 |
+
detail=last_error,
|
| 1700 |
+
)
|
| 1701 |
+
|
| 1702 |
+
if provider_attempt < max_attempts and PROVIDER_RETRY_DELAY_SECONDS:
|
| 1703 |
+
time.sleep(PROVIDER_RETRY_DELAY_SECONDS)
|
| 1704 |
+
|
| 1705 |
+
detail = last_error or f"provider stage failed after {max_attempts} attempts on same cs_live"
|
| 1706 |
+
log_warning(
|
| 1707 |
+
"provider.create.exhausted",
|
| 1708 |
+
cs_id=cs_id,
|
| 1709 |
+
provider_max_attempts=max_attempts,
|
| 1710 |
+
same_cs_id=True,
|
| 1711 |
+
last_payment_method_id=last_pm_id,
|
| 1712 |
+
last_stripe_redirect_url=last_stripe_redirect_url,
|
| 1713 |
+
detail=detail,
|
| 1714 |
)
|
| 1715 |
+
raise HTTPException(status_code=504, detail=detail)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1716 |
|
| 1717 |
|
| 1718 |
app = FastAPI(title="OpenAI Pay Long Link")
|
public/index.html
CHANGED
|
@@ -821,8 +821,35 @@
|
|
| 821 |
if (event === "chatgpt.approve.done") {
|
| 822 |
return `approve 成功:同一个 ${fields.cs_id || ""} 第 ${fields.attempt || "?"} 次返回 ${fields.result || "approved"}。`;
|
| 823 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
if (event === "stripe.confirm.retry_after_approval.start") {
|
| 825 |
-
return "approve 后复用同一 CS / payment method 重试 Stripe confirm。";
|
| 826 |
}
|
| 827 |
if (event === "stripe.confirm.retry_after_approval.done") {
|
| 828 |
return `approve 后 confirm 完成:has_redirect=${fields.has_redirect ?? ""},submission=${fields.submission_state || ""}。`;
|
|
@@ -842,6 +869,8 @@
|
|
| 842 |
"cs_id",
|
| 843 |
"attempt",
|
| 844 |
"max_attempts",
|
|
|
|
|
|
|
| 845 |
"window_seconds",
|
| 846 |
"retry_delay_seconds",
|
| 847 |
"remaining_ms",
|
|
@@ -851,6 +880,10 @@
|
|
| 851 |
"submission_state",
|
| 852 |
"has_redirect",
|
| 853 |
"route",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
];
|
| 855 |
return keys
|
| 856 |
.filter((key) => fields[key] !== undefined && fields[key] !== "")
|
|
|
|
| 821 |
if (event === "chatgpt.approve.done") {
|
| 822 |
return `approve 成功:同一个 ${fields.cs_id || ""} 第 ${fields.attempt || "?"} 次返回 ${fields.result || "approved"}。`;
|
| 823 |
}
|
| 824 |
+
if (event === "stripe.init.after_approval.start") {
|
| 825 |
+
return `approve 后复用同一个 ${fields.cs_id || ""} 重新 Stripe init,刷新 payment page / elements context。`;
|
| 826 |
+
}
|
| 827 |
+
if (event === "stripe.init.after_approval.done") {
|
| 828 |
+
return `同一个 CS 重新 Stripe init 完成:has_redirect=${fields.has_redirect ?? ""},refreshed_init_checksum=${fields.refreshed_init_checksum_present ?? ""}。`;
|
| 829 |
+
}
|
| 830 |
+
if (event === "stripe.init.provider_retry.start") {
|
| 831 |
+
return `Provider 第 ${fields.provider_attempt || "?"}/${fields.provider_max_attempts || "?"} 轮:复用同一个 ${fields.cs_id || ""} 重新 Stripe init。`;
|
| 832 |
+
}
|
| 833 |
+
if (event === "stripe.init.provider_retry.done") {
|
| 834 |
+
return `Provider 重试 Stripe init 完成:has_redirect=${fields.has_redirect ?? ""},同一个 cs_live 不变。`;
|
| 835 |
+
}
|
| 836 |
+
if (event === "provider.attempt.start") {
|
| 837 |
+
return `Provider 第 ${fields.provider_attempt || "?"}/${fields.provider_max_attempts || "?"} 轮开始:same_cs_id=${fields.same_cs_id ?? ""},refreshed_init=${fields.refreshed_init ?? ""}。`;
|
| 838 |
+
}
|
| 839 |
+
if (event === "provider.attempt.confirm_no_redirect") {
|
| 840 |
+
return `confirm 未拿到 redirect:submission=${fields.submission_state || "空"},准备按状态决定是否同 CS 重来。`;
|
| 841 |
+
}
|
| 842 |
+
if (event === "provider.attempt.requires_approval") {
|
| 843 |
+
return `本轮需要 approve:固定同一个 ${fields.cs_id || ""} approve,成功后下一轮重新 init + 新 payment_method。`;
|
| 844 |
+
}
|
| 845 |
+
if (event === "provider.attempt.submission_terminal") {
|
| 846 |
+
return `本轮 submission 已终止/失败:${fields.submission_state || ""},不再死 poll,下一轮同 CS 重新 init + 新 payment_method。`;
|
| 847 |
+
}
|
| 848 |
+
if (event === "provider.attempt.poll_no_redirect") {
|
| 849 |
+
return "本轮短轮询没有 redirect,下一轮同 CS 重新 init + 新 payment_method。";
|
| 850 |
+
}
|
| 851 |
if (event === "stripe.confirm.retry_after_approval.start") {
|
| 852 |
+
return "approve + 重新 Stripe init 后,复用同一 CS / payment method 重试 Stripe confirm。";
|
| 853 |
}
|
| 854 |
if (event === "stripe.confirm.retry_after_approval.done") {
|
| 855 |
return `approve 后 confirm 完成:has_redirect=${fields.has_redirect ?? ""},submission=${fields.submission_state || ""}。`;
|
|
|
|
| 869 |
"cs_id",
|
| 870 |
"attempt",
|
| 871 |
"max_attempts",
|
| 872 |
+
"provider_attempt",
|
| 873 |
+
"provider_max_attempts",
|
| 874 |
"window_seconds",
|
| 875 |
"retry_delay_seconds",
|
| 876 |
"remaining_ms",
|
|
|
|
| 880 |
"submission_state",
|
| 881 |
"has_redirect",
|
| 882 |
"route",
|
| 883 |
+
"refreshed_init",
|
| 884 |
+
"approved_once",
|
| 885 |
+
"refreshed_init_checksum_present",
|
| 886 |
+
"refreshed_elements_session_id",
|
| 887 |
];
|
| 888 |
return keys
|
| 889 |
.filter((key) => fields[key] !== undefined && fields[key] !== "")
|