How to set a return url for Paypal subscription button?

42 views Asked by At

Paypal subscription buttons now need to be registered on the Paypal website. Unlike the old methods that enabled us to set return urls and invoice number, we must now use JavaScript to call the Paypal identifier for the button registered at Paypal. However the help for custom options is scant and the only recommendation that I could find was tried using the code below. However it doesn't work... if it doesn't return, the client doesn't get our version of an invoice and we don't get notified of the new subscription.

<script src="https://www.paypal.com/sdk/js?client-id=EXAMPLEXYZ&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
<script>
    paypal.Buttons({
    style: {
    shape: 'pill',
    color: 'gold',
    layout: 'vertical',
    label: 'subscribe'
    },
    createSubscription: function(data, actions) {
    return actions.subscription.create({
    /* Creates the subscription */
    plan_id: 'P-EXAMPLEXYZ'
    });
    },
    onApprove: function(data, actions) {
    alert(data.subscriptionID); // You can add optional success message for the subscriber here
    actions.redirect('https://example.com/order-return.asp?item_number=<%= strOrderID %>');
    }
    }).render('#paypal-button-container-P-EXAMPLEXYZ'); // Renders the PayPal button
</script>

Line #18 is not part of the standard button code and was found from online search. But we have yet to see it work. Also, when a subscription is being added and paid at Paypal, we are not receiving a notification email. Has anyone been able to successfully modify these buttons?

NOTE: All help topics that we can find relate to the old method.

1

There are 1 answers

6
Preston PHX On
  • actions.redirect() is deprecated, do not use it anywhere. Set window.location.href instead.
  • no business logic should depend on the redirect actually happening. Instead, create a webhook listener URL and subscribe to webhooks for the REST App/client ID, particularly PAYMENT.SALE.COMPLETED. Other events are not important, all subscription logic can be based on just that one and refreshing a valid-through or (equivalently) a last-paid date every time it is received
  • if it's helful for reconciliation, add a custom_id parameter when creating the subscription (alongside the plan_id) with a relevant value such as your <%= strOrderID %>. This custom_id will be returned in all webhooks for the subscription.