11"use strict" ;
22
3+ const BILLING_SESSION_URL = API_BASE_URL + '/billing/session' ;
4+ const BILLING_CUSTOMER_URL = API_BASE_URL + '/billing/customers/by-hub-id' ;
35const CARD_CHECKOUT_URL = API_BASE_URL + '/billing/paddle-classic/checkout' ;
46const INVOICE_CHECKOUT_URL = API_BASE_URL + '/billing/espocrm/checkout' ;
57const MANAGE_SUBSCRIPTION_BASE_URL = API_BASE_URL + '/billing/manage/subscription' ;
6- const SESSION_REQUEST_URL = API_BASE_URL + '/billing/manage/session/request' ;
7- const SESSION_CONFIRM_URL = API_BASE_URL + '/billing/manage/session/confirm' ;
88const CUSTOM_BILLING_URL = LEGACY_STORE_URL + '/hub/custom-billing' ;
9- const REFRESH_LICENSE_URL = API_BASE_URL + '/licenses/hub/refresh ' ;
9+ const GET_LICENSE_URL = API_BASE_URL + '/licenses/hub' ;
1010
1111class HubSubscription {
1212
1313 constructor ( form , subscriptionData , searchParams ) {
1414 this . _form = form ;
1515 this . _subscriptionData = subscriptionData ;
16- let fragmentParams = new URLSearchParams ( location . hash . substring ( 1 ) ) ;
17- this . _subscriptionData . oldLicense = fragmentParams . get ( 'oldLicense' ) ;
16+ this . _subscriptionData . oldLicense = searchParams . get ( 'oldLicense' ) ;
1817 if ( this . _subscriptionData . oldLicense ) {
1918 this . _subscriptionData . hubId = this . extractHubId ( this . _subscriptionData . oldLicense ) ;
2019 if ( ! this . _subscriptionData . hubId ) {
2120 this . _subscriptionData . oldLicense = null ;
2221 }
2322 }
24- this . _subscriptionData . hubId = this . _subscriptionData . hubId ?? fragmentParams . get ( 'hub_id' ) ?? searchParams . get ( 'hub_id' ) ;
25- let returnUrl = fragmentParams . get ( 'returnUrl' ) ?? searchParams . get ( 'return_url' ) ;
23+ this . _subscriptionData . hubId = this . _subscriptionData . hubId ?? searchParams . get ( 'hub_id' ) ;
24+ let returnUrl = searchParams . get ( 'return_url' ) ;
2625 if ( returnUrl ) {
2726 this . _subscriptionData . returnUrl = returnUrl ;
2827 }
29- let nonce = fragmentParams . get ( 'nonce' ) ;
30- if ( nonce ) {
31- this . restoreHubContext ( ) ;
32- history . replaceState ( null , '' , location . pathname + location . search ) ;
28+ // Capture the Hub's `token_transfer` value (how the license should be delivered) so it can be stored in
29+ // the billing session.
30+ this . _subscriptionData . tokenTransfer = searchParams . get ( 'token_transfer' ) ?? 'queryParam' ;
31+ this . _subscriptionData . session = searchParams . get ( 'session' ) ;
32+ if ( this . _subscriptionData . session ) {
33+ // We returned from the confirmation link (/hub/billing?session=<id>): resolve the verified
34+ // billing session and continue into the manage or checkout flow.
3335 this . _subscriptionData . state = 'LOADING' ;
34- this . confirmSession ( nonce ) ;
36+ this . loadBillingSession ( ) ;
3537 } else if ( this . _subscriptionData . hubId && this . _subscriptionData . hubId . length > 0 && this . _subscriptionData . returnUrl && this . _subscriptionData . returnUrl . length > 0 ) {
36- this . _subscriptionData . state = 'LOADING' ;
37- this . loadCheckoutPrerequisites ( ) ;
38+ // Opened from the Hub without a verified session yet: ask the customer to request a
39+ // confirmation link before we can manage their subscription or check out.
40+ this . _subscriptionData . state = 'CREATE_SESSION' ;
3841 }
3942 this . _paddle = $ . ajax ( {
4043 url : 'https://cdn.paddle.com/paddle/paddle.js' ,
@@ -60,19 +63,7 @@ class HubSubscription {
6063 }
6164
6265 authHeaders ( ) {
63- return { Authorization : 'Bearer ' + this . _subscriptionData . sessionToken } ;
64- }
65-
66- restoreHubContext ( ) {
67- let hubId = this . _subscriptionData . hubId ;
68- let oldLicense = sessionStorage . getItem ( `hubOldLicense:${ hubId } ` ) ;
69- if ( oldLicense ) {
70- this . _subscriptionData . oldLicense = oldLicense ;
71- }
72- let returnUrl = sessionStorage . getItem ( `hubReturnUrl:${ hubId } ` ) ;
73- if ( returnUrl ) {
74- this . _subscriptionData . returnUrl = returnUrl ;
75- }
66+ return { Authorization : 'Bearer ' + this . _subscriptionData . session } ;
7667 }
7768
7869 loadCheckoutPrerequisites ( ) {
@@ -116,7 +107,7 @@ class HubSubscription {
116107 this . _subscriptionData . state = 'EXISTING_CUSTOMER' ;
117108 this . _subscriptionData . errorMessage = '' ;
118109 this . _subscriptionData . inProgress = false ;
119- this . _subscriptionData . needsTokenRefresh = ! ! this . _subscriptionData . oldLicense ;
110+ this . refreshToken ( ) ;
120111 }
121112
122113 onLoadSubscriptionFailed ( status , error ) {
@@ -136,68 +127,130 @@ class HubSubscription {
136127 this . _subscriptionData . inProgress = false ;
137128 }
138129
139- createSession ( ) {
140- if ( ! $ ( this . _form ) [ 0 ] . checkValidity ( ) ) {
141- $ ( this . _form ) . find ( ':input' ) . addClass ( 'show-invalid' ) ;
142- this . _subscriptionData . errorMessage = 'Please fill in all required fields.' ;
143- return ;
144- }
130+ loadBillingSession ( ) {
131+ this . _subscriptionData . inProgress = true ;
132+ this . _subscriptionData . errorMessage = '' ;
133+ $ . ajax ( {
134+ url : BILLING_SESSION_URL + '/' + encodeURIComponent ( this . _subscriptionData . session ) ,
135+ type : 'GET'
136+ } ) . done ( data => {
137+ this . onLoadBillingSessionSucceeded ( data ) ;
138+ } ) . fail ( xhr => {
139+ this . onLoadBillingSessionFailed ( xhr . status , xhr . responseJSON ?. message || 'Loading billing session failed.' ) ;
140+ } ) ;
141+ }
145142
146- let hubId = this . _subscriptionData . hubId ;
147- if ( this . _subscriptionData . oldLicense ) {
148- sessionStorage . setItem ( `hubOldLicense:${ hubId } ` , this . _subscriptionData . oldLicense ) ;
143+ onLoadBillingSessionSucceeded ( data ) {
144+ this . _subscriptionData . hubId = data . hubId ;
145+ this . _subscriptionData . email = data . email ;
146+ this . _subscriptionData . returnUrl = data . returnUrl ;
147+ this . _subscriptionData . tokenTransfer = data . tokenTransfer ;
148+ if ( ! this . _subscriptionData . invoice . contact_email ) {
149+ this . _subscriptionData . invoice . contact_email = data . email ;
150+ }
151+ this . _subscriptionData . errorMessage = '' ;
152+ // The session is verified; a session already linked to a billing manages it (the manage endpoints
153+ // only accept linked sessions), an unlinked one belongs to a new customer heading into checkout.
154+ if ( data . billingId ) {
155+ this . loadManageSubscription ( ) ;
156+ } else {
157+ this . loadCheckoutPrerequisites ( ) ;
149158 }
150- if ( this . _subscriptionData . returnUrl ) {
151- sessionStorage . setItem ( `hubReturnUrl:${ hubId } ` , this . _subscriptionData . returnUrl ) ;
159+ }
160+
161+ onLoadBillingSessionFailed ( status , error ) {
162+ if ( status == 404 ) {
163+ this . _subscriptionData . state = 'LINK_EXPIRED' ;
164+ this . _subscriptionData . errorMessage = '' ;
165+ } else if ( this . _subscriptionData . hubId ) {
166+ this . _subscriptionData . state = 'CREATE_SESSION' ;
167+ this . _subscriptionData . errorMessage = error ;
168+ } else {
169+ this . _subscriptionData . state = 'MISSING_PARAMS' ;
170+ this . _subscriptionData . errorMessage = '' ;
152171 }
172+ this . _subscriptionData . inProgress = false ;
173+ }
174+
175+ lookupCustomer ( ) {
153176 this . _subscriptionData . inProgress = true ;
154177 this . _subscriptionData . errorMessage = '' ;
178+ // First challenge (from /billing/customers/challenge) gates this lookup: it tells us whether
179+ // the Hub is already linked to a customer before we ask for a confirmation link.
155180 $ . ajax ( {
156- url : SESSION_REQUEST_URL ,
157- type : 'POST ' ,
181+ url : BILLING_CUSTOMER_URL + '/' + encodeURIComponent ( this . _subscriptionData . hubId ) ,
182+ type : 'GET ' ,
158183 data : {
159- captcha : this . _subscriptionData . captcha ,
160- hub_id : this . _subscriptionData . hubId
184+ captcha : this . _subscriptionData . captcha
161185 }
162- } ) . done ( _ => {
163- this . onCreateSessionSucceeded ( ) ;
186+ } ) . done ( data => {
187+ this . onLookupCustomerSucceeded ( data ) ;
164188 } ) . fail ( xhr => {
165- this . onCreateSessionFailed ( xhr . responseJSON ?. message || 'Requesting access failed.' ) ;
189+ this . onLookupCustomerFailed ( xhr . status , xhr . responseJSON ?. message || 'Looking up your subscription failed.' ) ;
166190 } ) ;
167191 }
168192
169- onCreateSessionSucceeded ( ) {
170- this . _subscriptionData . state = 'CREATE_SESSION_SUCCESS' ;
171- this . _subscriptionData . inProgress = false ;
193+ onLookupCustomerSucceeded ( data ) {
194+ // The Hub is already linked to a customer: the API returns their redacted email so we can
195+ // show where the confirmation link will be sent without revealing the full address.
196+ this . _subscriptionData . redactedEmail = data . email ;
197+ this . _subscriptionData . needsEmail = false ;
198+ this . _subscriptionData . lookupDone = true ;
172199 this . _subscriptionData . errorMessage = '' ;
200+ this . _subscriptionData . inProgress = false ;
173201 }
174202
175- onCreateSessionFailed ( error ) {
203+ onLookupCustomerFailed ( status , error ) {
204+ // 404 means the Hub is not linked to a customer yet: ask for the purchase email. Any other
205+ // failure falls back to the same manual entry (the lookup captcha solves only once, so a
206+ // transient failure cannot re-trigger the lookup, and for a known hub the server ignores the
207+ // entered address and mails the one on file) — but keeps the error visible.
176208 this . _subscriptionData . inProgress = false ;
177- this . _subscriptionData . errorMessage = error ;
209+ this . _subscriptionData . needsEmail = true ;
210+ this . _subscriptionData . redactedEmail = null ;
211+ this . _subscriptionData . lookupDone = true ;
212+ this . _subscriptionData . errorMessage = status == 404 ? '' : error ;
178213 }
179214
180- confirmSession ( nonce ) {
215+ createSession ( ) {
216+ if ( ! $ ( this . _form ) [ 0 ] . checkValidity ( ) ) {
217+ $ ( this . _form ) . find ( ':input' ) . addClass ( 'show-invalid' ) ;
218+ this . _subscriptionData . errorMessage = 'Please fill in all required fields.' ;
219+ return ;
220+ }
221+
181222 this . _subscriptionData . inProgress = true ;
182223 this . _subscriptionData . errorMessage = '' ;
224+ let body = {
225+ hubId : this . _subscriptionData . hubId ,
226+ returnUrl : this . _subscriptionData . returnUrl ,
227+ tokenTransfer : this . _subscriptionData . tokenTransfer ,
228+ captcha : this . _subscriptionData . captcha
229+ } ;
230+ if ( this . _subscriptionData . email ) {
231+ body . email = this . _subscriptionData . email ;
232+ }
183233 $ . ajax ( {
184- url : SESSION_CONFIRM_URL ,
234+ url : BILLING_SESSION_URL ,
185235 type : 'POST' ,
186- data : {
187- nonce : nonce
188- }
189- } ) . done ( data => {
190- this . _subscriptionData . sessionToken = data . token ;
191- this . loadManageSubscription ( ) ;
236+ contentType : 'application/json' ,
237+ data : JSON . stringify ( body )
238+ } ) . done ( _ => {
239+ this . onCreateSessionSucceeded ( ) ;
192240 } ) . fail ( xhr => {
193- this . onConfirmSessionFailed ( xhr . responseJSON ?. message || 'Confirming access failed.' ) ;
241+ this . onCreateSessionFailed ( xhr . responseJSON ?. message || 'Requesting confirmation link failed.' ) ;
194242 } ) ;
195243 }
196244
197- onConfirmSessionFailed ( error ) {
198- this . _subscriptionData . state = 'CREATE_SESSION' ;
199- this . _subscriptionData . errorMessage = error ;
245+ onCreateSessionSucceeded ( ) {
246+ this . _subscriptionData . state = 'CREATE_SESSION_SUCCESS' ;
200247 this . _subscriptionData . inProgress = false ;
248+ this . _subscriptionData . errorMessage = '' ;
249+ }
250+
251+ onCreateSessionFailed ( error ) {
252+ this . _subscriptionData . inProgress = false ;
253+ this . _subscriptionData . errorMessage = error ;
201254 }
202255
203256 loadCustomBilling ( continueHandler ) {
@@ -339,7 +392,8 @@ class HubSubscription {
339392 captcha : this . _subscriptionData . cardCaptcha ,
340393 hub_id : this . _subscriptionData . hubId ,
341394 product_id : productId ,
342- quantity : this . _subscriptionData . quantity
395+ quantity : this . _subscriptionData . quantity ,
396+ session : this . _subscriptionData . session
343397 }
344398 } ) . done ( data => {
345399 this . openPaddleCheckout ( data . pay_link , locale ) ;
@@ -354,7 +408,7 @@ class HubSubscription {
354408 override : payLink ,
355409 email : this . _subscriptionData . email ,
356410 locale : locale ,
357- passthrough : JSON . stringify ( { hub_id : this . _subscriptionData . hubId } ) ,
411+ passthrough : JSON . stringify ( { hub_id : this . _subscriptionData . hubId , session : this . _subscriptionData . session } ) ,
358412 successCallback : data => this . getPaddleOrderDetails ( data . checkout . id ) ,
359413 closeCallback : ( ) => {
360414 this . _subscriptionData . inProgress = false ;
@@ -394,6 +448,7 @@ class HubSubscription {
394448 hub_id : this . _subscriptionData . hubId ,
395449 product_id : this . selectedPlanId ( ) ,
396450 quantity : this . _subscriptionData . quantity ,
451+ session : this . _subscriptionData . session ,
397452 account_name : invoice . account_name ,
398453 vat_id : invoice . vat_id ,
399454 address_street : invoice . address_street ,
@@ -415,10 +470,8 @@ class HubSubscription {
415470 this . _subscriptionData . state = 'CHECKOUT_SUCCESS' ;
416471 this . _subscriptionData . errorMessage = '' ;
417472 this . _subscriptionData . inProgress = false ;
418- if ( this . _subscriptionData . oldLicense ) {
419- this . _subscriptionData . needsTokenRefresh = true ;
420- this . _subscriptionData . shouldTransferToHub = ! ! this . _subscriptionData . returnUrl ;
421- }
473+ this . _subscriptionData . shouldTransferToHub = ! ! this . _subscriptionData . returnUrl ;
474+ this . refreshToken ( ) ;
422475 }
423476
424477 onPostFailed ( error ) {
@@ -429,6 +482,7 @@ class HubSubscription {
429482 updatePaymentMethod ( locale ) {
430483 this . _subscriptionData . inProgress = true ;
431484 this . _subscriptionData . errorMessage = '' ;
485+ this . _subscriptionData . shouldTransferToHub = false ;
432486 $ . ajax ( {
433487 url : `${ MANAGE_SUBSCRIPTION_BASE_URL } /${ this . _subscriptionData . hubId } /payment-method` ,
434488 type : 'GET' ,
@@ -452,6 +506,8 @@ class HubSubscription {
452506 pause ( ) {
453507 this . _subscriptionData . inProgress = true ;
454508 this . _subscriptionData . errorMessage = '' ;
509+ // a stale transfer intent from an earlier action must not redirect after this refresh
510+ this . _subscriptionData . shouldTransferToHub = false ;
455511 $ . ajax ( {
456512 url : `${ MANAGE_SUBSCRIPTION_BASE_URL } /${ this . _subscriptionData . hubId } /pause` ,
457513 type : 'POST' ,
@@ -550,14 +606,15 @@ class HubSubscription {
550606 }
551607
552608 refreshToken ( ) {
609+ this . _subscriptionData . needsTokenRefresh = true ;
553610 this . _subscriptionData . inProgress = true ;
554611 this . _subscriptionData . errorMessage = '' ;
555612 $ . ajax ( {
556- url : REFRESH_LICENSE_URL ,
557- type : 'POST ' ,
613+ url : GET_LICENSE_URL ,
614+ type : 'GET ' ,
558615 data : {
559- token : this . _subscriptionData . oldLicense ,
560- captcha : this . _subscriptionData . captcha
616+ session : this . _subscriptionData . session ,
617+ legacy : this . _subscriptionData . tokenTransfer === 'queryParam'
561618 }
562619 } ) . done ( token => {
563620 this . _subscriptionData . token = token ;
@@ -568,14 +625,23 @@ class HubSubscription {
568625 this . transferTokenToHub ( ) ;
569626 }
570627 } ) . fail ( xhr => {
628+ // Expected for a card checkout until Paddle's payment webhook links the session to the new
629+ // billing; the license block then offers a retry.
571630 this . _subscriptionData . errorMessage = xhr . responseJSON ?. message || 'Refreshing license failed.' ;
572631 this . _subscriptionData . needsTokenRefresh = false ;
573632 this . _subscriptionData . inProgress = false ;
574633 } ) ;
575634 }
576635
577636 transferTokenToHub ( ) {
578- window . open ( this . _subscriptionData . returnUrl + '?token=' + this . _subscriptionData . token , '_self' ) ;
637+ if ( this . _subscriptionData . tokenTransfer === 'queryParam' ) {
638+ location . href = this . _subscriptionData . returnUrl + '?token=' + encodeURIComponent ( this . _subscriptionData . token ) ;
639+ } else if ( this . _subscriptionData . tokenTransfer === 'session' ) {
640+ // Hand the Hub the billing session id instead; it resolves the license itself.
641+ location . href = this . _subscriptionData . returnUrl + '?session=' + encodeURIComponent ( this . _subscriptionData . session ) ;
642+ } else {
643+ console . error ( 'Unknown token transfer method:' , this . _subscriptionData . tokenTransfer ) ;
644+ }
579645 }
580646
581647}
0 commit comments