0

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 + '\n\nSee 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');

1 Answer 1

1

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

3
  • Can you be more specific? what do i need in php? Is there a demo that I can refer to?
    – 九条凛
    Commented May 5, 2022 at 10:28
  • The PayPal Checkout documentation developer.paypal.com/docs/checkout/standard/integrate has a full stack example, usng node.js for the backend. That backend can of course be implemented in any environment capable of doing the API calls and returning JSON when called -- including PHP of course. There is a Checkout-PHP-SDK you can use if desired. (Do not use the deprecated PayPal-PHP-SDK) Commented May 5, 2022 at 10:39
  • I have learnt it! I'm done! Love U!@
    – 九条凛
    Commented May 5, 2022 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.