| 1 |
<?php |
| 2 |
|
| 3 |
namespace Monei\Services; |
| 4 |
|
| 5 |
use Monei\Services\payment\MoneiPaymentServices; |
| 6 |
use Monei\ApiException; |
| 7 |
use WC_Admin_Settings; |
| 8 |
use WC_Monei_Logger; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class to verify Apple Pay Registration. |
| 12 |
* Expose apple-developer-merchantid-domain-association at https://example.com/.well-known/apple-developer-merchantid-domain-association. |
| 13 |
* |
| 14 |
* @since 5.5 |
| 15 |
*/ |
| 16 |
class MoneiApplePayVerificationService { |
| 17 |
|
| 18 |
const DOMAIN_ASSOCIATION_FILE_NAME = 'apple-developer-merchantid-domain-association'; |
| 19 |
const DOMAIN_ASSOCIATION_DIR = '.well-known'; |
| 20 |
|
| 21 |
private MoneiPaymentServices $moneiPaymentServices; |
| 22 |
|
| 23 |
public function __construct( MoneiPaymentServices $moneiPaymentServices ) { |
| 24 |
$this->moneiPaymentServices = $moneiPaymentServices; |
| 25 |
add_action( 'parse_request', array( $this, 'expose_on_domain_association_request' ), 1 ); |
| 26 |
add_action( 'woocommerce_update_options_payment_gateways_monei_apple_google', array( $this, 'apple_domain_register' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Apple API Domain registration. |
| 31 |
* Automatically registers domain with Apple Pay when gateway is enabled. |
| 32 |
*/ |
| 33 |
public function apple_domain_register() { |
| 34 |
if ( ! check_admin_referer( 'woocommerce-settings' ) ) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
// Check if Apple/Google Pay is enabled |
| 39 |
if ( ! isset( $_POST['woocommerce_monei_apple_google_enabled'] ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
// phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 43 |
$enabled_value = wc_clean( wp_unslash( $_POST['woocommerce_monei_apple_google_enabled'] ) ); |
| 44 |
|
| 45 |
if ( 'yes' !== $enabled_value && '1' !== $enabled_value ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
try { |
| 50 |
// Extract domain from site URL (WordPress best practice) |
| 51 |
$parsed_url = wp_parse_url( home_url() ); |
| 52 |
$domain = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; |
| 53 |
|
| 54 |
// Ensure domain is not empty before registering |
| 55 |
if ( empty( $domain ) ) { |
| 56 |
WC_Monei_Logger::logError( 'Apple Pay domain registration failed: empty domain' ); |
| 57 |
WC_Admin_Settings::add_error( __( 'Apple Pay domain registration failed: unable to determine domain.', 'monei' ) ); |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$this->moneiPaymentServices->register_apple_domain( $domain ); |
| 62 |
|
| 63 |
WC_Monei_Logger::logDebug( 'Apple Pay domain registered successfully: ' . $domain ); |
| 64 |
WC_Admin_Settings::add_message( __( 'Apple Pay domain registered successfully.', 'monei' ) ); |
| 65 |
} catch ( ApiException $e ) { |
| 66 |
WC_Monei_Logger::logError( 'Apple Pay domain registration failed for ' . $domain . ': ' . $e->getMessage() ); |
| 67 |
$response_body = json_decode( $e->getResponseBody() ); |
| 68 |
if ( $response_body && isset( $response_body->message ) ) { |
| 69 |
WC_Admin_Settings::add_error( __( 'Apple Pay', 'monei' ) . ': ' . $response_body->message ); |
| 70 |
} else { |
| 71 |
WC_Admin_Settings::add_error( __( 'Apple Pay domain registration failed. Please check the logs.', 'monei' ) ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Expose DOMAIN_ASSOCIATION_FILE_NAME on https://example.com/.well-known/apple-developer-merchantid-domain-association request. |
| 78 |
* |
| 79 |
* @param $wp |
| 80 |
*/ |
| 81 |
public function expose_on_domain_association_request( $wp ) { |
| 82 |
if ( isset( $wp->request ) && ( self::DOMAIN_ASSOCIATION_DIR . '/' . self::DOMAIN_ASSOCIATION_FILE_NAME ) === $wp->request ) { |
| 83 |
$file_path = WC_Monei()->plugin_path() . '/' . self::DOMAIN_ASSOCIATION_FILE_NAME; |
| 84 |
|
| 85 |
if ( file_exists( $file_path ) ) { |
| 86 |
header( 'Content-Type: text/plain' ); |
| 87 |
header( 'Content-Disposition: inline; filename="' . self::DOMAIN_ASSOCIATION_FILE_NAME . '"' ); |
| 88 |
readfile( $file_path ); |
| 89 |
} else { |
| 90 |
status_header( 404 ); |
| 91 |
echo 'Apple Pay domain verification file not found'; |
| 92 |
} |
| 93 |
exit; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|