@@ -104,6 +104,7 @@ import type {
104104 PresentExpressPurchaseButtonParams ,
105105} from "./entities/present-express-purchase-button-params" ;
106106import { ExpressPurchaseButtonWrapper } from "./ui/express-purchase-button/express-purchase-button-wrapper.svelte" ;
107+ import { getWindow , getDocument } from "./helpers/browser-globals" ;
107108
108109export { ProductType } from "./entities/offerings" ;
109110export type {
@@ -464,12 +465,12 @@ export class Purchases {
464465 const htmlTarget = paywallParams . htmlTarget ;
465466 let wasRootAutoCreated = false ;
466467
467- let resolvedHTMLTarget =
468- htmlTarget ?? document . getElementById ( "rcb-ui-pw-root" ) ;
468+ const doc = getDocument ( ) ;
469+ let resolvedHTMLTarget = htmlTarget ?? doc . getElementById ( "rcb-ui-pw-root" ) ;
469470
470471 if ( resolvedHTMLTarget === null ) {
471472 wasRootAutoCreated = true ;
472- const element = document . createElement ( "div" ) ;
473+ const element = doc . createElement ( "div" ) ;
473474 element . id = "rcb-ui-pw-root" ;
474475 element . className = "rcb-ui-pw-root" ;
475476 // one point less than the purchase flow modal.
@@ -481,14 +482,14 @@ export class Purchases {
481482 element . style . height = "100%" ;
482483 element . style . overflow = "auto" ;
483484 element . style . backgroundColor = "rgba(0, 0, 0, 0.4)" ;
484- if ( document . body . offsetWidth > 968 ) {
485+ if ( doc . body . offsetWidth > 968 ) {
485486 element . style . display = "flex" ;
486487 element . style . justifyContent = "center" ;
487488 element . style . alignItems = "center" ;
488489 }
489490 element . style . transition = "opacity 0.3s" ;
490491 element . style . opacity = "0" ;
491- document . body . appendChild ( element ) ;
492+ doc . body . appendChild ( element ) ;
492493 resolvedHTMLTarget = element ;
493494 }
494495
@@ -598,7 +599,8 @@ export class Purchases {
598599
599600 // Opinionated approach:
600601 // navigating to the URL in a new tab.
601- window . open ( url , "_blank" ) ?. focus ( ) ;
602+ const win = getWindow ( ) ;
603+ win . open ( url , "_blank" ) ?. focus ( ) ;
602604 } ;
603605
604606 const onRestorePurchasesClicked = ( ) => {
@@ -1036,8 +1038,9 @@ export class Purchases {
10361038 const isInElement = htmlTarget !== undefined ;
10371039
10381040 return new Promise ( ( resolve , reject ) => {
1041+ const win = getWindow ( ) ;
10391042 if ( ! isInElement ) {
1040- window . history . pushState ( { checkoutOpen : true } , "" ) ;
1043+ win . history . pushState ( { checkoutOpen : true } , "" ) ;
10411044 }
10421045
10431046 const unmountPurchaseUi = ( ) => {
@@ -1053,7 +1056,7 @@ export class Purchases {
10531056 ) ;
10541057
10551058 if ( ! isInElement && onClose ) {
1056- window . addEventListener ( "popstate" , onClose as EventListener ) ;
1059+ win . addEventListener ( "popstate" , onClose as EventListener ) ;
10571060 }
10581061
10591062 const onFinished = this . createCheckoutOnFinishedHandler (
@@ -1147,8 +1150,9 @@ export class Purchases {
11471150 const isInElement = htmlTarget !== undefined ;
11481151
11491152 return new Promise ( ( resolve , reject ) => {
1153+ const win = getWindow ( ) ;
11501154 if ( ! isInElement ) {
1151- window . history . pushState ( { checkoutOpen : true } , "" ) ;
1155+ win . history . pushState ( { checkoutOpen : true } , "" ) ;
11521156 }
11531157
11541158 const unmountPaddlePurchaseUi = ( ) => {
@@ -1209,13 +1213,13 @@ export class Purchases {
12091213 * If no element is found, creates a new div with className "rcb-ui-root".
12101214 */
12111215 private resolveHTMLTarget ( htmlTarget ?: HTMLElement ) : HTMLElement {
1212- let resolvedHTMLTarget =
1213- htmlTarget ?? document . getElementById ( "rcb-ui-root" ) ;
1216+ const doc = getDocument ( ) ;
1217+ let resolvedHTMLTarget = htmlTarget ?? doc . getElementById ( "rcb-ui-root" ) ;
12141218
12151219 if ( resolvedHTMLTarget === null ) {
1216- const element = document . createElement ( "div" ) ;
1220+ const element = doc . createElement ( "div" ) ;
12171221 element . className = "rcb-ui-root" ;
1218- document . body . appendChild ( element ) ;
1222+ doc . body . appendChild ( element ) ;
12191223 resolvedHTMLTarget = element ;
12201224 }
12211225
@@ -1242,7 +1246,8 @@ export class Purchases {
12421246 const onClose = ( ) => {
12431247 const event = createCheckoutSessionEndClosedEvent ( ) ;
12441248 this . eventsTracker . trackSDKEvent ( event ) ;
1245- window . removeEventListener ( "popstate" , onClose as EventListener ) ;
1249+ const win = getWindow ( ) ;
1250+ win . removeEventListener ( "popstate" , onClose as EventListener ) ;
12461251
12471252 callback ?.( ) ;
12481253
0 commit comments