buttonpaypalsubscriptionpaypal-subscriptions

How to set a return url for Paypal subscription button?


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.


Solution

  • The original code provided in the question above is actually correct. The error occurred due to a blank OrderID and consequently, because no order was found to exist, it failed to get the item details and provide a receipt or email the user. This code now works perfectly.

    <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) {
    actions.redirect('https://example.com/order-return.asp?item_number=<%= strOrderID %>');
    }
    }).render('#paypal-button-container-P-EXAMPLEXYZ'); // Renders the PayPal button
    

    In the code above I have removed the alert which was only useful for testing.

    actions.redirect() is a supported function and not deprecated.