django paypal paypalrestsdk - 如何进行付款?没有返回一个HttpResponse对象

时间:2018-04-15 13:49:48

标签: python django paypal paypal-rest-sdk

我正在尝试使用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付款?

1 个答案:

答案 0 :(得分:1)

这个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)