🐛 Refresh total price when checkout form changes

Applies the selected shipping costs to the Google Payment button
This commit is contained in:
Philipp Stracker 2024-08-08 21:01:22 +02:00
parent c382b5cee1
commit 7106413f87
No known key found for this signature in database

View file

@ -128,6 +128,7 @@ class GooglepayButton extends PaymentButton {
contextHandler
);
this.init = this.init.bind( this );
this.onPaymentAuthorized = this.onPaymentAuthorized.bind( this );
this.onPaymentDataChanged = this.onPaymentDataChanged.bind( this );
this.onButtonClick = this.onButtonClick.bind( this );
@ -207,6 +208,17 @@ class GooglepayButton extends PaymentButton {
return this.#transactionInfo;
}
/**
* Assign the new transaction details to the payment button.
*
* @param {TransactionInfo} newTransactionInfo - Transaction details.
*/
set transactionInfo( newTransactionInfo ) {
this.#transactionInfo = newTransactionInfo;
this.refresh();
}
/**
* Configures the button instance. Must be called before the initial `init()`.
*
@ -257,14 +269,23 @@ class GooglepayButton extends PaymentButton {
this.isEligible = !! response.result;
} )
.catch( ( err ) => {
console.error( err );
this.error( err );
this.isEligible = false;
} );
}
reinit() {
super.reinit();
this.init();
if ( this.contextHandler?.transactionInfo ) {
// If possible, fetch the current transaction details from the checkout form.
this.contextHandler.transactionInfo().then( ( transactionInfo ) => {
this.transactionInfo = transactionInfo;
this.init();
} );
} else {
this.init();
}
}
/**
@ -320,7 +341,7 @@ class GooglepayButton extends PaymentButton {
* correct wrapper.
*/
addButton() {
if ( ! this.isInitialized || ! this.paymentsClient ) {
if ( ! this.paymentsClient ) {
return;
}
@ -466,7 +487,7 @@ class GooglepayButton extends PaymentButton {
resolve( paymentDataRequestUpdate );
} catch ( error ) {
console.error( 'Error during onPaymentDataChanged:', error );
this.error( 'Error during onPaymentDataChanged:', error );
reject( error );
}
} );