gpt4 book ai didi

python - django paypal paypalrestsdk - 如何执行付款?没有返回 HttpResponse 对象

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:14 26 4
gpt4 key购买 nike

我正在尝试使用 paypalrestsdk 将 paypal 与我的 django 应用程序集成 https://github.com/paypal/PayPal-Python-SDK

我使用相同的代码生成付款请求

views.py:

def payment_create(request):
paypalrestsdk.configure({
"mode": "sandbox", # sandbox or live
"client_id": "xxxxx",
"client_secret": "xxxxxx" })

payment = paypalrestsdk.Payment({
"intent": "sale",
"payer": {
"payment_method": "paypal"},
"redirect_urls": {
"return_url": "http://localhost:3000/payment/execute",
"cancel_url": "http://localhost:3000/"},
"transactions": [{
"item_list": {
"items": [{
"name": "item",
"sku": "item",
"price": "5.00",
"currency": "USD",
"quantity": 1}]},
"amount": {
"total": "5.00",
"currency": "USD"},
"description": "This is the payment transaction description."}]})

if payment.create():
print("Payment created successfully")
else:
print(payment.error)

但是它产生了这个错误:

ValueError at /payment/
The view somecomapp.views.payment_create didn't return an HttpResponse object. It returned None instead.

问题是这有什么问题以及如何使用 django 生成正确的 paypal 付款?

最佳答案

这个 HttpResponseRedirect 解决了这个问题:

def payment_create(request):
paypalrestsdk.configure({
"mode": "sandbox", # sandbox or live
"client_id": "xxxxx",
"client_secret": "xxxxxx" })

payment = paypalrestsdk.Payment({
"intent": "sale",
"payer": {
"payment_method": "paypal"},
"redirect_urls": {
"return_url": "http://localhost:3000/payment/execute",
"cancel_url": "http://localhost:3000/"},
"transactions": [{
"item_list": {
"items": [{
"name": "item",
"sku": "item",
"price": "5.00",
"currency": "USD",
"quantity": 1}]},
"amount": {
"total": "5.00",
"currency": "USD"},
"description": "This is the payment transaction description."}]})

if payment.create():
print("Payment created successfully")

for link in payment.links:
if link.rel == "approval_url":
# Convert to str to avoid Google App Engine Unicode issue
# https://github.com/paypal/rest-api-sdk-python/pull/58
approval_url = str(link.href)
print("Redirect for approval: %s" % (approval_url))
return HttpResponseRedirect(approval_url)
else:
print(payment.error)

关于python - django paypal paypalrestsdk - 如何执行付款?没有返回 HttpResponse 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49842691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com