namespace WPForms\Integrations\Stripe\Api;
use WPForms\Helpers\File;
use WPForms\Vendor\Stripe\PaymentMethodDomain;
use WPForms\Integrations\Stripe\DomainHealthCheck;
use WPForms\Integrations\Stripe\Helpers;
* Domain status option name.
const STATUS_OPTION = 'wpforms_stripe_domain_status';
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';
public function validate() {
if ( $this->is_exists_and_valid() ) {
$this->set_status( self::STATUS_ACTIVE );
( new DomainHealthCheck() )->maybe_schedule_task();
! $this->maybe_create_domain_association_file() ||
$this->set_status( self::STATUS_INACTIVE );
$this->set_status( self::STATUS_ACTIVE );
* @param string $status Status.
private function set_status( $status ) {
update_option( self::STATUS_OPTION, $status );
* Determine whether domain is active.
public function is_domain_active() {
return get_option( self::STATUS_OPTION, self::STATUS_ACTIVE ) === self::STATUS_ACTIVE;
private function register() {
$domain = PaymentMethodDomain::create(
'domain_name' => $this->get_site_domain(),
} catch ( Exception $e ) {
'Stripe: Unable to create a domain.',
'type' => [ 'payment', 'error' ],
return $this->is_apple_pay_valid( $domain );
* Check if domain already exists and valid.
private function is_exists_and_valid() {
$all_domains = PaymentMethodDomain::all(
} catch ( Exception $e ) {
'Stripe: Unable to get list of domains.',
'type' => [ 'payment', 'error' ],
if ( empty( $all_domains ) || ! isset( $all_domains->data ) ) {
$site_domain = $this->get_site_domain();
foreach ( $all_domains->data as $domain ) {
if ( $domain->domain_name !== $site_domain ) {
if ( ! $this->is_apple_pay_valid( $domain ) ) {
* Verify if Apple Pay active and valid.
* @param object $domain Stripe domain object.
private function is_apple_pay_valid( $domain ) {
return isset( $domain->apple_pay ) && $domain->apple_pay->status === 'active';
private function get_site_domain() {
$site_url_parts = wp_parse_url( site_url() );
return $site_url_parts['host'];
* Maybe create domain association file.
private function maybe_create_domain_association_file() {
$wp_filesystem = File::get_filesystem();
if ( is_null( $wp_filesystem ) ) {
$association_dir = $wp_filesystem->abspath() . '.well-known';
$file_name = 'apple-developer-merchantid-domain-association';
$association_file = $association_dir . '/' . $file_name;
// Return early if file already exists.
if ( $wp_filesystem->exists( $association_file ) ) {
if ( ! $wp_filesystem->mkdir( $association_dir, 0755 ) ) {
$this->log_error( 'Stripe: Unable to create domain association folder in site root.' );
if ( ! $wp_filesystem->copy( WPFORMS_PLUGIN_DIR . 'src/Integrations/Stripe/' . $file_name, $association_file, true ) ) {
$this->log_error( 'Stripe: Unable to copy domain association file to domain .well-known directory.' );
* @param string $error Error message.
private function log_error( $error ) {
'type' => [ 'payment', 'error' ],