woocommerce-paypal-payments/modules/ppcp-googlepay/src/GooglepayModule.php

137 lines
3.6 KiB
PHP
Raw Normal View History

2023-08-22 08:44:32 +01:00
<?php
/**
* The Googlepay module.
*
* @package WooCommerce\PayPalCommerce\Googlepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay;
2023-08-25 16:25:20 +01:00
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
2023-09-21 18:30:43 +01:00
use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message;
use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository;
2023-08-24 17:30:29 +01:00
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
2023-09-08 18:43:33 +01:00
use WooCommerce\PayPalCommerce\Googlepay\Helper\ApmProductStatus;
use WooCommerce\PayPalCommerce\Googlepay\Helper\AvailabilityNotice;
2023-08-22 08:44:32 +01:00
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
2023-09-11 10:04:38 +01:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2023-08-22 08:44:32 +01:00
/**
* Class GooglepayModule
*/
class GooglepayModule implements ModuleInterface {
/**
* {@inheritDoc}
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): void {
2023-09-01 11:32:26 +01:00
// Clears product status when appropriate.
add_action(
'woocommerce_paypal_payments_clear_apm_product_status',
function( Settings $settings = null ) use ( $c ): void {
$apm_status = $c->get( 'googlepay.helpers.apm-product-status' );
assert( $apm_status instanceof ApmProductStatus );
$apm_status->clear( $settings );
}
);
// Check if the module is applicable, correct country, currency, ... etc.
2023-09-01 11:32:26 +01:00
if ( ! $c->get( 'googlepay.eligible' ) ) {
return;
}
// Load the button handler.
2023-09-08 18:43:33 +01:00
$button = $c->get( 'googlepay.button' );
assert( $button instanceof ButtonInterface );
2023-09-01 11:32:26 +01:00
$button->initialize();
// Check if this merchant can activate / use the buttons.
2023-09-01 11:32:26 +01:00
if ( ! $c->get( 'googlepay.available' ) ) {
$availability_notice = $c->get( 'googlepay.availability_notice' );
assert( $availability_notice instanceof AvailabilityNotice );
$availability_notice->execute();
2023-09-01 11:32:26 +01:00
return;
}
2023-08-22 08:44:32 +01:00
// Initializes button rendering.
2023-08-22 08:44:32 +01:00
add_action(
'wp',
2023-09-01 11:32:26 +01:00
static function () use ( $c, $button ) {
2023-08-22 08:44:32 +01:00
if ( is_admin() ) {
return;
}
2023-09-01 11:32:26 +01:00
$button->render();
2023-08-22 08:44:32 +01:00
}
);
// Enqueue frontend scripts.
2023-08-22 08:44:32 +01:00
add_action(
'wp_enqueue_scripts',
2023-09-01 11:32:26 +01:00
static function () use ( $c, $button ) {
$button->enqueue();
2023-08-22 08:44:32 +01:00
}
);
// Enqueue backend scripts.
add_action(
'admin_enqueue_scripts',
static function () use ( $c, $button ) {
if ( ! is_admin() ) {
return;
}
/**
* Should add this to the ButtonInterface.
*
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_admin();
}
);
// Registers buttons on blocks pages.
2023-08-25 16:25:20 +01:00
add_action(
'woocommerce_blocks_payment_method_type_registration',
2023-09-01 11:32:26 +01:00
function( PaymentMethodRegistry $payment_method_registry ) use ( $c, $button ): void {
if ( $button->is_enabled() ) {
$payment_method_registry->register( $c->get( 'googlepay.blocks-payment-method' ) );
}
2023-08-25 16:25:20 +01:00
}
);
2023-09-08 18:43:33 +01:00
// Adds GooglePay component to the backend button preview settings.
add_action(
'woocommerce_paypal_payments_admin_gateway_settings',
function( array $settings ) use ( $c, $button ): array {
if ( is_array( $settings['components'] ) ) {
$settings['components'][] = 'googlepay';
}
return $settings;
}
);
2023-08-22 08:44:32 +01:00
}
/**
* Returns the key for the module.
*
* @return string|void
*/
public function getKey() {
}
}