-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wc-novalnet.php
1317 lines (1171 loc) · 50.4 KB
/
class-wc-novalnet.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
/**
* Woocommerce Novalnet Gateway Plugin class.
*
* @package woocommerce-novalnet-gateway
* @category Class WC_Novalnet
* @author Novalnet
*/
/**
* Main WC_Novalnet Class.
*
* @class WC_Novalnet
*/
final class WC_Novalnet {
/**
* Available payment ID and its type in this module.
*
* @var array
*/
private $payments = array(
'novalnet_sepa' => 'DIRECT_DEBIT_SEPA',
'novalnet_guaranteed_sepa' => 'GUARANTEED_DIRECT_DEBIT_SEPA',
'novalnet_cc' => 'CREDITCARD',
'novalnet_applepay' => 'APPLEPAY',
'novalnet_googlepay' => 'GOOGLEPAY',
'novalnet_invoice' => 'INVOICE',
'novalnet_guaranteed_invoice' => 'GUARANTEED_INVOICE',
'novalnet_prepayment' => 'PREPAYMENT',
'novalnet_ideal' => 'IDEAL',
'novalnet_instantbank' => 'ONLINE_TRANSFER',
'novalnet_online_bank_transfer' => 'ONLINE_BANK_TRANSFER',
'novalnet_giropay' => 'GIROPAY',
'novalnet_twint' => 'TWINT',
'novalnet_barzahlen' => 'CASHPAYMENT',
'novalnet_przelewy24' => 'PRZELEWY24',
'novalnet_eps' => 'EPS',
'novalnet_instalment_invoice' => 'INSTALMENT_INVOICE',
'novalnet_instalment_sepa' => 'INSTALMENT_DIRECT_DEBIT_SEPA',
'novalnet_paypal' => 'PAYPAL',
'novalnet_postfinance_card' => 'POSTFINANCE_CARD',
'novalnet_postfinance' => 'POSTFINANCE',
'novalnet_bancontact' => 'BANCONTACT',
'novalnet_alipay' => 'ALIPAY',
'novalnet_wechatpay' => 'WECHATPAY',
'novalnet_trustly' => 'TRUSTLY',
'novalnet_multibanco' => 'MULTIBANCO',
'novalnet_mbway' => 'MBWAY',
'novalnet_blik' => 'BLIK',
'novalnet_payconiq' => 'PAYCONIQ',
'novalnet_ach' => 'DIRECT_DEBIT_ACH',
);
/**
* Supported payment types based on process.
*
* @var array
*/
private $supports = array(
'tokenization' => array(
'novalnet_cc',
'novalnet_sepa',
'novalnet_guaranteed_sepa',
'novalnet_instalment_sepa',
'novalnet_ach',
),
'subscription' => array(
'novalnet_cc',
'novalnet_sepa',
'novalnet_guaranteed_sepa',
'novalnet_paypal',
'novalnet_invoice',
'novalnet_guaranteed_invoice',
'novalnet_prepayment',
'novalnet_googlepay',
'novalnet_applepay',
'novalnet_ach',
),
'authorize' => array(
'novalnet_cc',
'novalnet_applepay',
'novalnet_googlepay',
'novalnet_sepa',
'novalnet_paypal',
'novalnet_invoice',
'novalnet_guaranteed_sepa',
'novalnet_guaranteed_invoice',
'novalnet_instalment_invoice',
'novalnet_instalment_sepa',
),
'amount_update' => array(
'novalnet_guaranteed_sepa',
'novalnet_guaranteed_invoice',
'novalnet_sepa',
'novalnet_invoice',
'novalnet_prepayment',
'novalnet_barzahlen',
),
'instalment' => array(
'novalnet_instalment_sepa',
'novalnet_instalment_invoice',
),
'guarantee' => array(
'novalnet_guaranteed_sepa',
'novalnet_guaranteed_invoice',
),
'pay_later' => array(
'novalnet_invoice',
'novalnet_prepayment',
'novalnet_barzahlen',
'novalnet_multibanco',
),
'invoice_payments' => array(
'novalnet_invoice',
'novalnet_instalment_invoice',
'novalnet_guaranteed_invoice',
),
'sepa_payments' => array(
'novalnet_sepa',
'novalnet_instalment_sepa',
'novalnet_guaranteed_sepa',
),
'zero_amount_booking' => array(
'novalnet_cc',
'novalnet_sepa',
'novalnet_applepay',
'novalnet_googlepay',
'novalnet_ach',
),
);
/**
* Novalnet plugin URL
*
* @var $plugin_url
*/
public $plugin_url;
/**
* Novalnet plugin directory path
*
* @var $plugin_dir_path
*/
public $plugin_dir_path;
/**
* Store the POST/GET request
*
* @var array
*/
public $request;
/**
* The single instance of the class.
*
* @var WC_Novalnet The single instance of the class.
*
* @since 12.0.0
*/
protected static $instance = null;
/**
* Main WC_Novalnet Instance.
*
* Ensures only one instance of Novalnet is loaded.
*
* @since 12.0.0
* @static
* @see novalnet()
* @return WC_Novalnet - Main instance.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* WC_Novalnet Constructor.
*/
public function __construct() {
// Including required files.
include_once 'includes/wc-novalnet-functions.php';
include_once 'includes/class-wc-novalnet-db-handler.php';
include_once 'includes/admin/class-wc-novalnet-amount-refund.php';
include_once 'includes/admin/class-wc-novalnet-configuration.php';
include_once 'includes/admin/class-wc-novalnet-admin.php';
include_once 'includes/abstracts/class-wc-novalnet-abstract-payment-gateways.php';
include_once 'includes/class-wc-novalnet-install.php';
include_once 'includes/class-wc-novalnet-validation.php';
include_once 'includes/class-wc-payment-token-novalnet.php';
include_once 'includes/class-wc-novalnet-helper.php';
include_once 'includes/class-wc-novalnet-subscription.php';
include_once 'includes/class-wc-novalnet-guaranteed-process.php';
// Store the request data.
$this->request = $_REQUEST; // phpcs:ignore WordPress.Security.NonceVerification
// Handle webhook action.
add_action( 'woocommerce_api_novalnet_callback', array( $this, 'handle_webhook_process' ) );
// Including available Novalnet payment gateway files.
foreach ( glob( dirname( __FILE__ ) . '/includes/gateways/*.php' ) as $filename ) {
include_once $filename;
}
$this->plugin_url = untrailingslashit( plugins_url( '/', NN_PLUGIN_FILE ) );
$this->plugin_dir_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
// Initiate the text domain.
load_plugin_textdomain( 'woocommerce-novalnet-gateway', false, dirname( plugin_basename( NN_PLUGIN_FILE ) ) . '/i18n/languages/' );
// Handle installation process on plugin activation.
register_activation_hook( NN_PLUGIN_FILE, array( 'WC_Novalnet_Install', 'install' ) );
// Handle uninstallation process on plugin deactivation.
register_deactivation_hook( NN_PLUGIN_FILE, array( 'WC_Novalnet_Install', 'uninstall' ) );
// Handle version process on plugin update.
add_action( 'admin_init', array( 'WC_Novalnet_Install', 'update' ) );
// Add Novalnet payments to the WooCommerce.
add_filter( 'woocommerce_payment_gateways', array( $this, 'add_novalnet_payments' ) );
// Align the transaction details.
add_action( 'woocommerce_order_item_meta_end', array( $this, 'align_transaction_info' ), 10, 3 );
// Add plugin scripts (front-end).
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_script' ) );
// Restrict instant mail from Germanized plugin.
add_filter( 'woocommerce_gzd_instant_order_confirmation', array( $this, 'restrict_instant_email' ) );
// Update Novalnet settings.
add_action( 'woocommerce_update_options_novalnet-settings', array( 'WC_Novalnet_Configuration', 'update_novalnet_settings' ) );
// Add Novalnet settings tab.
add_action( 'woocommerce_settings_tabs_novalnet-settings', array( 'WC_Novalnet_Configuration', 'novalnet_settings_page' ) );
// Customize script data.
add_filter( 'script_loader_tag', array( $this, 'customize_script' ), 10, 3 );
// Customize style sheet data.
add_filter( 'style_loader_tag', array( $this, 'customize_style_sheet' ), 10, 4 );
// add virtual product in to the cart.
add_action( 'wp_ajax_add_virtual_product_in_cart', array( $this, 'add_virtual_product_in_cart' ) );
add_action( 'wp_ajax_nopriv_add_virtual_product_in_cart', array( $this, 'add_virtual_product_in_cart' ) );
// register the ajax action for authenticated users.
add_action( 'wp_ajax_novalnet_order_creation', array( $this, 'novalnet_order_creation' ) );
add_action( 'wp_ajax_nopriv_novalnet_order_creation', array( $this, 'novalnet_order_creation' ) );
// Check shipping address.
add_action( 'wp_ajax_novalnet_shipping_address_update', array( $this, 'novalnet_shipping_address_update' ) );
add_action( 'wp_ajax_nopriv_novalnet_shipping_address_update', array( $this, 'novalnet_shipping_address_update' ) );
// register the ajax action for authenticated users.
add_action( 'wp_ajax_novalnet_shipping_method_update', array( $this, 'novalnet_shipping_method_update' ) );
add_action( 'wp_ajax_nopriv_novalnet_shipping_method_update', array( $this, 'novalnet_shipping_method_update' ) );
// Add button in cart page.
add_action( 'woocommerce_proceed_to_checkout', array( $this, 'wallet_cart_hook' ), 1000 );
// Add button in minicart page.
add_action( 'woocommerce_widget_shopping_cart_after_buttons', array( $this, 'wallet_minicart_hook' ), 1000 );
// Add button in checkout page.
add_action( 'woocommerce_before_checkout_form', array( $this, 'wallet_checkout_hook' ), 1000 );
// Add button in myaccount page.
add_action( 'woocommerce_before_customer_login_form', array( $this, 'wallet_myaccount_hook' ), 1000 );
// Add button in product page.
add_action( 'woocommerce_after_add_to_cart_form', array( $this, 'wallet_product_hook' ), 1000 );
// Add button in pay later page.
if ( version_compare( WOOCOMMERCE_VERSION, '6.6.0', '>=' ) ) {
add_action( 'before_woocommerce_pay_form', array( $this, 'wallet_paylater_hook' ), 1000 );
} else {
add_action( 'before_woocommerce_pay', array( $this, 'wallet_paylater_hook' ), 1000 );
}
if ( class_exists( 'woocommerce_wpml' ) ) {
add_filter( 'wcml_multi_currency_ajax_actions', array( $this, 'add_novalnet_wallet_action' ) );
}
// Check multistep payment availability.
add_action( 'wp_ajax_check_mutlistep_payment', array( $this, 'check_multistep_checkout_payment' ) );
add_action( 'wp_ajax_nopriv_check_mutlistep_payment', array( $this, 'check_multistep_checkout_payment' ) );
add_action( 'before_woocommerce_init', array( $this, 'novalnet_wc_hpos_compatibility' ) );
add_filter( 'woocommerce_order_needs_payment', array( $this, 'needs_payment_pending_order' ), 10, 3 );
// Prevents unpaid order auto cancelling.
add_action( 'woocommerce_cancel_unpaid_order', array( $this, 'prevent_unpaid_order_cancelling' ), 10, 2 );
}
/**
* Prevents WooCommerce's hold stock feature from cancelling Novalnet orders.
*
* @since 12.8.1
* @param bool $cancel_order Order cancelling status.
* @param WC_Order $order The order object.
*/
public function prevent_unpaid_order_cancelling( $cancel_order, $order ) {
if ( ! $cancel_order || ! $order instanceof WC_Order ) {
return $cancel_order;
}
if ( WC_Novalnet_Validation::check_string( $order->get_payment_method() ) ) {
$cancel_order = false;
}
return $cancel_order;
}
/**
* Needs Payment for pending order.
* If the order created in novalnet payment.
*
* @since 12.6.3
*
* @param bool $status Need payment status.
* @param object $wc_order Order details.
* @param array $valid_status Valid order status.
*/
public function needs_payment_pending_order( $status, $wc_order, $valid_status ) {
if ( $status ) {
$draft_order_id = ( null !== wc()->session ) ? wc()->session->get( 'store_api_draft_order', 0 ) : null;
$order_id = $wc_order->get_id();
if ( $draft_order_id === $order_id && WC_Novalnet_Validation::check_string( $wc_order->get_payment_method() ) && 'pending' === $wc_order->get_status() ) {
return false;
}
}
return $status;
}
/**
* Novalet compatibility for WooCommerce custome order table
*
* @since 12.6.2
*/
public function novalnet_wc_hpos_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', NN_PLUGIN_FILE, true );
}
}
/**
* Check previously selected payment method available after changes multicheckout.
*
* @since 12.5.6
*/
public function check_multistep_checkout_payment() {
$chosen_payment = '';
if ( class_exists( 'WooCommerce_Germanized_Pro' ) && get_option( 'woocommerce_gzdp_checkout_enable' ) === 'yes' ) {
$chosen_payment = WC()->session->get( 'chosen_payment_method' );
}
wp_send_json_success(
array(
'wc_chosen_payment' => $chosen_payment,
)
);
}
/**
* Add Novalnet ajax action in WooCommerce Multilingual & Multicurrency load currency action list.
*
* @since 12.5.5
* @param array $ajax_action Allowed currency load action list.
*/
public function add_novalnet_wallet_action( $ajax_action ) {
$ajax_action[] = 'novalnet_order_creation';
$ajax_action[] = 'add_virtual_product_in_cart';
$ajax_action[] = 'novalnet_shipping_address_update';
$ajax_action[] = 'novalnet_shipping_method_update';
$ajax_action[] = 'novalnet_wc_order_recalculate_success';
return $ajax_action;
}
/**
* Process of Wallet for product page.
*
* @since 12.5.0
*/
public function wallet_paylater_hook() {
// Get wallet settings.
$data['wallet_area'] = 'paylater_page';
// For paylater page get avilable_wallets in Checkout Page.
$data['available_wallets'] = get_available_wallets( 'checkout_page' );
if ( count( $data['available_wallets'] ) > 0 ) {
novalnet()->helper()->load_template( 'render-wallet-button.php', $data );
}
}
/**
* Process of Wallet for product page.
*
* @since 12.4.0
*/
public function wallet_product_hook() {
// Get wallet settings.
$data['wallet_area'] = 'product_page';
$data['available_wallets'] = ( $this->can_display_wallet_button() ) ? get_available_wallets( 'product_page' ) : array();
if ( count( $data['available_wallets'] ) > 0 ) {
novalnet()->helper()->load_template( 'render-wallet-button.php', $data );
}
}
/**
* Process of Wallet for my-account page.
*
* @since 12.4.0
*/
public function wallet_myaccount_hook() {
// Get wallet settings.
$data['wallet_area'] = 'guest_checkout_page';
$data['available_wallets'] = ( $this->can_display_wallet_button() ) ? get_available_wallets( 'guest_checkout_page' ) : array();
if ( count( $data['available_wallets'] ) > 0 ) {
novalnet()->helper()->load_template( 'render-wallet-button.php', $data );
}
}
/**
* Process of Wallet for checkout page.
*
* @since 12.4.0
*/
public function wallet_checkout_hook() {
// Get wallet settings.
$data['wallet_area'] = 'checkout_page';
$data['available_wallets'] = ( $this->can_display_wallet_button() ) ? get_available_wallets( 'checkout_page' ) : array();
if ( count( $data['available_wallets'] ) > 0 ) {
novalnet()->helper()->load_template( 'render-wallet-button.php', $data );
}
}
/**
* Process of Wallet for minicart page.
*
* @since 12.4.0
*/
public function wallet_minicart_hook() {
// Get wallet settings.
$data['wallet_area'] = 'mini_cart_page';
$data['available_wallets'] = ( $this->can_display_wallet_button() ) ? get_available_wallets( 'mini_cart_page' ) : array();
if ( count( $data['available_wallets'] ) > 0 ) {
novalnet()->helper()->load_template( 'render-wallet-button.php', $data );
}
}
/**
* Process of Wallet for cart page.
*
* @since 12.4.0
*/
public function wallet_cart_hook() {
// Get wallet settings.
$data['wallet_area'] = 'shopping_cart_page';
$data['available_wallets'] = ( $this->can_display_wallet_button() ) ? get_available_wallets( 'shopping_cart_page' ) : array();
if ( count( $data['available_wallets'] ) > 0 ) {
novalnet()->helper()->load_template( 'render-wallet-button.php', $data );
}
}
/**
* Check the shop is enable for guest to place order.
*
* @since 12.5.5
*/
public function can_display_wallet_button() {
// Wallet for guests is also disabled even though account creation during checkout is enabled.
if ( ! is_user_logged_in() && 'yes' !== get_option( 'woocommerce_enable_guest_checkout' ) ) {
return false;
}
return true;
}
/**
* Shipping address update in wallet page.
*
* @since 12.4.0
*/
public function novalnet_shipping_address_update() {
global $woocommerce;
if ( in_array( $this->request['source_page'], array( 'mini_cart_page_googlepay_button', 'mini_cart_page_applepay_button', 'product_page_googlepay_button', 'product_page_applepay_button' ), true ) ) {
if ( empty( $this->request['variable_variant_id'] ) ) {
$product_id = array();
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id[] = $cart_item['product_id'];
}
if ( isset( $this->request['simple_product_id'] ) && ! in_array( (int) $this->request['simple_product_id'], $product_id, true ) ) {
WC()->cart->add_to_cart( $this->request['simple_product_id'] );
}
}
if ( ! empty( $this->request['variable_product_id'] ) && ! empty( $this->request['variable_variant_id'] ) ) {
$variation_id = array();
foreach ( WC()->cart->get_cart() as $cart_item ) {
$variation_id[] = $cart_item['variation_id'];
}
if ( isset( $this->request['variable_variant_id'] ) && ! in_array( (int) $this->request['variable_variant_id'], $variation_id, true ) ) {
WC()->cart->add_to_cart( $this->request['variable_product_id'], 1, $this->request['variable_variant_id'] );
}
}
}
$items = $woocommerce->cart->get_cart();
$article_details = array();
foreach ( $items as $item => $values ) {
$_product = wc_get_product( $values['data']->get_id() );
if ( wc_prices_include_tax() ) {
$product_price = wc_get_price_excluding_tax( $_product );
$product_price = wc_novalnet_amount( $product_price );
} else {
$product_price = $_product->get_price();
$product_price = wc_novalnet_amount( $product_price );
}
$total = $product_price * $values['quantity'];
$product_details = $_product->get_name() . ' (' . $values['quantity'] . ' X ' . $product_price . ')';
if ( in_array( $_product->get_type(), array( 'subscription', 'subscription_variation' ), true ) ) {
$signup_fee = $_product->get_meta( '_subscription_sign_up_fee' );
if ( $signup_fee > 0 ) {
$article_details[] = array(
'label' => 'Signup Fee',
'amount' => wc_novalnet_amount_as_string( $signup_fee ),
'type' => 'SUBTOTAL',
);
}
}
$article_details[] = array(
'label' => $product_details,
'amount' => wc_novalnet_amount_as_string( $total ),
'type' => 'SUBTOTAL',
);
}
$received_shipping_address = json_decode( $this->request['shippingInfo'], true );
WC()->customer->set_shipping_city( wc_clean( $received_shipping_address['address']['locality'] ) );
WC()->customer->set_shipping_postcode( wc_clean( $received_shipping_address['address']['postalCode'] ) );
WC()->customer->set_shipping_country( wc_clean( $received_shipping_address['address']['countryCode'] ) );
WC()->customer->set_shipping_state( wc_clean( $received_shipping_address['address']['administrativeArea'] ) );
WC()->customer->save();
WC()->cart->calculate_shipping();
$packages = WC()->shipping()->get_packages();
$shipping_details = array();
$count = 1;
foreach ( $packages as $package ) {
foreach ( $package['rates'] as $values ) {
$shipping_total = wc_novalnet_amount( $values->cost );
if ( 1 === $count ) {
$article_details[] = array(
'label' => $values->label,
'amount' => wc_novalnet_amount_as_string( $shipping_total ),
'type' => 'SUBTOTAL',
);
WC()->session->set( 'chosen_shipping_methods', array( $values->id ) );
}
$shipping_details[] = array(
'label' => $values->label,
'amount' => wc_novalnet_amount_as_string( $shipping_total ),
'identifier' => $values->id,
'detail' => '',
);
$count++;
}
}
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
$cart_tax_amount = 0;
foreach ( WC()->cart->get_taxes() as $tax_amount ) {
$cart_tax_amount += $tax_amount;
}
if ( $cart_tax_amount > 0 ) {
$article_details[] = array(
'label' => 'Tax',
'amount' => wc_novalnet_amount_as_string( $cart_tax_amount ),
'type' => 'SUBTOTAL',
);
}
$applied_coupon = WC()->cart->get_applied_coupons();
foreach ( $applied_coupon as $coupon ) {
if ( ! empty( $coupon ) ) {
$coupon_obj = new WC_Coupon( $coupon );
$article_details[] = array(
'label' => 'discount(' . $coupon . ')',
'amount' => '-' . wc_novalnet_amount_as_string( WC()->cart->get_coupon_discount_amount( $coupon_obj->get_code(), WC()->cart->display_cart_ex_tax ) ),
'type' => 'SUBTOTAL',
);
}
}
$total = wc_novalnet_amount( WC()->cart->total );
if ( WC()->cart->show_shipping() && ( class_exists( 'WC_Subscriptions_Cart' ) && WC_Subscriptions_Cart::cart_contains_subscriptions_needing_shipping() ) ) {
$recurring_carts = WC()->cart->recurring_carts;
$recurring_amount = 0;
if ( ! empty( $recurring_carts ) ) {
foreach ( WC()->cart->recurring_carts as $recurring_cart ) {
$recurring_amount += $recurring_cart->total;
}
}
WC_Subscriptions_Cart::calculate_subscription_totals( $recurring_amount, WC()->cart->recurring_carts );
foreach ( WC()->cart->recurring_carts as $recurring_cart_key => $recurring_cart ) {
// This ensures we get the correct package IDs (these are filtered by WC_Subscriptions_Cart).
WC_Subscriptions_Cart::set_calculation_type( 'recurring_total' );
WC_Subscriptions_Cart::set_recurring_cart_key( $recurring_cart_key );
WC_Subscriptions_Cart::set_cached_recurring_cart( $recurring_cart );
$shipping_packages = array();
// Allow third parties to filter whether the recurring cart has a shipment.
$cart_has_next_shipment = apply_filters( 'woocommerce_subscriptions_cart_has_next_shipment', 0 !== $recurring_cart->next_payment_date, $recurring_cart );
if ( $cart_has_next_shipment && WC_Subscriptions_Cart::cart_contains_subscriptions_needing_shipping( $recurring_cart ) ) {
foreach ( $recurring_cart->get_shipping_packages() as $recurring_cart_package_key => $recurring_cart_package ) {
if ( version_compare( WC_Subscriptions::$version, '4.9.0', '<' ) ) {
$package = WC_Subscriptions_Cart::get_calculated_shipping_for_package( $recurring_cart_package );
} else {
$package = WC()->shipping->calculate_shipping_for_package( $recurring_cart_package );
}
$shipping_packages = $package['rates'];
}
}
}
$methods_id = array();
foreach ( $shipping_details as $shipments ) {
$methods_id [] = $shipments['identifier'];
}
$count = 1;
foreach ( $shipping_packages as $method ) {
$shipping_total = wc_novalnet_amount( $method->cost );
if ( ! empty( $shipping_details ) ) {
if ( ! in_array( $method->id, $methods_id, true ) ) {
$shipping_details[] = array(
'label' => $method->label,
'amount' => wc_novalnet_amount_as_string( $shipping_total ),
'identifier' => $method->id,
'detail' => '',
);
}
} else {
if ( 1 === $count ) {
$article_details[] = array(
'label' => $method->label,
'amount' => wc_novalnet_amount_as_string( $shipping_total ),
'type' => 'SUBTOTAL',
);
}
$shipping_details[] = array(
'label' => $method->label,
'amount' => wc_novalnet_amount_as_string( $shipping_total ),
'identifier' => $method->id,
'detail' => '',
);
}
}
}
$shipping_address_change = array(
'amount' => wc_novalnet_amount_as_string( $total ),
'shipping_address' => $shipping_details,
'article_details' => $article_details,
);
wp_send_json( $shipping_address_change );
}
/**
* Shipping method update in wallet page.
*
* @since 12.4.0
*/
public function novalnet_shipping_method_update() {
global $woocommerce;
$received_shipping_method = json_decode( $this->request['shippingInfo'], true );
$items = $woocommerce->cart->get_cart();
$article_details = array();
foreach ( $items as $item => $values ) {
$_product = wc_get_product( $values['data']->get_id() );
if ( wc_prices_include_tax() ) {
$product_price = wc_get_price_excluding_tax( $_product );
$product_price = wc_novalnet_amount( $product_price );
} else {
$product_price = $_product->get_price();
$product_price = wc_novalnet_amount( $product_price );
}
$total = $product_price * $values['quantity'];
$total = wc_novalnet_amount( $total );
$product_details = $_product->get_name() . ' (' . $values['quantity'] . ' X ' . $product_price . ')';
if ( in_array( $_product->get_type(), array( 'subscription', 'subscription_variation' ), true ) ) {
$signup_fee = $_product->get_meta( '_subscription_sign_up_fee' );
if ( $signup_fee > 0 ) {
$article_details[] = array(
'label' => 'Signup Fee',
'amount' => wc_novalnet_amount_as_string( $signup_fee ),
'type' => 'SUBTOTAL',
);
}
}
$article_details[] = array(
'label' => $product_details,
'amount' => wc_novalnet_amount_as_string( $total ),
'type' => 'SUBTOTAL',
);
}
if ( count( WC()->session->get( 'shipping_for_package_0' )['rates'] ) > 0 ) {
foreach ( WC()->session->get( 'shipping_for_package_0' )['rates'] as $rate_id => $rate ) {
if ( $rate->id === $received_shipping_method['shippingMethod']['identifier'] ) {
$default_rate_id = array( $rate_id );
break;
}
}
WC()->session->set( 'chosen_shipping_methods', $default_rate_id );
}
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
$cart_tax_amount = 0;
foreach ( WC()->cart->get_taxes() as $tax_amount ) {
$cart_tax_amount += $tax_amount;
}
if ( $cart_tax_amount > 0 ) {
$article_details[] = array(
'label' => 'Tax',
'amount' => wc_novalnet_amount_as_string( $cart_tax_amount ),
'type' => 'SUBTOTAL',
);
}
$article_details[] = array(
'label' => $received_shipping_method['shippingMethod']['label'],
'amount' => wc_novalnet_amount_as_string( $received_shipping_method['shippingMethod']['amount'] ),
'type' => 'SUBTOTAL',
);
$applied_coupon = WC()->cart->get_applied_coupons();
foreach ( $applied_coupon as $coupon ) {
if ( ! empty( $coupon ) ) {
$coupon_obj = new WC_Coupon( $coupon );
$article_details[] = array(
'label' => 'discount(' . $coupon . ')',
'amount' => '-' . wc_novalnet_amount_as_string( $coupon_obj->get_amount() ),
'type' => 'SUBTOTAL',
);
}
}
$total = wc_novalnet_amount( WC()->cart->total );
$shipping_method_change = array(
'amount' => wc_novalnet_amount_as_string( $total ),
'order_info' => $article_details,
);
wp_send_json( $shipping_method_change );
}
/**
* Add virtual product in cart.
*
* @since 12.4.0
*/
public function add_virtual_product_in_cart() {
if ( empty( $this->request['variable_variant_id'] ) ) {
$product_id = array();
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product_id[] = $cart_item['product_id'];
}
if ( ! in_array( (int) $this->request['simple_product_id'], $product_id, true ) ) {
WC()->cart->add_to_cart( $this->request['simple_product_id'] );
}
}
if ( ! empty( $this->request['variable_product_id'] ) && ! empty( $this->request['variable_variant_id'] ) ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$variation_id[] = $cart_item['variation_id'];
}
if ( ! in_array( (int) $this->request['variable_variant_id'], $variation_id, true ) ) {
WC()->cart->add_to_cart( $this->request['variable_product_id'], 1, $this->request['variable_variant_id'] );
}
}
}
/**
* Order creation using wallet.
*
* @since 12.0.0
*/
public function novalnet_order_creation() {
global $woocommerce;
$token_name = 'novalnet_' . $this->request['payment'] . '_token';
if ( WC()->session->__isset( $token_name ) ) {
WC()->session->__unset( $token_name );
}
WC()->session->set( $token_name, $this->request['variable_name']['response']['transaction']['token'] );
if ( WC()->session->__isset( 'googlepay_do_redirect' ) ) {
WC()->session->__unset( 'googlepay_do_redirect' );
}
if ( ! empty( $this->request['variable_name']['response']['transaction']['doRedirect'] ) ) {
WC()->session->set( 'googlepay_do_redirect', $this->request['variable_name']['response']['transaction']['doRedirect'] );
}
$payment_method = 'novalnet_' . $this->request['payment'];
if ( isset( $this->request['pay_for_order_id'] ) && ! empty( $this->request['pay_for_order_id'] ) ) {
// Process Payment.
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$settings = WC_Novalnet_Configuration::get_payment_settings( $payment_method );
$payment_text = WC_Novalnet_Configuration::get_payment_text( $payment_method );
$payment_method_title = wc_novalnet_get_payment_text( $settings, $payment_text, wc_novalnet_shop_language(), $payment_method, 'title' );
$wc_order = wc_get_order( wc_clean( $this->request['pay_for_order_id'] ) );
$wc_order->set_payment_method_title( $payment_method_title );
$wc_order->set_payment_method( $payment_method );
$wc_order->save();
$result = $available_gateways[ $payment_method ]->process_payment( $this->request['pay_for_order_id'] );
wp_send_json( $result );
} else {
$required_fields = array( 'firstName', 'lastName', 'addressLines', 'postalCode', 'locality', 'countryCode' );
$missing_fields = false;
$address = '';
if ( isset( $this->request['is_virtual_order'] ) && 'applepay' === (string) $this->request['payment'] && 1 === (int) $this->request['is_virtual_order'] ) {
$required_fields = array( 'email' );
}
$received_address = $this->request['variable_name']['response']['order'];
foreach ( $required_fields as $key ) {
if ( 'addressLines' === $key ) {
if ( empty( trim( $received_address['billing']['contact'][ $key ], ' ' ) ) ) {
$address = 'Billing';
$missing_fields = true;
}
if ( isset( $received_address['shipping']['contact'] ) && empty( trim( $received_address['shipping']['contact'][ $key ], ' ' ) ) ) {
$address = 'Shipping';
$missing_fields = true;
}
} else {
if ( 'email' !== $key && empty( trim( $received_address['billing']['contact'][ $key ], ' ' ) ) ) {
$address = 'Billing';
$missing_fields = true;
}
if ( isset( $received_address['shipping']['contact'] ) && empty( trim( $received_address['shipping']['contact'][ $key ], ' ' ) ) ) {
$address = 'Shipping';
$missing_fields = true;
}
}
if ( $missing_fields ) {
wp_send_json(
array(
'result' => 'error',
'redirect' => $address . ' ' . ucwords( preg_replace( '/(?<!\ )[A-Z]/', ' $0', $key ) ) . ' is required fields',
)
);
}
}
$billing_firstname = ! empty( $received_address['billing']['contact']['firstName'] ) ? $received_address['billing']['contact']['firstName'] : '';
$billing_lastname = ! empty( $received_address['billing']['contact']['lastName'] ) ? $received_address['billing']['contact']['lastName'] : '';
if ( isset( $received_address['shipping']['contact']['firstName'] ) || isset( $received_address['shipping']['contact']['lastName'] ) ) {
$shipping_firstname = ! empty( $received_address['shipping']['contact']['firstName'] ) ? $received_address['shipping']['contact']['firstName'] : '';
$shipping_lastname = ! empty( $received_address['shipping']['contact']['lastName'] ) ? $received_address['shipping']['contact']['lastName'] : '';
}
}
$customer_billing = array();
$customer_shipping = array();
$phone_number = ( ! empty( $received_address['billing']['contact']['phoneNumber'] ) ) ? $received_address['billing']['contact']['phoneNumber'] : ( ( ! empty( $received_address['shipping']['contact']['phoneNumber'] ) ) ? $received_address['shipping']['contact']['phoneNumber'] : '' );
if ( is_user_logged_in() ) {
// Update billing address from Applepay sheet.
WC()->customer->set_billing_first_name( wc_clean( $billing_firstname ) );
WC()->customer->set_billing_last_name( wc_clean( $billing_lastname ) );
WC()->customer->set_billing_address_1( wc_clean( ( ! empty( $received_address['billing']['contact']['addressLines'] ) ) ? $received_address['billing']['contact']['addressLines'] : WC()->customer->get_billing_address_1() ) );
WC()->customer->set_billing_city( wc_clean( $received_address['billing']['contact']['locality'] ) );
WC()->customer->set_billing_postcode( wc_clean( $received_address['billing']['contact']['postalCode'] ) );
WC()->customer->set_billing_state( wc_clean( $received_address['billing']['contact']['administrativeArea'] ) );
WC()->customer->set_billing_country( wc_clean( $received_address['billing']['contact']['countryCode'] ) );
WC()->customer->set_billing_phone( wc_clean( $phone_number ) );
$customer_billing['company'] = WC()->customer->get_billing_company();
$customer_billing['state'] = WC()->customer->get_billing_state();
if ( isset( $shipping_firstname ) || isset( $shipping_lastname ) ) {
// Update shipping address from Applepay sheet.
WC()->customer->set_shipping_first_name( wc_clean( $shipping_firstname ) );
WC()->customer->set_shipping_last_name( wc_clean( $shipping_lastname ) );
WC()->customer->set_shipping_address_1( wc_clean( ( ! empty( $received_address['shipping']['contact']['addressLines'] ) ) ? $received_address['shipping']['contact']['addressLines'] : WC()->customer->get_billing_address_1() ) );
WC()->customer->set_shipping_city( wc_clean( $received_address['shipping']['contact']['locality'] ) );
WC()->customer->set_shipping_postcode( wc_clean( $received_address['shipping']['contact']['postalCode'] ) );
WC()->customer->set_shipping_country( wc_clean( $received_address['shipping']['contact']['countryCode'] ) );
$customer_shipping['company'] = WC()->customer->get_shipping_company();
$customer_shipping['state'] = WC()->customer->get_shipping_state();
}
}
// Customer billing information details (from Applepay sheet).
$customer_billing['first_name'] = wc_clean( $billing_firstname );
$customer_billing['last_name'] = wc_clean( $billing_lastname );
$customer_billing['address_1'] = $received_address['billing']['contact']['addressLines'];
$customer_billing['city'] = $received_address['billing']['contact']['locality'];
$customer_billing['email'] = ( ! empty( $received_address['billing']['contact']['email'] ) ) ? $received_address['billing']['contact']['email'] : $received_address['shipping']['contact']['email'];
$customer_billing['postcode'] = $received_address['billing']['contact']['postalCode'];
$customer_billing['country'] = $received_address['billing']['contact']['countryCode'];
$customer_billing['phone'] = $phone_number;
if ( isset( $shipping_firstname ) || isset( $shipping_lastname ) ) {
// Customer shipping information details (from Applepay sheet).
$customer_shipping['first_name'] = wc_clean( $shipping_firstname );
$customer_shipping['last_name'] = wc_clean( $shipping_lastname );
$customer_shipping['address_1'] = $received_address['shipping']['contact']['addressLines'];
$customer_shipping['email'] = ( ! empty( $received_address['billing']['contact']['email'] ) ) ? $received_address['billing']['contact']['email'] : $received_address['shipping']['contact']['email'];
$customer_shipping['city'] = $received_address['shipping']['contact']['locality'];
$customer_shipping['postcode'] = $received_address['shipping']['contact']['postalCode'];
$customer_shipping['country'] = $received_address['shipping']['contact']['countryCode'];
$customer_shipping['phone'] = $received_address['shipping']['contact']['phoneNumber'];
}
WC()->cart->calculate_shipping();
WC()->cart->calculate_totals();
$checkout = WC()->checkout();
$data['payment_method'] = $payment_method;
$items = $woocommerce->cart->get_cart();
$cart_has_subs = 0;
foreach ( $items as $item => $values ) {
$_product = wc_get_product( $values['data']->get_id() );
if ( in_array( $_product->get_type(), array( 'subscription', 'subscription_variation', 'variable-subscription' ), true ) ) {
$cart_has_subs = 1;
}
}
// Check subscription condition.
if ( $cart_has_subs ) {
if ( is_user_logged_in() ) {
if ( ! class_exists( 'WC_Subscriptions_Checkout' ) ) {
wp_send_json(
array(
'result' => 'error',
'redirect' => 'WooCommerce Subscription not installed properly.',
)
);
}
} else {
wp_send_json(
array(
'result' => 'error',
'redirect' => 'Please login and try again.',
)
);
}
} elseif ( wc_novalnet_amount( WC()->cart->total ) > 0 ) {
$data['payment_method'] = $payment_method;
}
if ( WC()->cart->needs_shipping() ) {
$chosen_shipping_methods = ( WC()->session !== null ) ? wc()->session->get( 'chosen_shipping_methods' ) : array();
if ( ! is_array( $chosen_shipping_methods ) || empty( $chosen_shipping_methods ) ) {
wp_send_json(
array(
'result' => 'error',
'redirect' => 'This order requires a shipping option.',
)
);
}
foreach ( $chosen_shipping_methods as $chosen_shipping_method ) {
if ( false === $chosen_shipping_method ) {
wp_send_json(
array(
'result' => 'error',
'redirect' => 'This order requires a shipping option.',