Skip to content

Commit

Permalink
Merge pull request #155 from develodesign/AddJStoHead
Browse files Browse the repository at this point in the history
Defer beforeRegistration Hook if config value `addPaypalJsToHead` is …
  • Loading branch information
collymore authored Apr 23, 2021
2 parents 3c561c7 + a44611e commit 956d535
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.6] - 13.11.2020

- Defer beforeRegistration Hook if config value `addPaypalJsToHead` is false + Documentation to add JS to head

## [1.1.5] - 06.11.2020

- Pass telephone number parameter to PayPal
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,35 @@ $ git clone [email protected]:develodesign/vsf-payment-paypal.git ./vue-storefront/
```json
"paypal": {
"clientId": "",
"addJsToGlobalHead": true,
"endpoint": {
"complete": "/api/ext/paypal/complete",
"setExpressCheckout": "/api/ext/paypal/setExpressCheckout"
}
}
```

#### Add JS to Head
If you want to defer adding the JS to head globally, you can set `addJsToGlobalHead` to `false`

This will defer the beforeRegistration hook and then you can add the below into the mounted lifecycle on your checkout component where you will import the Paypal button.

```js
mounted () {
if (!isServer && window.paypalScriptLoaded === undefined) {
const storeView = currentStoreView()
const { currencyCode } = storeView.i18n
const clientId = config.paypal.hasOwnProperty('clientId') ? config.paypal.clientId : ''
const sdkUrl = `https://www.paypal.com/sdk/js?client-id=${clientId}&currency=${currencyCode}&disable-funding=card,credit,mybank,sofort`
var script = document.createElement('script')
script.setAttribute('src', sdkUrl)
document.head.appendChild(script)
window.paypalScriptLoaded = true
}
}

```

## Registration the Paypal module

Let's edit `config/modules.ts`
Expand Down
3 changes: 2 additions & 1 deletion hooks/beforeRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { currentStoreView } from '@vue-storefront/core/lib/multistore'
export function beforeRegistration(config, store) {
const VSF_PAYPAL_CODE = 'paypal_express'

if (!isServer && config.hasOwnProperty('paypal')) {
if (!isServer && config.hasOwnProperty('paypal') && config.paypal.addJsToGlobalHead && window.paypalScriptLoaded === undefined) {
const storeView = currentStoreView()
const { currencyCode } = storeView.i18n
const clientId = config.paypal.hasOwnProperty('clientId') ? config.paypal.clientId : ''
const sdkUrl = `https://www.paypal.com/sdk/js?client-id=${clientId}&currency=${currencyCode}&disable-funding=card,credit,mybank,sofort`
var script = document.createElement('script')
script.setAttribute('src', sdkUrl)
document.head.appendChild(script)
window.paypalScriptLoaded = true
}
}

0 comments on commit 956d535

Please sign in to comment.