@@ -6,14 +6,15 @@ const GENERATE_PAY_LINK_URL = LEGACY_STORE_URL + '/hub/generate-pay-link';
66const MANAGE_SUBSCRIPTION_URL = LEGACY_STORE_URL + '/hub/manage-subscription' ;
77const UPDATE_PAYMENT_METHOD_URL = LEGACY_STORE_URL + '/hub/update-payment-method' ;
88const REFRESH_LICENSE_URL = API_BASE_URL + '/licenses/hub/refresh' ;
9+ const FRAGMENT_PARAMS_STORAGE_KEY = 'hubSubscriptionParams' ;
910
1011class HubSubscription {
1112
1213 constructor ( form , subscriptionData , searchParams ) {
1314 this . _form = form ;
1415 this . _subscriptionData = subscriptionData ;
15- let fragmentParams = new URLSearchParams ( location . hash . substring ( 1 ) ) ;
16- this . _subscriptionData . oldLicense = fragmentParams . get ( ' oldLicense' ) ;
16+ let restoredParams = this . resolveParams ( ) ;
17+ this . _subscriptionData . oldLicense = restoredParams . oldLicense ;
1718 if ( this . _subscriptionData . oldLicense ) {
1819 try {
1920 let base64 = this . _subscriptionData . oldLicense . split ( '.' ) [ 1 ] . replace ( / - / g, '+' ) . replace ( / _ / g, '/' ) ;
@@ -24,9 +25,10 @@ class HubSubscription {
2425 }
2526 }
2627 this . _subscriptionData . hubId = this . _subscriptionData . hubId ?? searchParams . get ( 'hub_id' ) ;
27- let returnUrl = fragmentParams . get ( ' returnUrl' ) ?? searchParams . get ( 'return_url' ) ;
28+ let returnUrl = restoredParams . returnUrl ?? searchParams . get ( 'return_url' ) ;
2829 if ( returnUrl ) {
2930 this . _subscriptionData . returnUrl = returnUrl ;
31+ this . _subscriptionData . returnUrlFromFragment = restoredParams . returnUrlFromFragment ;
3032 }
3133 this . _subscriptionData . session = searchParams . get ( 'session' ) ;
3234 if ( this . _subscriptionData . hubId && this . _subscriptionData . hubId . length > 0 && this . _subscriptionData . returnUrl && this . _subscriptionData . returnUrl . length > 0 ) {
@@ -46,6 +48,39 @@ class HubSubscription {
4648 } ) ;
4749 }
4850
51+ resolveParams ( ) {
52+ let fragmentParams = new URLSearchParams ( location . hash . substring ( 1 ) ) ;
53+ let oldLicense = fragmentParams . get ( 'oldLicense' ) ;
54+ let returnUrl = fragmentParams . get ( 'returnUrl' ) ;
55+ if ( oldLicense || returnUrl ) {
56+ let params = { oldLicense : oldLicense , returnUrl : returnUrl , returnUrlFromFragment : returnUrl != null } ;
57+ try {
58+ sessionStorage . setItem ( FRAGMENT_PARAMS_STORAGE_KEY , JSON . stringify ( params ) ) ;
59+ } catch ( e ) {
60+ console . error ( 'Failed to persist hub billing parameters:' , e ) ;
61+ }
62+ // The oldLicense token is long enough that leaving it in the page URL breaks Paddle checkout, so we drop the fragment regardless of whether the stash succeeded.
63+ history . replaceState ( null , '' , location . pathname + location . search ) ;
64+ return params ;
65+ }
66+ let searchParams = new URLSearchParams ( location . search ) ;
67+ let emptyParams = { oldLicense : null , returnUrl : null , returnUrlFromFragment : false } ;
68+ try {
69+ if ( searchParams . has ( 'hub_id' ) || searchParams . has ( 'return_url' ) ) {
70+ // A fresh flow carries its own query params, so any stashed fragment from an earlier, abandoned flow is stale and must not override it.
71+ sessionStorage . removeItem ( FRAGMENT_PARAMS_STORAGE_KEY ) ;
72+ return emptyParams ;
73+ }
74+ let stored = sessionStorage . getItem ( FRAGMENT_PARAMS_STORAGE_KEY ) ;
75+ if ( stored ) {
76+ return JSON . parse ( stored ) ;
77+ }
78+ } catch ( e ) {
79+ console . error ( 'Failed to restore hub billing parameters:' , e ) ;
80+ }
81+ return emptyParams ;
82+ }
83+
4984 loadSubscription ( ) {
5085 this . loadCustomBilling ( ( ) => {
5186 this . loadPrice ( ( ) => {
@@ -535,7 +570,13 @@ class HubSubscription {
535570 }
536571
537572 transferTokenToHub ( ) {
538- window . open ( this . _subscriptionData . returnUrl + '?token=' + this . _subscriptionData . token , '_self' ) ;
573+ try {
574+ sessionStorage . removeItem ( FRAGMENT_PARAMS_STORAGE_KEY ) ;
575+ } catch ( e ) {
576+ console . error ( 'Failed to clear hub billing parameters:' , e ) ;
577+ }
578+ let separator = this . _subscriptionData . returnUrlFromFragment ? '#' : '?' ;
579+ location . href = this . _subscriptionData . returnUrl + separator + 'token=' + this . _subscriptionData . token ;
539580 }
540581
541582}
0 commit comments