我是django和python的新手。我正在开发一个网站,我使用贝宝进行交易。我已经成功地将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是作为取消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视图中看到,我放置了静态payer id和静态payer id。有了这个静态付款id和支付者id,我已经成功地使用贝宝完成了一次付款。但是这个payer id和payer id必须是动态。如何在视图中动态地设置payment id和payer id。我已经在用户会话中保存了付款id (在我的subscribe_plan视图中),我知道付款人id是在return url中提供的,但我不知道如何获取它们,因为我缺乏知识。我该怎么做呢?
发布于 2014-03-22 04:47:54
request.session"payment_id“= payment.id
稍后在payment_execute视图中,获取付款id:
payment_id = request.session"payment_id“
payment = paypalrestsdk.Payment.find(payment_id)
您似乎已经知道,付款人id是返回url中提供的一个参数。在您的支付者视图中,您应该能够通过using:request.GET.get("PayerID")访问payment_execute id
下面的链接可以帮助你尝试更多更详细的文档:
https://devtools-paypal.com/guide/pay_paypal/python?interactive=ON&env=sandbox
https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/
发布于 2018-07-29 01:47:05
使用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())),
...
]
https://stackoverflow.com/questions/22293876
复制相似问题