使用 python Paypal REST SDK 在 django 中动态获取付款和付款人 ID
Posted
技术标签:
【中文标题】使用 python Paypal REST SDK 在 django 中动态获取付款和付款人 ID【英文标题】:Getting payment and payer id dynamically in django using python Paypal REST SDK 【发布时间】:2014-04-13 04:26:15 【问题描述】:我是 django 和 python 的新手。
我正在开发一个使用 Paypal 进行交易的网站。我已成功将 python Paypal REST SDK 与我的项目集成。这是我的views.py
与集成。
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)
在支付字典中 www.mydomain.com/execute 是作为返回 url 和 www.mydomain.com/cancel em> 作为取消 url 给出。 现在对于该返回 url 和取消 url,我必须创建另一个视图,如下所示。
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)
您可以在 payment_execute 视图中看到这里,我放了静态 payment id 和静态 payer id .有了这个静态付款 ID 和付款人 ID,我已经成功地使用 Paypal 完成了一次付款。但是这个payment id和payer id必须是Dynamic强>。我如何动态地设置payment id和payer id 在 payment_execute 视图中。我已将付款 ID 保存在用户会话中(在我的 subscribe_plan 视图中),并且我知道付款人 ID 在 return url 中提供,但我不知道如何因为我缺乏知识而去取它们。我该怎么做?
【问题讨论】:
嗨@zogo,你是如何通过这个PayPal请求从Django模板传递csrf_token的?我一直坚持下去,请你帮帮我吗? 【参考方案1】:payment id : 在订阅视图中,保存payment id
request.session["payment_id"] = payment.id
稍后在 payment_execute 视图中,获取付款 id:
payment_id = request.session["payment_id"]
payment = paypalrestsdk.Payment.find(payment_id)
付款人 ID:
您似乎已经知道付款人 ID 是返回 url 中提供的参数。 在您的 payment_execute 视图中,您应该能够使用以下命令访问付款人 ID:
request.GET.get("PayerID")
以下链接应该有助于尝试更多和详细的文档:
https://devtools-paypal.com/guide/pay_paypal/python?interactive=ON&env=sandbox
https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/
【讨论】:
【参考方案2】:使用 Avi Das 的答案,我设法使用 POST 而不是 GET 来获取此信息:
class PaypalExecutePayment:
def __call__(self, request):
payer_id = request.POST.get("payerID")
payment_id = request.POST.get("paymentID")
payment = paypalrestsdk.Payment.find(payment_id)
if payment.execute("payer_id": payer_id):
print("Payment execute successfully")
else:
print(payment.error) # Error Hash
将课程连接到我的 api:
from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
...
url('^api/execute-payment/', csrf_exempt(views.PaypalExecutePayment())),
...
]
【讨论】:
以上是关于使用 python Paypal REST SDK 在 django 中动态获取付款和付款人 ID的主要内容,如果未能解决你的问题,请参考以下文章
贝宝 sdk '类 'PayPal\Rest\ApiContext' 未找到'
使用 paypal/rest-api-sdk-php 的 laravel paypal 集成错误