getting webhook events into database 2

This commit is contained in:
rizwanisready
2024-04-19 16:05:47 +05:30
parent 2466736936
commit 9cd0b9ef7c
4 changed files with 22 additions and 1 deletions

View File

@@ -285,7 +285,7 @@ class RegistrationPasswordView(APIView):
token_data["type"] = str(principal.principal_type)
return ApiResponse.success(
message=constants.REGISTRATION_SUCCESS, data=token_data
message="Password updated successfully.", data=token_data
)

View File

@@ -176,6 +176,7 @@ class StripeWebhookTest(APIView):
try:
event = stripe.Event.construct_from(json.loads(payload), stripe.api_key)
event_id = event["id"]
event_type = event["type"]
# Check if the event has been processed already
if WebhookEvent.objects.filter(event_id=event_id, status="processed").exists():
return ApiResponse.success(
@@ -186,6 +187,7 @@ class StripeWebhookTest(APIView):
# Log event processing
WebhookEvent.objects.create(
event_id=event_id,
event_type=event_type,
event_payload=json.loads(payload), # optional
)

View File

@@ -0,0 +1,18 @@
# Generated by Django 5.0.2 on 2024-04-19 10:28
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("manage_subscriptions", "0004_webhookevent"),
]
operations = [
migrations.AddField(
model_name="webhookevent",
name="event_type",
field=models.CharField(blank=True, max_length=255, null=True),
),
]

View File

@@ -78,6 +78,7 @@ class PrincipalSubscription(BaseModel):
class WebhookEvent(BaseModel):
event_id = models.CharField(max_length=255, unique=True, db_index=True)
event_type = models.CharField(max_length=255, null=True, blank=True)
received_at = models.DateTimeField(auto_now_add=True)
processed_at = models.DateTimeField(null=True, blank=True)
status = models.CharField(