fix: acknowledge signed payment notice before querying provider
This commit is contained in:
@@ -896,19 +896,10 @@ def _process_huihui_org_notice(payload: dict, db: Session):
|
||||
raise HTTPException(status_code=409, detail="payment number conflicts with prior notice")
|
||||
if not order.provider_order_no:
|
||||
raise HTTPException(status_code=422, detail="provider order number missing")
|
||||
if not user.huihui_token or not user.huihui_user_id:
|
||||
_save_huihui_org_notice(db, order, payment_no, amount_cents, expected_product_id, "unavailable")
|
||||
return ok({"received": True, "paid": False, "awaitingProvider": True})
|
||||
try:
|
||||
provider_status = _huihui_org_provider_status(order, user, expected_product_id, payment_no)
|
||||
except HuihuiPaymentError:
|
||||
_save_huihui_org_notice(db, order, payment_no, amount_cents, expected_product_id, "unavailable")
|
||||
return ok({"received": True, "paid": False, "awaitingProvider": True})
|
||||
if provider_status not in {2, 3, 4}:
|
||||
_save_huihui_org_notice(db, order, payment_no, amount_cents, expected_product_id, str(provider_status))
|
||||
return ok({"received": True, "paid": False, "awaitingProvider": True})
|
||||
_settle_verified_huihui_org_order(db, order, payment_no, notice)
|
||||
return ok({"received": True, "paid": True})
|
||||
# The provider may not mark its order paid until it receives our HTTP 200.
|
||||
# Never synchronously query the provider before acknowledging a signed notice.
|
||||
_save_huihui_org_notice(db, order, payment_no, amount_cents, expected_product_id)
|
||||
return ok({"received": True, "paid": False, "awaitingProvider": True})
|
||||
|
||||
|
||||
def _huihui_org_provider_status(order: TokenPaymentOrder, user: User, product_id: str, payment_no: str) -> int:
|
||||
@@ -936,7 +927,7 @@ def _huihui_org_provider_status(order: TokenPaymentOrder, user: User, product_id
|
||||
|
||||
def _save_huihui_org_notice(
|
||||
db: Session, order: TokenPaymentOrder, payment_no: str, amount_cents: int,
|
||||
product_id: str, provider_status: str,
|
||||
product_id: str,
|
||||
) -> None:
|
||||
notice = db.get(HuihuiOrgPaymentNotice, order.order_no)
|
||||
if notice and (
|
||||
@@ -952,10 +943,11 @@ def _save_huihui_org_notice(
|
||||
)
|
||||
db.add(notice)
|
||||
notice.status = "awaiting_provider"
|
||||
notice.last_provider_status = provider_status
|
||||
notice.last_provider_status = "not_checked"
|
||||
notice.last_error = ""
|
||||
notice.attempts = int(notice.attempts or 0) + 1
|
||||
notice.next_check_at = datetime.utcnow() + timedelta(seconds=_huihui_notice_retry_seconds(notice.attempts))
|
||||
first_check = datetime.utcnow() + timedelta(seconds=5)
|
||||
if notice.next_check_at is None or notice.next_check_at > first_check:
|
||||
notice.next_check_at = first_check
|
||||
try:
|
||||
db.commit()
|
||||
except IntegrityError:
|
||||
|
||||
Reference in New Issue
Block a user