feat(payment): expire pending orders after one hour
This commit is contained in:
@@ -7,6 +7,7 @@ from threading import Barrier
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pytest
|
||||
from datetime import datetime, timedelta
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from database import SessionLocal
|
||||
@@ -16,6 +17,7 @@ from routers.chat import _resolve_reply, _stream_reply
|
||||
from services.token_billing import (
|
||||
DEFAULT_TOKEN_GRANT,
|
||||
InsufficientTokensError,
|
||||
close_expired_pending_orders,
|
||||
get_or_create_account,
|
||||
release_reservation,
|
||||
reserve_avatar_tokens,
|
||||
@@ -107,7 +109,7 @@ def test_org_recharge_only_credits_after_verified_provider_order(authorization_c
|
||||
provider_no = "261016111200090123456"
|
||||
created_data = {"orderNo": provider_no, "chargeType": 9,
|
||||
"chargeAmount": 10, "payStatus": 1}
|
||||
with patch("routers.tokens.HuihuiOrgPaymentClient") as client_factory:
|
||||
with patch.dict(os.environ, {"HUIHUI_ORG_BASE_URL": "https://fat-open.99hui.com/api/huihuiorg"}), patch("routers.tokens.HuihuiOrgPaymentClient") as client_factory:
|
||||
provider = client_factory.return_value
|
||||
provider.create_order.return_value = created_data
|
||||
created = client.post(
|
||||
@@ -117,18 +119,65 @@ def test_org_recharge_only_credits_after_verified_provider_order(authorization_c
|
||||
assert created["providerOrderNo"] == provider_no
|
||||
assert created["status"] == "pending"
|
||||
assert created["balance"] == DEFAULT_TOKEN_GRANT
|
||||
assert provider.create_order.call_args.kwargs["pay_type"] == 1
|
||||
assert created["checkoutToken"] == f"huihui-payment-{context['suffix']}"
|
||||
assert "pay_type" not in provider.create_order.call_args.kwargs
|
||||
|
||||
details = {**created_data, "payStatus": 2,
|
||||
"userId": context["owner"].huihui_user_id}
|
||||
provider.get_order.return_value = {**details, "chargeAmount": 9}
|
||||
assert client.get(f"/api/token/payment/{created['id']}", headers=context["owner_headers"]).json()["data"]["status"] == "pending"
|
||||
provider.get_order.return_value = details
|
||||
paid = client.get(f"/api/token/payment/{created['id']}", headers=context["owner_headers"]).json()["data"]
|
||||
assert paid["status"] == "paid"
|
||||
assert paid["balance"] == DEFAULT_TOKEN_GRANT + 2_000_000
|
||||
duplicate = client.get(f"/api/token/payment/{created['id']}", headers=context["owner_headers"]).json()["data"]
|
||||
assert duplicate["balance"] == paid["balance"]
|
||||
unchanged = client.get(f"/api/token/payment/{created['id']}", headers=context["owner_headers"]).json()["data"]
|
||||
assert unchanged["status"] == "pending"
|
||||
assert unchanged["balance"] == DEFAULT_TOKEN_GRANT
|
||||
provider.get_order.assert_not_called()
|
||||
|
||||
|
||||
def test_org_recharge_does_not_return_checkout_token_outside_test_environment(authorization_context):
|
||||
seed()
|
||||
context = authorization_context
|
||||
_enable_huihui_payment_login(context)
|
||||
with patch.dict(os.environ, {"HUIHUI_ORG_BASE_URL": "https://open.99hui.com/api/huihuiorg"}), patch(
|
||||
"routers.tokens.HuihuiOrgPaymentClient"
|
||||
) as client_factory:
|
||||
client_factory.return_value.create_order.return_value = {
|
||||
"orderNo": "261016111200090123457", "chargeType": 9,
|
||||
"chargeAmount": 10, "payStatus": 1,
|
||||
}
|
||||
response = client.post(
|
||||
"/api/token/charge/org", headers=context["owner_headers"],
|
||||
json={"planId": "1", "paymentMethod": "wechat", "payScene": "APP"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert "checkoutToken" not in response.json()["data"]
|
||||
|
||||
|
||||
def test_expired_pending_payment_orders_are_closed_without_touching_recent_orders(authorization_context):
|
||||
context = authorization_context
|
||||
db = SessionLocal()
|
||||
try:
|
||||
expired = TokenPaymentOrder(
|
||||
order_no=f"EXPIRED-{uuid.uuid4().hex}", user_id=context["owner"].id, plan_id="1",
|
||||
payment_method="wechat", pay_type="WECHAT", pay_way="H5", points_amount=100,
|
||||
price_cents=100, status="pending", provider="huihui_org",
|
||||
created_at=datetime.utcnow() - timedelta(hours=1, seconds=1),
|
||||
)
|
||||
recent = TokenPaymentOrder(
|
||||
order_no=f"RECENT-{uuid.uuid4().hex}", user_id=context["owner"].id, plan_id="1",
|
||||
payment_method="wechat", pay_type="WECHAT", pay_way="H5", points_amount=100,
|
||||
price_cents=100, status="pending", provider="huihui_org",
|
||||
created_at=datetime.utcnow() - timedelta(minutes=59),
|
||||
)
|
||||
db.add_all([expired, recent])
|
||||
db.commit()
|
||||
|
||||
assert close_expired_pending_orders(db) == 1
|
||||
db.refresh(expired)
|
||||
db.refresh(recent)
|
||||
assert expired.status == "closed"
|
||||
assert expired.failure_reason == "支付超时(超过1小时)"
|
||||
assert recent.status == "pending"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def test_success_callback_credits_once_and_status_is_user_scoped(authorization_context):
|
||||
|
||||
Reference in New Issue
Block a user