Search code examples
djangopaypalpaypal-rest-sdk

Paypal standard checkout and Django/Python


I am trying to integrate paypal standard checkout, i have imported paypalrestsdk, I have my correct paypal client id and secret, but I keep getting an error returned:

if order.create():
       ^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not callable

I have done some debugging and this is what gets returned into console:

Transaction: {'amount': {'value': '10.00', 'currency_code': 'USD'}, 'description': 'My awesome product'} Order: {'intent': 'CAPTURE', 'purchase_units': [{'amount': {'currency_code': 'USD', 'value': '10.00'}, 'description': 'My awesome product'}]} Internal Server Error: /create_order/

here are my views and scripts:

def create_order(request):
    # Get the transaction details from the client-side
    transaction = json.loads(request.body)
    print('Transaction:', transaction)

    # Create a new PayPal order
    order = paypalrestsdk.Order({
        "intent": "CAPTURE",
        "purchase_units": [{
            "amount": {
                "currency_code": transaction['amount']['currency_code'],
                "value": transaction['amount']['value']
            },
            "description": transaction['description']
        }]
    })
    print('Order:', order)

    if order.create():
    # Return the PayPal order ID to the client-side
        return JsonResponse({"orderID": order.id})
    else:
        # Return an error message to the client-side
        error_dict = {"error": str(order.error)}
        return JsonResponse(error_dict, status=400)



def capture_payment(request):
    # Get the PayPal order ID from the client-side
    order_id = json.loads(request.body)['orderID']

    # Capture the PayPal payment
    order = paypalrestsdk.Order.find(order_id)
    if order and order.status == "CREATED":
        capture = paypalrestsdk.Capture({
            "amount": {
                "currency_code": order.purchase_units[0].amount.currency_code,
                "value": order.purchase_units[0].amount.value
            }
        })
        if capture.create(order_id):
            # Return a success message to the client-side
            return JsonResponse({"status": "Payment captured successfully."})
        else:
            # Return an error message to the client-side
            return JsonResponse({"error": capture.error})
    else:
        # Return an error message to the client-side
        return JsonResponse({"error": "Invalid PayPal order ID."})



 <script>
      paypal.Buttons({
        style: {
          shape: 'rect',
          color: 'gold',
          layout: 'vertical',
          label: 'paypal'
        },
        createOrder: function(data, actions) {
          // Set up the transaction details
          var transaction = {
            amount: {
              value: '10.00',
              currency_code: 'USD'
            },
            description: 'My awesome product'
          };
    
          // Call the server to create a new PayPal order
          return fetch('/create_order/', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'X-CSRFToken': csrftoken,
            },
            body: JSON.stringify(transaction)
          }).then(function(response) {
            return response.json();
          }).then(function(data) {
            // Return the PayPal order ID to the PayPal SDK
            return data.orderID;
          });
        },
        onApprove: function(data, actions) {
          // Call the server to capture the PayPal payment
          return fetch('/capture_payment/', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json',
              'X-CSRFToken': csrftoken,
            },
            body: JSON.stringify({
              orderID: data.orderID
            })
          }).then(function(response) {
            return response.json();
          }).then(function(data) {
            // Show a success message to the user
            alert('Payment successful!');
          });
        }
      }).render('#paypal-button-container');
    </script>

Solution

  • My issue was not using sandbox api links to PayPal on my views. Make sure you are using the sandbox api links when testing.