This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwoocommerce-gateway-payex-payment.php
779 lines (675 loc) · 24.9 KB
/
woocommerce-gateway-payex-payment.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
<?php
/*
* Plugin Name: WooCommerce PayEx Payments Gateway
* Plugin URI: http://payex.com/
* Description: Provides a Credit Card Payment Gateway through PayEx for WooCommerce.
* Author: AAIT Team
* Author URI: http://aait.se/
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Version: 2.2.2
* Text Domain: woocommerce-gateway-payex-payment
* Domain Path: /languages
* WC requires at least: 3.0.0
* WC tested up to: 3.4.5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
$vendorsDir = dirname( __FILE__ ) . '/vendor';
if ( ! class_exists( '\\PayEx\\Px', FALSE ) ) {
require_once $vendorsDir . '/payex-php-api/vendor/autoload.php';
}
if ( ! class_exists( 'FullNameParser', FALSE ) ) {
require_once $vendorsDir . '/php-name-parser/vendor/autoload.php';
}
class WC_Payex_Payment {
public static $_methods = array(
'payex_bankdebit',
'payex_factoring',
'payex_invoice',
'payex_masterpass',
'payex_mobilepay',
'payex',
'payex_swish',
'payex_wywallet'
);
/**
* Constructor
*/
public function __construct() {
// Activation
register_activation_hook( __FILE__, __CLASS__ . '::install' );
// Actions
add_action( 'init', array( $this, 'create_credit_card_post_type' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ) );
add_action( 'plugins_loaded', array( $this, 'init' ), 0 );
add_action( 'woocommerce_loaded', array( $this, 'woocommerce_loaded' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );
add_filter( 'woocommerce_payment_gateways', array( $this, 'register_gateway' ) );
// Add admin menu
add_action( 'admin_menu', array( &$this, 'admin_menu' ), 99 );
// Payment fee
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_cart_fee' ) );
// Add statuses for payment complete
add_filter( 'woocommerce_valid_order_statuses_for_payment_complete', array(
$this,
'add_valid_order_statuses'
), 10, 2 );
// Add MasterPass button to Cart Page
add_action( 'woocommerce_proceed_to_checkout', array( $this, 'add_mp_button_to_cart' ) );
// Add MasterPass button to Product Page
add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_mp_button_to_product_page' ) );
// Check is MasterPass Purchase
add_action( 'template_redirect', array( $this, 'check_mp_purchase' ) );
// PayEx Credit Card: Payment Method Change Callback
add_action( 'template_redirect', array( $this, 'check_payment_method_changed' ) );
// Add Upgrade Notice
if ( version_compare( get_option( 'woocommerce_payex_version', '1.0.0' ), '2.0.0', '<' ) ) {
add_action( 'admin_notices', __CLASS__ . '::upgrade_notice' );
}
add_action( 'admin_notices', __CLASS__ . '::admin_notices' );
add_action( 'admin_notices', __CLASS__ . '::check_backward_compatibility', 40 );
// Add SSN Checkout Field
add_action( 'woocommerce_before_checkout_billing_form', array( $this, 'before_checkout_billing_form' ) );
add_action( 'wp_ajax_payex_process_ssn', array( $this, 'ajax_payex_process_ssn' ) );
add_action( 'wp_ajax_nopriv_payex_process_ssn', array( $this, 'ajax_payex_process_ssn' ) );
// Add Email Classes
add_filter( 'woocommerce_email_classes', array( $this, 'add_email_classes' ), 10, 1 );
// Init Cron Tasks
add_action( 'wp', __CLASS__ . '::add_cron_tasks' );
// Cron Tasks Actions
add_action( 'payex_check_cards', __CLASS__ . '::check_cards' );
// Add Countries for SSN field
add_filter( 'woocommerce_payex_countries_ssn', array( $this, 'add_countries_ssn' ), 10, 1 );
// WC_Order Compatibility for WC < 3.0
add_action( 'woocommerce_init', __CLASS__ . '::add_compatibility' );
}
/**
* Install
*/
public static function install() {
if ( ! get_option( 'woocommerce_payex_version' ) ) {
add_option( 'woocommerce_payex_version', '2.0.0' );
}
}
/**
* Add relevant links to plugins page
*
* @param array $links
*
* @return array
*/
public function plugin_action_links( $links ) {
$plugin_links = array(
'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=payex' ) ) . '">' . __( 'Settings', 'woocommerce-gateway-payex-payment' ) . '</a>'
);
return array_merge( $plugin_links, $links );
}
/**
* Init localisations and files
*/
public function init() {
// Localization
load_plugin_textdomain( 'woocommerce-gateway-payex-payment', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
if ( class_exists('WC_Payment_Gateway', false) ) {
// Includes
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-abstract.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-payment.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-bankdebit.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-invoice.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-factoring.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-wywallet.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-masterpass.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-swish.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-payex-mobilepay.php' );
}
}
/**
* WooCommerce Loaded: load classes
*/
public function woocommerce_loaded() {
include_once( dirname( __FILE__ ) . '/includes/class-wc-payex-credit-cards.php' );
include_once( dirname( __FILE__ ) . '/includes/class-wc-payex-admin-actions.php' );
}
/**
* Admin Notices
*/
public static function admin_notices() {
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
$list = array(
'payex', 'payex_bankdebit', 'payex_invoice',
'payex_factoring', 'payex_wywallet', 'payex_masterpass',
'payex_swish'
);
foreach ($list as $item) {
if ( isset( $available_gateways[$item] ) ) {
$gateway = $available_gateways[$item];
$settings = $gateway->settings;
if ( empty( $settings['account_no'] ) || empty( $settings['encrypted_key'] ) ) {
echo '<div class="error"><p>' . sprintf( __( 'PayEx Payments for WooCommerce is almost ready. To get started <a href="%s">connect your PayEx account</a>.', 'woocommerce-gateway-payex-payment' ), esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=' . $gateway->id ) ) ) . '</p></div>';
break;
}
}
}
}
/**
* Upgrade Notice
*/
public static function upgrade_notice() {
if ( current_user_can( 'update_plugins' ) ) {
?>
<div id="message" class="error">
<p>
<?php
echo esc_html__( 'Warning! WooCommerce PayEx Payments plugin requires to update the database structure.', 'woocommerce-gateway-payex-payment' );
echo ' ' . sprintf( esc_html__( 'Please click %s here %s to start upgrade.', 'woocommerce-gateway-payex-payment' ), '<a href="' . esc_url( admin_url( 'admin.php?page=wc-payex-upgrade' ) ) . '">', '</a>' );
?>
</p>
</div>
<?php
}
}
/**
* Check for backward compatibility with woocommerce-gateway-payex
*/
public static function check_backward_compatibility() {
if ( current_user_can( 'update_plugins' ) ) {
// Check is already migrated
if ( get_option( 'woocommerce_payex_migrated' ) !== false ) {
return;
}
// Check woocommerce-gateway-payex is active
if ( ! in_array( 'woocommerce-gateway-payex/gateway-payex.php', get_option( 'active_plugins' ) ) ) {
return;
}
// Check payex settings is unconfigured yet
$settings = get_option( 'woocommerce_payex_settings' );
if ( $settings !== false ) {
return;
}
// Check settings of woocommerce-gateway-payex plugin
$settings = get_option( 'woocommerce_payex_pm_settings' );
if ( $settings === false ) {
return;
}
// Check account_no and encrypted_key are configured
if ( empty( $settings['account_no'] ) || empty( $settings['encrypted_key'] ) ) {
return;
}
?>
<div id="message" class="updated woocommerce-message">
<p class="main">
<strong><?php echo esc_html__( 'Data migration.', 'woocommerce-gateway-payex-payment' ); ?></strong>
</p>
<p>
<?php
echo esc_html__( 'We\'ve detected that you\'ve used an older version of the WooCommerce PayEx integration.', 'woocommerce-gateway-payex-payment' );
echo '<br />';
echo esc_html__( 'Click the "Upgrade" button below to setup this new integration with your existing PayEx details', 'woocommerce-gateway-payex-payment' );
echo '<br />';
echo esc_html__( 'Please be sure to backup your website before doing this. Thank you for choosing PayEx!', 'woocommerce-gateway-payex-payment' );
?>
</p>
<p class="submit">
<a class="button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-payex-migrate' ) ); ?>">
<?php echo esc_html__( 'Upgrade', 'woocommerce-gateway-payex-payment' ); ?>
</a>
</p>
</div>
<?php
}
}
/**
* Upgrade Page
*/
public static function upgrade_page() {
if ( ! current_user_can( 'update_plugins' ) ) {
return;
}
// Run Database Update
include_once( dirname( __FILE__ ) . '/includes/class-wc-payex-update.php' );
WC_Payex_Update::update();
echo esc_html__( 'Upgrade finished.', 'woocommerce-gateway-payex-payment' );
}
/**
* Migrate Page
*/
public static function migrate_page() {
if ( ! current_user_can( 'update_plugins' ) ) {
return;
}
// Check is already migrated
if ( get_option( 'woocommerce_payex_migrated' ) !== false ) {
return;
}
// Copy settings
$settings = get_option( 'woocommerce_payex_pm_settings' );
if ( $settings ) {
$new_settings = array(
'enabled' => $settings['enabled'],
'title' => $settings['title'],
'description' => $settings['description'],
'account_no' => $settings['account_no'],
'encrypted_key' => $settings['encrypted_key'],
'testmode' => $settings['testmode'],
'debug' => $settings['debug'],
'purchase_operation' => $settings['purchase_operation'],
'checkout_info' => $settings['send_order_lines'],
'payment_view' => 'CREDITCARD',
'language' => 'en-US',
'responsive' => 'no',
'save_cards' => 'no',
'agreement_max_amount' => '1000',
'agreement_url' => get_site_url()
);
update_option( 'woocommerce_payex_settings', $new_settings, true );
}
// Convert orders data
$orders = get_posts( array(
'numberposts' => -1,
'orderby' => 'ID',
'order' => 'ASC',
'meta_key' => '_payment_method',
'meta_value' => 'payex_pm',
'post_type' => 'shop_order',
'post_status' => 'any',
'post_parent' => 0,
'suppress_filters' => true,
));
foreach ($orders as $order) {
$order_id = $order->ID;
$order = wc_get_order( $order_id );
if ( version_compare(WC_Subscriptions::$version, '2.0.0', '<') &&
WC_Subscriptions_Order::order_contains_subscription( $order )
) {
// Change payment method
update_post_meta( $order_id, '_recurring_payment_method', 'payex' );
// Copy agreement reference
$agreement = get_post_meta( $order_id, '_payex_agreement_reference', true );
if ( ! empty( $agreement ) ) {
continue;
}
$agreement = get_post_meta( $order_id, 'payex_agreement_ref', true );
if ( ! empty( $agreement ) ) {
update_post_meta( $order_id, '_payex_agreement_reference', $agreement );
}
}
// Change payment method
update_post_meta( $order_id, '_payment_method', 'payex' );
// Migration flag
update_post_meta( $order_id, '_is_migrated_payex', true );
}
// Deactivate plugin
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
deactivate_plugins( 'woocommerce-gateway-payex/gateway-payex.php' );
// Migration flag
delete_option( 'woocommerce_payex_migrated' );
add_option( 'woocommerce_payex_migrated', true );
echo esc_html__( 'Migration finished.', 'woocommerce-gateway-payex-payment' );
?>
<script type="application/javascript">
window.onload = function() {
setTimeout(function () {
window.location.href = '<?php echo esc_url( admin_url( 'index.php' ) ); ?>';
}, 3000);
}
</script>
<?php
}
/**
* Add Scripts
*/
public function add_scripts() {
$mp_settings = get_option( 'woocommerce_payex_masterpass_settings' );
if ( $mp_settings['enabled'] === 'yes' ) {
wp_enqueue_style( 'wc-gateway-payex-masterpass', plugins_url( '/assets/css/masterpass.css', __FILE__ ), array(), false, 'all' );
}
$factoring_settings = get_option( 'woocommerce_payex_factoring_settings' );
if ( isset( $factoring_settings['checkout_field'] ) && $factoring_settings['checkout_field'] === 'yes' ) {
wp_enqueue_style( 'wc-payex-addons-ssn', plugins_url( '/assets/css/ssn.css', __FILE__ ), array(), false, 'all' );
wp_register_script( 'wc-payex-addons-ssn', plugins_url( '/assets/js/ssn.js', __FILE__ ), array( 'wc-checkout' ), false, true );
// Localize the script with new data
$translation_array = array(
'text_require_ssn' => __( 'Please enter Social Security Number', 'woocommerce-gateway-payex-payment' ),
);
wp_localize_script( 'wc-payex-addons-ssn', 'WC_Payex_Addons_SSN', $translation_array );
// Enqueued script with localized data
wp_enqueue_script( 'wc-payex-addons-ssn' );
}
}
/**
* Register the gateways for use
*/
public function register_gateway( $methods ) {
$methods[] = 'WC_Gateway_Payex_Payment';
$methods[] = 'WC_Gateway_Payex_Bankdebit';
$methods[] = 'WC_Gateway_Payex_InvoiceLedgerService';
$methods[] = 'WC_Gateway_Payex_Factoring';
$methods[] = 'WC_Gateway_Payex_Wywallet';
$methods[] = 'WC_Gateway_Payex_MasterPass';
$methods[] = 'WC_Gateway_Payex_Swish';
$methods[] = 'WC_Gateway_Payex_Mobilepay';
return $methods;
}
/**
* Add fee when selected payment method
*/
public function add_cart_fee() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
// Get Current Payment Method
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
$default = get_option( 'woocommerce_default_gateway' );
if ( ! $default ) {
$default = current( array_keys( $available_gateways ) );
}
$current = WC()->session->get( 'chosen_payment_method', $default );
$current_gateway = array_key_exists( $current, $available_gateways ) ? $available_gateways[ $current ] : null;
// Fee feature in Invoice and Factoring modules
if ( ! $current_gateway || ! in_array( $current_gateway->id, array( 'payex_invoice', 'payex_factoring' ) ) ) {
return;
}
// Is Fee is not specified
if ( abs( $current_gateway->fee ) < 0.01 ) {
return;
}
// Add Fee
$fee_title = $current_gateway->id === 'payex_invoice' ? __( 'Invoice Fee', 'woocommerce-gateway-payex-payment' ) : __( 'Factoring Fee', 'woocommerce-gateway-payex-payment' );
WC()->cart->add_fee( $fee_title, $current_gateway->fee, ( $current_gateway->fee_is_taxable === 'yes' ), $current_gateway->fee_tax_class );
}
/**
* Allow processing/completed statuses for capture
*
* @param array $statuses
* @param WC_Order $order
*
* @return array
*/
public function add_valid_order_statuses( $statuses, $order ) {
if ( in_array( $order->get_payment_method(), WC_Payex_Payment::$_methods ) ) {
$statuses = array_merge( $statuses, array( 'processing', 'completed' ) );
}
return $statuses;
}
/**
* Provide Credit Card Post Type
*/
public function create_credit_card_post_type() {
register_post_type( 'payex_credit_card',
array(
'labels' => array(
'name' => __( 'Credit Cards', 'woocommerce-gateway-payex-payment' )
),
'public' => false,
'show_ui' => false,
'map_meta_cap' => false,
'rewrite' => false,
'query_var' => false,
'supports' => false,
)
);
}
/**
* Provide Admin Menu items
*/
public function admin_menu() {
// Add Upgrade Page
global $_registered_pages;
$hookname = get_plugin_page_hookname( 'wc-payex-upgrade', '' );
if ( ! empty( $hookname ) ) {
add_action( $hookname, __CLASS__ . '::upgrade_page' );
}
$_registered_pages[ $hookname ] = true;
// Add Plugin migrate Page
$hookname = get_plugin_page_hookname( 'wc-payex-migrate', '' );
if ( ! empty( $hookname ) ) {
add_action( $hookname, __CLASS__ . '::migrate_page' );
}
$_registered_pages[ $hookname ] = true;
}
/**
* Add MasterPass Button to Cart page
*/
public function add_mp_button_to_cart() {
$mp_settings = get_option( 'woocommerce_payex_masterpass_settings' );
if ( $mp_settings['display_cart_button'] === 'yes' && $mp_settings['enabled'] === 'yes' ) {
wc_get_template(
'masterpass/cart-button.php',
array(
'image' => esc_url( plugins_url( '/assets/images/masterpass-button.png', __FILE__ ) ),
'description' => $mp_settings['description'],
'link' => esc_url( add_query_arg( 'mp_from_cart_page', 1, get_permalink() ) )
),
'',
dirname( __FILE__ ) . '/templates/'
);
}
}
/**
* Add MasterPass Button to Single Product page
*/
public function add_mp_button_to_product_page() {
$mp_settings = get_option( 'woocommerce_payex_masterpass_settings' );
if ( $mp_settings['display_pp_button'] === 'yes' && $mp_settings['enabled'] === 'yes' ) {
wc_get_template(
'masterpass/product-button.php',
array(
'image' => esc_url( plugins_url( '/assets/images/masterpass-button.png', __FILE__ ) ),
'description' => $mp_settings['description'],
'link' => esc_url( add_query_arg( 'mp_from_product_page', 1, get_permalink() ) )
),
'',
dirname( __FILE__ ) . '/templates/'
);
}
}
/**
* Check for MasterPass purchase from cart page
**/
public function check_mp_purchase() {
// Check for MasterPass purchase from cart page
if ( isset( $_GET['mp_from_cart_page'] ) && $_GET['mp_from_cart_page'] === '1' ) {
$gateway = new WC_Gateway_Payex_MasterPass;
$gateway->masterpass_button_action();
}
// Check for MasterPass purchase from product page
if ( isset( $_POST['mp_from_product_page'] ) && $_POST['mp_from_product_page'] === '1' ) {
$gateway = new WC_Gateway_Payex_MasterPass;
$gateway->masterpass_button_action();
}
}
/**
* Payment Method Change Callback
*/
public function check_payment_method_changed() {
$gateways = WC()->payment_gateways()->get_available_payment_gateways();
if ( isset( $gateways['payex'] ) ) {
/** @var WC_Gateway_Payex_Payment $gateway */
$gateway = $gateways['payex'];
if ( $gateway->enabled === 'yes' ) {
$gateway->check_payment_method_changed();
}
}
}
/**
* Hook before_checkout_billing_form
*
* @param $checkout
*/
public function before_checkout_billing_form( $checkout ) {
$factoring_settings = get_option( 'woocommerce_payex_factoring_settings' );
if ( isset( $factoring_settings['checkout_field'] ) && $factoring_settings['checkout_field'] === 'yes' ) {
echo '<div id="payex_ssn">';
woocommerce_form_field( 'payex_ssn', array(
'type' => 'text',
'class' => array( 'payex-ssn-class form-row-wide' ),
'label' => __( 'Social Security Number', 'woocommerce-gateway-payex-payment' ),
'placeholder' => __( 'Social Security Number', 'woocommerce-gateway-payex-payment' ),
), $checkout->get_value( 'payex_ssn' ) );
woocommerce_form_field( 'payex_ssn_zip', array(
'type' => 'text',
'class' => array( 'payex-ssn-zip-class form-row-wide' ),
'label' => __( 'Postcode / ZIP', 'woocommerce' ),
), $checkout->get_value( 'billing_postcode' ) );
woocommerce_form_field( 'payex_ssn_country', array(
'type' => 'select',
'class' => array( 'payex-ssn-country-class form-row-wide' ),
'label' => __( 'Country', 'woocommerce' ),
'options' => wp_parse_args(
apply_filters( 'woocommerce_payex_countries_ssn', $checkout ),
array( '' => '' )
),
'input_class' => array( 'country_select' )
), $checkout->get_value( 'billing_country' ) );
echo '<input type="button" class="button alt" name="woocommerce_checkout_payex_ssn" id="payex_ssn_button" value="' . __( 'Get Profile', 'woocommerce-gateway-payex-payment' ) . '" />';
echo '</div>';
}
}
/**
* Ajax Hook
*/
public function ajax_payex_process_ssn() {
// Init PayEx
$gateways = WC()->payment_gateways()->get_available_payment_gateways();
if ( ! $gateways['payex_factoring'] ) {
wp_send_json_error( array( 'message' => __( 'Financing Invoice method is inactive', 'woocommerce-gateway-payex-payment' ) ) );
exit();
}
/** @var WC_Gateway_Payex_Factoring $gateway */
$gateway = $gateways['payex_factoring'];
if ( empty( $_POST['billing_country'] ) ) {
wp_send_json_error( array( 'message' => __( 'Please select country', 'woocommerce-gateway-payex-payment' ) ) );
exit();
}
if ( empty( $_POST['billing_postcode'] ) ) {
wp_send_json_error( array( 'message' => __( 'Please enter postcode', 'woocommerce-gateway-payex-payment' ) ) );
exit();
}
// Init PayEx
$gateway->getPx()->setEnvironment( $gateway->account_no, $gateway->encrypted_key, $gateway->testmode === 'yes' );
// Call PxOrder.GetAddressByPaymentMethod
$params = array(
'accountNumber' => '',
'paymentMethod' => 'PXFINANCINGINVOICE' . mb_strtoupper( wc_clean( $_POST['billing_country'] ), 'UTF-8' ),
'ssn' => trim( wc_clean( $_POST['social_security_number'] ) ),
'zipcode' => trim( wc_clean( $_POST['billing_postcode'] ) ),
'countryCode' => trim( wc_clean( $_POST['billing_country'] ) ),
'ipAddress' => trim( $_SERVER['REMOTE_ADDR'] )
);
$result = $gateway->getPx()->GetAddressByPaymentMethod( $params );
if ( $result['code'] !== 'OK' || $result['description'] !== 'OK' || $result['errorCode'] !== 'OK' ) {
if ( preg_match( '/\bInvalid parameter:SocialSecurityNumber\b/i', $result['description'] ) ) {
wp_send_json_error( array( 'message' => __( 'Invalid Social Security Number', 'woocommerce-gateway-payex-payment' ) ) );
exit();
}
wp_send_json_error( array( 'message' => $result['errorCode'] . '(' . $result['description'] . ')' ) );
exit();
}
// Parse name field
$parser = new \FullNameParser();
$name = $parser->parse_name( $result['name'] );
$output = array(
'first_name' => $name['fname'],
'last_name' => $name['lname'],
'address_1' => $result['streetAddress'],
'address_2' => ! empty( $result['coAddress'] ) ? 'c/o ' . $result['coAddress'] : '',
'postcode' => $result['zipCode'],
'city' => $result['city'],
'country' => $result['countryCode']
);
wp_send_json_success( $output );
exit();
}
/**
* Add Email Classes
* @param $emails
*
* @return array
*/
public function add_email_classes($emails) {
if ( ! class_exists( 'WC_Email_Payex_Card_Expiring', false ) ) {
include( dirname( __FILE__ ) . '/includes/emails/class-wc-email-payex-card-expiring.php' );
}
$emails['WC_Email_Payex_Card_Expiring'] = new WC_Email_Payex_Card_Expiring();
return $emails;
}
/**
* Init Cron Tasks
*/
public static function add_cron_tasks() {
if ( ! wp_next_scheduled( 'payex_check_cards' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'payex_check_cards' );
}
}
/**
* Cron Task: Check Cards
*/
public static function check_cards() {
// Check notifications are enabled
$card_expiring_settings = get_option( 'woocommerce_payex_card_expiring_settings', array(
'enabled' => 'yes',
'notifications_days' => '30'
) );
if ( isset( $card_expiring_settings['enabled'] ) && $card_expiring_settings['enabled'] !== 'yes' ) {
return;
}
$notifications_days = (int) $card_expiring_settings['notifications_days'];
// Date to check
$check = time() + $notifications_days * 24 * 60 * 60;
// Get users
/** @var WP_User $user */
foreach ( get_users() as $user ) {
// Get Cards
$args = array(
'post_type' => 'payex_credit_card',
'author' => $user->ID,
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
);
$cards = get_posts( $args );
foreach ($cards as $card) {
$card_meta = get_post_meta( $card->ID, '_payex_card', true);
// Check reminder was sent
if ( empty( $card_meta['reminder_expiring_sent'] ) ) {
// Check is expiring
$expire_date = strtotime( $card_meta['expire_date'] );
if ( $check >= $expire_date ) {
do_action('payex_card_expiring_mail', $card->ID);
}
}
}
}
}
/**
* Add Countries for SSN Field
* @param $checkout
*
* @return array
*/
public function add_countries_ssn($checkout) {
$countries = array(
'SE' => __( 'Sweden', 'woocommerce' ),
'NO' => __( 'Norway', 'woocommerce' ),
);
return $countries;
}
/**
* Check is WooCommerce >= 3.0
* @return bool
*/
public static function is_wc3() {
return version_compare( WC()->version, '3.0', '>=' );
}
/**
* WC_Order Compatibility for WC < 3.0
*/
public static function add_compatibility() {
if ( ! self::is_wc3() ) {
include_once( dirname( __FILE__ ) . '/includes/deprecated/class-wc-order-compatibility-payex.php' );
}
}
}
new WC_Payex_Payment();