【发布时间】: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的?我一直坚持下去,请你帮帮我吗?