PluginProbe
HyperPay Payments / 5.3.0
HyperPay Payments v5.3.0
6.6.0 trunk 1.7 1.8 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.1.0 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.5 4.0.0 4.0.2 All 34 releases
hyperpay-gateways / src / Main.php

Main.php in HyperPay Payments 5.3.0, at src/Main.php

170 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hyperpay\Gateways;
4 //use class here
5
6 use Hyperpay\Gateways\App\Hyperpay_Blocks_Support;
7 use Hyperpay\Gateways\App\Log;
8 use Hyperpay\Gateways\App\Webhook;
9 use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
10 use Exception;
11 use Hyperpay\Gateways\App\View;
12
13 class Main
14 {
15
16 public const ROOT_PATH = __DIR__;
17 /**
18 * First function fire when click on active plugin
19 *
20 * @return void
21 */
22
23 public static function load(): void
24 {
25
26 // do not load the plugin if wooCommerce not installed
27 if (!self::isWooCommerceLoaded()) return;
28
29
30 /**
31 * this filter documented in wooCommerce to assign all gateways to [payments tab] inside wooCommerce settings
32 *
33 * @param string filter_name
34 * @param array[class_name,function_name]
35 * @return void
36 */
37
38 add_filter('woocommerce_payment_gateways', [self::class, 'get_gateways']);
39 add_action('rest_api_init', [Webhook::class, 'hyperpay_rest_orders']); //register the webhook class
40
41
42 // Registers WooCommerce Blocks integration.
43 add_action('woocommerce_blocks_loaded', [self::class, 'my_extension_woocommerce_blocks_support']);
44
45
46 if (is_admin()) {
47 add_action('admin_menu', function () {
48 add_options_page('Hyperpay logs', 'Hyperpay logs', 'manage_options', 'hyperpay-logs', [Log::class, "load"]);
49 add_options_page('Hyperpay Options', 'Hyperpay Options', 'manage_options', 'hyperpay-options', [Main::class, "settings"]);
50 });
51 }
52 }
53
54
55 public static function settings()
56 {
57 if (isset($_POST['gateways']) && is_array($_POST['gateways'])) {
58
59 $gateways = [];
60
61 foreach ($_POST['gateways'] as $gateway) {
62 $gateway = sanitize_text_field($gateway);
63 if (\class_exists("\\Hyperpay\\Gateways\\Brands\\$gateway")) {
64 $gateways[] = "\\Hyperpay\\Gateways\\Brands\\$gateway";
65 }
66 };
67
68 if (!empty($gateways)) {
69 update_option('hyperpay_payments_available_gateways', json_encode($gateways));
70 }
71 }
72
73 $selected = static::get_gateways([]);
74
75
76 $selected = array_values(array_map(function ($class) {
77 return \str_replace("\\Hyperpay\\Gateways\\Brands\\", "", $class);
78 }, $selected));
79
80
81
82 $files = scandir(Main::ROOT_PATH . "/Brands");
83 $files = \array_filter($files, function ($name) {
84 return !preg_match('/^[.]/', $name);
85 });
86
87
88 $files = array_values(array_map(function ($file) {
89 return \str_replace(".php", "", $file);
90 }, $files));
91
92 return View::render('settings.html', \compact('selected', 'files'));
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(
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 = get_option('hyperpay_payments_available_gateways' , "[]");
140 $selected = json_decode($selected, true);
141
142 if (empty($selected)) {
143 $selected = [ // default gateways
144 "\\Hyperpay\\Gateways\\Brands\\Mada",
145 "\\Hyperpay\\Gateways\\Brands\\CreditCard",
146 "\\Hyperpay\\Gateways\\Brands\\STCPay",
147 "\\Hyperpay\\Gateways\\Brands\\ApplePay",
148 "\\Hyperpay\\Gateways\\Brands\\Tabby",
149 "\\Hyperpay\\Gateways\\Brands\\Tamara",
150 "\\Hyperpay\\Gateways\\Brands\\UrPay",
151 ];
152 }
153
154 return array_merge($gateways, $selected );
155 }
156
157
158 public static function my_extension_woocommerce_blocks_support()
159 {
160 if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
161 add_action(
162 'woocommerce_blocks_payment_method_type_registration',
163 function (PaymentMethodRegistry $payment_method_registry) {
164 $payment_method_registry->register(new Hyperpay_Blocks_Support);
165 }
166 );
167 }
168 }
169 }
170