Spaces:
Paused
Paused
Align UPI with local specialized tax approve and second-confirm flow
Browse files- app.py +132 -2
- tests/test_app.py +71 -1
app.py
CHANGED
|
@@ -1987,6 +1987,73 @@ def update_ideal_tax_context(
|
|
| 1987 |
log_info("ideal.tax_context.done", cs_id=checkout["cs_id"], country="NL")
|
| 1988 |
|
| 1989 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1990 |
def stripe_error_fields(response: Any) -> dict[str, str]:
|
| 1991 |
try:
|
| 1992 |
payload = response.json() or {}
|
|
@@ -4428,7 +4495,8 @@ def redirect_url_after_confirm(
|
|
| 4428 |
checkout_status=confirm_payload.get("status", "") if isinstance(confirm_payload, dict) else "",
|
| 4429 |
checkout_state=confirm_payload.get("state", "") if isinstance(confirm_payload, dict) else "",
|
| 4430 |
)
|
| 4431 |
-
|
|
|
|
| 4432 |
apply_approve_proxy(chatgpt, approve_proxy)
|
| 4433 |
runtime_config = get_runtime_config()
|
| 4434 |
approve_pool_mode = normalize_proxy_pool_mode(req.proxy_pool_mode or runtime_config.get("proxyPoolMode"))
|
|
@@ -4446,7 +4514,56 @@ def redirect_url_after_confirm(
|
|
| 4446 |
stripe_route="provider_proxy" if getattr(stripe, "proxies", {}) else "direct",
|
| 4447 |
approve_route="approve_proxy" if approve_proxy else "direct",
|
| 4448 |
)
|
| 4449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4450 |
log_info(
|
| 4451 |
"provider.redirect.poll_required",
|
| 4452 |
submission_state=submission.get("state") if isinstance(submission, dict) else "",
|
|
@@ -6126,6 +6243,19 @@ def build_long_link_response(
|
|
| 6126 |
processor_entity=checkout.get("processor_entity", ""),
|
| 6127 |
)
|
| 6128 |
require_payment_method_available(init_payload, "ideal")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6129 |
enforce_zero_checkout_amount(init_payload, req, checkout["cs_id"])
|
| 6130 |
stop_if_requested()
|
| 6131 |
checkout_amount = expected_amount(init_payload)
|
|
|
|
| 1987 |
log_info("ideal.tax_context.done", cs_id=checkout["cs_id"], country="NL")
|
| 1988 |
|
| 1989 |
|
| 1990 |
+
def update_upi_tax_context(
|
| 1991 |
+
chatgpt: Any,
|
| 1992 |
+
checkout: dict[str, Any],
|
| 1993 |
+
req: LongLinkRequest,
|
| 1994 |
+
stripe_pk: str,
|
| 1995 |
+
provider_proxy: str,
|
| 1996 |
+
) -> None:
|
| 1997 |
+
"""Synchronize the IN billing context before creating the UPI PaymentMethod."""
|
| 1998 |
+
billing = billing_for_link_type("upi", "IN")
|
| 1999 |
+
path = "/backend-api/payments/checkout/taxes"
|
| 2000 |
+
original_proxy = session_proxy_url(chatgpt)
|
| 2001 |
+
if provider_proxy:
|
| 2002 |
+
set_proxy_url(chatgpt, provider_proxy)
|
| 2003 |
+
try:
|
| 2004 |
+
response = chatgpt.post(
|
| 2005 |
+
f"https://chatgpt.com{path}",
|
| 2006 |
+
json={
|
| 2007 |
+
"checkout_session_id": checkout["cs_id"],
|
| 2008 |
+
"checkout_email": billing["email"],
|
| 2009 |
+
"billing_country": "IN",
|
| 2010 |
+
"billing_name": billing["name"],
|
| 2011 |
+
"currency": "INR",
|
| 2012 |
+
"tax_id": None,
|
| 2013 |
+
"processor_entity": checkout["processor_entity"],
|
| 2014 |
+
"billing_address": {
|
| 2015 |
+
"line1": billing["line1"],
|
| 2016 |
+
"city": billing["city"],
|
| 2017 |
+
"country": "IN",
|
| 2018 |
+
"postal_code": billing["postal_code"],
|
| 2019 |
+
"state": billing.get("state", ""),
|
| 2020 |
+
},
|
| 2021 |
+
},
|
| 2022 |
+
headers={
|
| 2023 |
+
"Referer": f"https://chatgpt.com/checkout/{checkout['processor_entity']}/{checkout['cs_id']}",
|
| 2024 |
+
"x-openai-target-path": path,
|
| 2025 |
+
"x-openai-target-route": path,
|
| 2026 |
+
},
|
| 2027 |
+
timeout=DEFAULT_TIMEOUT,
|
| 2028 |
+
)
|
| 2029 |
+
finally:
|
| 2030 |
+
if original_proxy:
|
| 2031 |
+
set_proxy_url(chatgpt, original_proxy)
|
| 2032 |
+
else:
|
| 2033 |
+
clear_proxy_url(chatgpt)
|
| 2034 |
+
if response.status_code >= 400:
|
| 2035 |
+
raise HTTPException(status_code=response.status_code, detail=http_error_detail("UPI checkout taxes failed", response))
|
| 2036 |
+
|
| 2037 |
+
stripe = build_stripe_session(req, proxy_override=provider_proxy)
|
| 2038 |
+
tax_response = stripe.post(
|
| 2039 |
+
f"https://api.stripe.com/v1/payment_pages/{checkout['cs_id']}",
|
| 2040 |
+
data={
|
| 2041 |
+
"eid": "NA",
|
| 2042 |
+
"tax_region[country]": "IN",
|
| 2043 |
+
"tax_region[state]": billing.get("state", ""),
|
| 2044 |
+
"tax_region[postal_code]": billing["postal_code"],
|
| 2045 |
+
"tax_region[line1]": billing["line1"],
|
| 2046 |
+
"tax_region[city]": billing["city"],
|
| 2047 |
+
"key": stripe_pk,
|
| 2048 |
+
"_stripe_version": stripe_version_for("upi"),
|
| 2049 |
+
},
|
| 2050 |
+
timeout=DEFAULT_TIMEOUT,
|
| 2051 |
+
)
|
| 2052 |
+
if tax_response.status_code >= 400:
|
| 2053 |
+
raise HTTPException(status_code=tax_response.status_code, detail=http_error_detail("UPI Stripe tax region failed", tax_response))
|
| 2054 |
+
log_info("upi.tax_context.done", cs_id=checkout["cs_id"], country="IN")
|
| 2055 |
+
|
| 2056 |
+
|
| 2057 |
def stripe_error_fields(response: Any) -> dict[str, str]:
|
| 2058 |
try:
|
| 2059 |
payload = response.json() or {}
|
|
|
|
| 4495 |
checkout_status=confirm_payload.get("status", "") if isinstance(confirm_payload, dict) else "",
|
| 4496 |
checkout_state=confirm_payload.get("state", "") if isinstance(confirm_payload, dict) else "",
|
| 4497 |
)
|
| 4498 |
+
upi_mode = normalize_link_type(req.link_type) == "upi"
|
| 4499 |
+
approve_proxy = provider_stage_proxy(req) if upi_mode else approve_stage_proxy(req)
|
| 4500 |
apply_approve_proxy(chatgpt, approve_proxy)
|
| 4501 |
runtime_config = get_runtime_config()
|
| 4502 |
approve_pool_mode = normalize_proxy_pool_mode(req.proxy_pool_mode or runtime_config.get("proxyPoolMode"))
|
|
|
|
| 4514 |
stripe_route="provider_proxy" if getattr(stripe, "proxies", {}) else "direct",
|
| 4515 |
approve_route="approve_proxy" if approve_proxy else "direct",
|
| 4516 |
)
|
| 4517 |
+
try:
|
| 4518 |
+
return stripe_payment_page_redirect_url(stripe, cs_id, stripe_pk, req, ctx, timeout_seconds=45)
|
| 4519 |
+
except HTTPException as exc:
|
| 4520 |
+
if not upi_mode or exc.status_code not in {502, 504}:
|
| 4521 |
+
raise
|
| 4522 |
+
log_warning(
|
| 4523 |
+
"provider.upi.second_confirm.start",
|
| 4524 |
+
cs_id=cs_id,
|
| 4525 |
+
detail=str(exc.detail),
|
| 4526 |
+
provider_proxy=provider_stage_proxy(req),
|
| 4527 |
+
)
|
| 4528 |
+
refreshed_init = stripe_init(
|
| 4529 |
+
cs_id,
|
| 4530 |
+
req,
|
| 4531 |
+
proxy_override=provider_stage_proxy(req),
|
| 4532 |
+
stripe_pk_override=stripe_pk,
|
| 4533 |
+
stripe_key_source=str(checkout.get("stripe_key_source") or "resolved"),
|
| 4534 |
+
processor_entity=str(checkout.get("processor_entity") or ""),
|
| 4535 |
+
)
|
| 4536 |
+
require_payment_method_available(refreshed_init, "upi")
|
| 4537 |
+
enforce_zero_checkout_amount(refreshed_init, req, cs_id)
|
| 4538 |
+
refreshed_ctx = stripe_context(cs_id, refreshed_init, req)
|
| 4539 |
+
second_payload = stripe_confirm(
|
| 4540 |
+
stripe,
|
| 4541 |
+
cs_id,
|
| 4542 |
+
pm_id,
|
| 4543 |
+
stripe_pk,
|
| 4544 |
+
"upi",
|
| 4545 |
+
refreshed_init,
|
| 4546 |
+
refreshed_ctx,
|
| 4547 |
+
checkout,
|
| 4548 |
+
req,
|
| 4549 |
+
str(refreshed_init.get("stripe_hosted_url") or stripe_hosted_url),
|
| 4550 |
+
)
|
| 4551 |
+
second_redirect = extract_redirect_to_url(second_payload)
|
| 4552 |
+
if second_redirect:
|
| 4553 |
+
log_info("provider.upi.second_confirm.done", cs_id=cs_id, has_redirect=True)
|
| 4554 |
+
return second_redirect
|
| 4555 |
+
terminal_error = stripe_terminal_intent_error(second_payload)
|
| 4556 |
+
if terminal_error:
|
| 4557 |
+
raise HTTPException(status_code=402, detail=terminal_error)
|
| 4558 |
+
log_info("provider.upi.second_confirm.poll", cs_id=cs_id)
|
| 4559 |
+
return stripe_payment_page_redirect_url(
|
| 4560 |
+
stripe,
|
| 4561 |
+
cs_id,
|
| 4562 |
+
stripe_pk,
|
| 4563 |
+
req,
|
| 4564 |
+
refreshed_ctx,
|
| 4565 |
+
timeout_seconds=45,
|
| 4566 |
+
)
|
| 4567 |
log_info(
|
| 4568 |
"provider.redirect.poll_required",
|
| 4569 |
submission_state=submission.get("state") if isinstance(submission, dict) else "",
|
|
|
|
| 6243 |
processor_entity=checkout.get("processor_entity", ""),
|
| 6244 |
)
|
| 6245 |
require_payment_method_available(init_payload, "ideal")
|
| 6246 |
+
elif link_type == "upi":
|
| 6247 |
+
advance(50, "IN 税务同步", "正在同步印度账单与 Stripe 税务地区")
|
| 6248 |
+
update_upi_tax_context(chatgpt, checkout, req, stripe_pk, post_checkout_proxy)
|
| 6249 |
+
stop_if_requested()
|
| 6250 |
+
init_payload = stripe_init(
|
| 6251 |
+
checkout["cs_id"],
|
| 6252 |
+
req,
|
| 6253 |
+
proxy_override=post_checkout_proxy,
|
| 6254 |
+
stripe_pk_override=stripe_pk,
|
| 6255 |
+
stripe_key_source=stripe_key_source,
|
| 6256 |
+
processor_entity=checkout.get("processor_entity", ""),
|
| 6257 |
+
)
|
| 6258 |
+
require_payment_method_available(init_payload, "upi")
|
| 6259 |
enforce_zero_checkout_amount(init_payload, req, checkout["cs_id"])
|
| 6260 |
stop_if_requested()
|
| 6261 |
checkout_amount = expected_amount(init_payload)
|
tests/test_app.py
CHANGED
|
@@ -548,6 +548,9 @@ class PaymentFlowTests(unittest.TestCase):
|
|
| 548 |
call_order.append("promotion")
|
| 549 |
return {}
|
| 550 |
|
|
|
|
|
|
|
|
|
|
| 551 |
with (
|
| 552 |
patch.object(app, "build_chatgpt_session", return_value=object()),
|
| 553 |
patch.object(app, "create_checkout", return_value=checkout),
|
|
@@ -555,6 +558,7 @@ class PaymentFlowTests(unittest.TestCase):
|
|
| 555 |
patch.object(app, "stripe_init", side_effect=fake_stripe_init),
|
| 556 |
patch.object(app, "promotion_stage_proxy", return_value=""),
|
| 557 |
patch.object(app, "update_checkout_promotion", side_effect=fake_promotion),
|
|
|
|
| 558 |
patch.object(
|
| 559 |
app,
|
| 560 |
"create_provider_link",
|
|
@@ -568,10 +572,76 @@ class PaymentFlowTests(unittest.TestCase):
|
|
| 568 |
):
|
| 569 |
response = app.build_long_link_response(req)
|
| 570 |
|
| 571 |
-
self.assertEqual(call_order, ["stripe_init", "promotion", "stripe_init"])
|
| 572 |
self.assertEqual(response.checkout_amount, "0")
|
| 573 |
self.assertFalse(response.fallback)
|
| 574 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
def test_cdk_task_requires_matching_header(self):
|
| 576 |
code = "CDK-UNITTEST1234"
|
| 577 |
task = {"owner_role": "cdk", "owner": app.cdk_owner(code)}
|
|
|
|
| 548 |
call_order.append("promotion")
|
| 549 |
return {}
|
| 550 |
|
| 551 |
+
def fake_upi_tax(*args, **kwargs):
|
| 552 |
+
call_order.append("upi_tax")
|
| 553 |
+
|
| 554 |
with (
|
| 555 |
patch.object(app, "build_chatgpt_session", return_value=object()),
|
| 556 |
patch.object(app, "create_checkout", return_value=checkout),
|
|
|
|
| 558 |
patch.object(app, "stripe_init", side_effect=fake_stripe_init),
|
| 559 |
patch.object(app, "promotion_stage_proxy", return_value=""),
|
| 560 |
patch.object(app, "update_checkout_promotion", side_effect=fake_promotion),
|
| 561 |
+
patch.object(app, "update_upi_tax_context", side_effect=fake_upi_tax),
|
| 562 |
patch.object(
|
| 563 |
app,
|
| 564 |
"create_provider_link",
|
|
|
|
| 572 |
):
|
| 573 |
response = app.build_long_link_response(req)
|
| 574 |
|
| 575 |
+
self.assertEqual(call_order, ["stripe_init", "promotion", "stripe_init", "upi_tax", "stripe_init"])
|
| 576 |
self.assertEqual(response.checkout_amount, "0")
|
| 577 |
self.assertFalse(response.fallback)
|
| 578 |
|
| 579 |
+
def test_upi_approve_uses_provider_and_second_confirms_after_empty_poll(self):
|
| 580 |
+
provider_proxy = "http://buyer-region-IN-session-provider:secret@proxy.example:8080"
|
| 581 |
+
checkout_proxy = "http://buyer-region-IN-session-checkout:secret@proxy.example:8080"
|
| 582 |
+
req = app.LongLinkRequest(
|
| 583 |
+
accessToken="token-value",
|
| 584 |
+
link_type="upi",
|
| 585 |
+
billing_country="IN",
|
| 586 |
+
pmCountry="IN",
|
| 587 |
+
checkoutProxy=checkout_proxy,
|
| 588 |
+
approveProxy=checkout_proxy,
|
| 589 |
+
providerProxy=provider_proxy,
|
| 590 |
+
checkout_ui_mode="custom",
|
| 591 |
+
)
|
| 592 |
+
confirm_payload = {"submission_attempt": {"state": "requires_approval"}}
|
| 593 |
+
refreshed_init = {
|
| 594 |
+
"init_checksum": "fresh-checksum",
|
| 595 |
+
"currency": "inr",
|
| 596 |
+
"total_summary": {"due": 0},
|
| 597 |
+
"payment_method_types": ["upi"],
|
| 598 |
+
"stripe_hosted_url": "https://checkout.stripe.com/c/pay/cs_upi_test",
|
| 599 |
+
}
|
| 600 |
+
checkout = {
|
| 601 |
+
"cs_id": "cs_upi_test",
|
| 602 |
+
"processor_entity": "openai_ie",
|
| 603 |
+
"stripe_key_source": "test",
|
| 604 |
+
}
|
| 605 |
+
applied = []
|
| 606 |
+
|
| 607 |
+
with (
|
| 608 |
+
patch.object(app, "apply_approve_proxy", side_effect=lambda session, proxy: applied.append(proxy)),
|
| 609 |
+
patch.object(app, "chatgpt_approve"),
|
| 610 |
+
patch.object(app, "get_runtime_config", return_value=fake_runtime_config()),
|
| 611 |
+
patch.object(
|
| 612 |
+
app,
|
| 613 |
+
"stripe_payment_page_redirect_url",
|
| 614 |
+
side_effect=[HTTPException(status_code=504, detail="no redirect"), "https://hooks.stripe.com/upi/instructions"],
|
| 615 |
+
),
|
| 616 |
+
patch.object(app, "stripe_init", return_value=refreshed_init),
|
| 617 |
+
patch.object(app, "stripe_confirm", return_value={"status": "open", "state": "active"}) as second_confirm,
|
| 618 |
+
):
|
| 619 |
+
result = app.redirect_url_after_confirm(
|
| 620 |
+
object(),
|
| 621 |
+
FakeStripeSession([]),
|
| 622 |
+
confirm_payload,
|
| 623 |
+
"cs_upi_test",
|
| 624 |
+
"pm_upi_test",
|
| 625 |
+
"pk_test_value",
|
| 626 |
+
{"init_checksum": "old-checksum", "total_summary": {"due": 0}},
|
| 627 |
+
{
|
| 628 |
+
"stripe_js_id": "stripe-js-test",
|
| 629 |
+
"elements_session_id": "elements-session-test",
|
| 630 |
+
"elements_session_config_id": "config-test",
|
| 631 |
+
"checkout_amount": "0",
|
| 632 |
+
"locale": "en",
|
| 633 |
+
"checkout_ui_mode": "custom",
|
| 634 |
+
},
|
| 635 |
+
checkout,
|
| 636 |
+
req,
|
| 637 |
+
"https://checkout.stripe.com/c/pay/cs_upi_test",
|
| 638 |
+
)
|
| 639 |
+
|
| 640 |
+
self.assertEqual(applied, [provider_proxy])
|
| 641 |
+
self.assertEqual(result, "https://hooks.stripe.com/upi/instructions")
|
| 642 |
+
self.assertEqual(second_confirm.call_count, 1)
|
| 643 |
+
self.assertEqual(second_confirm.call_args.args[5]["init_checksum"], "fresh-checksum")
|
| 644 |
+
|
| 645 |
def test_cdk_task_requires_matching_header(self):
|
| 646 |
code = "CDK-UNITTEST1234"
|
| 647 |
task = {"owner_role": "cdk", "owner": app.cdk_owner(code)}
|