-
Notifications
You must be signed in to change notification settings - Fork 10
/
woocommerce-gateway-ebanx.php
936 lines (791 loc) · 28.9 KB
/
woocommerce-gateway-ebanx.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
<?php
//phpcs:disable WordPress.Files.FileName
/**
* Plugin Name: EBANX Payment Gateway for WooCommerce
* Plugin URI: https://www.ebanx.com/business/en/developers/integrations/extensions-and-plugins/woocommerce-plugin
* Description: Offer Latin American local payment methods & increase your conversion rates with the solution used by
* AliExpress, AirBnB and Spotify in Brazil.
* Author: EBANX
* Author URI: https://www.ebanx.com/business/en
* Version: 2.0.1
* License: MIT
* Text Domain: woocommerce-gateway-ebanx
* Domain Path: /languages
* Requires PHP: 7.3
* Requires at least: 5.5.0
* Tested up to: 5.8.1
* WC requires at least: 4.5.0
* WC tested up to: 5.7.1
*
* @package WooCommerce_EBANX
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'WC_EBANX_MIN_PHP_VER', '7.3.0' );
define( 'WC_EBANX_MIN_WC_VER', '4.5.0' );
define( 'WC_EBANX_MIN_WP_VER', '5.5.0' );
define( 'WC_EBANX_DIR', __DIR__ . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_PLUGIN_DIR_URL', plugin_dir_url( __FILE__ ) . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_PLUGIN_NAME', WC_EBANX_PLUGIN_DIR_URL . basename( __FILE__ ) );
define( 'WC_EBANX_GATEWAYS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'gateways' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_SERVICES_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'services' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_EXCEPTIONS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'exceptions' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_LANGUAGES_DIR', dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
define( 'WC_EBANX_TEMPLATES_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_VENDOR_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_ASSETS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_CONTROLLERS_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR );
define( 'WC_EBANX_DATABASE_DIR', __DIR__ . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR );
require_once WC_EBANX_VENDOR_DIR . '/autoload.php';
use EBANX\Plugin\Services\WC_EBANX_Constants;
use EBANX\Plugin\Services\WC_EBANX_Notice;
use EBANX\Plugin\Services\WC_EBANX_Query_Router;
use EBANX\Plugin\Services\WC_EBANX_Assets;
use EBANX\Plugin\Services\WC_EBANX_Checker;
use EBANX\Plugin\Services\WC_EBANX_Hooks;
use EBANX\Plugin\Services\WC_EBANX_My_Account;
WC_EBANX_Hooks::init();
new WC_EBANX_My_Account();
if ( ! class_exists( 'WC_EBANX' ) ) {
/**
* Hooks
*/
register_activation_hook( __FILE__, array( 'WC_EBANX', 'activate_plugin' ) );
register_deactivation_hook( __FILE__, array( 'WC_EBANX', 'deactivate_plugin' ) );
include_once WC_EBANX_DATABASE_DIR . 'class-wc-ebanx-database.php';
register_activation_hook( __FILE__, array( 'WC_EBANX_Database', 'migrate' ) );
/**
* WooCommerce WC_EBANX main class.
*/
class WC_EBANX {
/**
* Plugin version.
*
* @var string
*/
const DIR = __FILE__;
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
*
* @var $log
*/
private static $log;
/**
*
* @var string $my_account_endpoint
*/
private static $my_account_endpoint = 'ebanx-saved-cards';
/**
*
* @var string $my_account_name
*/
private static $my_account_menu_name = 'EBANX - Saved Cards';
/**
*
* @var array $settings
*/
private $settings = [];
/**
* Initialize the plugin public actions.
*/
private function __construct() {
$this->notices = new WC_EBANX_Notice();
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
$this->notices
->with_view( 'missing-woocommerce' )
->enqueue();
return;
}
/**
* Includes.
*/
$this->includes();
$configs = new WC_EBANX_Global_Gateway();
/**
* Actions.
*/
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
add_action( 'wp_loaded', array( $this, 'enable_i18n' ) );
add_action( 'init', array( $this, 'ebanx_router' ) );
add_action( 'init', array( 'WC_EBANX_Third_Party_Compability_Layer', 'check_and_solve' ) );
add_action( 'admin_init', array( $this, 'ebanx_sidebar_shortcut' ) );
add_action( 'admin_init', array( '\EBANX\Plugin\Services\WC_EBANX_Flash', 'enqueue_admin_messages' ) );
if ( WC_EBANX_Request::is_post_empty() ) {
add_action( 'admin_init', array( $this, 'setup_configs' ), 10 );
add_action( 'admin_init', array( $this, 'checker' ), 30 );
}
add_action( 'admin_head', array( '\EBANX\Plugin\Services\WC_EBANX_Capture_Payment', 'add_order_capture_button_css' ) );
add_action( 'woocommerce_order_actions', array( '\EBANX\Plugin\Services\WC_EBANX_Capture_Payment', 'add_auto_capture_dropdown' ) );
add_action( 'woocommerce_order_action_ebanx_capture_order', array( '\EBANX\Plugin\Services\WC_EBANX_Capture_Payment', 'capture_from_order_dropdown' ) );
add_action( 'admin_footer', array( '\EBANX\Plugin\Services\WC_EBANX_Assets', 'render' ), 0 );
add_action( 'woocommerce_settings_save_checkout', array( $this, 'on_before_save_settings' ), 10 );
add_action( 'woocommerce_settings_saved', array( $this, 'setup_configs' ), 10 );
add_action( 'woocommerce_settings_saved', array( $this, 'on_save_settings' ), 10 );
add_action( 'woocommerce_settings_saved', array( $this, 'update_lead' ), 20 );
add_action( 'woocommerce_settings_saved', array( $this, 'checker' ), 20 );
add_action( 'woocommerce_admin_order_data_after_order_details', array( $this, 'ebanx_admin_order_details' ), 10, 1 );
add_action( 'upgrader_process_complete', array( $this, 'on_update' ), 10, 2 );
add_action( 'woocommerce_checkout_process', array( '\EBANX\Plugin\Services\WC_EBANX_Checker', 'validate_document' ), 10 );
/**
* Payment by Link
*/
add_action( 'woocommerce_order_actions_end', array( $this, 'ebanx_metabox_save_post_render_button' ) );
add_action( 'save_post', array( $this, 'ebanx_metabox_payment_link_save' ) );
/**
* My account
*/
if ( $configs
&& $configs->get_setting_or_default( 'save_card_data', 'no' ) === 'yes' ) {
add_action( 'init', array( $this, 'my_account_endpoint' ) );
add_action( 'woocommerce_account_' . self::$my_account_endpoint . '_endpoint', array( $this, 'my_account_template' ) );
add_filter( 'query_vars', array( $this, 'my_account_query_vars' ), 0 );
add_filter( 'woocommerce_account_menu_items', array( $this, 'my_account_menus' ) );
add_filter( 'the_title', array( $this, 'my_account_menus_title' ) );
}
/**
* Filters
*/
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_gateway' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
add_filter( 'woocommerce_my_account_my_orders_actions', array( '\EBANX\Plugin\Services\WC_EBANX_Cancel_Order', 'add_my_account_cancel_order_action' ), 10, 2 );
add_filter( 'woocommerce_admin_order_actions', array( '\EBANX\Plugin\Services\WC_EBANX_Capture_Payment', 'add_order_capture_button' ), 10, 2 );
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'get_instalments_admin_html' ) );
}
/**
* @param array $methods
*
* @return array
*/
private static function argentina_gateway( $methods ) {
$methods[] = 'WC_EBANX_Credit_Card_AR_Gateway';
$methods[] = 'WC_EBANX_Efectivo_Gateway';
$methods[] = 'WC_EBANX_Wallet_MercadoPago_Gateway';
return $methods;
}
/**
* @param array $methods
*
* @return array
*/
private function peru_gateway( $methods ) {
$methods[] = 'WC_EBANX_Pagoefectivo_Gateway';
$methods[] = 'WC_EBANX_Safetypay_Gateway';
return $methods;
}
/**
* @param array $methods
*
* @return array
*/
private static function colombia_gateway( $methods ) {
$methods[] = 'WC_EBANX_Credit_Card_CO_Gateway';
$methods[] = 'WC_EBANX_Baloto_Gateway';
$methods[] = 'WC_EBANX_Eft_Gateway';
$methods[] = 'WC_EBANX_Wallet_Nequi_Gateway';
return $methods;
}
/**
* @param array $methods
*
* @return array
*/
private static function chile_gateway( $methods ) {
$methods[] = 'WC_EBANX_Webpay_Gateway';
$methods[] = 'WC_EBANX_Multicaja_Gateway';
$methods[] = 'WC_EBANX_Sencillito_Gateway';
$methods[] = 'WC_EBANX_Servipag_Gateway';
$methods[] = 'WC_EBANX_Wallet_MACHPay_Gateway';
return $methods;
}
/**
* @param array $methods
*
* @return array
*/
private static function mexico_gateway( $methods ) {
$methods[] = 'WC_EBANX_Credit_Card_MX_Gateway';
$methods[] = 'WC_EBANX_Debit_Card_Gateway';
$methods[] = 'WC_EBANX_Oxxo_Gateway';
$methods[] = 'WC_EBANX_Spei_Gateway';
$methods[] = 'WC_EBANX_Wallet_MercadoPago_Gateway';
return $methods;
}
/**
* @param array $methods
*
* @return array
*/
private static function bolivia_gateway( $methods ) {
$methods[] = 'WC_EBANX_Pagosnet_Gateway';
return $methods;
}
/**
* @param array $methods
*
* @return array
*/
private static function brazil_gateway( $methods ) {
$methods[] = 'WC_EBANX_Banking_Ticket_Gateway';
$methods[] = 'WC_EBANX_Credit_Card_BR_Gateway';
$methods[] = 'WC_EBANX_Tef_Gateway';
$methods[] = 'WC_EBANX_Bank_Transfer_Gateway';
$methods[] = 'WC_EBANX_Pix_Gateway';
$methods[] = 'WC_EBANX_Wallet_MercadoPago_Gateway';
$methods[] = 'WC_EBANX_Wallet_Paypal_Gateway';
$methods[] = 'WC_EBANX_Wallet_Picpay_Gateway';
return $methods;
}
/**
* Sets up the configuration object
*
* @return void
*/
public function setup_configs() {
/**
* Configs
*/
$this->configs = new WC_EBANX_Global_Gateway();
$this->is_sandbox_mode = 'yes' === $this->configs->settings['sandbox_mode_enabled'];
$this->private_key = $this->is_sandbox_mode ? $this->configs->settings['sandbox_private_key'] : $this->configs->settings['live_private_key'];
$this->public_key = $this->is_sandbox_mode ? $this->configs->settings['sandbox_public_key'] : $this->configs->settings['live_public_key'];
}
/**
* Extract some informations from the plugi.n
*
* @param string $info The information that you want to extract, possible values: version, name, description, author, network.
* @return string The value extracted.
*/
public static function get_plugin_info( $info = 'name' ) {
$plugin = get_file_data( __FILE__, array( $info => $info ) );
return $plugin[ $info ];
}
/**
* Extract the plugin version described on plugin's header.
*
* @return string The plugin version.
*/
public static function get_plugin_version() {
return self::get_plugin_info( 'version' );
}
/**
* Performs checks on some system status.
*
* @return void
*/
public function checker() {
WC_EBANX_Checker::check_sandbox_mode( $this );
WC_EBANX_Checker::check_merchant_api_keys( $this );
WC_EBANX_Checker::check_environment( $this );
WC_EBANX_Checker::check_currency( $this );
WC_EBANX_Checker::check_https_protocol( $this );
}
/**
* Call when the plugins are loaded.
*
* @return mixed
*/
public function plugins_loaded() {
if ( $this->get_environment_warning() ) {
return;
}
}
/**
* Checks if we are receiving a third-party request and routes it
*
* @return void
*/
public function ebanx_router() {
$ebanx_router = new WC_EBANX_Query_Router( 'ebanx' );
$this->setup_configs();
$api_controller = new WC_EBANX_Api_Controller( $this->configs );
$ebanx_router->map( 'dashboard-check', array( $api_controller, 'dashboard_check' ) );
$ebanx_router->map( 'order-received', array( $api_controller, 'order_received' ) );
$ebanx_router->map( 'cancel-order', array( $api_controller, 'cancel_order' ) );
$ebanx_router->map( 'capture-payment', array( $api_controller, 'capture_payment' ) );
$ebanx_router->map( 'retrieve-logs', array( $api_controller, 'retrieve_logs' ) );
$ebanx_router->map( 'plugin-check', array( $api_controller, 'plugin_check' ) );
$ebanx_router->serve();
}
/**
* It enables the i18n of the plugin using the languages folders and the domain 'woocommerce-gateway-ebanx'
*
* @return void
*/
public function enable_i18n() {
load_plugin_textdomain( 'woocommerce-gateway-ebanx', false, WC_EBANX_LANGUAGES_DIR );
}
/**
* It enables the my account page for logged users.
*
* @return void
*/
public function my_account_template() {
if ( WC_EBANX_Request::has( 'credit-card-delete' )
&& is_account_page() ) {
// Find credit cards saved and delete the selected.
$cards = get_user_meta( get_current_user_id(), '_ebanx_credit_card_token', true );
foreach ( $cards as $k => $cd ) {
if ( $cd && in_array( $cd->masked_number, WC_EBANX_Request::read( 'credit-card-delete' ) ) ) {
unset( $cards[ $k ] );
}
}
update_user_meta( get_current_user_id(), '_ebanx_credit_card_token', $cards );
}
$cards = array_filter(
(array) get_user_meta( get_current_user_id(), '_ebanx_credit_card_token', true ), function ( $card ) {
return ! empty( $card->brand ) && ! empty( $card->token ) && ! empty( $card->masked_number ); // TODO: Implement token due date.
}
);
wc_get_template(
'my-account/ebanx-credit-cards.php',
array(
'cards' => (array) $cards,
),
'woocommerce/ebanx/',
WC_EBANX::get_templates_path()
);
}
/**
* Mount query vars on my account for credit cards
*
* @param array $vars
* @return array
*/
public function my_account_query_vars( $vars ) {
$vars[] = self::$my_account_endpoint;
return $vars;
}
/**
* It creates a endpoint to my account.
*
* @return void
*/
public function my_account_endpoint() {
// My account endpoint.
add_rewrite_endpoint( self::$my_account_endpoint, EP_ROOT | EP_PAGES );
add_option( 'woocommerce_ebanx-global_settings', WC_EBANX_Global_Gateway::$defaults );
flush_rewrite_rules();
}
/**
* Save some informations from merchant and send to EBANX servers.
*
* @return void
*/
public static function save_merchant_infos() {
// Prevent fatal error if WooCommerce isn't installed.
if ( ! defined( 'WC_VERSION' ) ) {
return;
}
// Save merchant informations.
$user = get_userdata( get_current_user_id() );
if ( ! $user || is_wp_error( $user ) ) {
return;
}
$url = 'https://dashboard.ebanx.com/api/lead';
$args = array(
'body' => array(
'lead' => array(
'user_email' => $user->user_email,
'user_display_name' => $user->display_name,
'user_last_name' => $user->last_name,
'user_first_name' => $user->first_name,
'site_email' => get_bloginfo( 'admin_email' ),
'site_url' => get_bloginfo( 'url' ),
'site_name' => get_bloginfo( 'name' ),
'site_language' => get_bloginfo( 'language' ),
'wordpress_version' => get_bloginfo( 'version' ),
'woocommerce_version' => WC()->version,
'type' => 'Woocommerce',
),
),
);
// Call EBANX API to save a lead.
$request = wp_remote_post( $url, $args );
if ( ! is_wp_error( $request ) && isset( $request['body'] ) ) {
$data = json_decode( $request['body'] );
// Update merchant.
update_option( '_ebanx_lead_id', $data->id, false );
}
}
/**
* A method that will be called every time settings are saved.
*
* @return void
*/
public function on_before_save_settings() {
$this->settings['before'] = $this->configs->settings;
}
/**
* Action triggered on save settings
*/
public function on_save_settings() {
$this->settings['after'] = $this->configs->settings;
delete_option( '_ebanx_api_was_checked' );
// phpcs:disable
do_action( 'ebanx_settings_saved', $_POST );
// phpcs:enable
WC_EBANX_Plugin_Settings_Change_Logger::persist( $this->settings );
$this->settings = [];
}
/**
* Update and inegrate the lead to the merchant using the merchant's integration key.
*
* @return void
*/
public function update_lead() {
$url = 'https://dashboard.ebanx.com/api/lead';
$lead_id = get_option( '_ebanx_lead_id' );
$args = array(
'body' => array(
'lead' => array(
'id' => $lead_id,
'integration_key' => $this->private_key,
'site_url' => get_bloginfo( 'url' ),
'type' => 'Woocommerce',
),
),
);
// Call EBANX API to save a lead.
wp_remote_post( $url, $args );
}
/**
* Method that will be called when plugin is activated.
*
* @return void
*/
public static function activate_plugin() {
self::save_merchant_infos();
self::include_log_classes();
WC_EBANX_Plugin_Activate_Logger::persist();
flush_rewrite_rules();
do_action( 'ebanx_activate_plugin' );
}
/**
* Method that will be called when plugin is deactivated.
*
* @return void
*/
public static function deactivate_plugin() {
flush_rewrite_rules();
self::include_log_classes();
WC_EBANX_Plugin_Deactivate_Logger::persist();
do_action( 'ebanx_deactivate_plugin' );
}
/**
* Method that will be called when plugin is updated.
*
* @param WP_Upgrader $plugin_upgrader
* @param array $data
*/
public function on_update( $plugin_upgrader, $data ) {
$ebanx_path = plugin_basename( __FILE__ );
$ebanx_database = new WC_EBANX_Database();
if ( 'update' === $data['action'] && 'plugin' === $data['type'] ) {
foreach ( $data['plugins'] as $plugin_path ) {
if ( $plugin_path === $ebanx_path ) {
self::save_merchant_infos();
$ebanx_database->migrate();
}
}
}
}
/**
* It enables a tab on WooCommerce My Account page.
*
* @param string $title
* @return string Return the title to show on tab
*/
public function my_account_menus_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ self::$my_account_endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// phpcs:disable
$title = __( self::$my_account_menu_name, 'woocommerce-gateway-ebanx' );
// phpcs:enable
remove_filter( 'the_title', array( $this, 'my_account_menus_title' ) );
}
return $title;
}
/**
* It enalbes the menu as a tab on My Account page.
*
* @param array $menu The all menus supported by WooCoomerce.
* @return array The new menu.
*/
public function my_account_menus( $menu ) {
// Remove the logout menu item.
$logout = $menu['customer-logout'];
unset( $menu['customer-logout'] );
// phpcs:disable
$menu[ self::$my_account_endpoint ] = __( self::$my_account_menu_name, 'woocommerce-gateway-ebanx' );
// phpcs:enable
// Insert back the logout item.
$menu['customer-logout'] = $logout;
return $menu;
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Include log classes.
*/
private static function include_log_classes() {
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-plugin-activate-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-plugin-deactivate-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-plugin-settings-change-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-refund-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-notification-received-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-notification-query-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-payment-by-link-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-checkout-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-subscription-renewal-logger.php';
include_once WC_EBANX_SERVICES_DIR . 'loggers/class-wc-ebanx-cancel-logger.php';
}
/**
* Include all plugin classes.
*/
private function includes() {
// Utils.
include_once WC_EBANX_SERVICES_DIR . 'class-wc-ebanx-request.php';
include_once WC_EBANX_SERVICES_DIR . 'class-wc-ebanx-third-party-compability-layer.php';
// Load plugin log classes.
self::include_log_classes();
// Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-new-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-redirect-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-flow-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-global-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-credit-card-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-wallet-gateway.php';
// Digital Wallets
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-wallet-machpay-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-wallet-mercadopago-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-wallet-nequi-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-wallet-paypal-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-wallet-picpay-gateway.php';
// Chile Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-servipag-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-sencillito-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-webpay-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-multicaja-gateway.php';
// Brazil Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-banking-ticket-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-credit-card-br-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-tef-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-bank-transfer-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-pix-gateway.php';
// Mexico Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-credit-card-mx-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-debit-card-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-oxxo-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-spei-gateway.php';
// Bolivian Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-pagosnet-gateway.php';
// Argentina Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-credit-card-ar-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-efectivo-gateway.php';
// Colombia Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-baloto-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-eft-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-credit-card-co-gateway.php';
// Peru Gateways.
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-pagoefectivo-gateway.php';
include_once WC_EBANX_GATEWAYS_DIR . 'class-wc-ebanx-safetypay-gateway.php';
// Hooks/Actions.
include_once WC_EBANX_SERVICES_DIR . 'class-wc-ebanx-payment-by-link.php';
include_once WC_EBANX_SERVICES_DIR . 'class-wc-ebanx-one-click.php';
// Controllers.
include_once WC_EBANX_CONTROLLERS_DIR . 'class-wc-ebanx-api-controller.php';
// Exceptions.
include_once WC_EBANX_EXCEPTIONS_DIR . 'class-wc-ebanx-payment-exception.php';
}
/**
* Get templates path.
*
* @return string
*/
public static function get_templates_path() {
return WC_EBANX_TEMPLATES_DIR;
}
/**
* Add the gateways to WooCommerce.
*
* @param array $methods WooCommerce payment methods.
*
* @return array
*/
public function add_gateway( $methods ) {
// Global.
$methods[] = 'WC_EBANX_Global_Gateway';
$methods = self::brazil_gateway( $methods );
$methods = self::mexico_gateway( $methods );
$methods = self::bolivia_gateway( $methods );
$methods = self::chile_gateway( $methods );
$methods = self::colombia_gateway( $methods );
$methods = self::peru_gateway( $methods );
$methods = self::argentina_gateway( $methods );
return $methods;
}
/**
* Action links.
*
* @param array $links Plugin links.
*
* @return array
*/
public function plugin_action_links( $links ) {
$plugin_links = array();
$ebanx_global = 'ebanx-global';
$plugin_links[] = '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=' . $ebanx_global ) ) . '">' . __( 'Settings', 'woocommerce-gateway-ebanx' ) . '</a>';
return array_merge( $plugin_links, $links );
}
/**
* WooCommerce fallback notice.
*/
public function woocommerce_missing_notice() {
// TODO: Others notice here.
include_once WC_EBANX_TEMPLATES_DIR . 'views/html-notice-missing-woocommerce.php';
}
/**
* Log messages.
*
* @param string $message The log message.
* @return void
*/
public static function log( $message ) {
$configs = new WC_EBANX_Global_Gateway();
if ( 'yes' !== $configs->settings['debug_enabled'] ) {
return;
}
if ( empty( self::$log ) ) {
self::$log = new WC_Logger();
}
self::$log->add( 'woocommerce-gateway-ebanx', $message );
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( $message );
}
}
/**
* It inserts a EBANX Settings shortcut on WordPress sidebar.
*
* @return void
*/
public function ebanx_sidebar_shortcut() {
add_menu_page(
'EBANX Settings',
'EBANX Settings',
'administrator',
// TODO: Create a dynamic url.
WC_EBANX_Constants::SETTINGS_URL,
'',
WC_EBANX_Assets::get_logo(),
21
);
}
/**
* Checks if this post is an EBANX Order and call WC_EBANX_Payment_By_Link.
*
* @param int $post_id The post id.
* @return void
*/
public function ebanx_metabox_payment_link_save( $post_id ) {
$order = wc_get_order( $post_id );
$checkout_url = get_post_meta( $order->id, '_ebanx_checkout_url', true );
// Check if is an EBANX request.
if ( WC_EBANX_Request::has( 'create_ebanx_payment_link' )
&& WC_EBANX_Request::read( 'create_ebanx_payment_link' ) === __( 'Create EBANX Payment Link', 'woocommerce-gateway-ebanx' )
&& ! $checkout_url ) {
$this->setup_configs();
update_post_meta( $order->get_id(), '_ebanx_instalments', WC_EBANX_Request::read( 'ebanx_instalments', 1 ) );
WC_EBANX_Payment_By_Link::create( $post_id );
}
return;
}
/**
* Checks if the button can be renderized and renders it.
*
* @param int $post_id The post id.
* @return void
*/
public function ebanx_metabox_save_post_render_button( $post_id ) {
$ebanx_currencies = array( 'BRL', 'USD', 'EUR', 'PEN', 'CLP', 'MXN', 'COP' );
$order = wc_get_order( $post_id );
$checkout_url = get_post_meta( $order->get_id(), '_ebanx_checkout_url', true );
if ( ! $checkout_url
&& in_array( $order->get_status(), array( 'auto-draft', 'pending' ) )
&& in_array( strtoupper( get_woocommerce_currency() ), $ebanx_currencies ) ) {
wc_get_template(
'payment-by-link-action.php',
array(),
'woocommerce/ebanx/',
WC_EBANX::get_templates_path()
);
}
}
/**
* It inserts informations about the order on admin order details.
*
* @param WC_Object $order The WC order object.
* @return void
*/
public function ebanx_admin_order_details( $order ) {
$payment_hash = get_post_meta( $order->get_id(), '_ebanx_payment_hash', true );
if ( $payment_hash ) {
wc_get_template(
'admin-order-details.php',
array(
'order' => $order,
'payment_hash' => $payment_hash,
'payment_checkout_url' => get_post_meta( $order->get_id(), '_ebanx_checkout_url', true ),
'is_sandbox_mode' => $this->is_sandbox_mode,
'dashboard_link' => 'https://dashboard.ebanx.com/' . ( $this->is_sandbox_mode ? 'test/' : '' ) . "payments/?hash=$payment_hash",
),
'woocommerce/ebanx/',
WC_EBANX::get_templates_path()
);
}
}
/**
* Get instalments select.
*/
public function get_instalments_admin_html() {
echo '<div class="edit_address">
<p class="form-field form-field-wide">
<label>' . esc_html( __( 'Instalment for EBANX Credit Card:', 'woocommerce-gateway-ebanx' ) ) . '</label>
<select name="ebanx_instalments" id="_payment_method_instalment" class="first">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
</p>
</div>';
}
}
add_action( 'plugins_loaded', array( 'WC_EBANX', 'get_instance' ) );
}