-
Notifications
You must be signed in to change notification settings - Fork 7
/
class-woocommerce-gateway-monei.php
439 lines (379 loc) · 13 KB
/
class-woocommerce-gateway-monei.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
<?php
/**
* Installation related functions and actions.
*
* @author MONEI
* @category Core
* @package Woocommerce_Gateway_Monei
* @version 6.1.2
*/
if ( ! class_exists( 'Woocommerce_Gateway_Monei' ) ) :
final class Woocommerce_Gateway_Monei {
/**
* Woocommerce_Gateway_Monei version.
*
* @var string
*/
public $version = '6.1.2';
/**
* The single instance of the class.
*
* @var Woocommerce_Gateway_Monei
* @since 1.0.0
*/
protected static $_instance = null;
/**
* @var bool
*/
protected static $_initialized = false;
/**
* Main Woocommerce_Gateway_Monei Instance.
*
* Ensures only one instance of Woocommerce_Gateway_Monei is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @return Woocommerce_Gateway_Monei - Main instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
self::$_instance->initalize_plugin();
}
return self::$_instance;
}
/**
* Woocommerce_Gateway_Monei Initializer.
*/
public function initalize_plugin() {
if ( self::$_initialized ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Use Singleton.', 'monei' ), '1.0.0' );
return;
}
self::$_initialized = true;
// Declare block compatibility
$this->block_compatiblity();
add_action( 'plugins_loaded', array( $this, 'continue_init' ), -1 );
}
public function block_compatiblity() {
// Load checkout block class
add_action( 'woocommerce_blocks_loaded', function() {
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
return;
}
require_once 'includes/class-monei-cc-blocks.php';
require_once 'includes/MoneiBizumBlocksSupport.php';
require_once 'includes/AppleGoogleBlocksSupport.php';
require_once 'includes/MoneiMultibancoBlocksSupport.php';
require_once 'includes/MoneiMBWayBlocksSupport.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_Monei_CC_Blocks );
$payment_method_registry->register( new MoneiBizumBlocksSupport );
$payment_method_registry->register( new AppleGoogleBlocksSupport );
$payment_method_registry->register( new MoneiMultibancoBlocksSupport );
$payment_method_registry->register( new MoneiMBWayBlocksSupport );
} );
} );
}
/**
* Plugin initialization.
*/
public function continue_init() {
if ( ! $this->check_dependencies() ) {
add_action( 'admin_notices', array( $this, 'dependency_notice' ) );
return;
}
if ( ! $this->get_installed_version() ) {
add_action( 'admin_notices', array( $this, 'admin_new_install_notice' ) );
}
$this->define_constants();
$this->includes();
$this->init_hooks();
do_action( 'woocommerce_gateway_monei_loaded' );
}
/**
* WC_Monei Constants.
*/
private function define_constants() {
$this->define( 'MONEI_GATEWAY_ID', 'monei' );
$this->define( 'MONEI_VERSION', $this->version );
$this->define( 'MONEI_SIGNUP', 'https://dashboard.monei.com/?action=signUp' );
$this->define( 'MONEI_WEB', 'https://monei.com/' );
$this->define( 'MONEI_REVIEW', 'https://wordpress.org/support/plugin/monei/reviews/?rate=5#new-post' );
$this->define( 'MONEI_SUPPORT', 'https://support.monei.com/' );
$this->define( 'MONEI_MAIN_FILE', __FILE__ );
}
/**
* Include required core files used in admin and on the frontend.
*/
private function includes() {
include_once 'includes/woocommerce-gateway-monei-template-functions.php';
include_once 'includes/woocommerce-gateway-monei-core-functions.php';
include_once 'includes/class-wc-monei-ipn.php';
include_once 'includes/class-wc-monei-api.php';
include_once 'includes/class-wc-monei-logger.php';
include_once 'includes/addons/trait-wc-monei-addons-helper.php';
include_once 'includes/addons/trait-wc-monei-subscriptions.php';
include_once 'includes/addons/class-wc-monei-apple-pay-verification.php';
if ( $this->is_request( 'admin' ) ) {
include_once 'includes/class-wc-monei-pre-auth.php';
add_filter('woocommerce_get_settings_pages', function ($settings) {
include_once 'src/Settings/MoneiSettings.php';
$settings[] = new MoneiSettings();
return $settings;
});
}
if ( $this->is_request( 'frontend' ) ) {
include_once 'includes/class-wc-monei-redirect-hooks.php';
include_once 'includes/addons/class-wc-monei-addons-redirect-hooks.php';
}
}
/**
* Hook into actions and filters.
* @since 1.0.0
*/
private function init_hooks() {
add_action( 'init', array( $this, 'init' ), 0 );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 0 );
}
/**
* Prints notice when it is a new install. It will save the current install version on Dismiss.
*
* @return void
*/
function admin_new_install_notice() {
/**
* If Dismissed, we save the versions installed.
*/
if ( isset( $_GET['monei-hide-new-version'] ) && 'hide-new-version-monei' === sanitize_text_field( $_GET['monei-hide-new-version'] ) ) {
if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_monei_hide_new_version_nonce'] ) ), 'monei_hide_new_version_nonce' ) ) {
update_option( 'hide-new-version-monei-notice', MONEI_VERSION );
}
return;
}
woocommerce_gateway_monei_get_template( 'notice-admin-new-install.php' );
}
/**
* Prints notice about requirements not met.
*
* @return void
*/
public function dependency_notice() {
woocommerce_gateway_monei_get_template( 'notice-admin-dependency.php' );
}
/**
* Returns true if Woo is activated.
*
* @return bool
*/
public function check_dependencies() {
if ( ! class_exists( 'WooCommerce' ) ) {
return false;
}
return true;
}
/**
* Define constant if not already set.
*
* @param string $name
* @param string|bool $value
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* @param $type
*
* @return bool
*/
private function is_request( $type ) {
switch ( $type ) {
case 'admin':
return is_admin();
case 'ajax':
return defined( 'DOING_AJAX' );
case 'frontend':
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
/**
* Init Woocommerce_Gateway_Monei when WordPress Initialises.
*/
public function init() {
// Before init
do_action( 'before_woocommerce_gateway_monei_init' );
// todo: not translation yet.
//$this->load_plugin_textdomain();
add_filter( 'option_woocommerce_monei_bizum_settings', array( $this, 'monei_settings_by_default' ), 1 );
add_filter( 'option_woocommerce_monei_paypal_settings', array( $this, 'monei_settings_by_default' ), 1 );
add_filter( 'option_woocommerce_monei_multibanco_settings', array( $this, 'monei_settings_by_default' ), 1 );
add_filter( 'option_woocommerce_monei_mbway_settings', array( $this, 'monei_settings_by_default' ), 1 );
add_filter( 'option_woocommerce_monei_settings', array( $this, 'copyKeysToCentralSettings' ), 1 );
// Init action.
do_action( 'woocommerce_gateway_monei_init' );
wp_register_style(
'monei-icons',
$this->plugin_url() . '/public/css/monei-icons-classic.css',
[],
filemtime( $this->plugin_path() . '/public/css/monei-icons-classic.css' ),
'screen'
);
wp_enqueue_style( 'monei-icons' );
wp_register_style(
'monei-blocks-checkout-cc',
WC_Monei()->plugin_url(). '/public/css/monei-blocks-checkout.css',
array(),
WC_Monei()->version,
'all'
);
wp_enqueue_style( 'monei-blocks-checkout-cc' );
}
public function copyKeysToCentralSettings($default_params)
{
$centralApiKey = get_option('monei_apikey');
$centralAccountId = get_option('monei_accountid');
$ccApiKey = $default_params['apikey'] ?? false;
$ccAccountId = $default_params['accountid'] ?? false;
// Update API key if centralApiKey is empty
if ( empty( $centralApiKey ) && !empty( $ccApiKey ) ) {
update_option( 'monei_apikey', $ccApiKey );
}
// Update Account ID if centralAccountId is empty
if ( empty( $centralAccountId ) && !empty( $ccAccountId ) ) {
update_option( 'monei_accountid', $ccAccountId );
}
return $default_params;
}
/**
* We have more than a Monei payment provider, we will use by default the main monei set up in case they don't set them up.
*
* @param $default_params
*
* @return array
*/
public function monei_settings_by_default( $default_params ) {
$default_params['testmode'] = $this->get_setting_with_default( 'testmode', $default_params );
$default_params['apikey'] = $this->get_setting_with_default( 'apikey', $default_params );
$default_params['debug'] = $this->get_setting_with_default( 'debug', $default_params );
$default_params['orderdo'] = ( empty( $default_params['orderdo'] ) ) ? monei_get_settings( 'orderdo' ) : $default_params['orderdo'];
return $default_params;
}
private function get_setting_with_default( $key, $params ) {
if ( ! empty( $params[ $key ] ) ) {
return $params[ $key ];
}
$option_value = get_option( "monei_$key" );
if ( ! empty( $option_value ) ) {
return $option_value;
}
$monei_setting_value = monei_get_settings( $key );
if ( ! empty( $monei_setting_value ) ) {
return $monei_setting_value;
}
return '';
}
/**
* Hooks when plugin_loaded
*/
public function plugins_loaded() {
$this->include_payment_methods();
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateways' ) );
}
/**
* Include Payment Methods.
*/
private function include_payment_methods() {
// Including abstract.
include_once 'includes/abstracts/abstract-wc-monei-payment-gateway.php';
include_once 'includes/abstracts/abstract-wc-monei-payment-gateway-hosted.php';
include_once 'includes/abstracts/abstract-wc-monei-payment-gateway-component.php';
// Including hosted payments.
include_once 'includes/payment-methods/class-wc-gateway-monei-cc.php';
include_once 'includes/payment-methods/MoneiAppleGoogleGateway.php';
include_once 'includes/payment-methods/class-wc-gateway-monei-hosted-cofidis.php';
include_once 'includes/payment-methods/class-wc-gateway-monei-hosted-bizum.php';
include_once 'includes/payment-methods/class-wc-gateway-monei-hosted-paypal.php';
include_once 'includes/payment-methods/MoneiMultibanco.php';
include_once 'includes/payment-methods/MoneiMBWay.php';
}
/**
* Add Monei Gateways.
*
* @param $methods
*
* @return array
*/
public function add_gateways( $methods ) {
$methods[] = 'WC_Gateway_Monei_CC';
if (!is_admin()) {
$methods[] = 'MoneiAppleGoogleGateway';
}
$methods[] = 'WC_Gateway_Monei_Cofidis';
$methods[] = 'WC_Gateway_Monei_Bizum';
$methods[] = 'WC_Gateway_Monei_Paypal';
$methods[] = 'MoneiMultibanco';
$methods[] = 'MoneiMBWay';
return $methods;
}
/**private function load_plugin_textdomain() {
}**/
/**
* Get installed version. For retro compat we keep "hide-new-version-monei-notice"
*
* @return false|string
*/
private function get_installed_version() {
return get_option( 'hide-new-version-monei-notice' );
}
/**
* Get IPN url.
*
* @return string
*/
public function get_ipn_url() {
return WC()->api_request_url( 'monei_ipn' );
}
/**
* Get the plugin url.
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Get the template path.
*
* @return string
*/
public function template_path() {
return apply_filters( 'woocommerce_gateway_monei_template', 'woocommerce-gateway-monei/' );
}
/**
* Get Image URL.
*
* @param string $image
*
* @return string
*/
public function image_url( $image = '' ) {
return $this->plugin_url() . '/public/images/' . $image;
}
/**
* Get Ajax URL.
* @return string
*/
public function ajax_url() {
return admin_url( 'admin-ajax.php', 'relative' );
}
}
endif;