I am new to django and python.
I am developing a website where i am using Paypal for transaction. I have successfully integrated python Paypal REST SDK with my project. Here is my views.py with the integration.
def subscribe_plan(request):
exact_plan = Plan.objects.get(id = request.POST['subscribe'])
exact_validity = exact_plan.validity_period
exp_date = datetime.datetime.now()+datetime.timedelta(exact_validity)
plan = Plan.objects.get(id = request.POST['subscribe'])
subs_plan = SubscribePlan(plan = plan,user = request.user,expiriary_date = exp_date)
subs_plan.save()
logging.basicConfig(level=logging.INFO)
paypalrestsdk.configure({
"mode": "sandbox", # sandbox or live
"client_id": "AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd",
"client_secret": "EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX" })
payment = Payment({
"intent": "sale",
# ###Payer
# A resource representing a Payer that funds a payment
# Payment Method as 'paypal'
"payer": {
"payment_method": "paypal" },
# ###Redirect URLs
"redirect_urls": {
"return_url": "www.mydomain.com/execute",
"cancel_url": "www.mydomain.com/cancel" },
# ###Transaction
# A transaction defines the contract of a
# payment - what is the payment for and who
# is fulfilling it.
"transactions": [ {
# ### ItemList
"item_list": {
"items": [{
"name": exact_plan.plan,
"sku": "item",
"price": "5.00",
"currency": "USD",
"quantity": 1 }]},
"amount": {
"total": "5.00",
"currency": "USD" },
"description": "This is the payment transaction description." } ] } )
selected_plan = request.POST['subscribe']
context = RequestContext(request)
if payment.create():
print("Payment %s created successfully"%payment.id)
for link in payment.links:#Payer that funds a payment
if link.method=="REDIRECT":
redirect_url=link.href
ctx_dict = {'selected_plan':selected_plan,"payment":payment}
print("Redirect for approval: %s"%redirect_url)
return redirect(redirect_url,context)
else:
print("Error %s"%payment.error)
ctx_dict = {'selected_plan':selected_plan,"payment":payment}
return render_to_response('photo/fail.html',ctx_dict,context)
Here in the payment dictionary www.mydomain.com/execute is given as return url and www.mydomain.com/cancel given as a cancel url. Now for that return url and cancel url I have to make a another view which is given below.
def payment_execute(request):
logging.basicConfig(level=logging.INFO)
# ID of the payment. This ID is provided when creating payment.
payment = paypalrestsdk.Payment.find("PAY-57363176S1057143SKE2HO3A")
ctx = {'payment':payment}
context = RequestContext(request)
# PayerID is required to approve the payment.
if payment.execute({"payer_id": "DUFRQ8GWYMJXC" }): # return True or False
print("Payment[%s] execute successfully"%(payment.id))
return render_to_response('photo/execute.html',ctx,context)
else:
print(payment.error)
return render_to_response('photo/dismiss.html',ctx,context)
You can see here in the payment_execute view, I have put static payment id and static payer id. With this static payment id and payer id, I have successfully completed one single payment using Paypal. But this payment id and payer id must be Dynamic. How can i dynamically set the payment id and payer id in the payment_execute view. I have saved the payment id in the user session(in my subscribe_plan view) and I know that the payer id is supplied in the return url but I don't know how to fetch them because of my lack of knowledge. How can I do this?
payment id : In subscribe view, save the payment id
request.session["payment_id"] = payment.id
Later in payment_execute view, get the payment id :
payment_id = request.session["payment_id"]
payment = paypalrestsdk.Payment.find(payment_id)
payer id :
It seems that you are already aware that the payer id is a parameter supplied in the return url. In your payment_execute view, you should be able to access the payer id by using:
request.GET.get("PayerID")
The following links should be helpful for trying it out more and detailed documentaion:
https://devtools-paypal.com/guide/pay_paypal/python?interactive=ON&env=sandbox
https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With