mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
🚧 Add new validation & configuration methods
This commit is contained in:
parent
7b2397f86b
commit
2646cf4a44
1 changed files with 48 additions and 3 deletions
|
@ -84,6 +84,11 @@ class ApplePayButton extends PaymentButton {
|
|||
*/
|
||||
#initialPaymentRequest = null;
|
||||
|
||||
/**
|
||||
* Apple Pay specific API configuration.
|
||||
*/
|
||||
#applePayConfig = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
@ -170,21 +175,61 @@ class ApplePayButton extends PaymentButton {
|
|||
return this.wrapperElement instanceof HTMLElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
validateConfiguration( silent = false ) {
|
||||
const validEnvs = [ 'PRODUCTION', 'TEST' ];
|
||||
|
||||
const isInvalid = ( ...args ) => {
|
||||
if ( ! silent ) {
|
||||
this.error( ...args );
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
if ( ! validEnvs.includes( this.buttonConfig.environment ) ) {
|
||||
return isInvalid(
|
||||
'Invalid environment:',
|
||||
this.buttonConfig.environment
|
||||
);
|
||||
}
|
||||
|
||||
// Preview buttons only need a valid environment.
|
||||
if ( this.isPreview ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( ! typeof this.contextHandler?.validateContext() ) {
|
||||
return isInvalid( 'Invalid context handler.', this.contextHandler );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the button instance. Must be called before the initial `init()`.
|
||||
*
|
||||
* @param {Object} apiConfig - API configuration.
|
||||
*/
|
||||
configure( apiConfig ) {
|
||||
this.#applePayConfig = apiConfig;
|
||||
}
|
||||
|
||||
init( config ) {
|
||||
// Use `reinit()` to force a full refresh of an initialized button.
|
||||
if ( this.isInitialized ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! this.contextHandler.validateContext() ) {
|
||||
// Stop, if configuration is invalid.
|
||||
if ( ! this.validateConfiguration() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
super.init();
|
||||
this.initEventHandlers();
|
||||
|
||||
this.applePayConfig = config;
|
||||
|
||||
if ( this.isSeparateGateway ) {
|
||||
document
|
||||
.querySelectorAll( '#ppc-button-applepay-container' )
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue