-
Notifications
You must be signed in to change notification settings - Fork 4
/
woocommerce-postfinancecheckout.php
1341 lines (1226 loc) · 39.8 KB
/
woocommerce-postfinancecheckout.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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: PostFinance Checkout
* Plugin URI: https://wordpress.org/plugins/woo-postfinance-checkout
* Description: Process WooCommerce payments with PostFinance Checkout.
* Version: 3.3.2
* Author: postfinancecheckout AG
* Author URI: https://postfinance.ch/en/business/products/e-commerce/postfinance-checkout-all-in-one.html
* Text Domain: postfinancecheckout
* Domain Path: /languages/
* Requires at least: 6.0
* Requires PHP: 7.4
* WC requires at least: 8.0.0
* WC tested up to 9.3.1
* License: Apache 2
* License URI: http://www.apache.org/licenses/LICENSE-2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit(); // Exit if accessed directly.
}
if ( ! class_exists( 'WooCommerce_PostFinanceCheckout' ) ) {
/**
* Main WooCommerce PostFinanceCheckout Class
*
* @class WooCommerce_PostFinanceCheckout
*/
final class WooCommerce_PostFinanceCheckout {
const POSTFINANCECHECKOUT_CK_SPACE_ID = 'wc_postfinancecheckout_space_id';
const POSTFINANCECHECKOUT_CK_SPACE_VIEW_ID = 'wc_postfinancecheckout_space_view_id';
const POSTFINANCECHECKOUT_CK_APP_USER_ID = 'wc_postfinancecheckout_application_user_id';
const POSTFINANCECHECKOUT_CK_APP_USER_KEY = 'wc_postfinancecheckout_application_user_key';
const POSTFINANCECHECKOUT_CK_CUSTOMER_INVOICE = 'wc_postfinancecheckout_customer_invoice';
const POSTFINANCECHECKOUT_CK_CUSTOMER_PACKING = 'wc_postfinancecheckout_customer_packing';
const POSTFINANCECHECKOUT_CK_SHOP_EMAIL = 'wc_postfinancecheckout_shop_email';
const POSTFINANCECHECKOUT_CK_INTEGRATION = 'wc_postfinancecheckout_integration';
const POSTFINANCECHECKOUT_CK_ORDER_REFERENCE = 'wc_postfinancecheckout_order_reference';
const POSTFINANCECHECKOUT_CK_ENFORCE_CONSISTENCY = 'wc_postfinancecheckout_enforce_consistency';
const POSTFINANCECHECKOUT_UPGRADE_VERSION = '3.1.1';
const WC_MAXIMUM_VERSION = '9.3.1';
/**
* WooCommerce PostFinanceCheckout version.
*
* @var string
*/
private $version = '3.3.2';
/**
* The single instance of the class.
*
* @var WooCommerce_PostFinanceCheckout
*/
protected static $instance = null;
/**
* Logger.
*
* @var mixed $logger logger.
*/
private $logger = null;
/**
* Main WooCommerce PostFinanceCheckout Instance.
*
* Ensures only one instance of WooCommerce PostFinanceCheckout is loaded or can be loaded.
*
* @return WooCommerce_PostFinanceCheckout - Main instance.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* WooCommerce PostFinanceCheckout Constructor.
*/
protected function __construct() {
$this->define_constants();
$this->includes();
$this->init_hooks();
}
/**
* Get version.
*/
public function get_version() {
return $this->version;
}
/**
* Define WC PostFinanceCheckout Constants.
*/
protected function define_constants() {
$this->define( 'WC_POSTFINANCECHECKOUT_PLUGIN_FILE', __FILE__ );
$this->define( 'WC_POSTFINANCECHECKOUT_ABSPATH', __DIR__ . '/' );
$this->define( 'WC_POSTFINANCECHECKOUT_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
$this->define( 'WC_POSTFINANCECHECKOUT_VERSION', $this->version );
$this->define( 'WC_POSTFINANCECHECKOUT_REQUIRED_PHP_VERSION', '5.6' );
$this->define( 'WC_POSTFINANCECHECKOUT_REQUIRED_WP_VERSION', '4.7' );
$this->define( 'WC_POSTFINANCECHECKOUT_REQUIRED_WC_VERSION', '3.0' );
$this->define( 'WC_POSTFINANCECHECKOUT_REQUIRED_WC_MAXIMUM_VERSION', self::WC_MAXIMUM_VERSION );
}
/**
* Include required core files used in admin and on the frontend.
*/
protected function includes() {
/**
* Class autoloader.
*/
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-autoloader.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'postfinancecheckout-sdk/autoload.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-migration.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-email.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-return-handler.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-webhook-handler.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-unique-id.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-customer-document.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/class-wc-postfinancecheckout-cron.php';
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/packages/coupon/class-wc-postfinancecheckout-packages-coupon-discount.php';
if ( is_admin() ) {
require_once WC_POSTFINANCECHECKOUT_ABSPATH . 'includes/admin/class-wc-postfinancecheckout-admin.php';
}
}
/**
* Init hooks.
*
* @return void
*/
protected function init_hooks() {
register_activation_hook(
__FILE__,
array(
'WooCommerce_PostFinanceCheckout',
'migrate_plugin_data_on_activation'
)
);
register_activation_hook(
__FILE__,
array(
$this,
'plugin_activate',
)
);
register_deactivation_hook(
__FILE__,
array(
$this,
'plugin_deactivate',
)
);
register_uninstall_hook(
__FILE__,
array(
'WooCommerce_PostFinanceCheckout',
'plugin_uninstall',
)
);
register_activation_hook(
__FILE__,
array(
'WC_PostFinanceCheckout_Migration',
'install_db',
)
);
register_activation_hook(
__FILE__,
array(
'WC_PostFinanceCheckout_Cron',
'activate',
)
);
register_deactivation_hook(
__FILE__,
array(
'WC_PostFinanceCheckout_Cron',
'deactivate',
)
);
// Hook to run migration after plugin update.
add_action(
'upgrader_process_complete',
array(
$this,
'migrate_plugin_data_after_update',
),
10,
2
);
add_action(
'plugins_loaded',
array(
$this,
'loaded',
),
0
);
add_action(
'init',
array(
$this,
'register_order_statuses',
)
);
add_action(
'init',
array(
$this,
'set_device_id_cookie',
)
);
add_action(
'wp_enqueue_scripts',
array(
$this,
'enqueue_javascript_script',
)
);
add_action(
'wp_enqueue_scripts',
array(
$this,
'enqueue_stylesheets',
)
);
add_filter(
'script_loader_tag',
array(
$this,
'set_js_async',
),
20,
3
);
// Endpoints needed for supporting Woocommerce Blocks checkout block.
add_action(
'wp_ajax_is_payment_method_available',
array(
'WC_PostFinanceCheckout_Blocks_Support',
'is_payment_method_available',
)
);
add_action(
'wp_ajax_nopriv_is_payment_method_available',
array(
'WC_PostFinanceCheckout_Blocks_Support',
'is_payment_method_available',
)
);
add_action(
'wp_ajax_get_payment_methods',
array(
'WC_PostFinanceCheckout_Blocks_Support',
'get_payment_methods_json',
)
);
add_action(
'wp_ajax_nopriv_get_payment_methods',
array(
'WC_PostFinanceCheckout_Blocks_Support',
'get_payment_methods_json',
)
);
add_action(
'woocommerce_blocks_enqueue_checkout_block_scripts_after',
array(
'WC_PostFinanceCheckout_Blocks_Support',
'enqueue_portal_scripts',
)
);
add_action(
'woocommerce_rest_checkout_process_payment_with_context',
array(
'WC_PostFinanceCheckout_Blocks_Support',
'process_payment',
),
10,
2
);
// Clear the permalinks after the post type has been registered.
}
/**
* Activation hook.
* Fired when the plugin is activated.
*/
public static function plugin_activate() {
// Clear the permalinks after the post type has been registered.
flush_rewrite_rules();
}
/**
* Deactivation hook.
* Fired when the plugin is deactivated.
*/
public static function plugin_deactivate() {
// Get the plugin version.
$old_plugin_prefix = WC_PostFinanceCheckout_Migration::POSTFINANCECHECKOUT_DEPRECATED_PLUGIN_PREFIX;
$plugin_current_version = self::get_installed_plugin_version( $old_plugin_prefix . 'postfinancecheckout' ); // The slug of the old plugin.
// Check if the plugin version is lower than 3.1.0.
if ( version_compare( $plugin_current_version, self::POSTFINANCECHECKOUT_UPGRADE_VERSION, '<' ) ) {
// Start output buffering to prevent "headers already sent" errors.
ob_start();
}
// Hook to run migration after plugin update.
add_action(
'upgrader_process_complete',
array(
'WooCommerce_PostFinanceCheckout',
'migrate_plugin_data_after_update',
),
10,
2
);
// Clear the permalinks to remove our post type's rules from the database.
flush_rewrite_rules();
}
/**
* Uninstall hook.
* Fired when the plugin is uninstalled.
*/
public static function plugin_uninstall() {
// code to run on plugin uninstall.
// delete the registered options.
}
/**
* Function to get the installed version of a plugin.
*
* @param string $plugin_slug The slug of the plugin.
* @return string|null The version of the plugin or null if not found.
*/
public static function get_installed_plugin_version( $plugin_slug ) {
if (!function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$all_plugins = get_plugins();
foreach ( $all_plugins as $plugin_path => $plugin_info ) {
if ( strpos( $plugin_path, $plugin_slug ) !== false ) {
return $plugin_info[ 'Version' ];
}
}
return null;
}
/**
* Security check in the thank you page.
*
* Note: If for some reason order status is still pending, it will redirect you to the payment form.
*
* @param int $order_id Order id.
*/
public function secure_redirect_order_confirmed( $order_id ) {
$order = wc_get_order( $order_id );
$wc_service_transaction = WC_PostFinanceCheckout_Service_Transaction::instance();
$sdk_service_transaction = new \PostFinanceCheckout\Sdk\Service\TransactionService( WC_PostFinanceCheckout_Helper::instance()->get_api_client() );
$wc_transaction_info = WC_PostFinanceCheckout_Entity_Transaction_Info::load_by_order_id( $order_id );
if ( property_exists( $wc_transaction_info, 'get_transaction_id' ) ) {
$state = $sdk_service_transaction->read( get_option( self::POSTFINANCECHECKOUT_CK_SPACE_ID ), $wc_transaction_info->get_transaction_id() )->getState();
if ( \PostFinanceCheckout\Sdk\Model\TransactionState::CONFIRMED === $state ) {
wp_redirect($wc_service_transaction->get_payment_page_url(get_option(self::POSTFINANCECHECKOUT_CK_SPACE_ID), $wc_transaction_info->get_transaction_id())); //phpcs:ignore
exit;
}
}
}
/**
* Migrates settings from the old plugin version to the new version 4.0.0.
*
* This function checks if the settings from the old plugin version exist.
* If they do, it transfers these settings to the new plugin's options
* and deletes the old settings from the database.
*
* This ensures that users retain their configurations when updating
* from the old plugin version to the new version.
*
* @return void
*/
public static function migrate_plugin_data_on_activation() {
$old_option_prefix = WC_PostFinanceCheckout_Migration::POSTFINANCECHECKOUT_DEPRECATED_TABLE_PREFIX;
$old_plugin_prefix = WC_PostFinanceCheckout_Migration::POSTFINANCECHECKOUT_DEPRECATED_PLUGIN_PREFIX;
$plugin_slug = $old_plugin_prefix . 'postfinancecheckout'; // The slug of the old plugin.
$installed_version = self::get_installed_plugin_version( $plugin_slug );
/**
* Check if the installed version is 3.1.1 or higher.
* If the version is 3.1.1 or higher, do not run the migration.
*/
if ( version_compare( $installed_version, self::POSTFINANCECHECKOUT_UPGRADE_VERSION, '>=' ) ) {
return;
}
global $wpdb;
$options_to_migrate = [
$old_option_prefix . WC_PostFinanceCheckout_Migration::POSTFINANCECHECKOUT_CK_DB_VERSION,
$old_option_prefix . WC_PostFinanceCheckout_Service_Manual_Task::POSTFINANCECHECKOUT_CONFIG_KEY,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_SPACE_ID,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_SPACE_VIEW_ID,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_APP_USER_ID,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_APP_USER_KEY,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_CUSTOMER_INVOICE,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_CUSTOMER_PACKING,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_SHOP_EMAIL,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_INTEGRATION,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_ORDER_REFERENCE,
$old_option_prefix . self::POSTFINANCECHECKOUT_CK_ENFORCE_CONSISTENCY,
$old_option_prefix . self::WC_MAXIMUM_VERSION,
];
// If the old plugin options exist, perform the migration
foreach ( $options_to_migrate as $option_name ) {
$option_value = get_option( $option_name );
if ( $option_value !== false ) {
// Rename the options to the new prefix 'postfinancecheckout_'.
$new_option_name = str_replace( $old_option_prefix . 'postfinancecheckout_', 'postfinancecheckout_', $option_name );
if ( get_option( $new_option_name ) !== false ) {
// Update the option if it already exists.
update_option( $new_option_name, $option_value );
} else {
// Add the option if it doesn't exist.
add_option( $new_option_name, $option_value );
}
// Delete the old option.
//delete_option( $option_name );.
}
}
}
/**
* Function to migrate plugin data after update
* This function is triggered after the plugin is updated
*
* @param object $upgrader_object The upgrader object.
* @param array $options The options array.
*/
public static function migrate_plugin_data_after_update( $upgrader_object, $options ) {
// Check if the plugin was just updated.
if ( $options['action'] == 'update' && $options['type'] == 'plugin' ) {
$plugin_basename = plugin_basename( __FILE__ );
foreach ( $options['plugins'] as $plugin ) {
if ( $plugin == $plugin_basename ) {
self::migrate_plugin_data_on_activation();
break;
}
}
}
}
/**
* Load Localization files.
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present.
*
* Locales found in:
* - WP_LANG_DIR/woo-postfinancecheckout/woo-postfinancecheckout-LOCALE.mo
*/
public function load_plugin_textdomain() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'woo-postfinancecheckout' );
load_textdomain( 'woo-postfinancecheckout', WP_LANG_DIR . '/woo-postfinancecheckout/woo-postfinancecheckout' . $locale . '.mo' );
load_plugin_textdomain( 'woo-postfinancecheckout', false, plugin_basename( __DIR__ ) . '/languages' );
}
/**
* Init WooCommerce PostFinanceCheckout when plugins are loaded.
*/
public function loaded() {
// Set up localisation.
$this->load_plugin_textdomain();
add_filter(
'woocommerce_payment_gateways',
array(
$this,
'add_gateways',
)
);
add_filter(
'wc_order_statuses',
array(
$this,
'add_order_statuses',
)
);
add_filter(
'wc_order_is_editable',
array(
$this,
'order_editable_check',
),
10,
2
);
add_filter(
'woocommerce_before_calculate_totals',
array(
$this,
'before_calculate_totals',
),
10
);
add_filter(
'woocommerce_after_calculate_totals',
array(
$this,
'after_calculate_totals',
),
10
);
add_filter(
'woocommerce_valid_order_statuses_for_payment_complete',
array(
$this,
'valid_order_status_for_completion',
),
10,
2
);
add_filter(
'woocommerce_form_field_args',
array(
$this,
'modify_form_fields_args',
),
10,
3
);
add_action(
'woocommerce_checkout_update_order_review',
array(
$this,
'update_additional_customer_data',
)
);
add_action(
'woocommerce_before_checkout_form',
array(
$this,
'register_checkout_error_msg',
),
5,
0
);
add_action(
'before_woocommerce_pay',
array(
$this,
'show_checkout_error_msg',
),
5,
0
);
// woocommerce_after_checkout_form is used by the legacy checkout.
add_action(
'woocommerce_after_checkout_form',
array(
$this,
'show_checkout_error_msg',
),
5,
0
);
// pre_render_block is used by the new Woocomerce Blocks.
add_filter(
'pre_render_block',
array(
$this,
'pre_render_block',
),
5,
2
);
add_action(
'woocommerce_attribute_added',
array(
$this,
'woocommerce_attribute_added',
),
10,
2
);
add_action(
'woocommerce_attribute_updated',
array(
$this,
'woocommerce_attribute_updated',
),
10,
3
);
add_action(
'woocommerce_attribute_deleted',
array(
$this,
'woocommerce_attribute_deleted',
),
10,
3
);
add_action(
'woocommerce_rest_insert_product_attribute',
array(
$this,
'woocommerce_rest_insert_product_attribute',
),
10,
3
);
add_action(
'woocommerce_cart_item_removed',
array(
$this,
'after_remove_product_from_cart',
),
10,
2
);
add_filter(
'woocommerce_rest_prepare_product_attribute',
array(
$this,
'woocommerce_rest_prepare_product_attribute',
),
10,
3
);
add_filter(
'nocache_headers',
array(
$this,
'add_cache_no_store',
),
10,
1
);
add_filter(
'woocommerce_valid_order_statuses_for_payment',
array(
$this,
'valid_order_statuses_for_payment',
),
10,
2
);
add_action(
'before_woocommerce_init',
function () {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
);
add_filter('the_content', function($content) {
if (is_checkout()) {
// When in checkout, we inject the list of payment methods in the HTML.
// The goal here is to speed up the process of registering the payment methods.
$payment_methods = WC_PostFinanceCheckout_Blocks_Support::get_payment_methods();
$json_data = json_encode( $payment_methods );
$content .= '<div id="whitelabel-payment-methods" data-json="' . esc_attr($json_data) . '"></div>';
}
return $content;
});
}
/**
* After remove product from cart.
*
* @param mixed $removed_cart_item_key removed product from cart.
* @param mixed $cart cart.
* @return void
*/
public function after_remove_product_from_cart( $removed_cart_item_key, $cart ) {
$line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
$product = wc_get_product( $line_item['product_id'] );
if ( $this->is_product_type_subscription( $product ) ) {
// create new transaction in portal by clearing transaction cache.
$service_transaction = WC_PostFinanceCheckout_Service_Transaction::instance();
$service_transaction->clear_transaction_cache();
}
}
/**
* Is product type subscription.
*
* @param mixed $product product.
* @return bool
*/
public function is_product_type_subscription( $product ) {
if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
return true;
}
return false;
}
/**
* Register order statuses.
*
* @return void
*/
public function register_order_statuses() {
register_post_status(
'wc-postfi-redirected',
array(
'label' => 'Processing',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: replaces string */
'label_count' => _n_noop( 'PostFinance Checkout Processing <span class="count">(%s)</span>', 'PostFinance Checkout Processing <span class="count">(%s)</span>', 'woo-postfinancecheckout' ),
)
);
register_post_status(
'wc-postfi-waiting',
array(
'label' => 'Waiting',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: replaces string */
'label_count' => _n_noop( 'Waiting <span class="count">(%s)</span>', 'Waiting <span class="count">(%s)</span>', 'woo-postfinancecheckout' ),
)
);
register_post_status(
'wc-postfi-manual',
array(
'label' => 'Manual Decision',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: replaces string */
'label_count' => _n_noop( 'Manual Decision <span class="count">(%s)</span>', 'Manual Decision <span class="count">(%s)</span>', 'woo-postfinancecheckout' ),
)
);
}
/**
* Add order statuses.
*
* @param mixed $order_statuses order statuses.
* @return mixed
*/
public function add_order_statuses( $order_statuses ) {
$order_statuses['wc-postfi-redirected'] = _x( 'Redirected', 'Order status', 'woocommerce' );
$order_statuses['wc-postfi-waiting'] = _x( 'Waiting', 'Order status', 'woocommerce' );
$order_statuses['wc-postfi-manual'] = _x( 'Manual Decision', 'Order status', 'woocommerce' );
return $order_statuses;
}
/**
* Valid order statuses for payment.
*
* @param mixed $statuses statuses.
* @param mixed $order order.
* @return mixed
*/
public function valid_order_statuses_for_payment( $statuses, $order = null ) { //phpcs:ignore
$statuses[] = 'postfi-redirected';
return $statuses;
}
/**
* Set device id cookie.
*
* @return void
*/
public function set_device_id_cookie() {
$value = WC_PostFinanceCheckout_Unique_Id::get_uuid();
if ( isset( $_COOKIE['wc_postfinancecheckout_device_id'] ) && ! empty( $_COOKIE['wc_postfinancecheckout_device_id'] ) ) {
$value = sanitize_text_field( wp_unslash( $_COOKIE['wc_postfinancecheckout_device_id'] ) );
}
setcookie( 'wc_postfinancecheckout_device_id', $value, time() + YEAR_IN_SECONDS, '/' );
}
/**
* Set JS async.
*
* @param mixed $tag tag.
* @param mixed $handle handle.
* @param mixed $src src.
* @return array|mixed|string|string[]
*/
public function set_js_async( $tag, $handle, $src ) { //phpcs:ignore
$async_script_handles = array( 'postfinancecheckout-device-id-js' );
foreach ( $async_script_handles as $async_handle ) {
if ( $async_handle === $handle ) {
return str_replace( ' src', ' async="async" src', $tag );
}
}
return $tag;
}
/**
* Enqueue javascript script.
*
* @return void
* @throws Exception Exception.
*/
public function enqueue_javascript_script() {
if ( is_cart() || is_checkout() ) {
$unique_id = isset( $_COOKIE['wc_postfinancecheckout_device_id'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['wc_postfinancecheckout_device_id'] ) ) : null;
$space_id = get_option( self::POSTFINANCECHECKOUT_CK_SPACE_ID );
$script_url = WC_PostFinanceCheckout_Helper::instance()->get_base_gateway_url() . 's/' .
$space_id . '/payment/device.js?sessionIdentifier=' .
$unique_id;
wp_enqueue_script( 'postfinancecheckout-device-id-js', $script_url, array(), $this->get_version(), true );
}
}
/**
* Enqueue stylesheets.
*
* @return void
*/
public function enqueue_stylesheets() {
if ( is_checkout() ) {
wp_enqueue_style( 'postfinancecheckout-checkout-css', $this->plugin_url() . '/assets/css/checkout.css', array(), $this->get_version() );
}
}
/**
* Order editable check.
*
* @param mixed $allowed allowed.
* @param WC_Order|null $order order.
* @return false|mixed
*/
public function order_editable_check( $allowed, WC_Order $order = null ) {
if ( is_null( $order ) ) {
return $allowed;
}
if ( $order->get_meta( '_postfinancecheckout_authorized', true ) ) {
return false;
}
return $allowed;
}
/**
* Valid order status for completion.
*
* @param mixed $statuses statuses.
* @param WC_Order|null $order order.
* @return mixed
*/
public function valid_order_status_for_completion( $statuses, WC_Order $order = null ) { //phpcs:ignore
$statuses[] = 'postfi-waiting';
$statuses[] = 'postfi-manual';
$statuses[] = 'postfi-redirected';
return $statuses;
}
/**
* Before calculate totals.
*
* @param mixed $cart cart.
* @return void
*/
public function before_calculate_totals( $cart ) { //phpcs:ignore
$GLOBALS['_wc_postfinancecheckout_calculating'] = true;
}
/**
* After calculate totals.
*
* @param mixed $cart cart.
* @return void
*/
public function after_calculate_totals( $cart ) { //phpcs:ignore
unset( $GLOBALS['_wc_postfinancecheckout_calculating'] );
}
/**
* Add gateways.
*
* @param mixed $methods methods.
* @return mixed
*/
public function add_gateways( $methods ) {
$space_id = get_option( self::POSTFINANCECHECKOUT_CK_SPACE_ID );
$method_configurations = WC_PostFinanceCheckout_Entity_Method_Configuration::load_by_states_and_space_id(
$space_id,
array(
WC_PostFinanceCheckout_Entity_Method_Configuration::POSTFINANCECHECKOUT_STATE_ACTIVE,
WC_PostFinanceCheckout_Entity_Method_Configuration::POSTFINANCECHECKOUT_STATE_INACTIVE,
)
);
try {
foreach ( $method_configurations as $configuration ) {
$gateway = new WC_PostFinanceCheckout_Gateway( $configuration );
$methods[] = apply_filters( 'wc_postfinancecheckout_enhance_gateway', $gateway );
}
} catch ( \PostFinanceCheckout\Sdk\ApiException $e ) {
if ( $e->getCode() === 401 ) {
// Ignore it because we simply are not allowed to access the API.
return $methods;
} else {
$this->log( $e->getMessage(), WC_Log_Levels::CRITICAL );
}
}
return $methods;
}
/**
* Modify form fields args.
*
* @param mixed $arguments arguments.
* @param mixed $key key.
* @param mixed $value calue.
* @return array
*/
public function modify_form_fields_args( $arguments, $key, $value = null ) { //phpcs:ignore
if ( 'billing_company' === $key ) {
$arguments['class'][] = 'address-field';
}
if ( 'billing_email' === $key ) {
$arguments['class'][] = 'address-field';
}
if ( 'billing_phone' === $key ) {
$arguments['class'][] = 'address-field';
}
if ( 'billing_first_name' === $key ) {
$arguments['class'][] = 'address-field';
}
if ( 'billing_last_name' === $key ) {
$arguments['class'][] = 'address-field';
}
if ( 'shipping_first_name' === $key ) {
$arguments['class'][] = 'address-field';
}
if ( 'shipping_last_name' === $key ) {
$arguments['class'][] = 'address-field';
}
return $arguments;
}
/**
* Update additional customer data.
*
* @param mixed $arguments arguments.
* @return void
*/
public function update_additional_customer_data( $arguments ) {
$post_data = array();
if ( ! empty( $arguments ) ) {
wp_parse_str( $arguments, $post_data );
}
WC()->customer->set_props(
array(
'billing_first_name' => isset( $post_data['billing_first_name'] ) ? sanitize_text_field( wp_unslash( $post_data['billing_first_name'] ) ) : null,
'billing_last_name' => isset( $post_data['billing_last_name'] ) ? sanitize_text_field( wp_unslash( $post_data['billing_last_name'] ) ) : null,
'billing_company' => isset( $post_data['billing_company'] ) ? sanitize_text_field( wp_unslash( $post_data['billing_company'] ) ) : null,
'billing_phone' => isset( $post_data['billing_phone'] ) ? sanitize_text_field( wp_unslash( $post_data['billing_phone'] ) ) : null,
'billing_email' => isset( $post_data['billing_email'] ) && is_email( wp_unslash( $post_data['billing_email'] ) ) ? sanitize_email( wp_unslash( $post_data['billing_email'] ) ) : null,
)