django paypal paypalrestsdk - 如何执行付款? return 不是 HttpResponse 对象

django paypal paypalrestsdk - how to perform payment ? didn't return an HttpResponse object

我正在尝试使用 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)