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

262 lines
25 KiB
PHP
Raw Normal View History

<?php
/**
* The Applepay module.
*
* @package WooCommerce\PayPalCommerce\Applepay
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Applepay;
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;
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-08 16:58:32 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class ApplepayModule
*/
class ApplepayModule 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-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;
}
2023-10-26 10:58:05 +01:00
$module->load_assets( $c, $apple_payment_method );
$module->handle_validation_file( $c );
$module->render_buttons( $c, $apple_payment_method );
2023-10-26 10:58:05 +01:00
$apple_payment_method->bootstrap_ajax_request();
}
);
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
);
}
/**
* Returns the key for the module.
*
* @return string|void
*/
public function getKey() {
}
/**
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();
}
if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) ) {
/**
* Should add this to the ButtonInterface.
*
* @psalm-suppress UndefinedInterfaceMethod
*/
$button->enqueue_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' ) );
}
);
}
/**
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.
2023-08-31 12:48:01 +02:00
* @return void
*/
2023-09-08 16:58:32 +02:00
public function handle_validation_file( ContainerInterface $c ): void {
$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
*/
public function validation_string( bool $is_sandbox ) {
$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',
'7B227073704964223A2241443631324543383841333039314132314539434132433035304439454130353741414535444341304542413237424243333838463239344231353534434233222C2276657273696F6E223A312C22637265617465644F6E223A313632343438393037393630362C227369676E6174757265223A22333038303036303932613836343838366637306430313037303261303830333038303032303130313331306633303064303630393630383634383031363530333034303230313035303033303830303630393261383634383836663730643031303730313030303061303830333038323033653433303832303338626130303330323031303230323038353964386131626361616634653363643330306130363038326138363438636533643034303330323330376133313265333032633036303335353034303330633235343137303730366336353230343137303730366336393633363137343639366636653230343936653734363536373732363137343639366636653230343334313230326432303437333333313236333032343036303335353034306230633164343137303730366336353230343336353732373436393636363936333631373436393666366532303431373537343638366637323639373437393331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533333031653137306433323331333033343332333033313339333333373330333035613137306433323336333033343331333933313339333333363335333935613330363233313238333032363036303335353034303330633166363536333633326437333664373032643632373236663662363537323264373336393637366535663535343333343264353334313465343434323466353833313134333031323036303335353034306230633062363934663533323035333739373337343635366437333331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533333035393330313330363037326138363438636533643032303130363038326138363438636533643033303130373033343230303034383233306664616263333963663735653230326335306439396234353132653633376532613930316464366362336530623163643462353236373938663863663465626465383161323561386332316534633333646463653865326139366332663661666131393330333435633465383761343432366365393531623132393561333832303231313330383230323064333030633036303335353164313330313031666630343032333030303330316630363033353531643233303431383330313638303134323366323439633434663933653465663237653663346636323836633366613262626664326534623330343530363038326230363031303530353037303130313034333933303337333033353036303832623036303130353035303733303031383632393638373437343730336132663266366636333733373032653631373037303663363532653633366636643266366636333733373033303334326436313730373036633635363136393633363133333330333233303832303131643036303335353164323030343832303131343330383230313130333038323031306330363039326138363438383666373633363430353031333038316665333038316333303630383262303630313035303530373032303233303831623630633831623335323635366336393631366536333635323036663665323037343638363937333230363336353732373436393636363936333631373436353230363237393230363136653739323037303631373237343739323036313733373337353664363537333230363136333633363537303734363136653633363532303666363632303734363836353230373436383635366532303631373037303663363936333631363236633635323037333734363136653634363137323634323037343635373236643733323036313665363432303633366636653634363937343639366636653733323036663636323037353733363532633230363336353732373436393636363936333631373436353230373036663663363936333739323036313665363432303633363537323734363936363639363336313734363936663665323037303732363136333734363936333635323037333734363137343635366436353665373437333265333033363036303832623036303130353035303730323031313632613638373437343730336132663266373737373737326536313730373036633635326536333666366432663633363537323734363936363639363336313734363536313735373436383666373236393734373932663330333430363033353531643166303432643330326233303239613032376130323538363233363837343734373033613266326636333732366332653631373037303663363532653633366636643266363137303730366336353631363936333631333332653633373236633330316430363033353531643065303431363034313430323234333030623961656565643436333139376134613635613239396534323731383231633435333030653036303335353164306630313031666630343034303330323037383033303066303630393261383634383
);
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',
'7B227073704964223A2246354246304143324336314131413238313043373531453439333444414433384346393037313041303935303844314133453241383436314141363232414145222C2276657273696F6E223A312C22637265617465644F6E223A313633343737323736393531342C227369676E6174757265223A22333038303036303932613836343838366637306430313037303261303830333038303032303130313331306633303064303630393630383634383031363530333034303230313035303033303830303630393261383634383836663730643031303730313030303061303830333038323033653333303832303338386130303330323031303230323038346333303431343935313964353433363330306130363038326138363438636533643034303330323330376133313265333032633036303335353034303330633235343137303730366336353230343137303730366336393633363137343639366636653230343936653734363536373732363137343639366636653230343334313230326432303437333333313236333032343036303335353034306230633164343137303730366336353230343336353732373436393636363936333631373436393666366532303431373537343638366637323639373437393331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533333031653137306433313339333033353331333833303331333333323335333735613137306433323334333033353331333633303331333333323335333735613330356633313235333032333036303335353034303330633163363536333633326437333664373032643632373236663662363537323264373336393637366535663535343333343264353035323466343433313134333031323036303335353034306230633062363934663533323035333739373337343635366437333331313333303131303630333535303430613063306134313730373036633635323034393665363332653331306233303039303630333535303430363133303235353533333035393330313330363037326138363438636533643032303130363038326138363438636533643033303130373033343230303034633231353737656465626436633762323231386636386464373039306131323138646337623062643666326332383364383436303935643934616634613534313162383334323065643831316633343037653833333331663163353463336637656233323230643662616435643465666634393238393839336537633066313361333832303231313330383230323064333030633036303335353164313330313031666630343032333030303330316630363033353531643233303431383330313638303134323366323439633434663933653465663237653663346636323836633366613262626664326534623330343530363038326230363031303530353037303130313034333933303337333033353036303832623036303130353035303733303031383632393638373437343730336132663266366636333733373032653631373037303663363532653633366636643266366636333733373033303334326436313730373036633635363136393633363133333330333233303832303131643036303335353164323030343832303131343330383230313130333038323031306330363039326138363438383666373633363430353031333038316665333038316333303630383262303630313035303530373032303233303831623630633831623335323635366336393631366536333635323036663665323037343638363937333230363336353732373436393636363936333631373436353230363237393230363136653739323037303631373237343739323036313733373337353664363537333230363136333633363537303734363136653633363532303666363632303734363836353230373436383635366532303631373037303663363936333631363236633635323037333734363136653634363137323634323037343635373236643733323036313665363432303633366636653634363937343639366636653733323036663636323037353733363532633230363336353732373436393636363936333631373436353230373036663663363936333739323036313665363432303633363537323734363936363639363336313734363936663665323037303732363136333734363936333635323037333734363137343635366436353665373437333265333033363036303832623036303130353035303730323031313632613638373437343730336132663266373737373737326536313730373036633635326536333666366432663633363537323734363936363639363336313734363536313735373436383666373236393734373932663330333430363033353531643166303432643330326233303239613032376130323538363233363837343734373033613266326636333732366332653631373037303663363532653633366636643266363137303730366336353631363936333631333332653633373236633330316430363033353531643065303431363034313439343537646236666435373438313836383938393736326637653537383530376537396235383234333030653036303335353164306630313031666630343034303330323037383033303066303630393261383634383836663736333
);
2023-08-31 12:48:01 +02:00
}
}