ists( $payment_method, self::get_direct_debit_methods() ); } /** * Get recurring methods. * * @since 1.3.14 * @return array */ public static function get_recurring_methods() { // Get the direct debit methods. $payment_methods = self::get_direct_debit_methods(); // Add additional methods suitable for recurring payments. $payment_methods[ self::APPLE_PAY ] = self::APPLE_PAY; $payment_methods[ self::CREDIT_CARD ] = self::CREDIT_CARD; return $payment_methods; } /** * Is recurring method. * * @since 1.3.14 * * @param string $payment_method The payment method to check for recurring. * * @return bool True if the specified payment method supports recurring, false otherwise. */ public static function is_recurring_method( $payment_method ) { return array_key_exists( $payment_method, self::get_recurring_methods() ); } /** * Get first method for payment method. * * @param string|null $payment_method The payment method to get the first payment method for. * * @return string|null */ public static function get_first_payment_method( $payment_method ) { if ( empty( $payment_method ) ) { return null; } if ( self::is_direct_debit_method( $payment_method ) ) { $direct_debit_methods = self::get_direct_debit_methods(); return $direct_debit_methods[ $payment_method ]; } return $payment_method; } /** * Maybe update active payment methods. * * @return void */ public static function maybe_update_active_payment_methods() { $payment_methods = get_option( 'pronamic_pay_active_payment_methods' ); // Update active payment methods option if necessary. if ( ! is_array( $payment_methods ) ) { self::update_active_payment_methods(); } } /** * Update active payment methods option. * * @since 2.0.0 * @return void */ public static function update_active_payment_methods() { $active_payment_methods = array(); $query = new WP_Query( array( 'post_type' => 'pronamic_gateway', 'nopaging' => true, 'fields' => 'ids', ) ); foreach ( $query->posts as $config_id ) { $gateway = Plugin::get_gateway( $config_id ); if ( ! $gateway ) { continue; } if ( ! method_exists( $gateway, 'get_supported_payment_methods' ) ) { continue; } try { $payment_methods = $gateway->get_transient_available_payment_methods(); } catch ( \Exception $e ) { // Do not update active payment methods on error. return; } if ( null === $payment_methods ) { $payment_methods = $gateway->get_supported_payment_methods(); } foreach ( $payment_methods as $payment_method ) { if ( ! isset( $active_payment_methods[ $payment_method ] ) ) { $active_payment_methods[ $payment_method ] = array(); } // Check if payment method is supported. if ( ! \in_array( $payment_method, $gateway->get_supported_payment_methods(), true ) ) { continue; } $active_payment_methods[ $payment_method ][] = $config_id; } } update_option( 'pronamic_pay_active_payment_methods', $active_payment_methods ); } /** * Get active payment methods. * * @return array */ public static function get_active_payment_methods() { self::maybe_update_active_payment_methods(); $payment_methods = array(); $active_methods = get_option( 'pronamic_pay_active_payment_methods' ); if ( is_array( $active_methods ) ) { $payment_methods = array_keys( $active_methods ); } return $payment_methods; } /** * Get config IDs for payment method. * * @param string $payment_method Payment method. * * @return array */ public static function get_config_ids( $payment_method = null ) { self::maybe_update_active_payment_methods(); $config_ids = array(); $active_methods = get_option( 'pronamic_pay_active_payment_methods' ); // Make sure active payments methods is an array. if ( ! is_array( $active_methods ) ) { return $config_ids; } // Get config IDs for payment method. if ( isset( $active_methods[ $payment_method ] ) ) { $config_ids = $active_methods[ $payment_method ]; } // Get all config IDs if payment method is empty. if ( empty( $payment_method ) ) { foreach ( $active_methods as $method_config_ids ) { $config_ids = array_merge( $config_ids, $method_config_ids ); } $config_ids = array_unique( $config_ids ); } // Make sure payment method is also supported. if ( null !== $payment_method ) { foreach ( $config_ids as $key => $config_id ) { $gateway = Plugin::get_gateway( $config_id ); if ( null === $gateway || ! \in_array( $payment_method, $gateway->get_supported_payment_methods(), true ) ) { unset( $config_ids[ $key ] ); } } } return $config_ids; } /** * Check if payment method is active. * * @param string $payment_method Payment method. * * @since 2.0.0 * * @return bool */ public static function is_active( $payment_method = null ) { return in_array( $payment_method, self::get_active_payment_methods(), true ); } }