-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-gateway-kekspay.php
440 lines (384 loc) · 13.5 KB
/
woocommerce-gateway-kekspay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Plugin Name: KEKS Pay for WooCommerce
* Plugin URI: https://www.kekspay.hr/
* Description: Incredibly fast and user friendly payment method created by Erste Bank Croatia.
* Version: 2.1.0
* Requires at least: 6.3
* Requires PHP: 7.4
* Author: Erste bank Croatia
* Author URI: https://www.erstebank.hr/hr/gradjanstvo
* License: GPL v3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: kekspay
* Domain Path: /languages
*
* WC requires at least: 8.2
* WC tested up to: 9.3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'kekspay_wc_active' ) ) {
/**
* Return true if the WooCommerce plugin is active or false otherwise.
*
* @since 0.1
* @return boolean
*/
function kekspay_wc_active() {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
return is_plugin_active( 'woocommerce/woocommerce.php' );
}
}
if ( ! function_exists( 'kekspay_admin_notice_missing_woocommerce' ) ) {
/**
* Echo admin notice HTML for missing WooCommerce plugin.
*
* @since 0.1
*/
function kekspay_admin_notice_missing_woocommerce() {
/* translators: 1. URL link. */
echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'KEKS Pay zahtijeva WooCommerce dodatak instaliran i aktivan. Možete ga skinuti ovdje %s.', 'kekspay' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
}
}
if ( ! kekspay_wc_active() ) {
add_action( 'admin_notices', 'kekspay_admin_notice_missing_woocommerce' );
return;
}
/**
* Declare Kekspay plugin compatibility for certain WooCommerce features:
* - HPOS
* - Checkout Blocks
*
* @return void
*/
function kekspay_wc_features_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
}
}
add_action( 'before_woocommerce_init', 'kekspay_wc_features_compatibility' );
if ( ! class_exists( 'WC_Kekspay' ) ) {
/**
* The main plugin class.
*
* @since 0.1
*/
class WC_Kekspay {
/**
* Instance of the current class, null before first usage.
*
* @var WC_Kekspay
*/
protected static $instance = null;
/**
* Class constructor, initialize constants and settings.
*
* @since 0.1
*/
protected function __construct() {
self::register_constants();
$this->load_textdomain();
$this->check_requirements();
// Require all necessary files.
require_once 'includes/utilities/class-kekspay-data.php';
require_once 'includes/utilities/class-kekspay-logger.php';
require_once 'includes/core/class-kekspay-connector.php';
require_once 'includes/core/class-kekspay-sell.php';
require_once 'includes/core/class-kekspay-ipn.php';
require_once 'includes/core/class-kekspay-order-admin.php';
require_once 'includes/core/class-kekspay-payment-gateway.php';
// Add hooks.
add_filter( 'woocommerce_payment_gateways', [ $this, 'add_gateway' ] );
add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 10, 2 );
add_action( 'admin_enqueue_scripts', [ $this, 'register_admin_script' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'register_client_script' ] );
add_action( 'admin_init', [ $this, 'check_settings' ], 20 );
add_action( 'admin_init', [ $this, 'check_for_other_kekspay_gateways' ], 1 );
add_action( 'activated_plugin', [ $this, 'set_kekspay_plugins_check_required' ] );
add_action( 'woocommerce_admin_field_payment_gateways', [ $this, 'set_kekspay_plugins_check_required' ] );
add_action( 'woocommerce_blocks_loaded', [ $this, 'register_checkout_block_gateway' ] );
}
/**
* Register plugin's constants.
*/
public static function register_constants() {
if ( ! defined( 'KEKSPAY_PLUGIN_ID' ) ) {
define( 'KEKSPAY_PLUGIN_ID', 'erste-kekspay-woocommerce' );
}
if ( ! defined( 'KEKSPAY_PLUGIN_VERSION' ) ) {
define( 'KEKSPAY_PLUGIN_VERSION', '2.1.0' );
}
if ( ! defined( 'KEKSPAY_DIR_PATH' ) ) {
define( 'KEKSPAY_DIR_PATH', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'KEKSPAY_DIR_URL' ) ) {
define( 'KEKSPAY_DIR_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'KEKSPAY_ADMIN_SETTINGS_URL' ) ) {
define( 'KEKSPAY_ADMIN_SETTINGS_URL', get_admin_url( null, 'admin.php?page=wc-settings&tab=checkout§ion=' . KEKSPAY_PLUGIN_ID ) );
}
if ( ! defined( 'KEKSPAY_REQUIRED_PHP_VERSION' ) ) {
define( 'KEKSPAY_REQUIRED_PHP_VERSION', '7.4' );
}
if ( ! defined( 'KEKSPAY_REQUIRED_WC_VERSION' ) ) {
define( 'KEKSPAY_REQUIRED_WC_VERSION', '8.2' );
}
}
/**
* Load plugin's textdomain.
*/
public function load_textdomain() {
load_plugin_textdomain( 'kekspay', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Check versions of requirements.
*/
public function check_requirements() {
$requirements = [
'php' => [
'current_version' => phpversion(),
'requred_version' => KEKSPAY_REQUIRED_PHP_VERSION,
'name' => 'PHP',
],
'wc' => [
'current_version' => WC_VERSION,
'requred_version' => KEKSPAY_REQUIRED_WC_VERSION,
'name' => 'WooCommerce',
],
];
$error_notices = [];
foreach ( $requirements as $requirement ) {
if ( version_compare( $requirement['current_version'], $requirement['requred_version'], '<' ) ) {
$error_notices[] = sprintf(
/* translators: minimum version dependancies */
__( 'Minimalna verzija %1$s je %2$s. Verzija koju trenutno koristite je %3$s. Molimo instalirajte minimalnu verziju %1$s ako želite koristiti KEKS Pay.', 'kekspay' ),
$requirement['name'],
$requirement['requred_version'],
$requirement['current_version']
);
}
}
if ( $error_notices ) {
add_action( 'admin_init', [ $this, 'deactivate_self' ] );
foreach ( $error_notices as $error_notice ) {
self::admin_notice( $error_notice );
}
}
}
/**
* Add KEKS Pay payment method.
*/
public function add_gateway( $methods ) {
$methods[] = 'Kekspay_Payment_Gateway';
return $methods;
}
/**
* Check gateway settings and dispatch notice.
*/
public function check_settings() {
// If payment gateway is not enabled bail.
if ( ! Kekspay_Data::enabled() ) {
return;
}
// Check if gateway is currently in test mode.
if ( Kekspay_Data::test_mode() ) {
self::admin_notice( __( 'KEKS Pay je trenutno u testom načinu rada, ne zaboravite ga ugasiti po završetku testiranja.', 'kekspay' ), 'warning' );
}
// Check if all setting keys required for gateway to work are set.
if ( ! Kekspay_Data::required_keys_set() ) {
self::admin_notice( __( 'KEKS Pay je trenutno onemogućen, molimo provjerite da su TID, CID i tajni ključ pravilno postavljeni.', 'kekspay' ), 'warning' );
}
// Check if correct currency is set in webshop.
if ( ! Kekspay_Data::currency_supported() ) {
self::admin_notice( __( 'KEKS Pay je trenutno onemogućen, valuta web trgovine nije podržana. Podržane valute: "EUR".', 'kekspay' ), 'warning' );
}
}
/**
* Check if there are other KEKS Pay gateways.
*/
public static function check_for_other_kekspay_gateways() {
if ( ! get_option( 'kekspay_plugins_check_required' ) ) {
return;
}
delete_option( 'kekspay_plugins_check_required' );
// Check if there already is payment method with id "nrlb-kekspay-woocommerce".
$payment_gateways = WC_Payment_Gateways::instance()->payment_gateways();
if ( isset( $payment_gateways[ KEKSPAY_PLUGIN_ID ] ) && ! $payment_gateways[ KEKSPAY_PLUGIN_ID ] instanceof Kekspay_Payment_Gateway ) {
self::admin_notice( __( 'Možete imati samo jedan KEKS Pay način plaćanja aktivan u isto vrijeme. Dodatak "KEKS Pay for WooCommerce" je deaktiviran.', 'kekspay' ) );
self::deactivate_self();
}
}
/**
* Set check required.
*/
public static function set_kekspay_plugins_check_required() {
update_option( 'kekspay_plugins_check_required', 'yes' );
}
/**
* Register Kekspay method for block checkout.
*/
public function register_checkout_block_gateway() {
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
return;
}
require_once KEKSPAY_DIR_PATH . 'includes/core/class-kekspay-block-checkout.php';
add_action(
'woocommerce_blocks_payment_method_type_registration',
function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
$payment_method_registry->register( new Kekspay_Block_Checkout() );
}
);
}
/**
* Deactivate plugin.
*/
public static function deactivate_self() {
remove_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [ self::get_instance(), 'add_settings_link' ] );
remove_action( 'admin_init', [ self::get_instance(), 'check_settings' ], 20 );
deactivate_plugins( plugin_basename( __FILE__ ) );
unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
}
/**
* Add admin notice.
*
* @param string $notice Notice content.
* @param string $type Notice type.
*/
public static function admin_notice( $notice, $type = 'error' ) {
add_action(
'admin_notices',
function() use ( $notice, $type ) {
printf( '<div class="notice notice-%2$s"><p>%1$s</p></div>', wp_kses_post( $notice ), esc_html( $type ) );
}
);
}
/**
* Register plugin's admin JS script.
*/
public function register_admin_script() {
$screen = get_current_screen();
$screen_id = $screen ? $screen->id : '';
if ( strpos( $screen_id, 'wc-settings' ) !== false ) {
$section = filter_input( INPUT_GET, 'section', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( $section && KEKSPAY_PLUGIN_ID === $section ) {
wp_enqueue_script( 'kekspay-admin-script', KEKSPAY_DIR_URL . '/assets/dist/js/kekspay-admin.js', [ 'jquery' ], KEKSPAY_PLUGIN_VERSION, true );
}
}
}
/**
* Register plugin's client JS script.
*/
public function register_client_script() {
if ( is_checkout() ) {
wp_enqueue_style( 'kekspay-client-style', KEKSPAY_DIR_URL . '/assets/dist/css/kekspay.css', [], KEKSPAY_PLUGIN_VERSION );
// Add redirect js only on order-pay endpoint but not on wp-admin generated customer payment page.
if ( is_wc_endpoint_url( 'order-pay' ) && ! filter_input( INPUT_GET, 'pay_for_order', FILTER_VALIDATE_BOOLEAN ) ) {
$order_id = get_query_var( 'order-pay' );
$order = new WC_Order( $order_id );
if ( 'erste-kekspay-woocommerce' === $order->get_payment_method() ) {
wp_enqueue_script( 'kekspay-client-script', KEKSPAY_DIR_URL . '/assets/dist/js/kekspay.js', [ 'jquery' ], KEKSPAY_PLUGIN_VERSION, true );
$localize_data = [
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'kekspay_advice_status' ),
'ipn_refresh' => apply_filters( 'kekspay_ipn_refresh_rate', 5000 ),
'order_id' => $order_id,
];
wp_localize_script( 'kekspay-client-script', 'kekspayClientScript', $localize_data );
}
}
}
}
/**
* Adds the link to the settings page on the plugins WP page.
*
* @param array $links
* @return array
*/
public function add_settings_link( $links, $file ) {
if ( $file === plugin_basename( __FILE__ ) ) {
array_unshift( $links, '<a href="' . KEKSPAY_ADMIN_SETTINGS_URL . '">' . __( 'Postavke', 'kekspay' ) . '</a>' );
}
return $links;
}
/**
* Delete gateway settings. Return true if option is successfully deleted or
* false on failure or if option does not exist.
*
* @return bool
*/
public static function delete_settings() {
return delete_option( 'woocommerce_' . KEKSPAY_PLUGIN_ID . '_settings' ) && delete_option( 'kekspay_plugins_check_required' );
}
/**
* Installation procedure.
*
* @static
*/
public static function install() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return false;
}
self::set_kekspay_plugins_check_required();
self::register_constants();
}
/**
* Uninstallation procedure.
*
* @static
*/
public static function uninstall() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return false;
}
self::register_constants();
self::delete_settings();
wp_cache_flush();
}
/**
* Deactivation function.
*
* @static
*/
public static function deactivate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return false;
}
self::register_constants();
}
/**
* Return class instance.
*
* @static
* @return WC_Kekspay
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Cloning is forbidden.
*
* @since 0.1
*/
public function __clone() {
return wp_die( 'Cloning is forbidden!' );
}
/**
* Unserializing instances of this class is forbidden.
*
* @since 0.1
*/
public function __wakeup() {
return wp_die( 'Unserializing instances is forbidden!' );
}
}
}
register_activation_hook( __FILE__, [ 'WC_Kekspay', 'install' ] );
register_uninstall_hook( __FILE__, [ 'WC_Kekspay', 'uninstall' ] );
register_deactivation_hook( __FILE__, [ 'WC_Kekspay', 'deactivate' ] );
add_action( 'plugins_loaded', [ 'WC_Kekspay', 'get_instance' ], 0 );