refactor module

This commit is contained in:
carmenmaymo 2023-08-24 18:05:05 +02:00
parent 9a85d86ef5
commit 8ebfb596fb
No known key found for this signature in database
GPG key ID: 6023F686B0F3102E
11 changed files with 210 additions and 117 deletions

View file

@ -0,0 +1,6 @@
#applepay-container {
margin-top: 0.5rem;
overflow: hidden;
border-radius: 50px;
height: 45px;
}

View file

@ -10,6 +10,9 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
use WooCommerce\PayPalCommerce\Applepay\Assets\DataToAppleButtonScripts;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
@ -42,13 +45,6 @@ return array(
}
return $settings->get( 'applepay_validated' ) === 'yes';
},
'applepay.payment_method' => static function ( ContainerInterface $container ): ApplepayPaymentMethod {
$settings = $container->get( 'wcgateway.settings' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$order_processor = $container->get( 'wcgateway.order-processor' );
return new ApplepayPaymentMethod( $settings, $logger, $order_processor );
},
'applepay.url' => static function ( ContainerInterface $container ): string {
$path = realpath( __FILE__ );
if ( false === $path ) {
@ -62,29 +58,18 @@ return array(
'applepay.sdk_script_url' => static function ( ContainerInterface $container ): string {
return 'https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js';
},
'applepay.script_url' => static function ( ContainerInterface $container ): string {
return trailingslashit( $container->get( 'applepay.url' ) ) . '/assets/js/applePayDirect.js';
},
'applepay.style_url' => static function ( ContainerInterface $container ): string {
return trailingslashit( $container->get( 'applepay.url' ) ) . '/assets/css/applepaydirect.css';
},
'applepay.setting_button_enabled_product' => static function ( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
return $settings->has( 'applepay_button_enabled_product' ) ?
(bool) $settings->get( 'applepay_button_enabled_product' ) :
false;
},
'applepay.setting_button_enabled_cart' => static function ( ContainerInterface $container ): bool {
$settings = $container->get( 'wcgateway.settings' );
assert( $settings instanceof ContainerInterface );
return $settings->has( 'applepay_button_enabled_cart' ) ?
(bool) $settings->get( 'applepay_button_enabled_cart' ) :
false;
},
'applepay.data_to_scripts' => static function ( ContainerInterface $container ): DataToAppleButtonScripts {
return new DataToAppleButtonScripts();
$sdk_url = $container->get( 'applepay.sdk_script_url' );
return new DataToAppleButtonScripts($sdk_url);
},
'applepay.button' => static function ( ContainerInterface $container ): ApplePayButton {
$settings = $container->get( 'wcgateway.settings' );
$logger = $container->get( 'woocommerce.logger.woocommerce' );
$order_processor = $container->get( 'wcgateway.order-processor' );
$version = $container->get('ppcp.asset-version');
$module_url = $container->get( 'applepay.url' );
$data = $container->get('applepay.data_to_scripts');
return new ApplePayButton( $settings, $logger, $order_processor, $module_url, $version, $data, $settings );
},
);

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApplePay\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
@ -86,6 +87,13 @@ class ApplepayModule implements ModuleInterface {
$this->render_buttons( $c );
$apple_payment_method->bootstrap_ajax_request();
add_filter(
'woocommerce_paypal_payments_sdk_components_hook',
function( $components ) {
$components[] = 'applepay';
return $components;
}
);
add_action(
'woocommerce_paypal_payments_gateway_migrate_on_update',
static function() use ( $c ) {
@ -152,52 +160,16 @@ class ApplepayModule implements ModuleInterface {
*/
public function load_assets( ContainerInterface $c ): void {
add_action(
'wp',
'wp_enqueue_scripts',
function () use ( $c ) {
wp_register_script(
'wc-ppcp-applepay-sdk',
$c->get( 'applepay.sdk_script_url' ),
array(),
$c->get( 'ppcp.asset-version' ),
true
);
wp_enqueue_script( 'wc-ppcp-applepay-sdk' );
wp_register_script(
'wc-ppcp-applepay',
$c->get( 'applepay.script_url' ),
array( 'wc-ppcp-applepay-sdk' ),
$c->get( 'ppcp.asset-version' ),
true
);
wp_register_style(
'wc-ppcp-applepay',
$c->get( 'applepay.style_url' ),
array(),
$c->get( 'ppcp.asset-version' )
);
wp_enqueue_style( 'wc-ppcp-applepay' );
wp_enqueue_script( 'wc-ppcp-applepay' );
$data = $c->get( 'applepay.data_to_scripts' )->apple_pay_script_data();
wp_localize_script(
'wc-ppcp-applepay',
'wc_ppcp_applepay',
$data
);
}
);
}
$button = $c->get( 'applepay.button' );
assert( $button instanceof ButtonInterface );
/**
* ApplePay button markup
*/
protected function apple_pay_direct_button(): void {
?>
<div class="ppc-button-wrapper">
<div id="applepay-container">
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
if ( $button->should_load_script() ) {
$button->enqueue();
}
}
);
}
/**
@ -207,34 +179,21 @@ class ApplepayModule implements ModuleInterface {
* @return void
*/
public function render_buttons( ContainerInterface $c ): void {
$button_enabled_product = $c->get( 'applepay.setting_button_enabled_product' );
$button_enabled_cart = $c->get( 'applepay.setting_button_enabled_cart' );
add_filter(
'woocommerce_paypal_payments_sdk_components_hook',
function( $components ) {
$components[] = 'applepay';
return $components;
}
);
if ( $button_enabled_product ) {
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_applepay_render_hook_product', 'woocommerce_after_add_to_cart_form' );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : 'woocommerce_after_add_to_cart_form';
add_action(
$render_placeholder,
function () {
$this->apple_pay_direct_button();
'wp',
static function () use ( $c ) {
if ( is_admin() ) {
return;
}
);
}
if ( $button_enabled_cart ) {
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_applepay_render_hook_cart', 'woocommerce_cart_totals_after_order_total' );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : 'woocommerce_cart_totals_after_order_total';
add_action(
$render_placeholder,
function () {
$this->apple_pay_direct_button();
$button = $c->get( 'applepay.button' );
/**
* The Button.
*
* @var ButtonInterface $button
*/
$button->render_buttons();
}
);
}
}
}

View file

@ -7,7 +7,7 @@
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use Psr\Log\LoggerInterface;
use WC_Cart;
@ -27,14 +27,14 @@ use WooCommerce\PayPalCommerce\Webhooks\Handler\RequestHandlerTrait;
/**
* Class PayPalPaymentMethod
*/
class ApplepayPaymentMethod {
class ApplePayButton implements ButtonInterface {
use RequestHandlerTrait;
/**
* The settings.
*
* @var Settings
*/
private $plugin_settings;
private $settings;
/**
* The logger.
*
@ -73,25 +73,40 @@ class ApplepayPaymentMethod {
* @var bool Whether to reload the cart after the order is processed.
*/
protected $reload_cart = false;
private $version;
/**
* @var string
*/
private $module_url;
/**
* @var string
*/
private $script_data;
/**
* PayPalPaymentMethod constructor.
*
* @param Settings $plugin_settings The settings.
* @param Settings $settings The settings.
* @param LoggerInterface $logger The logger.
* @param OrderProcessor $order_processor The Order processor.
*/
public function __construct(
Settings $plugin_settings,
Settings $settings,
LoggerInterface $logger,
OrderProcessor $order_processor
OrderProcessor $order_processor,
string $module_url,
string $version,
DataToAppleButtonScripts $data
) {
$this->plugin_settings = $plugin_settings;
$this->settings = $settings;
$this->response_templates = new ResponsesToApple();
$this->logger = $logger;
$this->id = 'applepay';
$this->method_title = __( 'Apple Pay', 'woocommerce-paypal-payments' );
$this->order_processor = $order_processor;
$this->module_url = $module_url;
$this->version = $version;
$this->script_data = $data;
}
/**
@ -103,7 +118,7 @@ class ApplepayPaymentMethod {
'ppcp_partner_referrals_data',
function ( array $data ): array {
try {
$onboard_with_apple = $this->plugin_settings->get( 'ppcp-onboarding-apple' );
$onboard_with_apple = $this->settings->get( 'ppcp-onboarding-apple' );
if ( $onboard_with_apple !== '1' ) {
return $data;
}
@ -150,7 +165,7 @@ class ApplepayPaymentMethod {
public function add_apple_onboarding_option( $options ): string {
$checked = '';
try {
$onboard_with_apple = $this->plugin_settings->get( 'ppcp-onboarding-apple' );
$onboard_with_apple = $this->settings->get( 'ppcp-onboarding-apple' );
if ( $onboard_with_apple === '1' ) {
$checked = 'checked';
}
@ -790,4 +805,80 @@ class ApplepayPaymentMethod {
}
);
}
public function render_buttons(): bool
{
$button_enabled_product = $this->settings->has( 'applepay_button_enabled_product' ) ? $this->settings->get( 'applepay_button_enabled_product' ) : false;
$button_enabled_cart = $this->settings->has( 'applepay_button_enabled_cart' ) ? $this->settings->get( 'applepay_button_enabled_cart' ) : false;
if ( $button_enabled_product ) {
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_applepay_render_hook_product', 'woocommerce_after_add_to_cart_form' );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : 'woocommerce_after_add_to_cart_form';
add_action(
$render_placeholder,
function () {
$this->apple_pay_direct_button();
}
);
}
if ( $button_enabled_cart ) {
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_applepay_render_hook_cart', 'woocommerce_cart_totals_after_order_total' );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : 'woocommerce_cart_totals_after_order_total';
add_action(
$render_placeholder,
function () {
$this->apple_pay_direct_button();
}
);
}
return true;
}
/**
* ApplePay button markup
*/
protected function apple_pay_direct_button(): void {
?>
<div class="ppc-button-wrapper">
<div id="applepay-container">
<?php wp_nonce_field( 'woocommerce-process_checkout', 'woocommerce-process-checkout-nonce' ); ?>
</div>
</div>
<?php
}
public function should_load_script(): bool
{
return true;
}
public function enqueue(): void
{
wp_register_script(
'wc-ppcp-applepay',
untrailingslashit( $this->module_url ) . '/assets/js/applePayDirect.js',
array(),
$this->version,
true
);
wp_enqueue_script( 'wc-ppcp-applepay' );
wp_register_style(
'wc-ppcp-applepay',
untrailingslashit( $this->module_url ) . '/assets/css/styles.css',
array(),
$this->version
);
wp_enqueue_style( 'wc-ppcp-applepay' );
wp_localize_script(
'wc-ppcp-applepay',
'wc_ppcp_applepay',
$this->script_data()
);
}
public function script_data(): array
{
return $this->script_data->apple_pay_script_data();
}
}

View file

@ -7,7 +7,7 @@
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use Psr\Log\LoggerInterface as Logger;

View file

@ -7,7 +7,7 @@
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\Applepay;
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;

View file

@ -0,0 +1,40 @@
<?php
/**
* The interface for the smart button asset renderer.
*
* @package WooCommerce\PayPalCommerce\Button\Assets
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApplePay\Assets;
/**
* Interface SmartButtonInterface
*/
interface ButtonInterface {
/**
* Renders the necessary HTML.
*
* @return bool
*/
public function render_buttons(): bool;
/**
* Whether any of the scripts should be loaded.
*/
public function should_load_script(): bool;
/**
* Enqueues scripts/styles.
*/
public function enqueue(): void;
/**
* The configuration for the smart buttons.
*
* @return array
*/
public function script_data(): array;
}

View file

@ -7,13 +7,24 @@
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
/**
* Class DataToAppleButtonScripts
*/
class DataToAppleButtonScripts {
/**
* The URL to the SDK.
*
* @var string
*/
private $sdk_url;
public function __construct($sdk_url) {
$this->sdk_url = $sdk_url;
}
/**
* Sets the appropriate data to send to ApplePay script
* Data differs between product page and cart page
@ -80,7 +91,6 @@ class DataToAppleButtonScripts {
$currency_code,
$total_label
) {
$product = wc_get_product( get_the_id() );
if ( ! $product ) {
return array();
@ -95,6 +105,7 @@ class DataToAppleButtonScripts {
$product_stock = $product->get_stock_status();
return array(
'sdk_url' => $this->sdk_url,
'product' => array(
'needShipping' => $product_need_shipping,
'id' => $product_id,
@ -131,6 +142,7 @@ class DataToAppleButtonScripts {
. $nonce
. '</div>';
return array(
'sdk_url' => $this->sdk_url,
'product' => array(
'needShipping' => $cart->needs_shipping(),
'subtotal' => $cart->get_subtotal(),

View file

@ -7,7 +7,7 @@
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
/**
* Class PropertiesDictionary

View file

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
namespace WooCommerce\PayPalCommerce\Applepay\Assets;
use Psr\Log\LoggerInterface;
use WC_Payment_Gateway;

View file

@ -10,7 +10,7 @@ module.exports = {
plugins: [ new DependencyExtractionWebpackPlugin() ],
entry: {
'applepayDirect': path.resolve('./resources/js/applepayDirect.js'),
'applepayDirectCart': path.resolve('./resources/js/applepayDirectCart.js')
"styles": path.resolve('./resources/css/styles.scss')
},
output: {
path: path.resolve(__dirname, 'assets/'),