Search code examples
pythondjangopaypalpaypal-rest-sdk

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


I am trying to integrate paypal with my django app using paypalrestsdk https://github.com/paypal/PayPal-Python-SDK

I use the same code to generate payment request

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)

however it produced this error:

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

The question is what is the problem with this and how to generate a proper paypal payment with django ?


Solution

  • this HttpResponseRedirect fix the problem:

    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)