Search code examples
djangopaypalpaypal-ipndjango-2.0

implementing PayPal IPN in Django Application


I'm writing an application in Django 2.0

It is a multi membership application. I have to implement PayPal payment method and activate the membership based on payment received.

For this, I have created a button in PayPal with generates code as

upgrade.html

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="<button-id>">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

and views.py is

class Pricing(TemplateView):
    template_name = 'membership/pricing.html'


class Upgrade(TemplateView):
    template_name = 'membership/upgrade.html'


class PaymentSuccess(TemplateView):
    template_name = 'membership/payment-success.html'


class PaymentFailed(TemplateView):
    template_name = 'membership/payment-failed.html'


def paypal_ipn(request):
    # IPN response goes here

and urls.py

urlpatterns = [
    path('pricing/', Pricing.as_view(), name='pricing'),
    path('upgrade/', Upgrade.as_view(), name='upgrade'),
    path('payment-success/', PaymentSuccess.as_view(), name='payment-success'),
    path('payment-failed/', PaymentFailed.as_view(), name='payment-failed'),
    path('paypal-ipn/', paypal_ipn, name='paypal-ipn'),
]

All is working fine as Payment is being made and user is redirected to the payment-success page.

But How do I receive the payment confirmation so that I could process the membership and record the transaction data in the database?

I have enabled instant payment notification from the settings in paypal sandbox accoun on url https://example.com/paypal-ipn/

I do not want to use django-paypal plugin due to some limitations.


Solution

  • You can create your own IPN handling page to receive PayPal IPN message. IPN Sample code below.

    https://github.com/paypal/ipn-code-samples