-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathwoocommerce-gateway-checkout-com.php
More file actions
2207 lines (1869 loc) · 85.2 KB
/
woocommerce-gateway-checkout-com.php
File metadata and controls
2207 lines (1869 loc) · 85.2 KB
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: Checkout.com Payment Gateway
* Plugin URI: https://www.checkout.com/
* Description: Extends WooCommerce by Adding the Checkout.com Gateway.
* Author: Checkout.com
* Author URI: https://www.checkout.com/
* Version: 5.1.2
* Requires at least: 5.0
* Tested up to: 6.7.0
* WC requires at least: 3.0
* WC tested up to: 8.3.1
* Requires PHP: 7.3
* Text Domain: checkout-com-unified-payments-api
* Domain Path: /languages
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* IMPORTANT: Plugin Update Compatibility
* WordPress identifies plugins by these three identifiers (must match existing installation):
* 1. Plugin folder name: checkout-com-unified-payments-api
* 2. Main plugin file: woocommerce-gateway-checkout-com.php
* 3. Plugin Name header: Checkout.com Payment Gateway
*
* These identifiers ensure the plugin updates over existing installations instead of creating duplicates.
*
* @package wc_checkout_com
*/
// use Checkout\CheckoutUtils;
// use Checkout\Payments\PaymentType;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Load Composer autoloader for Checkout.com SDK
$autoloader_path = __DIR__ . '/vendor/autoload.php';
if ( file_exists( $autoloader_path ) ) {
require_once $autoloader_path;
// Verify SDK classes are available (only log if WP_DEBUG is enabled)
if ( ! class_exists( 'Checkout\CheckoutSdk' ) && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Checkout.com SDK classes not loaded after autoloader inclusion. Path: ' . esc_html( $autoloader_path ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Checkout.com SDK autoloader not found at: ' . esc_html( $autoloader_path ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
// Load utility class early (needed for logging in early hooks)
$utility_path = __DIR__ . '/includes/api/class-wc-checkoutcom-utility.php';
if ( file_exists( $utility_path ) ) {
require_once $utility_path;
}
add_filter( 'woocommerce_checkout_registration_enabled', '__return_true' );
/**
* Handler for cleanup of old webhooks.
*/
function cko_cleanup_old_webhooks_handler() {
if ( class_exists( 'WC_Checkout_Com_Webhook_Queue' ) ) {
// Cleanup processed webhooks older than 7 days
WC_Checkout_Com_Webhook_Queue::cleanup_old_webhooks( 7 );
// Cleanup unprocessed webhooks older than 7 days (orphaned)
WC_Checkout_Com_Webhook_Queue::cleanup_old_unprocessed_webhooks( 7 );
}
}
/**
* Force Flow gateway to be available if checkout mode is 'flow'.
*
* @param array $available_gateways Available payment gateways.
* @return array Filtered available payment gateways.
*/
function cko_force_flow_gateway_available( $available_gateways ) {
// Process on checkout page, order-pay page, and during checkout processing (when POST data exists)
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified by WooCommerce checkout
$payment_method = isset( $_POST['payment_method'] ) ? sanitize_text_field( wp_unslash( $_POST['payment_method'] ) ) : '';
$is_checkout_context = is_checkout() || is_wc_endpoint_url( 'order-pay' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( 'wc_checkout_com_flow' === $payment_method );
if ( $is_checkout_context ) {
$checkout_setting = get_option( 'woocommerce_wc_checkout_com_cards_settings', array() );
$checkout_mode = isset( $checkout_setting['ckocom_checkout_mode'] ) ? $checkout_setting['ckocom_checkout_mode'] : 'classic';
if ( 'flow' === $checkout_mode ) {
// Ensure Flow gateway is in the list if checkout mode is 'flow' and gateway is enabled
$all_gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $all_gateways['wc_checkout_com_flow'] ) ) {
$flow_gateway = $all_gateways['wc_checkout_com_flow'];
if ( isset( $flow_gateway->enabled ) && 'yes' === $flow_gateway->enabled ) {
$available_gateways['wc_checkout_com_flow'] = $flow_gateway;
}
}
}
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'cko_force_flow_gateway_available', 1 );
/**
* Backup filter to ensure Flow gateway is added AFTER WooCommerce's internal filtering.
*
* @param array $available_gateways Available payment gateways.
* @return array Filtered available payment gateways.
*/
function cko_backup_force_flow_gateway_available( $available_gateways ) {
// Only process if checkout mode is 'flow' and gateway is not already in list
$checkout_setting = get_option( 'woocommerce_wc_checkout_com_cards_settings', array() );
$checkout_mode = isset( $checkout_setting['ckocom_checkout_mode'] ) ? $checkout_setting['ckocom_checkout_mode'] : 'classic';
if ( 'flow' === $checkout_mode && ! isset( $available_gateways['wc_checkout_com_flow'] ) ) {
$all_gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $all_gateways['wc_checkout_com_flow'] ) ) {
$flow_gateway = $all_gateways['wc_checkout_com_flow'];
if ( isset( $flow_gateway->enabled ) && 'yes' === $flow_gateway->enabled ) {
$available_gateways['wc_checkout_com_flow'] = $flow_gateway;
}
}
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'cko_backup_force_flow_gateway_available', 999 );
/**
* Update order ID in session after order creation for Classic Cards payments.
*
* @param int $order_id Order ID.
*/
function cko_update_order_id_in_session( $order_id ) {
// Only apply to Classic Cards payment method
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified by WooCommerce checkout
$payment_method = isset( $_POST['payment_method'] ) ? sanitize_text_field( wp_unslash( $_POST['payment_method'] ) ) : '';
if ( 'wc_checkout_com_cards' !== $payment_method ) {
return;
}
// Check if there's a different order ID in session (meaning order was replaced)
if ( WC()->session ) {
$session_order_id = WC()->session->get( 'order_awaiting_payment' );
if ( ! empty( $session_order_id ) && $session_order_id != $order_id ) {
WC_Checkoutcom_Utility::logger( '[CLASSIC CARDS] Order ID mismatch detected - Session: ' . $session_order_id . ', New Order: ' . $order_id . ' - Order was likely replaced with existing one' );
}
}
}
add_action( 'woocommerce_new_order', 'cko_update_order_id_in_session', 5 );
/**
* Constants.
*/
define( 'WC_CHECKOUTCOM_PLUGIN_VERSION', '5.1.2' );
define( 'WC_CHECKOUTCOM_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
define( 'WC_CHECKOUTCOM_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
/**
* This function registers our PHP class as a WooCommerce payment gateway.
*/
if ( ! function_exists( 'init_checkout_com_gateway_class' ) ) {
add_action( 'plugins_loaded', 'init_checkout_com_gateway_class', 0 );
function init_checkout_com_gateway_class() {
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
load_plugin_textdomain( 'checkout-com-unified-payments-api', false, plugin_basename( __DIR__ ) . '/languages' );
// Core payment gateway classes
include_once 'includes/class-wc-checkout-com-webhook.php';
include_once 'includes/class-wc-checkout-com-webhook-queue.php';
// Admin pages
if ( is_admin() ) {
include_once 'includes/admin/class-wc-checkoutcom-webhook-queue-admin.php';
include_once 'includes/admin/class-wc-checkoutcom-diagnostics.php';
include_once 'includes/admin/class-wc-checkoutcom-order-columns.php';
}
// Note: You can also access the webhook queue table directly using:
// - view-webhook-queue.php script (command line or browser)
// - WordPress Admin: WooCommerce > Webhook Queue
// - Direct SQL queries to wp_cko_pending_webhooks table
// Create webhook queue table if it doesn't exist
if ( class_exists( 'WC_Checkout_Com_Webhook_Queue' ) ) {
WC_Checkout_Com_Webhook_Queue::create_table();
// Schedule cleanup of old webhooks (daily)
if ( ! wp_next_scheduled( 'cko_cleanup_old_webhooks' ) ) {
wp_schedule_event( time(), 'daily', 'cko_cleanup_old_webhooks' );
}
}
// Hook for cleanup of old webhooks
add_action( 'cko_cleanup_old_webhooks', 'cko_cleanup_old_webhooks_handler' );
include_once 'includes/class-wc-gateway-checkout-com-cards.php';
include_once 'includes/class-wc-gateway-checkout-com-apple-pay.php';
// Register Apple Pay CSR generation AJAX handler early
// Use a wrapper function to ensure it's always available
if ( class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
add_action( 'wp_ajax_cko_generate_apple_pay_csr', 'cko_ajax_generate_apple_pay_csr' );
add_action( 'wp_ajax_cko_upload_apple_pay_certificate', 'cko_ajax_upload_apple_pay_certificate' );
add_action( 'wp_ajax_cko_generate_apple_pay_merchant_certificate', 'cko_ajax_generate_apple_pay_merchant_certificate' );
add_action( 'wp_ajax_cko_upload_apple_pay_domain_association', 'cko_ajax_upload_apple_pay_domain_association' );
add_action( 'wp_ajax_cko_generate_apple_pay_merchant_identity_csr', 'cko_ajax_generate_apple_pay_merchant_identity_csr' );
add_action( 'wp_ajax_cko_upload_apple_pay_merchant_identity_certificate', 'cko_ajax_upload_apple_pay_merchant_identity_certificate' );
add_action( 'wp_ajax_cko_test_apple_pay_certificate', 'cko_ajax_test_apple_pay_certificate' );
}
include_once 'includes/class-wc-gateway-checkout-com-google-pay.php';
include_once 'includes/class-wc-gateway-checkout-com-paypal.php';
include_once 'includes/class-wc-gateway-checkout-com-alternative-payments.php';
// Flow integration
include_once 'flow-integration/class-wc-gateway-checkout-com-flow.php';
// Unified Express Checkout Element Handler
include_once 'includes/express/class-wc-checkoutcom-express-checkout-element.php';
// Initialize unified express checkout element
if ( class_exists( 'WC_Checkoutcom_Express_Checkout_Element' ) ) {
$express_checkout_element = new WC_Checkoutcom_Express_Checkout_Element();
$express_checkout_element->init();
}
// Enhanced logging classes (commented out temporarily)
// include_once 'includes/logging/class-wc-checkoutcom-enhanced-logger.php';
// include_once 'includes/logging/class-wc-checkoutcom-log-manager.php';
// include_once 'includes/logging/class-wc-checkoutcom-performance-monitor.php';
// include_once 'includes/settings/class-wc-checkoutcom-logging-settings.php';
// Initialize enhanced logging (commented out temporarily)
// WC_Checkoutcom_Logging_Settings::init();
// WooCommerce Blocks integration (safe/conditional)
// Check for safe version first, fallback to regular version
$blocks_safe_file = __DIR__ . '/includes/blocks/class-wc-checkoutcom-blocks-integration-safe.php';
$blocks_file = __DIR__ . '/includes/blocks/class-wc-checkoutcom-blocks-integration.php';
if ( file_exists( $blocks_safe_file ) ) {
include_once $blocks_safe_file;
} elseif ( file_exists( $blocks_file ) ) {
include_once $blocks_file;
} else {
// Log error but don't break the site - Blocks integration is optional
WC_Checkoutcom_Utility::logger( 'Checkout.com Blocks integration file not found.' );
}
// Load payment gateway class.
add_filter( 'woocommerce_payment_gateways', 'cko_checkout_com_add_gateway' );
}
}
/**
* Make billing details read-only on order-pay page
* This ensures customers can't modify billing information that was set when the order was created
*/
if ( ! function_exists( 'cko_make_order_pay_billing_readonly' ) ) {
add_action( 'wp_enqueue_scripts', 'cko_make_order_pay_billing_readonly' );
function cko_make_order_pay_billing_readonly() {
// Only on order-pay page
if ( ! is_wc_endpoint_url( 'order-pay' ) ) {
return;
}
// Add CSS to make billing details read-only
wp_add_inline_style( 'woocommerce-general', '
.woocommerce-order-pay #billing_first_name,
.woocommerce-order-pay #billing_last_name,
.woocommerce-order-pay #billing_company,
.woocommerce-order-pay #billing_address_1,
.woocommerce-order-pay #billing_address_2,
.woocommerce-order-pay #billing_city,
.woocommerce-order-pay #billing_state,
.woocommerce-order-pay #billing_postcode,
.woocommerce-order-pay #billing_country,
.woocommerce-order-pay #billing_phone,
.woocommerce-order-pay #billing_email,
.woocommerce-order-pay .woocommerce-billing-fields input,
.woocommerce-order-pay .woocommerce-billing-fields select,
.woocommerce-order-pay .woocommerce-billing-fields textarea {
background-color: #f9f9f9 !important;
border: 1px solid #ddd !important;
color: #666 !important;
cursor: not-allowed !important;
pointer-events: none !important;
opacity: 0.7 !important;
}
.woocommerce-order-pay .woocommerce-billing-fields label {
color: #666 !important;
font-weight: normal !important;
}
.woocommerce-order-pay .woocommerce-billing-fields h3::after {
content: " (from your order - read only)";
font-size: 12px;
font-weight: normal;
color: #999;
}
' );
// Add JavaScript to disable billing fields
wp_add_inline_script( 'wc-checkout', '
jQuery(document).ready(function($) {
console.log("🔥🔥🔥 [CKO DEBUG] Order-pay page detected - making billing fields read-only 🔥🔥🔥");
// Disable all billing fields
var billingFields = $("#billing_first_name, #billing_last_name, #billing_company, #billing_address_1, #billing_address_2, #billing_city, #billing_state, #billing_postcode, #billing_country, #billing_phone, #billing_email");
console.log("[CKO DEBUG] Found billing fields:", billingFields.length);
billingFields.prop("disabled", true);
// Also disable any selects in billing fields
var billingSelects = $(".woocommerce-billing-fields select");
console.log("[CKO DEBUG] Found billing selects:", billingSelects.length);
billingSelects.prop("disabled", true);
console.log("[CKO DEBUG] Billing fields disabled successfully on order-pay page");
});
' );
}
}
/**
* Declare compatibility with HPOS.
* https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book#declaring-extension-incompatibility
*/
add_action(
'before_woocommerce_init',
function () {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
);
/**
* AJAX handler wrapper for Apple Pay CSR generation.
* This ensures the handler is always available, even if the gateway class isn't fully instantiated.
*/
if ( ! function_exists( 'cko_ajax_generate_apple_pay_csr' ) ) {
function cko_ajax_generate_apple_pay_csr() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
// Get the gateway instance
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_generate_csr();
} else {
// Fallback: create a temporary instance
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_generate_csr();
}
}
}
/**
* AJAX handler wrapper for Apple Pay certificate upload.
* This ensures the handler is always available, even if the gateway class isn't fully instantiated.
*/
if ( ! function_exists( 'cko_ajax_upload_apple_pay_certificate' ) ) {
function cko_ajax_upload_apple_pay_certificate() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
// Get the gateway instance
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_upload_certificate();
} else {
// Fallback: create a temporary instance
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_upload_certificate();
}
}
}
/**
* AJAX handler wrapper for Apple Pay merchant certificate generation.
* This ensures the handler is always available, even if the gateway class isn't fully instantiated.
*/
if ( ! function_exists( 'cko_ajax_generate_apple_pay_merchant_certificate' ) ) {
function cko_ajax_generate_apple_pay_merchant_certificate() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
// Get the gateway instance
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_generate_merchant_certificate();
} else {
// Fallback: create a temporary instance
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_generate_merchant_certificate();
}
}
}
/**
* AJAX handler wrapper for Apple Pay domain association upload.
*/
if ( ! function_exists( 'cko_ajax_upload_apple_pay_domain_association' ) ) {
function cko_ajax_upload_apple_pay_domain_association() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_upload_domain_association();
} else {
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_upload_domain_association();
}
}
}
/**
* AJAX handler wrapper for Apple Pay merchant identity CSR generation.
*/
if ( ! function_exists( 'cko_ajax_generate_apple_pay_merchant_identity_csr' ) ) {
function cko_ajax_generate_apple_pay_merchant_identity_csr() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_generate_merchant_identity_csr();
} else {
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_generate_merchant_identity_csr();
}
}
}
/**
* AJAX handler wrapper for Apple Pay merchant identity certificate upload.
*/
if ( ! function_exists( 'cko_ajax_upload_apple_pay_merchant_identity_certificate' ) ) {
function cko_ajax_upload_apple_pay_merchant_identity_certificate() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_upload_merchant_identity_certificate();
} else {
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_upload_merchant_identity_certificate();
}
}
}
/**
* AJAX handler wrapper for Apple Pay certificate testing.
*/
if ( ! function_exists( 'cko_ajax_test_apple_pay_certificate' ) ) {
function cko_ajax_test_apple_pay_certificate() {
// Capability check for admin-only AJAX handler
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( [
'message' => __( 'Permission denied.', 'checkout-com-unified-payments-api' ),
] );
return;
}
if ( ! class_exists( 'WC_Gateway_Checkout_Com_Apple_Pay' ) ) {
wp_send_json_error( [
'message' => __( 'Apple Pay gateway class not found.', 'checkout-com-unified-payments-api' ),
] );
return;
}
$gateways = WC()->payment_gateways()->payment_gateways();
if ( isset( $gateways['wc_checkout_com_apple_pay'] ) ) {
$gateways['wc_checkout_com_apple_pay']->ajax_test_certificate();
} else {
$gateway = new WC_Gateway_Checkout_Com_Apple_Pay();
$gateway->ajax_test_certificate();
}
}
}
/**
* Add payment method class to WooCommerce.
*
* @param array $methods Array of payment methods.
*
* @return array
*/
function cko_checkout_com_add_gateway( $methods ) {
$array = cko_get_selected_apms_class();
$methods[] = 'WC_Gateway_Checkout_Com_Cards';
$methods[] = 'WC_Gateway_Checkout_Com_Apple_Pay';
$methods[] = 'WC_Gateway_Checkout_Com_Google_Pay';
$methods[] = 'WC_Gateway_Checkout_Com_PayPal';
$methods[] = 'WC_Gateway_Checkout_Com_Alternative_Payments';
$methods[] = 'WC_Gateway_Checkout_Com_Flow';
$methods = sizeof( $array ) > 0 ? array_merge( $methods, $array ) : $methods;
return $methods;
}
/**
* Return the class name of the apm selected.
*
* @return array
*/
function cko_get_selected_apms_class() {
$apms_settings = get_option( 'woocommerce_wc_checkout_com_alternative_payments_settings' );
$selected_apms_class = [];
// Check if alternative payment method is enabled.
if ( ! empty( $apms_settings['enabled'] ) && 'yes' === $apms_settings['enabled'] ) {
$apm_selected = ! empty( $apms_settings['ckocom_apms_selector'] ) ? $apms_settings['ckocom_apms_selector'] : [];
// Get apm selected and add the class name in array.
foreach ( $apm_selected as $value ) {
$selected_apms_class[] = 'WC_Gateway_Checkout_Com_Alternative_Payments' . '_' . $value;
}
}
return $selected_apms_class;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'cko_checkout_com_action_links' );
/**
* Add settings link.
*
* @param mixed $links Plugin Action links.
*
* @return array
*/
function cko_checkout_com_action_links( $links ) {
$plugin_links = [
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_checkout_com_cards' ) . '">' . __( 'Settings', 'checkout-com-unified-payments-api' ) . '</a>',
];
return array_merge( $plugin_links, $links );
}
// This action will register flagged order status in woocommerce.
add_action( 'init', 'cko_register_order_statuses' );
/**
* Register flagged order status.
*/
function cko_register_order_statuses() {
register_post_status(
'wc-flagged',
[
'label' => _x( 'Suspected Fraud', 'Order status', 'checkout-com-unified-payments-api' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: number of orders */
'label_count' => _n_noop( 'Suspected Fraud <span class="count">(%s)</span>', 'Suspected Frauds <span class="count">(%s)</span>', 'checkout-com-unified-payments-api' ),
]
);
}
add_filter( 'wc_order_statuses', 'cko_new_wc_order_statuses' );
/**
* Register flagged status in wc_order_statuses.
*
* @param array $order_statuses Array of order statuses.
*
* @return array
*/
function cko_new_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-flagged'] = _x( 'Suspected Fraud', 'Order status', 'checkout-com-unified-payments-api' );
return $order_statuses;
}
add_action( 'woocommerce_checkout_process', 'cko_check_if_empty' );
/**
* Validate cvv on checkout page.
* Nonce is verified by WooCommerce checkout process before this hook runs.
*/
function cko_check_if_empty() {
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verified by WooCommerce checkout process
$payment_method = isset( $_POST['payment_method'] ) ? sanitize_text_field( wp_unslash( $_POST['payment_method'] ) ) : '';
if ( 'wc_checkout_com_cards' === $payment_method ) {
$payment_token = isset( $_POST['wc-wc_checkout_com_cards-payment-token'] ) ? sanitize_text_field( wp_unslash( $_POST['wc-wc_checkout_com_cards-payment-token'] ) ) : '';
$cvv = isset( $_POST['wc_checkout_com_cards-card-cvv'] ) ? sanitize_text_field( wp_unslash( $_POST['wc_checkout_com_cards-card-cvv'] ) ) : '';
// Check if require cvv is enabled in module setting.
if (
WC_Admin_Settings::get_option( 'ckocom_card_saved' )
&& WC_Admin_Settings::get_option( 'ckocom_card_require_cvv' )
&& ! empty( $payment_token )
&& 'new' !== $payment_token
) {
// check if cvv is empty on checkout page.
if ( empty( $cvv ) ) {
wc_add_notice( esc_html__( 'Please enter a valid cvv.', 'checkout-com-unified-payments-api' ), 'error' );
}
}
}
// phpcs:enable
}
// Hook into order creation and updates
add_action( 'woocommerce_new_order', 'cko_validate_manual_order_creation' );
add_action( 'woocommerce_update_order', 'cko_validate_manual_order_creation' );
add_action( 'save_post', 'cko_validate_manual_order_on_save' );
add_action( 'admin_init', 'cko_check_incomplete_orders' );
/**
* Validate manual order creation to ensure billing address and email are set.
* This prevents issues when using Flow payment on order-pay page.
*
* @param int $order_id Order ID.
*/
function cko_validate_manual_order_creation( $order_id ) {
$order = wc_get_order( $order_id );
// Only validate admin-created orders (manual orders)
if ( ! $order || ! $order->is_created_via( 'admin' ) ) {
return;
}
// Log for debugging
WC_Checkoutcom_Utility::logger( 'Validating manual order creation. Order ID: ' . $order_id );
// Check if billing email is missing or invalid
$billing_email = $order->get_billing_email();
if ( empty( $billing_email ) || ! is_email( $billing_email ) ) {
// Log the issue
WC_Checkoutcom_Utility::logger( 'Manual order created without valid billing email. Order ID: ' . $order_id );
// Set a transient to show notice on next admin page load
set_transient( 'cko_validation_email_' . $order_id, true, 60 );
}
// Check if billing address is missing
$billing_address_1 = $order->get_billing_address_1();
$billing_city = $order->get_billing_city();
$billing_country = $order->get_billing_country();
if ( empty( $billing_address_1 ) || empty( $billing_city ) || empty( $billing_country ) ) {
// Log the issue
WC_Checkoutcom_Utility::logger( 'Manual order created without complete billing address. Order ID: ' . $order_id );
// Set a transient to show notice on next admin page load
set_transient( 'cko_validation_address_' . $order_id, true, 60 );
}
}
/**
* Validate order when saved via admin (different hook)
*
* @param int $post_id Post ID.
*/
function cko_validate_manual_order_on_save( $post_id ) {
// Only process shop_order posts
if ( get_post_type( $post_id ) !== 'shop_order' ) {
return;
}
$order = wc_get_order( $post_id );
// Only validate admin-created orders (manual orders)
if ( ! $order || ! $order->is_created_via( 'admin' ) ) {
return;
}
// Log for debugging
WC_Checkoutcom_Utility::logger( 'Validating manual order on save. Order ID: ' . $post_id );
$validation_errors = array();
// Check if billing email is missing or invalid
$billing_email = $order->get_billing_email();
if ( empty( $billing_email ) || ! is_email( $billing_email ) ) {
$validation_errors[] = __( 'Billing email is required for Flow payments to work properly.', 'checkout-com-unified-payments-api' );
WC_Checkoutcom_Utility::logger( 'Manual order saved without valid billing email. Order ID: ' . $post_id );
}
// Check if billing address is missing
$billing_address_1 = $order->get_billing_address_1();
$billing_city = $order->get_billing_city();
$billing_country = $order->get_billing_country();
if ( empty( $billing_address_1 ) || empty( $billing_city ) || empty( $billing_country ) ) {
$validation_errors[] = __( 'Complete billing address is required for Flow payments to work properly.', 'checkout-com-unified-payments-api' );
WC_Checkoutcom_Utility::logger( 'Manual order saved without complete billing address. Order ID: ' . $post_id );
}
// If there are validation errors, prevent saving and show error
if ( ! empty( $validation_errors ) ) {
// Log the blocking
WC_Checkoutcom_Utility::logger( 'BLOCKING order creation due to validation errors: ' . implode( ', ', $validation_errors ) );
// Show error message and stop execution
$error_message = '<h2>' . esc_html__( 'Order Creation Blocked', 'checkout-com-unified-payments-api' ) . '</h2>';
$error_message .= '<p><strong>' . esc_html__( 'Checkout.com Flow:', 'checkout-com-unified-payments-api' ) . '</strong> ' . esc_html__( 'Cannot create order with missing required information.', 'checkout-com-unified-payments-api' ) . '</p>';
$error_message .= '<ul>';
foreach ( $validation_errors as $error ) {
$error_message .= '<li>' . esc_html( $error ) . '</li>';
}
$error_message .= '</ul>';
$error_message .= '<p><strong>' . esc_html__( 'Please:', 'checkout-com-unified-payments-api' ) . '</strong></p>';
$error_message .= '<ol>';
$error_message .= '<li>' . esc_html__( 'Go back to the order form', 'checkout-com-unified-payments-api' ) . '</li>';
$error_message .= '<li>' . esc_html__( 'Add the missing billing email and address', 'checkout-com-unified-payments-api' ) . '</li>';
$error_message .= '<li>' . esc_html__( 'Try creating the order again', 'checkout-com-unified-payments-api' ) . '</li>';
$error_message .= '</ol>';
$error_message .= '<p><a href="javascript:history.back()">' . esc_html__( 'Go Back', 'checkout-com-unified-payments-api' ) . '</a></p>';
// Define allowed HTML for wp_kses
$allowed_html = array(
'h2' => array(),
'p' => array(),
'strong' => array(),
'ul' => array(),
'ol' => array(),
'li' => array(),
'a' => array( 'href' => array() ),
);
wp_die( wp_kses( $error_message, $allowed_html ), esc_html__( 'Order Creation Blocked', 'checkout-com-unified-payments-api' ), array( 'response' => 400 ) );
}
}
/**
* Check for incomplete orders and show persistent warnings
*/
function cko_check_incomplete_orders() {
// Only run in admin and on order-related pages
if ( ! is_admin() || ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
// Get current screen
$screen = get_current_screen();
if ( ! $screen || ( $screen->id !== 'shop_order' && $screen->id !== 'edit-shop_order' ) ) {
return;
}
// Get order ID from URL
$order_id = 0;
if ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) === 'shop_order' ) {
$order_id = intval( $_GET['post'] );
}
if ( ! $order_id ) {
return;
}
$order = wc_get_order( $order_id );
if ( ! $order || ! $order->is_created_via( 'admin' ) ) {
return;
}
// Check if order is incomplete
$billing_email = $order->get_billing_email();
$billing_address_1 = $order->get_billing_address_1();
$billing_city = $order->get_billing_city();
$billing_country = $order->get_billing_country();
$is_incomplete = false;
$missing_fields = array();
if ( empty( $billing_email ) || ! is_email( $billing_email ) ) {
$is_incomplete = true;
$missing_fields[] = 'billing email';
}
if ( empty( $billing_address_1 ) || empty( $billing_city ) || empty( $billing_country ) ) {
$is_incomplete = true;
$missing_fields[] = 'complete billing address';
}
if ( $is_incomplete ) {
// Store notice data in transient for display
set_transient( 'cko_flow_incomplete_order_notice_' . $order_id, array(
'missing_fields' => $missing_fields,
'order_id' => $order_id,
), 3600 );
// Set a persistent notice
add_action( 'admin_notices', 'cko_show_flow_incomplete_order_notice' );
}
}
/**
* Show admin notice for incomplete Flow orders.
*/
function cko_show_flow_incomplete_order_notice() {
// Check if we're on the order edit page
if ( ! isset( $_GET['post'] ) || ! isset( $_GET['action'] ) || 'edit' !== $_GET['action'] ) {
return;
}
$order_id = absint( $_GET['post'] );
$notice_data = get_transient( 'cko_flow_incomplete_order_notice_' . $order_id );
if ( ! $notice_data || ! is_array( $notice_data ) ) {
return;
}
$missing_fields = isset( $notice_data['missing_fields'] ) ? $notice_data['missing_fields'] : array();
echo '<div class="notice notice-error is-dismissible">';
echo '<p><strong>⚠️ Checkout.com Flow Warning:</strong> ';
echo 'This manual order is missing: ' . esc_html( implode( ', ', $missing_fields ) ) . '. ';
echo 'Flow payments will fail without this information. ';
echo 'Please complete the order details before proceeding with payment.';
echo '</p></div>';
// Delete transient after showing notice once
delete_transient( 'cko_flow_incomplete_order_notice_' . $order_id );
}
/**
* Add resource hints for Flow checkout in wp_head.
*/
function cko_add_flow_resource_hints() {
$core_settings = get_option( 'woocommerce_wc_checkout_com_cards_settings', array() );
$environment = isset( $core_settings['ckocom_environment'] ) ? $core_settings['ckocom_environment'] : 'sandbox';
$api_domain = 'sandbox' === $environment ? 'api.sandbox.checkout.com' : 'api.checkout.com';
?>
<link rel="dns-prefetch" href="//checkout-web-components.checkout.com">
<link rel="preconnect" href="https://checkout-web-components.checkout.com" crossorigin>
<link rel="preconnect" href="https://<?php echo esc_attr( $api_domain ); ?>" crossorigin>
<!-- CDN resource hints for risk.js and other SDK resources -->
<link rel="dns-prefetch" href="//cdn.checkout.com">
<link rel="preconnect" href="https://cdn.checkout.com" crossorigin>
<link rel="dns-prefetch" href="//devices.checkout.com">
<link rel="preconnect" href="https://devices.checkout.com" crossorigin>
<?php
}
/**
* Add async attribute to Flow SDK script tag.
*
* @param string $tag Script tag HTML.
* @param string $handle Script handle.
* @return string Modified script tag.
*/
function cko_add_async_to_flow_script( $tag, $handle ) {
if ( 'checkout-com-flow-script' === $handle ) {
return str_replace( ' src', ' async src', $tag );
}
return $tag;
}
// Show admin notices based on transients
add_action( 'admin_notices', 'cko_show_validation_notices' );
/**
* Show validation notices in admin
*/
function cko_show_validation_notices() {
// Get current order ID if we're on an order edit page
global $post;
$order_id = 0;
if ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) === 'shop_order' ) {
$order_id = intval( $_GET['post'] );
} elseif ( $post && $post->post_type === 'shop_order' ) {
$order_id = $post->ID;
}
if ( ! $order_id ) {
return;
}
// Check for email validation notice
if ( get_transient( 'cko_validation_email_' . $order_id ) ) {
delete_transient( 'cko_validation_email_' . $order_id );
echo '<div class="notice notice-warning is-dismissible">';
echo '<p><strong>Checkout.com Flow:</strong> ';
echo esc_html__( 'Manual orders require a valid billing email address for Flow payments to work properly. Please add a billing email to this order.', 'checkout-com-unified-payments-api' );
echo ' <a href="' . esc_url( admin_url( 'post.php?post=' . $order_id . '&action=edit' ) ) . '">' . esc_html__( 'Edit Order', 'checkout-com-unified-payments-api' ) . '</a>';
echo '</p></div>';
}
// Check for address validation notice
if ( get_transient( 'cko_validation_address_' . $order_id ) ) {
delete_transient( 'cko_validation_address_' . $order_id );
echo '<div class="notice notice-warning is-dismissible">';
echo '<p><strong>Checkout.com Flow:</strong> ';
echo esc_html__( 'Manual orders require a complete billing address for Flow payments to work properly. Please add billing address details to this order.', 'checkout-com-unified-payments-api' );
echo ' <a href="' . esc_url( admin_url( 'post.php?post=' . $order_id . '&action=edit' ) ) . '">' . esc_html__( 'Edit Order', 'checkout-com-unified-payments-api' ) . '</a>';
echo '</p></div>';
}
}
add_action( 'admin_enqueue_scripts', 'cko_admin_enqueue_scripts', 10, 1 );
/**
* Load admin scripts.
*
* @return void
*/
function cko_admin_enqueue_scripts( $hook ) {
if ( ! is_admin() ) {
return;
}
$allowed_hooks = array(
'woocommerce_page_wc-settings',
'woocommerce_page_checkoutcom-diagnostics', // Legacy standalone page
'woocommerce_page_checkout-com-webhook-queue',
'checkout-com-webhook-queue',
);
if ( ! in_array( $hook, $allowed_hooks, true ) ) {
return;
}
if ( 'woocommerce_page_wc-settings' === $hook ) {