Skip to content

Commit

Permalink
feat: handle subscription charged webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
NagariaHussain committed Jun 26, 2024
1 parent 4c2ccbf commit 3b3edd3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def handle_webhook_event(self, event: str, payload: dict):
self.record_charge_for_subscription(payload)

self.save()
elif event == RazorpaySubscriptionWebhookEvents.SubscriptionCharged:
subscription_entity = payload["subscription"]["entity"]
self.status = frappe.unscrub(subscription_entity.get("status"))
self.record_charge_for_subscription(payload)
self.save()

def record_charge_for_subscription(self, webhook_payload: dict):
payment_entity = webhook_payload["payment"]["entity"]
Expand All @@ -125,5 +130,6 @@ def record_charge_for_subscription(self, webhook_payload: dict):
"method": payment_entity["method"],
"contact": payment_entity["contact"],
"customer_id": payment_entity.get("customer_id"),
"status": "Paid",
}
).save()
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,30 @@ def test_activated_with_upfront_charge_webhook(self):
"order_id": "test_subscription_upfront_order_id",
"payment_id": "test_subscription_upfront_payment_id",
"invoice_id": "test_subscription_upfront_invoice_id",
"status": "Paid",
},
)
self.assertIsNotNone(charge_order)

def test_subscription_charged_webhook(self):
self.trigger_webhook_handler_with_sample_payload("subscription.charged")

status = frappe.db.get_value(
"Razorpay Subscription",
self.test_subscription.name,
"status",
)
self.assertEqual(status, "Active")

charge_order = frappe.db.exists(
"Razorpay Order",
{
"subscription": self.test_subscription.name,
"type": "Subscription",
"order_id": "test_subscription_upfront_order_id",
"payment_id": "test_subscription_upfront_payment_id",
"invoice_id": "test_subscription_upfront_invoice_id",
"status": "Paid",
},
)
self.assertIsNotNone(charge_order)
Expand Down Expand Up @@ -346,13 +370,13 @@ def process_payload_string(webhook_payload, subscription_id):
},
"payment": {
"entity": {
"id": "pay_DEXFWroJ6LikKT",
"id": "test_subscription_upfront_payment_id",
"entity": "payment",
"amount": 100000,
"currency": "INR",
"status": "captured",
"order_id": "order_DEXFWXwO24pDxH",
"invoice_id": "inv_DEXFWVuM6rPqlK",
"order_id": "test_subscription_upfront_order_id",
"invoice_id": "test_subscription_upfront_invoice_id",
"international": false,
"method": "card",
"amount_refunded": 0,
Expand Down

0 comments on commit 3b3edd3

Please sign in to comment.