Skip to content
Advertisement

How to pass order ID to Paypal Button?

I create an order by my php sdk.

like this

$result = json_decode((string) $response->getBody());

echo $result->id; // id of the created order

and now i have this order id. but … How to pass order ID to Paypal Button? like this

`paypal.Buttons({

    // Set up the transaction
    createOrder: function(data, actions) { //I don't need create order,I have created an order
        return actions.order.create({
            purchase_units: [{
                amount: {
                    value: '88.44'
                }
            }]
        });
    },

    // Finalize the transaction
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(orderData) {
            // Successful capture! For demo purposes:
            console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
            var transaction = orderData.purchase_units[0].payments.captures[0];
            alert('Transaction '+ transaction.status + ': ' + transaction.id + 'nnSee console for all available details');

            // Replace the above to show a success message within this page, e.g.
            // const element = document.getElementById('paypal-button-container');
            // element.innerHTML = '';
            // element.innerHTML = '<h3>Thank you for your payment!</h3>';
            // Or go to another URL:  actions.redirect('thank_you.html');
        });
    }


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

Advertisement

Answer

Create two routes on your server, one to create an order (and return the resulting JSON), and one that takes an order id as a parameter and captures it (and returns the JSON result).

Both of these routes should return/output only JSON (no HTML or text).

Pair those two routes with this approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement