I am switching out a script block which used to use the subscription feature of PayPal. The code was basically this:
<script src="https://www.paypal.com/sdk/js?client-id=[id]=true&intent=subscription" data-sdk-integration-source="button-factory"></script><div id="paypal-button-container-P-[id]"></div><script> paypal.Buttons({ style: { shape: 'rect', color: 'gold', layout: 'vertical', label: 'subscribe' }, createSubscription: function(data, actions) { return actions.subscription.create({ /* Creates the subscription */ plan_id: 'P-[id]' }); }, onApprove: function(data, actions) { var subId = document.getElementById('subId'); subId.setAttribute('value', data.subscriptionID); var form = document.getElementById('membershipForm'); form.submit(); }}).render('#paypal-button-container-P-[id]'); // Renders the PayPal button</script>
After the payment goes through, I run the onApprove callback function to submit a form on the page with some site-specific information, and sends emails through the website.
The payment model has changed, and now we want to use single payments, not subscriptions.
I have tried using the "No-Code Checkout" AKA "HostedButton" AKA "Smart Buttons". It generates this:
<script src="https://www.paypal.com/sdk/js?client-id=[id]&components=hosted-buttons&disable-funding=venmo¤cy=CAD"></script><div id="paypal-container-[id]"></div><script>paypal.HostedButtons({ hostedButtonId: "[id]"}).render("#paypal-container-[id]")</script>
There's no callback function in here. When someone finishes paying, they stay on the page and there is no feedback from the website or indication the transaction was made other than the pop-up closes.
I tried adding onApprove like before, but still nothing happens. Even simplifying the callback function to a console.log or alert does nothing. It doesn't seem to ever get called.
paypal.HostedButtons({ hostedButtonId: "[id]", onApprove: function(data, actions) { var subId = document.getElementById('subId'); subId.setAttribute('value', data.subscriptionID); var form = document.getElementById('membershipForm'); form.submit(); }}).render("#paypal-container-[id]")
I've scoured PayPal's documentation but I can't find anything on this.If it makes a difference, this is a button made in sandbox, being purchased with a sandbox test account, running everything on localhost.
Also note: there are no errors at any point in this process, and my sandbox account is getting the money.