fix: harden payment provider handling
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import orjson
|
||||
from dataclasses import replace
|
||||
from datetime import datetime, timezone
|
||||
from decimal import Decimal
|
||||
from decimal import Decimal,InvalidOperation
|
||||
from typing import Any
|
||||
import aiohttp
|
||||
from aiohttp import BasicAuth, ClientTimeout
|
||||
@@ -72,8 +72,10 @@ class ItPayClient(IItPayService):
|
||||
body_raw = response_json.get('data')
|
||||
body = body_raw if isinstance(body_raw, dict) else response_json
|
||||
|
||||
status = str(body['status']).strip().lower()
|
||||
itpay_id = str(body['id'])
|
||||
status = str(body.get('status') or '').strip().lower()
|
||||
itpay_id = str(body.get('id') or '').strip()
|
||||
if not status or not itpay_id:
|
||||
raise PaymentProviderException(message='Payment provider response invalid')
|
||||
|
||||
if status == 'cancelled':
|
||||
return replace(order, status=OrderStatus.CANCELLED, itpay_id=itpay_id)
|
||||
@@ -82,19 +84,23 @@ class ItPayClient(IItPayService):
|
||||
if status == 'error':
|
||||
return replace(order, status=OrderStatus.ERROR, itpay_id=itpay_id)
|
||||
|
||||
qrc_id = str(body['qrc_id'])
|
||||
itpay_amount = Decimal(str(body['amount']))
|
||||
qrc_id = str(body.get('qrc_id') or '').strip()
|
||||
if not qrc_id:
|
||||
raise PaymentProviderException(message='Payment provider response invalid')
|
||||
itpay_amount = Decimal(str(body.get('amount')))
|
||||
|
||||
created_norm = str(body['created']).replace('Z', '+00:00')
|
||||
created_norm = str(body.get('created') or '').replace('Z', '+00:00')
|
||||
itpay_created_at = datetime.fromisoformat(created_norm)
|
||||
|
||||
payment_qr_urls = body['payment_qr_urls']
|
||||
payment_qr_urls = body.get('payment_qr_urls')
|
||||
if isinstance(payment_qr_urls, str):
|
||||
payment_qr_urls = orjson.loads(payment_qr_urls)
|
||||
|
||||
payment_qr_images = body['payment_qr_images']
|
||||
payment_qr_images = body.get('payment_qr_images')
|
||||
if isinstance(payment_qr_images, str):
|
||||
payment_qr_images = orjson.loads(payment_qr_images)
|
||||
if not isinstance(payment_qr_urls,dict) or not isinstance(payment_qr_images,dict):
|
||||
raise PaymentProviderException(message='Payment provider response invalid')
|
||||
|
||||
return replace(
|
||||
order,
|
||||
@@ -111,5 +117,7 @@ class ItPayClient(IItPayService):
|
||||
)
|
||||
except ApplicationException:
|
||||
raise
|
||||
except aiohttp.ClientError:
|
||||
except (TimeoutError,aiohttp.ClientError):
|
||||
raise PaymentProviderException(message='Payment provider unreachable')
|
||||
except (KeyError,ValueError,TypeError,InvalidOperation,orjson.JSONDecodeError) as exception:
|
||||
raise PaymentProviderException(message='Payment provider response invalid') from exception
|
||||
|
||||
Reference in New Issue
Block a user