Handle duplicate Tribute payments
This commit is contained in:
@@ -70,6 +70,16 @@ class TributeService:
|
|||||||
|
|
||||||
async with async_session_factory() as session:
|
async with async_session_factory() as session:
|
||||||
if event_name == 'new_subscription':
|
if event_name == 'new_subscription':
|
||||||
|
provider_payment_id = str(data.get('subscription_id'))
|
||||||
|
existing_payment = await payment_dal.get_payment_by_provider_payment_id(
|
||||||
|
session, provider_payment_id)
|
||||||
|
if existing_payment:
|
||||||
|
logging.info(
|
||||||
|
"Duplicate Tribute payment webhook ignored for provider_payment_id %s",
|
||||||
|
provider_payment_id,
|
||||||
|
)
|
||||||
|
payment_record = existing_payment
|
||||||
|
else:
|
||||||
payment_record = await payment_dal.create_payment_record(
|
payment_record = await payment_dal.create_payment_record(
|
||||||
session,
|
session,
|
||||||
{
|
{
|
||||||
@@ -79,7 +89,7 @@ class TributeService:
|
|||||||
'status': 'succeeded',
|
'status': 'succeeded',
|
||||||
'description': 'Tribute subscription',
|
'description': 'Tribute subscription',
|
||||||
'subscription_duration_months': months,
|
'subscription_duration_months': months,
|
||||||
'provider_payment_id': str(data.get('subscription_id')),
|
'provider_payment_id': provider_payment_id,
|
||||||
'provider': 'tribute',
|
'provider': 'tribute',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -46,6 +46,15 @@ async def get_payment_by_yookassa_id(
|
|||||||
return result.scalar_one_or_none()
|
return result.scalar_one_or_none()
|
||||||
|
|
||||||
|
|
||||||
|
async def get_payment_by_provider_payment_id(
|
||||||
|
session: AsyncSession, provider_payment_id: str) -> Optional[Payment]:
|
||||||
|
"""Fetch a payment by provider-specific identifier."""
|
||||||
|
stmt = select(Payment).where(
|
||||||
|
Payment.provider_payment_id == provider_payment_id)
|
||||||
|
result = await session.execute(stmt)
|
||||||
|
return result.scalar_one_or_none()
|
||||||
|
|
||||||
|
|
||||||
async def get_payment_by_db_id(session: AsyncSession,
|
async def get_payment_by_db_id(session: AsyncSession,
|
||||||
payment_db_id: int) -> Optional[Payment]:
|
payment_db_id: int) -> Optional[Payment]:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user