代码之家  ›  专栏  ›  技术社区  ›  Axil

django paypal paypalrestsdk-如何执行支付?未返回HttpResponse对象

  •  0
  • Axil  · 技术社区  · 8 年前

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

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

    视图。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 回复  |  直到 8 年前
        1
  •  1
  •   Axil    8 年前

    此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)
    
    推荐文章