Django & PayPal integration
Asked Answered
G

5

23

I am designing a website in Python (using Django), and I need to sell things through it.

Can somebody help me with the source code to integrate the paypal-pro (do-direct payment) or else paypal-standard (express checkout)?

Giacinta answered 26/4, 2010 at 14:34 Comment(0)
W
29

You might want to try django-paypal, there's even a tutorial right there on the front page.

Waterman answered 26/4, 2010 at 14:56 Comment(4)
You sir, are a saint... +1 for the service you've done the community in editing the question :)Edelsten
i can't see tutorial there :(Lulita
How to use express checkout ? Right now only website payment pro(wpp) is working and it throws error as wpp is available in selected countries.Gabfest
@Dominic Rodger kindly take a look at this #69955500Misfortune
P
13

paypal.standard.ipn

PayPal API Generates a Button which will call its API through paypal.standard.ipn.

For API Integration you have to follow below given steps:

Install django-paypal:

pip install django-paypal

Update settings.py file:

INSTALLED_APPS = [
    'paypal.standard.ipn',
]

PAYPAL_RECEIVER_EMAIL = 'XXXXX'
PAYPAL_TEST = True

Write Email address of Receiver. PAYPAL_TEST = True means you want a Test API payment. You can write "False" for Original payment API.

Run command:

python manage.py migrate 

In 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'),*

In views.py:

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})

Follow video tutorial for django-code given in reference.

In payment_process.html:

{{ form.render }}

For calling API you have request for /payment_process/. It will generate a button on HTML which calls PayPal API for transaction. Further process will be done on PayPal end, Login or Card Payment.

Potentiometer answered 18/12, 2017 at 16:1 Comment(3)
Can you please elaborate following code because I am new on django . you used pets/....... what is pets ? is it your app or generic app ?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'),*Akkad
@SatinderSingh, "pets" is a Django app which is the main app in this project.Potentiometer
Thank you for your reply . Can we customize that button like its color or size etc . Can you give me some idea .Akkad
B
3

Did you look at pypaypal? You could create a view that connects to PayPal and submit your payment commands.

Biebel answered 26/4, 2010 at 14:43 Comment(1)
Can we send payments from a paypal personal account? All the blogs and tutorials that I have seen shows it on business account.Phototonus
P
1

Better will be to use "native" docs from owner: docs paypal

Patel answered 3/6, 2015 at 16:53 Comment(0)
O
0

This tutorial guides how to accept Paypal apps payment via sandbox ClientId & SecretKey without any third party library.

You can also send payment tracking id as custom_id in purchase_units list's dictionary object to create_order.request_body function.
As shown below:

create_order.request_body (
        {
            "intent": "CAPTURE",
            "purchase_units": [
                {
                    "custom_id": "YOUR_TRACKING_ID",
                    "amount": {
                        "currency_code": "USD",
                        "value": course.price,
                        "breakdown": {
                            "item_total": {
                                "currency_code": "USD",
                                "value": course.price
                            }
                            },
                        },                               


                }
            ]
        }
    )
Oligosaccharide answered 19/4, 2021 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.