Search code examples
javascriptdjangopaypalpaypal-sandbox

Django & Paypal button integration


I am integrating a paypal payment method into my website which is working perfectly.... until I try to add a description so I can view all of the order details on the paypal invoice.

As soon as I add description (i have added order_id as an example of info i want passsing): {} to the createOrder function below, paypal fails to load:

<script>
    var total = '{{order.get_cart_total}}'
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({

        style: {
            color:  'blue',
            shape:  'rect',
        },

        // Set up the transaction
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value:parseFloat(total).toFixed(2)
                    },
                    description: {
                        order_id
                    },
                }]
            });
        },

        // Finalize the transaction
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                console.log(JSON.stringify(details));
        submitFormData()
            });
        }

    }).render('#paypal-button-container');
</script>

in console i get the following errors:

errors

I have added the settings below to my settings.py file:

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SAMESITE = 'None'
CSRF_COOKIE_SAMESITE = 'None'

Solution

  • actions.order.create( and actions.order.capture( are deprecated

    See the standard checkout integration guide for information on how to integrate with two backend routes that create and capture from a server. The samples there are in node.js, but these two routes (which return only JSON) can of course be implemented in any environment, including python/django.

    If order_id is your own unique order ID (never before used for a payment that completed successfully), typically the best field to pass this value in is invoice_id. After a successful payment, duplicate/repeat payments for the same invoice_id are blocked by default, and this is useful to prevent the possibility of accidental double payments.