Djanjo PayPal (2) – Standard IPN Django.How

Author avatar wrote on 01/06/2022

WAY 2 PayPal Standard IPN

+ No PayPal app should be created in PayPal
+ You change PayPal email only
– Package must be installed
– migrate is needed – changes to DB

Django-paypal provides a PayPalPaymentsForm the class which creates a standard PayPal “Buy Now” button configured for selling a single item with no shipping.

1. Create sandbox account -for testing
  • one for pesonal
  • one for business
  • https://developer.paypal.com/developer/accounts/create
  • 2. Install

    pip install django-paypal

    Add to installed app in settings.py

    INSTALLED_APPS = [
    ”’
    ‘paypal.standard.ipn’,
    ”’
    ]

    Set PayPal attributes in settings.py
    django-paypal settings

    PAYPAL_RECEIVER_EMAIL = ‘[email protected]
    PAYPAL_TEST = True

    Migrate

    python manage.py migrate

    Add to requirements.txt

    django-paypal=1.1.1

    3. Add URL to maine urls.py

    url(r’^paypal/’, include(‘paypal.standard.ipn.urls’)),
    url(r’^payment_process/$’, api_views.payment_process, name=’payment_process’ ),
    url(r’^payment_done/$’, TemplateView.as_view(template_name= “pets/payment_done.html”), name=’payment_done’),
    url(r’^payment_canceled/$’, TemplateView.as_view(template_name= “pets/payment_canceled.html”), name=’payment_canceled’),*

    4. Create views

  • from django.conf import settings
  • from django.shortcuts import render, redirect
  • from django.urls import reverse
  • Or

  • from django.core.urlresolvers import reverse
  • from django.conf import settings
  • from django.urls import reverse
  • from django.shortcuts import render, get_object_or_404
  • from paypal.standard.forms import PayPalPaymentsForm
  • 
    def payment_process(request):
        host = request.get_host()
        paypal_dict = {
            'business': settings.PAYPAL_RECEIVER_EMAIL,
            'amount': '100',	
            'item_name': 'Item_Name_xyz',
            'invoice': 'Test Payment Invoice',
            'currency_code': 'USD',
            'notify_url': 'http://{}{}'.format(host, reverse('paypal-ipn')),
            'return_url': 'http://{}{}'.format(host, reverse('payment_done')),
            'cancel_return': 'http://{}{}'.format(host, reverse('payment_canceled')),
        }
        form = PayPalPaymentsForm(initial=paypal_dict)
        return render(request, 'pets/payment_process.html', {'form': form})
    

    5. In payment_process.html

    {{ form.render }}

    Resources:
  • https://www.guguweb.com/2021/01/12/how-to-accept-paypal-payments-on-your-django-application
  • https://stackoverflow.com/questions/2714284/django-paypal-integration
  • https://www.youtube.com/watch?v=Z5dBopZWOzo&t=417s
  • https://django-paypal.readthedocs.io/en/stable/standard/ipn.html