pythondjangopaypalpaypal-sandboxpaypal-adaptive-payments

PayPal: Is it possible to pay for an order just by the order id/details


I am trying to create the order on the backend and then pass the order id to the smart button's javascript so that i can execute the order without allowing the user to edit the order. i have successfully done this before with a subscription by creating a plan and then passing in the plan id to execute the payment but i don't know what button action to use to do this with orders. How should i do this?

Here is how i am doing this on the backend:

headers = {
    "Content-Type": "application/json",
    "Authorization": bearer_token,
}

data = {
    "intent": "CAPTURE",
    "purchase_units": [
        {
            "amount": {
                "currency_code": "GBP",
                "value": str(order.credits)
            }
        }
    ]
}

response = requests.post('https://api-m.sandbox.paypal.com/v2/checkout/orders', headers=headers, json=data).json()

order_id = response["id"]

context = {
    'order_id': order_id,
}

and here is what i have tried on the frontend:

paypal.Buttons({

    style: {
        size: 'responsive',
    },

    createOrder: function(data, actions) {

      return actions.order.create({

            "id": "{{ order_id }}"

        });

      },

    onApprove: function(data, actions) {

            

    }

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

Solution

  • Do not use actions.order.create nor actions.order.capture. Simply return the id in the former case.

    See the code sample at https://developer.paypal.com/demo/checkout/#/pattern/server

          paypal.Buttons({
    
                // Call your server to set up the transaction
                createOrder: function(data, actions) {
                    return fetch('/some/path/on/your/server/paypal-order-create', {
                        method: 'post'
                    }).then(function(res) {
                        return res.json();
                    }).then(function(orderData) {
                        return orderData.id;
                    });
                },