1818
1919import { loadScript } from '../lib/load-script' ;
2020
21+ export interface ReadyToPayChangeResponse {
22+ isButtonVisible : boolean ;
23+ isReadyToPay : boolean ;
24+ paymentMethodPresent ?: boolean ;
25+ }
26+
2127export interface Config {
2228 environment ?: google . payments . api . Environment ;
2329 existingPaymentMethodRequired ?: boolean ;
@@ -27,7 +33,7 @@ export interface Config {
2733 onLoadPaymentData ?: ( paymentData : google . payments . api . PaymentData ) => void ;
2834 onCancel ?: ( reason : google . payments . api . PaymentsError ) => void ;
2935 onError ?: ( error : Error ) => void ;
30- onReadyToPayChange ?: ( isReadyToPay : boolean ) => void ;
36+ onReadyToPayChange ?: ( result : ReadyToPayChangeResponse ) => void ;
3137 buttonColor ?: google . payments . api . ButtonColor ;
3238 buttonType ?: google . payments . api . ButtonType ;
3339}
@@ -52,6 +58,7 @@ export class ButtonManager {
5258 private oldInvalidationValues ?: any [ ] ;
5359
5460 isReadyToPay ?: boolean ;
61+ paymentMethodPresent ?: boolean ;
5562
5663 constructor ( options : ButtonManagerOptions ) {
5764 this . options = options ;
@@ -278,11 +285,12 @@ export class ButtonManager {
278285 this . setClassName ( element , [ element . className , 'not-ready' ] ) ;
279286 element . appendChild ( button ) ;
280287
281- let isReadyToPay = false ;
288+ let showButton = false ;
289+ let readyToPay : google . payments . api . IsReadyToPayResponse | undefined ;
282290
283291 try {
284- const readyToPay = await this . client . isReadyToPay ( this . createIsReadyToPayRequest ( this . config ) ) ;
285- isReadyToPay =
292+ readyToPay = await this . client . isReadyToPay ( this . createIsReadyToPayRequest ( this . config ) ) ;
293+ showButton =
286294 ( readyToPay . result && ! this . config . existingPaymentMethodRequired ) ||
287295 ( readyToPay . result && readyToPay . paymentMethodPresent && this . config . existingPaymentMethodRequired ) ||
288296 false ;
@@ -292,18 +300,29 @@ export class ButtonManager {
292300
293301 if ( ! this . isMounted ( ) ) return ;
294302
295- if ( isReadyToPay ) {
303+ if ( showButton ) {
296304 // remove hidden className
297305 this . setClassName (
298306 element ,
299307 ( element . className || '' ) . split ( ' ' ) . filter ( className => className && className !== 'not-ready' ) ,
300308 ) ;
301309 }
302310
303- if ( this . isReadyToPay !== isReadyToPay ) {
304- this . isReadyToPay = isReadyToPay ;
311+ if ( this . isReadyToPay !== readyToPay ?. result || this . paymentMethodPresent !== readyToPay ?. paymentMethodPresent ) {
312+ this . isReadyToPay = ! ! readyToPay ?. result ;
313+ this . paymentMethodPresent = readyToPay ?. paymentMethodPresent ;
314+
305315 if ( this . config . onReadyToPayChange ) {
306- this . config . onReadyToPayChange ( isReadyToPay ) ;
316+ const readyToPayResponse : ReadyToPayChangeResponse = {
317+ isButtonVisible : showButton ,
318+ isReadyToPay : this . isReadyToPay ,
319+ } ;
320+
321+ if ( this . paymentMethodPresent ) {
322+ readyToPayResponse . paymentMethodPresent = this . paymentMethodPresent ;
323+ }
324+
325+ this . config . onReadyToPayChange ( readyToPayResponse ) ;
307326 }
308327 }
309328 }
0 commit comments