jebin2 commited on
Commit
02d7aa0
·
1 Parent(s): f21967a

pay failed status

Browse files
Files changed (1) hide show
  1. routers/payments.py +24 -4
routers/payments.py CHANGED
@@ -101,6 +101,7 @@ class PaymentHistoryItem(BaseModel):
101
  gateway: str
102
  created_at: str
103
  paid_at: Optional[str] = None
 
104
 
105
 
106
  class PaymentHistoryResponse(BaseModel):
@@ -404,7 +405,25 @@ async def razorpay_webhook(
404
  elif event == "payment.failed":
405
  payment = payload.get("payload", {}).get("payment", {}).get("entity", {})
406
  order_id = payment.get("order_id")
407
- error_reason = payment.get("error_description", "Payment failed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
408
 
409
  if order_id:
410
  result = await db.execute(
@@ -416,11 +435,11 @@ async def razorpay_webhook(
416
 
417
  if transaction and transaction.status == "created":
418
  transaction.status = "failed"
419
- transaction.error_message = error_reason
420
  await db.commit()
421
 
422
  logger.info(
423
- f"Webhook: Marked transaction {transaction.transaction_id} as failed"
424
  )
425
 
426
  return {"status": "ok"}
@@ -461,7 +480,8 @@ async def get_payment_history(
461
  status=txn.status,
462
  gateway=txn.gateway,
463
  created_at=txn.created_at.isoformat() if txn.created_at else None,
464
- paid_at=txn.paid_at.isoformat() if txn.paid_at else None
 
465
  ))
466
 
467
  return PaymentHistoryResponse(
 
101
  gateway: str
102
  created_at: str
103
  paid_at: Optional[str] = None
104
+ error_message: Optional[str] = None # Failure reason from webhook
105
 
106
 
107
  class PaymentHistoryResponse(BaseModel):
 
405
  elif event == "payment.failed":
406
  payment = payload.get("payload", {}).get("payment", {}).get("entity", {})
407
  order_id = payment.get("order_id")
408
+
409
+ # Razorpay provides detailed error information
410
+ error_code = payment.get("error_code", "")
411
+ error_description = payment.get("error_description", "Payment failed")
412
+ error_reason = payment.get("error_reason", "")
413
+ error_source = payment.get("error_source", "")
414
+
415
+ # Build a comprehensive error message
416
+ error_parts = []
417
+ if error_description:
418
+ error_parts.append(error_description)
419
+ if error_reason and error_reason != error_description:
420
+ error_parts.append(f"Reason: {error_reason}")
421
+ if error_source:
422
+ error_parts.append(f"Source: {error_source}")
423
+ if error_code:
424
+ error_parts.append(f"Code: {error_code}")
425
+
426
+ full_error_message = " | ".join(error_parts) if error_parts else "Payment failed"
427
 
428
  if order_id:
429
  result = await db.execute(
 
435
 
436
  if transaction and transaction.status == "created":
437
  transaction.status = "failed"
438
+ transaction.error_message = full_error_message
439
  await db.commit()
440
 
441
  logger.info(
442
+ f"Webhook: Marked transaction {transaction.transaction_id} as failed: {full_error_message}"
443
  )
444
 
445
  return {"status": "ok"}
 
480
  status=txn.status,
481
  gateway=txn.gateway,
482
  created_at=txn.created_at.isoformat() if txn.created_at else None,
483
+ paid_at=txn.paid_at.isoformat() if txn.paid_at else None,
484
+ error_message=txn.error_message
485
  ))
486
 
487
  return PaymentHistoryResponse(