Getting payment and payer id dynamically in django using python Paypal REST SDK -
i new django , python. developing website using paypal transaction. have integrated python paypal rest sdk project. here views.py
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 # resource representing payer funds payment # payment method 'paypal' "payer": { "payment_method": "paypal" }, # ###redirect urls "redirect_urls": { "return_url": "www.mydomain.com/execute", "cancel_url": "www.mydomain.com/cancel" }, # ###transaction # transaction defines contract of # payment - payment , # 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 payment transaction description." } ] } ) selected_plan = request.post['subscribe'] context = requestcontext(request) if payment.create(): print("payment %s created successfully"%payment.id) link in payment.links:#payer funds payment if link.method=="redirect": redirect_url=link.href ctx_dict = {'selected_plan':selected_plan,"payment":payment} print("redirect 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 payment dictionary www.mydomain.com/execute given return url , www.mydomain.com/cancel given cancel url. return url , cancel url have make view given below.
def payment_execute(request): logging.basicconfig(level=logging.info) # id of payment. id provided when creating payment. payment = paypalrestsdk.payment.find("pay-57363176s1057143ske2ho3a") ctx = {'payment':payment} context = requestcontext(request) # payerid required approve 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 payment_execute view, have put static payment id , static payer id. static payment id , payer id, have completed 1 single payment using paypal. payment id , payer id must dynamic. how can dynamically set payment id , payer id in payment_execute view. have saved payment id in user session(in subscribe_plan view) , know payer id supplied in return url don't know how fetch them because of lack of knowledge. how can this?
payment id : in subscribe view, save payment id
request.session["payment_id"] = payment.id
later in payment_execute view, payment id :
payment_id = request.session["payment_id"]
payment = paypalrestsdk.payment.find(payment_id)
payer id :
it seems aware payer id parameter supplied in return url. in payment_execute view, should able access payer id using:
request.get.get("payerid")
the following links should helpful trying out more , 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/
Comments
Post a Comment