<?php

/**
 * Plugin Name:          PrecisionPay Payments for WooCommerce
 * Plugin URI:           https://github.com/MakeCents-NYC/woocommerce-gateway-precisionpay
 * Description:          Accept online bank payments in your WooCommerce store with PrecisionPay.
 * Version:              4.0.4
 * Author:               PrecisionPay
 * Author URI:           https://www.myprecisionpay.com
 * Requires Plugins:     woocommerce
 * Requires at least:    6.6
 * Tested up to:         6.8
 * WC requires at least: 9.6
 * WC tested up to:      9.8
 * Requires PHP:         7.2
 * License:              GPL-3.0
 * License URI:          https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain:          precisionpay-payments-for-woocommerce
 * Domain Path:          /languages
 */

// Exit if accessed directly.
if (! defined('ABSPATH')) {
  exit;
}

// Make sure WooCommerce is active
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
  return;
}

/**
 * Constants available to the plugin
 */
define('PRCSNPY_PLUGIN_NAME', 'PrecisionPay Payments for WooCommerce');
define('PRCSNPY_VERSION', '4.0.4');

/**
 * WC PrecisionPay Payment gateway plugin class.
 *
 * @class WC_PrecisionPay_Payments
 */
class WC_PrecisionPay_Payments {
  /**
   * Plugin bootstrapping.
   */
  public static function init() {

    // PrecisionPay Payments gateway class.
    add_action('plugins_loaded', [__CLASS__, 'includes'], 0);

    // Make the PrecisionPay Payments gateway available to WC.
    add_filter('woocommerce_payment_gateways', [__CLASS__, 'add_gateway']);

    // Registers WooCommerce Blocks integration.
    add_action('woocommerce_blocks_loaded', [__CLASS__, 'woocommerce_gateway_precisionpay_woocommerce_block_support']);

    // Declare compatability with High-Performance order storage (HPOS - originally called custom_order_tables)
    add_action('before_woocommerce_init', function () {
      if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
      }
    });

    // Ajax call to get the merchant API Key
    add_action('wp_ajax_prcsnpy_get_merch_nonce', [__CLASS__, 'get_merchant_nonce']);
    add_action('wp_ajax_nopriv_prcsnpy_get_merch_nonce', [__CLASS__, 'get_merchant_nonce']);
  }

  /**
   * Add the PrecisionPay Payment gateway to the list of available gateways.
   *
   * @param array
   */
  public static function add_gateway($gateways) {
    $gateways[] = 'PrecisionPay_Payments_For_WC';

    return $gateways;
  }

  /**
   * Plugin includes.
   */
  public static function includes() {
    // Make the PrecisionPay_Payments_For_WC class available.
    if (class_exists('WC_Payment_Gateway')) {
      require_once 'includes/class-precisionpay-payments-for-wc.php';
    }
  }

  /**
   * Plugin url.
   *
   * @return string
   */
  public static function plugin_url() {
    return untrailingslashit(plugins_url('/', __FILE__));
  }

  /**
   * Plugin url.
   *
   * @return string
   */
  public static function plugin_abspath() {
    return trailingslashit(plugin_dir_path(__FILE__));
  }

  /**
   * Registers WooCommerce Blocks integration.
   *
   */
  public static function woocommerce_gateway_precisionpay_woocommerce_block_support() {
    if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
      require_once 'includes/blocks/class-wc-precisionpay-blocks-support.php';
      add_action(
        'woocommerce_blocks_payment_method_type_registration',
        function (Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) {
          $payment_method_registry->register(new WC_Gateway_PrecisionPay_Blocks_Support());
        }
      );
    }
  }


  public static function get_merchant_nonce() {
    check_ajax_referer('precision_pay_ajax_nonce', 'precisionPayNonce');
    if (!class_exists('PrecisionPay_Payments_For_WC')) return;

    $name     = PrecisionPay_Payments_For_WC::NAME;
    $gateways = WC()->payment_gateways->payment_gateways();
    $gateway  = $gateways[$name];
    $gateway->get_merchant_nonce();
  }
}

WC_PrecisionPay_Payments::init();
