1. Install django-paypal
There are a couple of version floating around namely:
Cramer's
is a fork from Boxall's version so it's more update. For this tutorial,
we will be using Boxall because we found Boxall's first.
- git clone git://github.com/johnboxall/django-paypal.git paypal
2. Create a PayPal developer account
- Goto https://developer.paypal.com and sign up.
- Once you're in, create a preconfigured account.
- Do this twice, one for account type Buyer and one for account type Seller
- Your dashboard should then look like this:
3. Modify settings.py file
- For the PAYPAL_RECEIVER_EMAIL, set this to the business account email you created in step 2. In this example, it is naiyun_1311845021_biz@od-eon.com
- After you have entered this, run syncdb to create the paypal tables
-
# settings.py
-
...
-
INSTALLED_APPS = (... 'paypal.standard.ipn', ...)
-
...
-
PAYPAL_RECEIVER_EMAIL = "naiyun_1311845021_biz@od-eon.com" #change this to yours
4. Create a url
# urls.py
-
from django.conf.urls.defaults import patterns, include, url
-
-
urlpatterns = patterns('',
-
# Examples:
-
url(r'^$', 'cat.homepage.views.home', name='home'),
-
url(r'^paypal/$', 'cat.homepage.views.paypal', name='paypal'),
-
-
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
-
url(r'^admin/', include(admin.site.urls)),
-
)
-
-
urlpatterns += patterns('',
-
(r'^something/hard/to/guess/', include('paypal.standard.ipn.urls')),
-
)
- Replace cat.homepage with your project.appname
5. Create a view and template
-
# views.py
-
...
-
from paypal.standard.forms import PayPalPaymentsForm
-
from django.conf import settings
-
from django.core.urlresolvers import reverse
-
-
def paypal(request):
-
-
# What you want the button to do.
-
-
paypal_dict = {
-
"business": settings.PAYPAL_RECEIVER_EMAIL,
-
"amount": "1.00",
-
"item_name": "name of the item",
-
"invoice": "unique-invoice-id",
-
"notify_url": "%s%s" % (settings.SITE_NAME, reverse('paypal-ipn')),
-
"return_url": "http://www.example.com/your-return-location/"
-
"cancel_return": "http://www.example.com/your-cancel-location/",
-
}
-
-
# Create the instance.
-
form = PayPalPaymentsForm(initial=paypal_dict)
-
context = {"form": form.sandbox()}
-
return render_to_response("paypal.html", context)
-
-
# paypal.html
-
-
{{ form }}
- Don't worry about what to put for settings.SITE_NAME first, we'll get to that in a bit
- The notify_url is the url that PayPal will try to send a POST request
- return_url is the page that Paypal redirects you to when the transaction is complete
- The form key in context renders a PayPal button. Right now we're using form.sandbox() to initiate the test process. Change this to form.render() to send the user to the real PayPal site
- You can add a "custom" key and value to the paypal_dict if you want to add a custom field later on
- If you're doing this test multiple times, you might run into an error message given by Paypal that says something like: This invoice has already been paid. For more information, please contact the merchant. To get around this, just change the invoice value everytime you try to make a purchase.
6. Now we fix the SITE_NAME
Sign up for an account at www.dyndns.com
- Add a new hostname. For service type, choose Host with IP address. Next, click on the link to populate the IP Address
- You should have something that looks like this:
- Add the Hostname to your settings.py file as SITE_NAME like so
-
# settings.py
-
...
-
SITE_NAME = 'http://nai.dyndns-free.com'
-
...
7. Run Django development server on your local IP
- For this to work, you need to configure your router to open port 80. The normal process is roughly like this:
- Goto your router IP e.g. 192.168.1.1
- Look for firewall or port forwarding
- Create an exception called 'django' or whatever with port 80 open
- Add this exception to the list of allowed applications that is tied to your computer
- Save
- Now, run Django's development server using sudo /etc/init.d/apache2 stop; sudo ./manage.py runserver 192.168.2.102:80
- Change 192.168.2.102:80 to your own IP address which you can locate by running ifconfig in bash. Retain port 80
- If all goes well, you should be able to open up in your browser the hostname you created at dyndns. In my case, nai.dyndns-free.com/paypal looks like this:
8. What goes on under the hood?
When
someone uses this button to buy something PayPal makes a HTTP POST to
your "notify_url" which comes as part of the Paypal package. PayPal
calls this Instant Payment Notification (IPN). The view
paypal.standard.ipn.views.ipn handles IPN processing. We have already
set the notify_url in step 4 as: urlpatterns += patterns('',(r'^something/hard/to/guess/', include('paypal.standard.ipn.urls')),)
When the notify_url is called, the ipn view is executed as shown below. You will need to include @csrf_exempt in the view as well
-
@require_POST
-
@csrf_exempt
-
def ipn(request, item_check_callable=None):
-
-
"""
-
PayPal IPN endpoint (notify_url).
-
Used by both PayPal Payments Pro and Payments Standard to confirm transactions.
-
http://tinyurl.com/d9vu9d
-
-
PayPal IPN Simulator:
-
https://developer.paypal.com/cgi-bin/devscr?cmd=_ipn-link-session
-
"""
-
-
flag = None
-
ipn_obj = None
-
form = PayPalIPNForm(request.POST)
-
if form.is_valid():
-
try:
-
ipn_obj = form.save(commit=False)
-
except Exception, e:
-
flag = "Exception while processing. (%s)" % e
-
else:
-
flag = "Invalid form. (%s)" % form.errors
-
-
if ipn_obj is None:
-
ipn_obj = PayPalIPN()
-
-
ipn_obj.initialize(request)
-
-
if flag is not None:
-
ipn_obj.set_flag(flag)
-
else:
-
# Secrets should only be used over SSL.
-
if request.is_secure() and 'secret' in request.GET:
-
ipn_obj.verify_secret(form, request.GET['secret'])
-
else:
-
try:
-
ipn_obj.verify(item_check_callable)
-
except Exception, e:
-
flag = "Exception while processing. (%s)" % e
-
ipn_obj.save()
-
-
return HttpResponse("OKAY")
Just to elaborate a bit on the view, this line here ipn_obj.verify(item_check_callable) is the part that will invoke the sending of the signals. The verify() method can be found in paypal/models/standard/models.py which in turn calls the send_signals() method which can be found in paypal/models/standard/ipn/models.py
9. Final Stretch
So
now we need to set up something to receive these signals. This can live
anywhere in the project. The examples use models, so lets go with that.
-
# models.py
-
...
-
from paypal.standard.ipn.signals import payment_was_successful
-
-
def show_me_the_money(sender, **kwargs):
-
ipn_obj = sender
-
# Undertake some action depending upon `ipn_obj`.
-
if ipn_obj.custom == "Upgrade all users!":
-
Users.objects.update(paid=True)
-
print __file__,1, 'This works'
-
payment_was_successful.connect(show_me_the_money)
- If everything works ok when we click the buy now button, console should print the line 'This works'
- So, go back to your domain/paypal page. Click on the buy button link, it should re-direct you to the Paypal sandbox page. Log in using the personal account you created in step 2. Mine is naiyun_1311850509_per@od-eon.com
- Go through the buying process and if everything works, you should see something like this in your console
-
[05/Aug/2011 03:06:50] "GET /paypal/ HTTP/1.1" 200 1075
-
/home/nai/GitProjects/cat/homepage/models.pyc 1 This works
-
[05/Aug/2011 03:08:02] "POST /something/hard/to/guess/ HTTP/1.0" 200 4
Hope this tutorial has been helpful. If you
have any questions, do leave them in the comments and I 'll do my best
to answer them.
UPDATE
Just in case anyone is running into DB related problems, django-paypal uses South to handle it's model creation. So running ./manage.py syncdb will *not* create the Paypal related tables.
kayseriescortu.com - alacam.org - xescortun.com
YanıtlaSilEN SON ÇIKAN PERDE MODELLERİ
YanıtlaSilsms onay
mobil ödeme bozdurma
nftnasilalinir
ANKARA EVDEN EVE NAKLİYAT
trafik sigortası
DEDEKTOR
web sitesi kurma
aşk kitapları