2024-07-26 12:39:38 +02:00
|
|
|
import GooglepayButton from './GooglepayButton';
|
|
|
|
import ContextHandlerFactory from './Context/ContextHandlerFactory';
|
|
|
|
|
|
|
|
class GooglepayManagerBlockEditor {
|
2024-10-07 15:30:27 +02:00
|
|
|
constructor( namespace, buttonConfig, ppcpConfig ) {
|
|
|
|
this.namespace = namespace;
|
2024-07-26 12:39:38 +02:00
|
|
|
this.buttonConfig = buttonConfig;
|
|
|
|
this.ppcpConfig = ppcpConfig;
|
|
|
|
this.googlePayConfig = null;
|
|
|
|
this.transactionInfo = null;
|
|
|
|
this.contextHandler = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
( async () => {
|
|
|
|
await this.config();
|
|
|
|
} )();
|
|
|
|
}
|
|
|
|
|
|
|
|
async config() {
|
|
|
|
try {
|
|
|
|
// Gets GooglePay configuration of the PayPal merchant.
|
2024-10-07 15:30:27 +02:00
|
|
|
this.googlePayConfig = await window[ this.namespace ]
|
|
|
|
.Googlepay()
|
|
|
|
.config();
|
2024-07-26 12:39:38 +02:00
|
|
|
|
|
|
|
// Fetch transaction information.
|
|
|
|
this.transactionInfo = await this.fetchTransactionInfo();
|
|
|
|
|
|
|
|
const button = new GooglepayButton(
|
|
|
|
this.ppcpConfig.context,
|
|
|
|
null,
|
|
|
|
this.buttonConfig,
|
|
|
|
this.ppcpConfig,
|
|
|
|
this.contextHandler
|
|
|
|
);
|
|
|
|
|
|
|
|
button.init( this.googlePayConfig, this.transactionInfo );
|
|
|
|
} catch ( error ) {
|
|
|
|
console.error( 'Failed to initialize Google Pay:', error );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchTransactionInfo() {
|
|
|
|
try {
|
|
|
|
if ( ! this.contextHandler ) {
|
|
|
|
this.contextHandler = ContextHandlerFactory.create(
|
|
|
|
this.ppcpConfig.context,
|
|
|
|
this.buttonConfig,
|
|
|
|
this.ppcpConfig,
|
|
|
|
null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch ( error ) {
|
|
|
|
console.error( 'Error fetching transaction info:', error );
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default GooglepayManagerBlockEditor;
|