Use paypal-js for loading the JS SDK #77
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Looking for feedback and consensus before merging.
Problem
The PayPal JS SDK script is an API that returns JavaScript. It takes in query string parameters as input and uses those settings to return a JavaScript payload that's customized for the merchant.
Setting these query string parameters in HTML code using a
<script>
tag is difficult for developers. There are two possible solutions to make the HTML dynamic:<script>
tag to the DOM once the query string inputs are known.Number 1 is currently taught in the Advanced Integration Guide and adds additional complexity. Number 2 works well but requires merchant developers to roll their own solution for dynamic script loading. It does require the merchant to think about script loading order too. The JS SDK script has to finish loading before any components can be used (ex:
window.paypal.Buttons()
). Also no JavaScript developer likes dealing with our kebab-case parameters.Solution
Let's have paypal-js be the recommended way for merchant developers to load the JS SDK script. The paypal-js script loader can be used in two ways:
loadScript
function using JavaScript Module syntax.The paypal-js library also supports passing in query string parameters in camelCase which is what JavaScript developers prefer.
Here's a code example of the new approach:
The HTML code does need to use the
type=module
setting on the script tag to support theimport
syntax.Risks
It seems like every modern browser supports JavaScript modules but we should do more research to confirm. This will not work in IE11 but no one should really care about that anymore. If browser support is a major concern we can update our docs to teach two different options with paypal-js:
window.paypalLoadScript()
option which does not use ES Modules: