0

I have implemented a Paypal webhook in Django. I have difficulty keeping track of resubscription on Paypal. I am trying to use the "PAYMENT.SALE.COMPLETED" event and I have seen other questions which suggest using the same event but the next billing date (next_billing_date) in my database does not change. Please see the code below, can you tell me what I am doing wrong or do I need to use some other event.

def paypal_webhooks(request):
    try:
        if "HTTP_PAYPAL_TRANSMISSION_ID" not in request.META:
            return Response(status=400)

        auth_algo = request.META['HTTP_PAYPAL_AUTH_ALGO']
        cert_url = request.META['HTTP_PAYPAL_CERT_URL']
        transmission_id = request.META['HTTP_PAYPAL_TRANSMISSION_ID']
        transmission_sig = request.META['HTTP_PAYPAL_TRANSMISSION_SIG']
        transmission_time = request.META['HTTP_PAYPAL_TRANSMISSION_TIME']


        event_body = request.body.decode(request.encoding or "utf-8")

        valid = notifications.WebhookEvent.verify(
            transmission_id=transmission_id,
            timestamp=transmission_time,
            webhook_id=webhook_id,
            event_body=event_body,
            cert_url=cert_url,
            actual_sig=transmission_sig,
            auth_algo=auth_algo,
        )

        if not valid:
            return Response(status=400)

        webhook_event = json.loads(event_body)

        event_type = webhook_event["event_type"]

        # payment completed
        if event_type == "PAYMENT.SALE.COMPLETED":
            subscription_id = webhook_event['resource']['id']
            subscription = Subscriptions.objects.filter(subscriptionID=subscription_id).last()
            if subscription:
                subscription.active = True
                subscription.active_billing = True
                subscription.next_billing_date = webhook_event['resource']['billing_info']['next_billing_time'][:10]

                subscription.save()

        # subscription cancelled
        if event_type == "BILLING.SUBSCRIPTION.CANCELLED":
            subscription_id = webhook_event['resource']['id']
            subscription = Subscriptions.objects.filter(subscriptionID=subscription_id).last()
            if subscription:
                subscription.active_billing = False
                subscription.save()

        # subscription failed
        if event_type == "BILLING.SUBSCRIPTION.PAYMENT.FAILED":
            subscription_id = webhook_event['resource']['id']
            subscription = Subscriptions.objects.filter(subscriptionID=subscription_id).last()
            if subscription:
                subscription.active = False
                subscription.active_billing = False
                subscription.save()

                PaymentMethods.resub_free(subscription.user)

        return Response({"success": True}, status=200)
    except Exception as e:
        return Response({"error": e}, status=500)
ahsan mukhtar
  • 423
  • 7
  • 21
  • That's the event. Are you receiving the events or not? Have you debugged your code and everything that happens when the event is received? (it would seem not) – Preston PHX Apr 15 '23 at 13:04

0 Answers0