-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show Express Checkout button previews in editor (#10141)
Co-authored-by: Francesco <[email protected]>
- Loading branch information
1 parent
d14e829
commit e4b3f99
Showing
7 changed files
with
340 additions
and
16 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
changelog/fix-show-preview-for-express-checkout-buttons-in-editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: fix | ||
|
||
Show Express Checkout button previews in template editor |
137 changes: 137 additions & 0 deletions
137
client/express-checkout/blocks/components/express-checkout-button-preview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getExpressCheckoutButtonAppearance } from '../../utils'; | ||
|
||
export const SUPPORTED_PREVIEW_PAYMENT_METHODS = [ 'googlePay', 'applePay' ]; | ||
|
||
const GooglePayButtonPreview = ( { options, buttonAttributes, theme } ) => { | ||
const googlePlayContainerRef = useRef( null ); | ||
const hasStartedLoadingGooglePlayButton = useRef( null ); | ||
const appearance = getExpressCheckoutButtonAppearance( buttonAttributes ); | ||
const borderRadius = appearance.variables.borderRadius; | ||
|
||
useEffect( () => { | ||
if ( | ||
googlePlayContainerRef.current && | ||
! hasStartedLoadingGooglePlayButton.current | ||
) { | ||
hasStartedLoadingGooglePlayButton.current = true; | ||
( async () => { | ||
// The container may be inside an iframe, so we need to retrieve a reference to the document and window objects. | ||
const targetDocument = | ||
googlePlayContainerRef.current.ownerDocument; | ||
const targetWindow = targetDocument.defaultView; | ||
if ( ! targetWindow.google?.payments?.api?.PaymentsClient ) { | ||
await new Promise( ( resolve ) => { | ||
const script = document.createElement( 'script' ); | ||
script.src = 'https://pay.google.com/gp/p/js/pay.js'; | ||
script.onload = resolve; | ||
targetDocument.head.appendChild( script ); | ||
} ); | ||
} | ||
|
||
const googlePayClient = new targetWindow.google.payments.api.PaymentsClient( | ||
{ | ||
environment: 'TEST', | ||
} | ||
); | ||
|
||
const buttonColor = theme === 'black' ? 'black' : 'white'; // There is no 'outline' theme in Google Pay. | ||
|
||
const button = googlePayClient.createButton( { | ||
buttonType: options.buttonType.googlePay, | ||
buttonColor, | ||
buttonRadius: parseFloat( borderRadius ), | ||
buttonSizeMode: 'fill', | ||
onClick: () => {}, | ||
} ); | ||
googlePlayContainerRef.current.appendChild( button ); | ||
} )(); | ||
} | ||
}, [ theme, borderRadius, options.buttonType.googlePay ] ); | ||
|
||
useEffect( () => { | ||
googlePlayContainerRef.current | ||
?.querySelector( 'button' ) | ||
?.style?.setProperty( 'border-radius', borderRadius ); | ||
}, [ borderRadius ] ); | ||
|
||
return ( | ||
<div | ||
ref={ googlePlayContainerRef } | ||
id="express-checkout-button-preview-googlePay" | ||
style={ { | ||
height: `${ options.buttonHeight }px`, | ||
width: '100%', | ||
} } | ||
/> | ||
); | ||
}; | ||
|
||
const ApplePayButtonPreview = ( { options, buttonAttributes, theme } ) => { | ||
const appearance = getExpressCheckoutButtonAppearance( buttonAttributes ); | ||
const borderRadius = appearance.variables.borderRadius; | ||
|
||
const buttonStyle = { | ||
height: `${ options.buttonHeight }px`, | ||
borderRadius, | ||
ApplePayButtonType: options.buttonType.applePay, | ||
WebkitAppearance: '-apple-pay-button', | ||
width: '100%', | ||
}; | ||
|
||
if ( [ 'black', 'white', 'white-outline' ].includes( theme ) ) { | ||
buttonStyle.ApplePayButtonStyle = theme; | ||
} else { | ||
buttonStyle.ApplePayButtonStyle = 'white'; | ||
} | ||
|
||
return ( | ||
<div> | ||
<button | ||
type="button" | ||
id="express-checkout-button-preview-applePay" | ||
className="express-checkout-button-preview" | ||
style={ buttonStyle } | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const ExpressCheckoutButtonPreview = ( { | ||
expressPaymentMethod, | ||
options, | ||
buttonAttributes, | ||
} ) => { | ||
const theme = options.buttonTheme[ expressPaymentMethod ]; | ||
|
||
if ( expressPaymentMethod === 'googlePay' ) { | ||
return ( | ||
<GooglePayButtonPreview | ||
options={ options } | ||
buttonAttributes={ buttonAttributes } | ||
theme={ theme } | ||
/> | ||
); | ||
} | ||
|
||
if ( expressPaymentMethod === 'applePay' ) { | ||
return ( | ||
<ApplePayButtonPreview | ||
options={ options } | ||
buttonAttributes={ buttonAttributes } | ||
theme={ theme } | ||
/> | ||
); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default ExpressCheckoutButtonPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
client/tokenized-express-checkout/blocks/components/express-checkout-button-preview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getExpressCheckoutButtonAppearance } from '../../utils'; | ||
|
||
export const SUPPORTED_PREVIEW_PAYMENT_METHODS = [ 'googlePay', 'applePay' ]; | ||
|
||
const GooglePayButtonPreview = ( { options, buttonAttributes, theme } ) => { | ||
const googlePlayContainerRef = useRef( null ); | ||
const hasStartedLoadingGooglePlayButton = useRef( null ); | ||
const appearance = getExpressCheckoutButtonAppearance( buttonAttributes ); | ||
const borderRadius = appearance.variables.borderRadius; | ||
|
||
useEffect( () => { | ||
if ( | ||
googlePlayContainerRef.current && | ||
! hasStartedLoadingGooglePlayButton.current | ||
) { | ||
hasStartedLoadingGooglePlayButton.current = true; | ||
( async () => { | ||
// The container may be inside an iframe, so we need to retrieve a reference to the document and window objects. | ||
const targetDocument = | ||
googlePlayContainerRef.current.ownerDocument; | ||
const targetWindow = targetDocument.defaultView; | ||
if ( ! targetWindow.google?.payments?.api?.PaymentsClient ) { | ||
await new Promise( ( resolve ) => { | ||
const script = document.createElement( 'script' ); | ||
script.src = 'https://pay.google.com/gp/p/js/pay.js'; | ||
script.onload = resolve; | ||
targetDocument.head.appendChild( script ); | ||
} ); | ||
} | ||
|
||
const googlePayClient = new targetWindow.google.payments.api.PaymentsClient( | ||
{ | ||
environment: 'TEST', | ||
} | ||
); | ||
|
||
const buttonColor = theme === 'black' ? 'black' : 'white'; // There is no 'outline' theme in Google Pay. | ||
|
||
const button = googlePayClient.createButton( { | ||
buttonType: options.buttonType.googlePay, | ||
buttonColor, | ||
buttonRadius: parseFloat( borderRadius ), | ||
buttonSizeMode: 'fill', | ||
onClick: () => {}, | ||
} ); | ||
googlePlayContainerRef.current.appendChild( button ); | ||
} )(); | ||
} | ||
}, [ theme, borderRadius, options.buttonType.googlePay ] ); | ||
|
||
useEffect( () => { | ||
googlePlayContainerRef.current | ||
?.querySelector( 'button' ) | ||
?.style?.setProperty( 'border-radius', borderRadius ); | ||
}, [ borderRadius ] ); | ||
|
||
return ( | ||
<div | ||
ref={ googlePlayContainerRef } | ||
id="express-checkout-button-preview-googlePay" | ||
style={ { | ||
height: `${ options.buttonHeight }px`, | ||
width: '100%', | ||
} } | ||
/> | ||
); | ||
}; | ||
|
||
const ApplePayButtonPreview = ( { options, buttonAttributes, theme } ) => { | ||
const appearance = getExpressCheckoutButtonAppearance( buttonAttributes ); | ||
const borderRadius = appearance.variables.borderRadius; | ||
|
||
const buttonStyle = { | ||
height: `${ options.buttonHeight }px`, | ||
borderRadius, | ||
ApplePayButtonType: options.buttonType.applePay, | ||
WebkitAppearance: '-apple-pay-button', | ||
width: '100%', | ||
}; | ||
|
||
if ( [ 'black', 'white', 'white-outline' ].includes( theme ) ) { | ||
buttonStyle.ApplePayButtonStyle = theme; | ||
} else { | ||
buttonStyle.ApplePayButtonStyle = 'white'; | ||
} | ||
|
||
return ( | ||
<div> | ||
<button | ||
type="button" | ||
id="express-checkout-button-preview-applePay" | ||
className="express-checkout-button-preview" | ||
style={ buttonStyle } | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const ExpressCheckoutButtonPreview = ( { | ||
expressPaymentMethod, | ||
options, | ||
buttonAttributes, | ||
} ) => { | ||
const theme = options.buttonTheme[ expressPaymentMethod ]; | ||
|
||
if ( expressPaymentMethod === 'googlePay' ) { | ||
return ( | ||
<GooglePayButtonPreview | ||
options={ options } | ||
buttonAttributes={ buttonAttributes } | ||
theme={ theme } | ||
/> | ||
); | ||
} | ||
|
||
if ( expressPaymentMethod === 'applePay' ) { | ||
return ( | ||
<ApplePayButtonPreview | ||
options={ options } | ||
buttonAttributes={ buttonAttributes } | ||
theme={ theme } | ||
/> | ||
); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default ExpressCheckoutButtonPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters