PayPal 'Pay Now' Button in Javascript without a Backend?

610 views Asked by At

In another question I found the following has been deprecated. Specifically, actions.order.create and actions.order.capture have been deprecated and should not be used in any new integrations

So what do we use in May 2023 to avoid the backend?

My DocType at the top of the Html file is:

<!DOCTYPE html>

I have the code in the <header>:

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

In the <body>.

<script src="https://www.paypal.com/sdk/js?client-id=sb&currency=GBP"></script>
<div id="paypal-button-10"></div>
<script>
    paypal.Buttons({
        style: {
            layout: 'vertical', /* 'horizontal', */
            color:  'gold',
            shape:  'pill',
            label:  'buynow'
        },
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [
                    {
                        amount: {
                            currency_code: "GBP",
                            value: "10.00"
                        }
                    }
                 ]
             });
        },
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                alert('Congratulations! payment success');
                console.log(details);
                console.log(details.id);  // order id
                console.log(details.status);  // 'COMPLETED'
                // send ajax request to update the db
            });
        },
        onCancel: function (data) {
            alert('Payment cancelled');
        },
        onError: function (err) {
            alert(err);
        }
    }).render('#paypal-button-10');
</script>
1

There are 1 answers

2
Preston PHX On

How do you code a Paypal 'Pay Now' Button in Javascript without a Backend in May 2023?

All such integrations were deprecated by PayPal, so there is no supported way to do so. Alternatives are:

  1. Integrate with a backend you create.
  2. Install/use a pre-integrated solution, such as just about any shopping cart software or similar, that has its own backend that integrates with PayPal.

There is no option to integrate regular "Pay Now" / "Buy Now" one-time PayPal payments without a backend. Any of the old ways you may find to do so (including actions.order.create / actions.order.capture with the JS SDK, or HTML-only redirects/buttons) are no longer supported.