After fighting with this for hours I found a solution that works great to show both buttons on the same page (or even many of each) and want to share it so I can save others the pain.
1 - Importing the library twice
The first problem is how to import both PayPal Javascript files at the same time with different parameters. The solution is using data-namespace
on the script tags like this:
<script src="https://www.paypal.com/sdk/js?client-id={{YOUR CLIENT ID}}¤cy=USD" data-namespace="paypal_one_time"></script>
<script src="https://www.paypal.com/sdk/js?client-id={{YOUR CLIENT ID}}¤cy=USD&intent=subscription&vault=true" data-namespace="paypal_subscriptions"></script>
2 - Displaying the two buttons (or more)
Now to render the buttons you need to use those namespaces instead of the paypal
variable. For example:
paypal_one_time.Buttons().render('#some-container');
paypal_subscriptions.Buttons().render('#some-subscription-container');
3 - Broken Subscriptions after One-Time payment flow
The last problem you will find is that clicking on the one_time payment buttons breaks the subscription buttons. If you try to click the subscription one after the one-time button, it won't work (saying that you don't have enough authorization and closing the popup window).
To fix this, the easiest way is to re-render the subscription buttons on your page every time a one-time payment flow finishes (in the onApproved/onCancelled/onError methods inside the Buttons() options for your one-time payment buttons).
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// process your one time payment
// re-render subscription buttons because now they are broken!
reRenderSubscriptionButtons();
});
},
onCancel: function(data) {
reRenderSubscriptionButtons();
},
On those events in the one_time_button, just call a function that does something like:
function reRenderSubscriptionButtons() {
// remove everything from the container
document.getElementById("some-subscription-container").innerHTML='';
// render the subscription buttons again (you can call the same function you use to render it the first time too)
paypal_subscriptions.Buttons().render('#some-subscription-container');
}