|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { __ } from '@wordpress/i18n'; |
| 5 | +import { getWooPayExpressData } from './utils'; |
| 6 | +import wcpayTracks from 'tracks'; |
| 7 | + |
| 8 | +export const expressCheckoutIframe = async ( api ) => { |
| 9 | + let userEmail = ''; |
| 10 | + |
| 11 | + const spinner = document.createElement( 'div' ); |
| 12 | + const parentDiv = document.body; |
| 13 | + spinner.classList.add( 'wc-block-components-spinner' ); |
| 14 | + |
| 15 | + // Make the otp iframe wrapper. |
| 16 | + const iframeWrapper = document.createElement( 'div' ); |
| 17 | + iframeWrapper.setAttribute( 'role', 'dialog' ); |
| 18 | + iframeWrapper.setAttribute( 'aria-modal', 'true' ); |
| 19 | + iframeWrapper.classList.add( 'platform-checkout-otp-iframe-wrapper' ); |
| 20 | + |
| 21 | + // Make the otp iframe. |
| 22 | + const iframe = document.createElement( 'iframe' ); |
| 23 | + iframe.title = __( 'WooPay SMS code verification', 'woocommerce-payments' ); |
| 24 | + iframe.classList.add( 'platform-checkout-otp-iframe' ); |
| 25 | + |
| 26 | + // To prevent twentytwenty.intrinsicRatioVideos from trying to resize the iframe. |
| 27 | + iframe.classList.add( 'intrinsic-ignore' ); |
| 28 | + |
| 29 | + // Maybe we could make this a configurable option defined in PHP so it could be filtered by merchants. |
| 30 | + const fullScreenModalBreakpoint = 768; |
| 31 | + |
| 32 | + // Track the current state of the header. This default |
| 33 | + // value should match the default state on the platform. |
| 34 | + let iframeHeaderValue = true; |
| 35 | + const getWindowSize = () => { |
| 36 | + if ( |
| 37 | + ( fullScreenModalBreakpoint <= window.innerWidth && |
| 38 | + iframeHeaderValue ) || |
| 39 | + ( fullScreenModalBreakpoint > window.innerWidth && |
| 40 | + ! iframeHeaderValue ) |
| 41 | + ) { |
| 42 | + iframeHeaderValue = ! iframeHeaderValue; |
| 43 | + iframe.contentWindow.postMessage( |
| 44 | + { |
| 45 | + action: 'setHeader', |
| 46 | + value: iframeHeaderValue, |
| 47 | + }, |
| 48 | + getWooPayExpressData( 'platformCheckoutHost' ) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + // Prevent scrolling when the iframe is open. |
| 53 | + document.body.style.overflow = 'hidden'; |
| 54 | + }; |
| 55 | + |
| 56 | + /** |
| 57 | + * Handles setting the iframe position based on the window size. |
| 58 | + * It tries to be positioned at the center of the screen unless |
| 59 | + * window is smaller than breakpoint which makes it full window size. |
| 60 | + */ |
| 61 | + const setPopoverPosition = () => { |
| 62 | + // If for some reason the iframe is not loaded, just return. |
| 63 | + if ( ! iframe ) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // If the window width is less than the breakpoint, set iframe to full window |
| 68 | + if ( fullScreenModalBreakpoint >= window.innerWidth ) { |
| 69 | + iframe.style.left = '0'; |
| 70 | + iframe.style.right = ''; |
| 71 | + iframe.style.top = '0'; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // Get references to the iframe bounding rects. |
| 76 | + const iframeRect = iframe.getBoundingClientRect(); |
| 77 | + |
| 78 | + // Set the iframe top and left to be centered. |
| 79 | + iframe.style.top = |
| 80 | + Math.floor( window.innerHeight / 2 - iframeRect.height / 2 ) + 'px'; |
| 81 | + iframe.style.left = |
| 82 | + Math.floor( window.innerWidth / 2 - iframeRect.width / 2 ) + 'px'; |
| 83 | + }; |
| 84 | + |
| 85 | + iframe.addEventListener( 'load', () => { |
| 86 | + // Set the initial value. |
| 87 | + iframeHeaderValue = true; |
| 88 | + |
| 89 | + getWindowSize(); |
| 90 | + window.addEventListener( 'resize', getWindowSize ); |
| 91 | + |
| 92 | + setPopoverPosition(); |
| 93 | + window.addEventListener( 'resize', setPopoverPosition ); |
| 94 | + |
| 95 | + iframe.classList.add( 'open' ); |
| 96 | + wcpayTracks.recordUserEvent( |
| 97 | + wcpayTracks.events.PLATFORM_CHECKOUT_OTP_START |
| 98 | + ); |
| 99 | + } ); |
| 100 | + |
| 101 | + // Add the iframe to the wrapper. |
| 102 | + iframeWrapper.insertBefore( iframe, null ); |
| 103 | + |
| 104 | + // Error message to display when there's an error contacting WooPay. |
| 105 | + const errorMessage = document.createElement( 'div' ); |
| 106 | + errorMessage.style[ 'white-space' ] = 'normal'; |
| 107 | + errorMessage.textContent = __( |
| 108 | + 'WooPay is unavailable at this time. Please complete your checkout below. Sorry for the inconvenience.', |
| 109 | + 'woocommerce-payments' |
| 110 | + ); |
| 111 | + |
| 112 | + const closeIframe = () => { |
| 113 | + window.removeEventListener( 'resize', getWindowSize ); |
| 114 | + window.removeEventListener( 'resize', setPopoverPosition ); |
| 115 | + |
| 116 | + iframeWrapper.remove(); |
| 117 | + iframe.classList.remove( 'open' ); |
| 118 | + |
| 119 | + document.body.style.overflow = ''; |
| 120 | + }; |
| 121 | + |
| 122 | + iframeWrapper.addEventListener( 'click', closeIframe ); |
| 123 | + |
| 124 | + const openIframe = ( email ) => { |
| 125 | + const urlParams = new URLSearchParams(); |
| 126 | + urlParams.append( 'email', email ); |
| 127 | + urlParams.append( |
| 128 | + 'needsHeader', |
| 129 | + fullScreenModalBreakpoint > window.innerWidth |
| 130 | + ); |
| 131 | + urlParams.append( |
| 132 | + 'wcpayVersion', |
| 133 | + getWooPayExpressData( 'wcpayVersionNumber' ) |
| 134 | + ); |
| 135 | + |
| 136 | + iframe.src = `${ getWooPayExpressData( |
| 137 | + 'platformCheckoutHost' |
| 138 | + ) }/otp/?${ urlParams.toString() }`; |
| 139 | + |
| 140 | + // Insert the wrapper into the DOM. |
| 141 | + parentDiv.insertBefore( iframeWrapper, null ); |
| 142 | + |
| 143 | + setPopoverPosition(); |
| 144 | + |
| 145 | + // Focus the iframe. |
| 146 | + iframe.focus(); |
| 147 | + }; |
| 148 | + |
| 149 | + const showErrorMessage = () => { |
| 150 | + parentDiv.insertBefore( errorMessage ); |
| 151 | + }; |
| 152 | + |
| 153 | + document.addEventListener( 'keyup', ( event ) => { |
| 154 | + if ( 'Escape' === event.key && closeIframe() ) { |
| 155 | + event.stopPropagation(); |
| 156 | + } |
| 157 | + } ); |
| 158 | + |
| 159 | + window.addEventListener( 'message', ( e ) => { |
| 160 | + if ( |
| 161 | + ! getWooPayExpressData( 'platformCheckoutHost' ).startsWith( |
| 162 | + e.origin |
| 163 | + ) |
| 164 | + ) { |
| 165 | + return; |
| 166 | + } |
| 167 | + |
| 168 | + switch ( e.data.action ) { |
| 169 | + case 'otp_email_submitted': |
| 170 | + userEmail = e.data.userEmail; |
| 171 | + break; |
| 172 | + case 'redirect_to_platform_checkout': |
| 173 | + wcpayTracks.recordUserEvent( |
| 174 | + wcpayTracks.events.PLATFORM_CHECKOUT_OTP_COMPLETE |
| 175 | + ); |
| 176 | + api.initPlatformCheckout( |
| 177 | + userEmail, |
| 178 | + e.data.platformCheckoutUserSession |
| 179 | + ).then( ( response ) => { |
| 180 | + if ( 'success' === response.result ) { |
| 181 | + window.location = response.url; |
| 182 | + } else { |
| 183 | + showErrorMessage(); |
| 184 | + closeIframe( false ); |
| 185 | + } |
| 186 | + } ); |
| 187 | + break; |
| 188 | + case 'otp_validation_failed': |
| 189 | + wcpayTracks.recordUserEvent( |
| 190 | + wcpayTracks.events.PLATFORM_CHECKOUT_OTP_FAILED |
| 191 | + ); |
| 192 | + break; |
| 193 | + case 'close_modal': |
| 194 | + closeIframe(); |
| 195 | + break; |
| 196 | + case 'iframe_height': |
| 197 | + if ( 300 < e.data.height ) { |
| 198 | + if ( fullScreenModalBreakpoint <= window.innerWidth ) { |
| 199 | + // set height to given value |
| 200 | + iframe.style.height = e.data.height + 'px'; |
| 201 | + |
| 202 | + // center top in window |
| 203 | + iframe.style.top = |
| 204 | + Math.floor( |
| 205 | + window.innerHeight / 2 - e.data.height / 2 |
| 206 | + ) + 'px'; |
| 207 | + } else { |
| 208 | + iframe.style.height = ''; |
| 209 | + iframe.style.top = ''; |
| 210 | + } |
| 211 | + } |
| 212 | + break; |
| 213 | + default: |
| 214 | + // do nothing, only respond to expected actions. |
| 215 | + } |
| 216 | + } ); |
| 217 | + |
| 218 | + window.addEventListener( 'pageshow', function ( event ) { |
| 219 | + if ( event.persisted ) { |
| 220 | + // Safari needs to close iframe with this. |
| 221 | + closeIframe( false ); |
| 222 | + } |
| 223 | + } ); |
| 224 | + |
| 225 | + openIframe(); |
| 226 | +}; |
0 commit comments