woocommerce-paypal-payments/modules/ppcp-applepay/src/ApplepayModule.php

413 lines
29 KiB
PHP
Raw Normal View History

<?php
/**
* The Applepay module.
*
* @package WooCommerce\PayPalCommerce\Applepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
2024-07-22 18:29:39 +02:00
use WC_Payment_Gateway;
2023-09-07 09:56:46 +02:00
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
2023-09-08 16:58:32 +02:00
use WooCommerce\PayPalCommerce\Applepay\Assets\ApplePayButton;
2023-08-31 12:48:01 +02:00
use WooCommerce\PayPalCommerce\Applepay\Assets\AppleProductStatus;
2023-10-26 16:33:00 +01:00
use WooCommerce\PayPalCommerce\Applepay\Assets\PropertiesDictionary;
2023-08-31 12:48:01 +02:00
use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
2023-09-10 11:41:04 +02:00
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Applepay\Helper\AvailabilityNotice;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
2024-08-23 15:52:56 +02:00
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
2023-09-08 16:58:32 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class ApplepayModule
*/
class ApplepayModule implements ServiceModule, ExtendingModule, ExecutableModule {
use ModuleClassNameIdTrait;
/**
* {@inheritDoc}
*/
public function services(): array {
return require __DIR__ . '/../services.php';
}
/**
* {@inheritDoc}
*/
public function extensions(): array {
return require __DIR__ . '/../extensions.php';
}
/**
* {@inheritDoc}
*/
public function run( ContainerInterface $c ): bool {
2023-10-26 10:58:05 +01:00
$module = $this;
// 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( 'applepay.apple-product-status' );
assert( $apm_status instanceof AppleProductStatus );
$apm_status->clear( $settings );
}
);
2023-10-26 10:58:05 +01:00
add_action(
'init',
static function () use ( $c, $module ) {
2023-10-26 10:58:05 +01:00
// Check if the module is applicable, correct country, currency, ... etc.
if ( ! $c->get( 'applepay.eligible' ) ) {
return;
}
2023-10-26 10:58:05 +01:00
// Load the button handler.
$apple_payment_method = $c->get( 'applepay.button' );
// add onboarding and referrals hooks.
assert( $apple_payment_method instanceof ApplepayButton );
$apple_payment_method->initialize();
2023-10-26 10:58:05 +01:00
// Show notice if there are product availability issues.
$availability_notice = $c->get( 'applepay.availability_notice' );
assert( $availability_notice instanceof AvailabilityNotice );
$availability_notice->execute();
2023-10-26 10:58:05 +01:00
// Return if server not supported.
if ( ! $c->get( 'applepay.server_supported' ) ) {
return;
}
// Check if this merchant can activate / use the buttons.
// We allow non referral merchants as they can potentially still use ApplePay, we just have no way of checking the capability.
if ( ( ! $c->get( 'applepay.available' ) ) && $c->get( 'applepay.is_referral' ) ) {
return;
}
if ( $apple_payment_method->is_enabled() ) {
$module->load_assets( $c, $apple_payment_method );
$module->handle_validation_file( $c, $apple_payment_method );
$module->render_buttons( $c, $apple_payment_method );
$apple_payment_method->bootstrap_ajax_request();
}
2023-11-06 09:41:19 +01:00
$module->load_admin_assets( $c, $apple_payment_method );
2024-04-23 15:00:05 +02:00
$module->load_block_editor_assets( $c, $apple_payment_method );
2023-11-06 09:41:19 +01:00
},
1
2023-10-26 10:58:05 +01:00
);
2023-10-26 16:33:00 +01:00
add_filter(
'nonce_user_logged_out',
/**
* Prevents nonce from being changed for non logged in users.
*
* @param int $uid The uid.
* @param string|int $action The action.
* @return int
*
* @psalm-suppress MissingClosureParamType
*/
function ( $uid, $action ) {
if ( $action === PropertiesDictionary::NONCE_ACTION ) {
return 0;
}
return $uid;
},
100,
2
);
2024-07-22 18:29:39 +02:00
add_filter(
'woocommerce_payment_gateways',
/**
* Param types removed to avoid third-party issues.
*
* @psalm-suppress MissingClosureParamType
*/
static function ( $methods ) use ( $c ): array {
if ( ! is_array( $methods ) ) {
return $methods;
}
$settings = $c->get( 'wcgateway.settings' );
assert( $settings instanceof Settings );
if ( $settings->has( 'applepay_button_enabled' ) && $settings->get( 'applepay_button_enabled' ) ) {
$applepay_gateway = $c->get( 'applepay.wc-gateway' );
assert( $applepay_gateway instanceof WC_Payment_Gateway );
$methods[] = $applepay_gateway;
}
return $methods;
}
);
add_action(
'woocommerce_review_order_after_submit',
function () {
// Wrapper ID: #ppc-button-ppcp-applepay.
echo '<div id="ppc-button-' . esc_attr( ApplePayGateway::ID ) . '"></div>';
}
);
add_action(
'woocommerce_pay_order_after_submit',
function () {
// Wrapper ID: #ppc-button-ppcp-applepay.
echo '<div id="ppc-button-' . esc_attr( ApplePayGateway::ID ) . '"></div>';
}
);
add_filter(
'woocommerce_paypal_payments_selected_button_locations',
2024-11-04 16:34:29 +01:00
function( array $locations, string $setting_name ): array {
$gateway = WC()->payment_gateways()->payment_gateways()[ ApplePayGateway::ID ] ?? '';
if ( $gateway && $gateway->enabled === 'yes' && $setting_name === 'smart_button_locations' ) {
$locations[] = 'checkout';
}
return $locations;
},
10,
2
);
add_filter(
'woocommerce_paypal_payments_rest_common_merchant_data',
function( array $features ) use ( $c ): array {
$product_status = $c->get( 'applepay.apple-product-status' );
assert( $product_status instanceof AppleProductStatus );
$apple_pay_enabled = $product_status->is_active();
$features['apple_pay'] = array(
'enabled' => $apple_pay_enabled,
);
return $features;
}
);
return true;
}
/**
2023-09-05 09:14:20 +02:00
* Loads the validation string.
*
2023-09-19 11:17:48 +02:00
* @param boolean $is_sandbox The environment for this merchant.
*/
2023-09-19 11:17:48 +02:00
protected function load_domain_association_file( bool $is_sandbox ): void {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return;
}
$request_uri = (string) filter_var( wp_unslash( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL );
if ( strpos( $request_uri, '.well-known/apple-developer-merchantid-domain-association' ) !== false ) {
2023-09-19 11:17:48 +02:00
$validation_string = $this->validation_string( $is_sandbox );
nocache_headers();
header( 'Content-Type: text/plain', true, 200 );
2023-09-05 09:14:20 +02:00
echo $validation_string;// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit;
}
}
/**
* Registers and enqueues the assets.
*
* @param ContainerInterface $c The container.
* @param ApplePayButton $button The button.
* @return void
*/
public function load_assets( ContainerInterface $c, ApplePayButton $button ): void {
if ( ! $button->is_enabled() ) {
return;
}
add_action(
2023-08-24 18:05:05 +02:00
'wp_enqueue_scripts',
function () use ( $c, $button ) {
2023-09-10 11:41:04 +02:00
$smart_button = $c->get( 'button.smart-button' );
assert( $smart_button instanceof SmartButtonInterface );
if ( $smart_button->should_load_ppcp_script() ) {
2023-08-24 18:05:05 +02:00
$button->enqueue();
return;
2023-08-24 18:05:05 +02:00
}
if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) ) {
/**
* Should add this to the ButtonInterface.
*
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_styles();
}
}
);
2024-04-23 15:00:05 +02:00
add_action(
'enqueue_block_editor_assets',
function () use ( $c, $button ) {
$button->enqueue_admin_styles();
}
);
2023-09-10 11:41:04 +02:00
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( PaymentMethodRegistry $payment_method_registry ) use ( $c ): void {
$payment_method_registry->register( $c->get( 'applepay.blocks-payment-method' ) );
}
);
}
/**
* Registers and enqueues the assets.
*
* @param ContainerInterface $c The container.
* @param ApplePayButton $button The button.
* @return void
*/
public function load_admin_assets( ContainerInterface $c, ApplePayButton $button ): void {
// Enqueue backend scripts.
add_action(
'admin_enqueue_scripts',
static function () use ( $c, $button ) {
if ( ! is_admin() || ! $c->get( 'wcgateway.is-ppcp-settings-payment-methods-page' ) ) {
return;
}
/**
* Should add this to the ButtonInterface.
*
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_admin();
2024-04-23 15:00:05 +02:00
$button->enqueue_admin_styles();
}
);
// Adds ApplePay component to the backend button preview settings.
add_action(
'woocommerce_paypal_payments_admin_gateway_settings',
function( array $settings ) use ( $c ): array {
if ( is_array( $settings['components'] ) ) {
$settings['components'][] = 'applepay';
}
return $settings;
}
);
}
2024-04-23 15:18:04 +02:00
/**
* Enqueues the editor assets.
*
* @param ContainerInterface $c The container.
* @param ApplePayButton $button The button.
* @return void
*/
2024-04-23 15:00:05 +02:00
public function load_block_editor_assets( ContainerInterface $c, ApplePayButton $button ): void {
// Enqueue backend scripts.
add_action(
'enqueue_block_editor_assets',
static function () use ( $c, $button ) {
/**
* Should add this to the ButtonInterface.
*
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_admin_styles();
}
);
}
/**
2023-07-19 09:45:03 +02:00
* Renders the Apple Pay buttons in the enabled places.
*
* @param ContainerInterface $c The container.
* @param ApplePayButton $button The button.
* @return void
*/
public function render_buttons( ContainerInterface $c, ApplePayButton $button ): void {
if ( ! $button->is_enabled() ) {
return;
}
2023-08-24 18:05:05 +02:00
add_action(
'wp',
static function () use ( $c ) {
if ( is_admin() ) {
return;
}
$button = $c->get( 'applepay.button' );
/**
* The Button.
*
* @var ButtonInterface $button
*/
2023-09-05 09:14:20 +02:00
$button->render();
}
);
}
2023-08-31 12:48:01 +02:00
/**
2023-09-08 16:58:32 +02:00
* Handles the validation file.
*
* @param ContainerInterface $c The container.
* @param ApplePayButton $button The button.
2023-08-31 12:48:01 +02:00
* @return void
*/
public function handle_validation_file( ContainerInterface $c, ApplePayButton $button ): void {
if ( ! $button->is_enabled() ) {
return;
}
2023-09-08 16:58:32 +02:00
$env = $c->get( 'onboarding.environment' );
assert( $env instanceof Environment );
$is_sandobx = $env->current_environment_is( Environment::SANDBOX );
2023-09-19 11:17:48 +02:00
$this->load_domain_association_file( $is_sandobx );
}
/**
* Returns the validation string, depending on the environment.
*
* @param bool $is_sandbox The environment for this merchant.
* @return string
*/
2024-07-22 18:29:39 +02:00
public function validation_string( bool $is_sandbox ) : string {
2023-09-19 11:17:48 +02:00
$sandbox_string = $this->sandbox_validation_string();
$live_string = $this->live_validation_string();
return $is_sandbox ? $sandbox_string : $live_string;
}
/**
* Returns the sandbox validation string.
*
* @return string
*/
private function sandbox_validation_string(): string {
2023-10-19 16:21:14 +01:00
return apply_filters(
'woocommerce_paypal_payments_applepay_sandbox_validation_string',
2024-05-22 11:05:38 +02:00
'7B227073704964223A2241443631324543383841333039314132314539434132433035304439454130353741414535444341304542413237424243333838463239344231353534434233222C2276657273696F6E223A312C22637265617465644F6E223A313731353237313630333931352C227369676E6174757265223A22333038303036303932613836343838366637306430313037303261303830333038303032303130313331306433303062303630393630383634383031363530333034303230313330383030363039326138363438383666373064303130373031303030306130383033303832303365333330383230333838613030333032303130323032303831363633346338623065333035373137333030613036303832613836343863653364303430333032333037613331326533303263303630333535303430333063323534313730373036633635323034313730373036633639363336313734363936663665323034393665373436353637373236313734363936663665323034333431323032643230343733333331323633303234303630333535303430623063316434313730373036633635323034333635373237343639363636393633363137343639366636653230343137353734363836663732363937343739333131333330313130363033353530343061306330613431373037303663363532303439366536333265333130623330303930363033353530343036313330323535353333303165313730643332333433303334333233393331333733343337333233373561313730643332333933303334333233383331333733343337333233363561333035663331323533303233303630333535303430333063316336353633363332643733366437303264363237323666366236353732326437333639363736653566353534333334326435303532346634343331313433303132303630333535303430623063306236393466353332303533373937333734363536643733333131333330313130363033353530343061306330613431373037303663363532303439366536333265333130623330303930363033353530343036313330323535353333303539333031333036303732613836343863653364303230313036303832613836343863653364303330313037303334323030303463323135373765646562643663376232323138663638646437303930613132313864633762306264366632633238336438343630393564393461663461353431316238333432306564383131663334303765383333333166316335346333663765623332323064366261643564346566663439323839383933653763306631336133383230323131333038323032306433303063303630333535316431333031303166663034303233303030333031663036303335353164323330343138333031363830313432336632343963343466393365346566323765366334663632383663336661326262666432653462333034353036303832623036303130353035303730313031303433393330333733303335303630383262303630313035303530373330303138363239363837343734373033613266326636663633373337303265363137303730366336353265363336663664326636663633373337303330333432643631373037303663363536313639363336313333333033323330383230313164303630333535316432303034383230313134333038323031313033303832303130633036303932613836343838366637363336343035303133303831666533303831633330363038326230363031303530353037303230323330383162363063383162333532363536633639363136653633363532303666366532303734363836393733323036333635373237343639363636393633363137343635323036323739323036313665373932303730363137323734373932303631373337333735366436353733323036313633363336353730373436313665363336353230366636363230373436383635323037343638363536653230363137303730366336393633363136323663363532303733373436313665363436313732363432303734363537323664373332303631366536343230363336663665363436393734363936663665373332303666363632303735373336353263323036333635373237343639363636393633363137343635323037303666366336393633373932303631366536343230363336353732373436393636363936333631373436393666366532303730373236313633373436393633363532303733373436313734363536643635366537343733326533303336303630383262303630313035303530373032303131363261363837343734373033613266326637373737373732653631373037303663363532653633366636643266363336353732373436393636363936333631373436353631373537343638366637323639373437393266333033343036303335353164316630343264333032623330323961303237613032353836323336383734373437303361326632663633373236633265363137303730366336353265363336663664326636313730373036633635363136393633363133333265363337323663333031643036303335353164306530343136303431343934353764623666643537343831383638393839373632663765353738353037653739623538323433303065303630333535316430663031303166663034303430333032303738303330306630363039326138363438383666373633363430363
2023-10-19 16:21:14 +01:00
);
2023-09-19 11:17:48 +02:00
}
/**
* Returns the live validation string.
*
* @return string
*/
private function live_validation_string(): string {
2023-10-19 16:21:14 +01:00
return apply_filters(
'woocommerce_paypal_payments_applepay_live_validation_string',
2024-05-22 11:05:38 +02:00
'7B227073704964223A2246354246304143324336314131413238313043373531453439333444414433384346393037313041303935303844314133453241383436314141363232414145222C2276657273696F6E223A312C22637265617465644F6E223A313731353230343037313232362C227369676E6174757265223A22333038303036303932613836343838366637306430313037303261303830333038303032303130313331306433303062303630393630383634383031363530333034303230313330383030363039326138363438383666373064303130373031303030306130383033303832303365333330383230333838613030333032303130323032303831363633346338623065333035373137333030613036303832613836343863653364303430333032333037613331326533303263303630333535303430333063323534313730373036633635323034313730373036633639363336313734363936663665323034393665373436353637373236313734363936663665323034333431323032643230343733333331323633303234303630333535303430623063316434313730373036633635323034333635373237343639363636393633363137343639366636653230343137353734363836663732363937343739333131333330313130363033353530343061306330613431373037303663363532303439366536333265333130623330303930363033353530343036313330323535353333303165313730643332333433303334333233393331333733343337333233373561313730643332333933303334333233383331333733343337333233363561333035663331323533303233303630333535303430333063316336353633363332643733366437303264363237323666366236353732326437333639363736653566353534333334326435303532346634343331313433303132303630333535303430623063306236393466353332303533373937333734363536643733333131333330313130363033353530343061306330613431373037303663363532303439366536333265333130623330303930363033353530343036313330323535353333303539333031333036303732613836343863653364303230313036303832613836343863653364303330313037303334323030303463323135373765646562643663376232323138663638646437303930613132313864633762306264366632633238336438343630393564393461663461353431316238333432306564383131663334303765383333333166316335346333663765623332323064366261643564346566663439323839383933653763306631336133383230323131333038323032306433303063303630333535316431333031303166663034303233303030333031663036303335353164323330343138333031363830313432336632343963343466393365346566323765366334663632383663336661326262666432653462333034353036303832623036303130353035303730313031303433393330333733303335303630383262303630313035303530373330303138363239363837343734373033613266326636663633373337303265363137303730366336353265363336663664326636663633373337303330333432643631373037303663363536313639363336313333333033323330383230313164303630333535316432303034383230313134333038323031313033303832303130633036303932613836343838366637363336343035303133303831666533303831633330363038326230363031303530353037303230323330383162363063383162333532363536633639363136653633363532303666366532303734363836393733323036333635373237343639363636393633363137343635323036323739323036313665373932303730363137323734373932303631373337333735366436353733323036313633363336353730373436313665363336353230366636363230373436383635323037343638363536653230363137303730366336393633363136323663363532303733373436313665363436313732363432303734363537323664373332303631366536343230363336663665363436393734363936663665373332303666363632303735373336353263323036333635373237343639363636393633363137343635323037303666366336393633373932303631366536343230363336353732373436393636363936333631373436393666366532303730373236313633373436393633363532303733373436313734363536643635366537343733326533303336303630383262303630313035303530373032303131363261363837343734373033613266326637373737373732653631373037303663363532653633366636643266363336353732373436393636363936333631373436353631373537343638366637323639373437393266333033343036303335353164316630343264333032623330323961303237613032353836323336383734373437303361326632663633373236633265363137303730366336353265363336663664326636313730373036633635363136393633363133333265363337323663333031643036303335353164306530343136303431343934353764623666643537343831383638393839373632663765353738353037653739623538323433303065303630333535316430663031303166663034303430333032303738303330306630363039326138363438383666373633363430363
2023-10-19 16:21:14 +01:00
);
2023-08-31 12:48:01 +02:00
}
}