Refactor provider payment ID generation in TributeService for improved uniqueness
- Updated the logic to generate a unique, idempotent provider payment ID based on explicit event/payment identifiers or a combination of subscription ID and a hash of the raw payload. - This change ensures better handling of webhook events and prevents potential conflicts in payment identification.
This commit is contained in:
@@ -110,8 +110,19 @@ 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":
|
||||||
# Build a stable provider payment id from subscription and timestamps
|
# Use a unique, idempotent provider payment id per webhook event
|
||||||
provider_payment_id = str(data.get("subscription_id"))
|
# Prefer explicit event/payment identifiers if present; otherwise fall back to payload hash suffix
|
||||||
|
candidate_event_id = (
|
||||||
|
str(data.get("event_id") or data.get("payment_id") or data.get("purchase_id") or data.get("invoice_id") or "")
|
||||||
|
)
|
||||||
|
if candidate_event_id:
|
||||||
|
provider_payment_id = candidate_event_id
|
||||||
|
else:
|
||||||
|
# Combine subscription_id (if any) with a stable hash of the raw payload to ensure uniqueness per event
|
||||||
|
sub_id_part = str(data.get("subscription_id") or "sub")
|
||||||
|
payload_hash = hashlib.sha256(raw_body).hexdigest()[:16]
|
||||||
|
provider_payment_id = f"{sub_id_part}:{payload_hash}"
|
||||||
|
|
||||||
# Idempotent ensure payment
|
# Idempotent ensure payment
|
||||||
payment_record = await payment_dal.ensure_payment_with_provider_id(
|
payment_record = await payment_dal.ensure_payment_with_provider_id(
|
||||||
session,
|
session,
|
||||||
|
|||||||
Reference in New Issue
Block a user