| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hyperpay\Gateways; |
| 4 |
|
| 5 |
use Hyperpay\Gateways\App\Hyperpay_Blocks_Support; |
| 6 |
use Hyperpay\Gateways\Helpers\Log; |
| 7 |
use Hyperpay\Gateways\App\Webhook; |
| 8 |
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry; |
| 9 |
use Hyperpay\Gateways\Helpers\View; |
| 10 |
|
| 11 |
class Main |
| 12 |
{ |
| 13 |
|
| 14 |
public const ROOT_PATH = __DIR__; |
| 15 |
/** |
| 16 |
* First function fire when click on active plugin |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
|
| 21 |
public static function load(): void |
| 22 |
{ |
| 23 |
|
| 24 |
// do not load the plugin if wooCommerce not installed |
| 25 |
if (!self::isWooCommerceLoaded()) return; |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* this filter documented in wooCommerce to assign all gateways to [payments tab] inside wooCommerce settings |
| 30 |
* |
| 31 |
* @param string filter_name |
| 32 |
* @param array[class_name,function_name] |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
|
| 36 |
add_filter('woocommerce_payment_gateways', [self::class, 'get_gateways']); |
| 37 |
add_action('rest_api_init', [Webhook::class, 'hyperpay_rest_orders']); //register the webhook class |
| 38 |
|
| 39 |
|
| 40 |
// Registers WooCommerce Blocks integration. |
| 41 |
add_action('woocommerce_blocks_loaded', [self::class, 'my_extension_woocommerce_blocks_support']); |
| 42 |
|
| 43 |
|
| 44 |
if (is_admin()) { |
| 45 |
add_action('admin_menu', function () { |
| 46 |
add_options_page('Hyperpay logs', 'Hyperpay logs', 'manage_options', 'hyperpay-logs', [Log::class, "load"]); |
| 47 |
add_options_page('Hyperpay Options', 'Hyperpay Options', 'manage_options', 'hyperpay-options', [Main::class, "settings"]); |
| 48 |
}); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
public static function settings() |
| 54 |
{ |
| 55 |
if (isset($_POST['hyperpay_options_nonce']) && wp_verify_nonce(sanitize_key($_POST['hyperpay_options_nonce']), 'hyperpay-options') && isset($_POST['gateways']) && is_array($_POST['gateways'])) { |
| 56 |
|
| 57 |
$gateways = []; |
| 58 |
|
| 59 |
foreach (array_map('sanitize_text_field', wp_unslash($_POST['gateways'])) as $gateway) { |
| 60 |
$gateway = sanitize_text_field($gateway); |
| 61 |
if (\class_exists("\\Hyperpay\\Gateways\\Brands\\$gateway")) { |
| 62 |
$gateways[] = "\\Hyperpay\\Gateways\\Brands\\$gateway"; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
if (!empty($gateways)) { |
| 67 |
update_option('hyperpay_payments_available_gateways', json_encode($gateways)); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$selected = static::get_gateways([]); |
| 72 |
|
| 73 |
|
| 74 |
$selected = array_values(array_map(function ($class) { |
| 75 |
return \str_replace("\\Hyperpay\\Gateways\\Brands\\", "", $class); |
| 76 |
}, $selected)); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
$files = scandir(Main::ROOT_PATH . "/Brands"); |
| 81 |
$files = \array_filter($files, function ($name) { |
| 82 |
return !preg_match('/^[.]/', $name); |
| 83 |
}); |
| 84 |
|
| 85 |
|
| 86 |
$files = array_values(array_map(function ($file) { |
| 87 |
return \str_replace(".php", "", $file); |
| 88 |
}, $files)); |
| 89 |
|
| 90 |
$nonce = wp_create_nonce('hyperpay-options'); |
| 91 |
|
| 92 |
return View::render('settings.html', \compact('selected', 'files', 'nonce')); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Check if the wooCommerce plugin is installed and active |
| 99 |
* @return boolean |
| 100 |
*/ |
| 101 |
|
| 102 |
private static function isWooCommerceLoaded() |
| 103 |
{ |
| 104 |
if (!class_exists('woocommerce')) { |
| 105 |
|
| 106 |
add_action( |
| 107 |
'admin_notices', |
| 108 |
function () { |
| 109 |
?> |
| 110 |
<div class="notice notice-error"> |
| 111 |
<p> |
| 112 |
<?php |
| 113 |
printf(/* translators: %s: WooCommerce plugin install URL */ |
| 114 |
esc_html__('Hyperpay plugin could not be loaded without wooCommerce please install wooCommerce first', 'hyperpay-gateways'), |
| 115 |
'<a href="' . esc_url('/wp-admin/plugin-install.php?s=WooCommerce&tab=search&type=term') . '" rel="noopener noreferrer">', |
| 116 |
'</a>' |
| 117 |
); |
| 118 |
?> |
| 119 |
</p> |
| 120 |
</div> |
| 121 |
<?php |
| 122 |
} |
| 123 |
); |
| 124 |
return false; |
| 125 |
} |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/** |
| 131 |
* Merge the previous gateways with hyperPay gateways |
| 132 |
* |
| 133 |
* @param array $gateways |
| 134 |
* @return array $updated_gateways |
| 135 |
*/ |
| 136 |
|
| 137 |
public static function get_gateways(array $gateways): array |
| 138 |
{ |
| 139 |
$selected = [ // default gateways |
| 140 |
"\\Hyperpay\\Gateways\\Brands\\Mada", |
| 141 |
"\\Hyperpay\\Gateways\\Brands\\CreditCard", |
| 142 |
"\\Hyperpay\\Gateways\\Brands\\STCPay", |
| 143 |
"\\Hyperpay\\Gateways\\Brands\\ApplePay", |
| 144 |
"\\Hyperpay\\Gateways\\Brands\\Tabby", |
| 145 |
"\\Hyperpay\\Gateways\\Brands\\Tamara", |
| 146 |
"\\Hyperpay\\Gateways\\Brands\\UrPay", |
| 147 |
"\\Hyperpay\\Gateways\\Brands\\Valu", |
| 148 |
"\\Hyperpay\\Gateways\\Brands\\GooglePay", |
| 149 |
"\\Hyperpay\\Gateways\\Brands\\Aani", |
| 150 |
"\\Hyperpay\\Gateways\\Brands\\Jaywan" |
| 151 |
// "\\Hyperpay\\Gateways\\Brands\\SamsungPay" |
| 152 |
|
| 153 |
]; |
| 154 |
|
| 155 |
|
| 156 |
return array_merge($gateways, $selected); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
public static function my_extension_woocommerce_blocks_support() |
| 161 |
{ |
| 162 |
if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) { |
| 163 |
add_action( |
| 164 |
'woocommerce_blocks_payment_method_type_registration', |
| 165 |
function (PaymentMethodRegistry $payment_method_registry) { |
| 166 |
$payment_method_registry->register(new Hyperpay_Blocks_Support); |
| 167 |
} |
| 168 |
); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|