Add block support in GooglePay

This commit is contained in:
Pedro Silva 2023-08-25 16:25:20 +01:00
parent eff390f564
commit 6a205d1413
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
8 changed files with 232 additions and 19 deletions

View file

@ -0,0 +1,114 @@
<?php
/**
* The googlepay blocks module.
*
* @package WooCommerce\PayPalCommerce\Googlepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay\Assets;
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodTypeInterface;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
/**
* Class BlocksPaymentMethod
*/
class BlocksPaymentMethod extends AbstractPaymentMethodType {
/**
* The URL of this module.
*
* @var string
*/
private $module_url;
/**
* The assets version.
*
* @var string
*/
private $version;
/**
* The button.
*
* @var ButtonInterface
*/
private $button;
/**
* The paypal payment method.
*
* @var PaymentMethodTypeInterface
*/
private $paypal_payment_method;
/**
* Assets constructor.
*
* @param string $name The name of this module.
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param ButtonInterface $button The button.
* @param PaymentMethodTypeInterface $paypal_payment_method The paypal payment method.
*/
public function __construct(
string $name,
string $module_url,
string $version,
ButtonInterface $button,
PaymentMethodTypeInterface $paypal_payment_method
) {
$this->name = $name;
$this->module_url = $module_url;
$this->version = $version;
$this->button = $button;
$this->paypal_payment_method = $paypal_payment_method;
}
/**
* {@inheritDoc}
*/
public function initialize() { }
/**
* {@inheritDoc}
*/
public function is_active() {
return $this->paypal_payment_method->is_active();
}
/**
* {@inheritDoc}
*/
public function get_payment_method_script_handles() {
$handle = $this->name . '-block';
wp_register_script(
$handle,
trailingslashit( $this->module_url ) . 'assets/js/boot-block.js',
array(),
$this->version,
true
);
return array( $handle );
}
/**
* {@inheritDoc}
*/
public function get_payment_method_data() {
$paypal_data = $this->paypal_payment_method->get_payment_method_data();
return array(
'id' => $this->name,
'title' => $paypal_data['title'], // TODO : see if we should use another.
'description' => $paypal_data['description'], // TODO : see if we should use another.
'enabled' => $paypal_data['enabled'], // This button is enabled when PayPal buttons are.
'scriptData' => $this->button->script_data(),
);
}
}

View file

@ -17,9 +17,9 @@ use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class SmartButton
* Class Button
*/
class GooglepayButton implements ButtonInterface {
class Button implements ButtonInterface {
/**
* The URL to the module.

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Googlepay;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
@ -63,6 +64,12 @@ class GooglepayModule implements ModuleInterface {
}
);
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
$payment_method_registry->register( $c->get( 'googlepay.blocks-payment-method' ) );
}
);
}
/**