-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-wallee-subscription.php
802 lines (743 loc) · 20.5 KB
/
woocommerce-wallee-subscription.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
<?php
/**
* Plugin Name: wallee Subscription
* Plugin URI: https://wordpress.org/plugins/woo-wallee-subscription
* Description: Addon to process WooCommerce Subscriptions with wallee
* Version: 1.1.5
* License: Apache2
* License URI: http://www.apache.org/licenses/LICENSE-2.0
* Author: wallee AG
* Author URI: https://www.wallee.com
* Requires at least: 4.7
* Tested up to: 6.5.3
* WC requires at least: 3.0.0
* WC tested up to: 8.9.1
*
* Text Domain: woo-wallee-subscription
* Domain Path: /languages/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit(); // Exit if accessed directly.
}
if ( ! class_exists( 'WooCommerce_Wallee_Subscription' ) ) {
/**
* Main WooCommerce Wallee Class
*
* @class WooCommerce_Wallee_Subscription
*/
final class WooCommerce_Wallee_Subscription {
/**
* WooCommerce Wallee version.
*
* @var string
*/
private $version = '1.1.5';
/**
* The single instance of the class.
*
* @var WooCommerce_Wallee_Subscription
*/
protected static $_instance = null;
/**
* Logger.
*
* @var mixed
*/
private $logger = null;
/**
* Main WooCommerce Wallee Instance.
*
* Ensures only one instance of WooCommerce Wallee is loaded or can be loaded.
*
* @return WooCommerce_Wallee_Subscription - Main instance.
*/
public static function instance() {
if ( null === self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* WooCommerce_Wallee_Subscription Constructor.
*/
protected function __construct() {
$this->define_constants();
$this->includes();
$this->init_hooks();
}
/**
* Get version.
*
* @return string
*/
public function get_version() {
return $this->version;
}
/**
* Define constant if not already set.
*
* @param string $name Name.
* @param string|bool $value Value.
*/
protected function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Log.
*
* @param mixed $message Message.
* @param WC_Log_Levels $level Level.
* @return void
*/
public function log( $message, $level = WC_Log_Levels::WARNING ) {
if ( null == $this->logger ) {
$this->logger = new WC_Logger();
}
$this->logger->log(
$level,
$message,
array(
'source' => 'woo-wallee-subscription',
)
);
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Woo Wallee Subscription: ' . $message );
}
}
/**
* Get the plugin url.
*
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
*
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Define WC Wallee Constants.
*/
protected function define_constants() {
$this->define( 'WC_WALLEE_SUBSCRIPTION_PLUGIN_FILE', __FILE__ );
$this->define( 'WC_WALLEE_SUBSCRIPTION_ABSPATH', dirname( __FILE__ ) . '/' );
$this->define( 'WC_WALLEE_SUBSCRIPTION_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
$this->define( 'WC_WALLEE_SUBSCRIPTION_VERSION', $this->version );
$this->define( 'WC_WALLEE_SUBSCRIPTION_REQUIRED_WALLEE_VERSION', '1.2.12' );
$this->define( 'WC_WALLEE_REQUIRED_WC_SUBSCRIPTION_VERSION', '2.5' );
}
/**
* Include required core files used in admin and on the frontend.
*/
protected function includes() {
/**
* Class autoloader.
*/
require_once( WC_WALLEE_SUBSCRIPTION_ABSPATH . 'includes/class-wc-wallee-subscription-autoloader.php' );
require_once( WC_WALLEE_SUBSCRIPTION_ABSPATH . 'includes/class-wc-wallee-subscription-migration.php' );
if ( is_admin() ) {
require_once( WC_WALLEE_SUBSCRIPTION_ABSPATH . 'includes/admin/class-wc-wallee-subscription-admin.php' );
}
}
/**
* Init hooks.
*
* @return void
*/
protected function init_hooks() {
register_activation_hook(
__FILE__,
array(
'WC_Wallee_Subscription_Migration',
'install_db',
)
);
add_action(
'plugins_loaded',
array(
$this,
'loaded',
),
0
);
}
/**
* Load Localization files.
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present.
*
* Locales found in:
* - WP_LANG_DIR/woo-wallee-subscription/woo-wallee-subscription-LOCALE.mo
*/
public function load_plugin_textdomain() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'woo-wallee-subscription' );
load_textdomain( 'woo-wallee-subscription', WP_LANG_DIR . '/woo-wallee/woo-wallee-subscription' . $locale . '.mo' );
load_plugin_textdomain( 'woo-wallee-subscription', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Init WooCommerce Wallee when plugins are loaded.
*/
public function loaded() {
// Set up localisation.
$this->load_plugin_textdomain();
add_filter(
'wc_wallee_enhance_gateway',
array(
$this,
'enhance_gateway',
)
);
add_filter(
'wc_wallee_modify_sesion_create_transaction',
array(
$this,
'update_transaction_from_session',
)
);
add_filter(
'wc_wallee_modify_session_pending_transaction',
array(
$this,
'update_transaction_from_session',
)
);
add_filter(
'wc_wallee_modify_order_create_transaction',
array(
$this,
'update_transaction_from_order',
),
10,
2
);
add_filter(
'wc_wallee_modify_order_pending_transaction',
array(
$this,
'update_transaction_from_order',
),
10,
2
);
add_filter(
'wc_wallee_modify_confirm_transaction',
array(
$this,
'update_transaction_from_order',
),
10,
2
);
add_filter(
'wc_wallee_modify_line_item_order',
array(
$this,
'modify_line_item_method_change',
),
10,
2
);
add_filter(
'wc_wallee_modify_total_to_check_order',
array(
$this,
'modify_order_total_method_change',
),
12,
2
);
add_action(
'wc_wallee_authorized',
array(
$this,
'update_subscription_data',
),
10,
2
);
add_action(
'wc_wallee_fulfill',
array(
$this,
'fulfill_in_progress',
),
10,
2
);
add_filter(
'woocommerce_valid_order_statuses_for_payment',
array(
$this,
'add_valid_order_statuses_for_subscription_completion',
),
10,
2
);
add_filter(
'wc_wallee_update_transaction_info',
array(
$this,
'update_transaction_info',
),
10,
3
);
add_filter(
'wc_wallee_confirmed_status',
array(
$this,
'ignore_status_update_for_subscription',
),
10,
2
);
add_filter(
'wc_wallee_authorized_status',
array(
$this,
'ignore_status_update_for_subscription',
),
10,
2
);
add_filter(
'wc_wallee_completed_status',
array(
$this,
'ignore_status_update_for_subscription',
),
10,
2
);
add_filter(
'wc_wallee_decline_status',
array(
$this,
'ignore_status_update_for_subscription',
),
10,
2
);
add_filter(
'wc_wallee_failed_status',
array(
$this,
'ignore_status_update_for_subscription',
),
10,
2
);
add_filter(
'wc_wallee_voided_status',
array(
$this,
'ignore_status_update_for_subscription',
),
10,
2
);
add_action(
'wcs_after_renewal_setup_cart_subscriptions',
array(
$this,
'set_transaction_ids_into_session',
),
10,
2
);
add_filter(
'woocommerce_subscriptions_is_failed_renewal_order',
array(
$this,
'check_failed_renewal_order',
),
10,
3
);
// Handle order to cart creation for subscriptions.
add_filter(
'wcs_before_renewal_setup_cart_subscriptions',
array(
$this,
'before_renewal_setup_cart',
),
10,
2
);
add_filter(
'wcs_before_parent_order_setup_cart',
array(
$this,
'before_renewal_setup_cart',
),
10,
2
);
add_filter(
'wcs_after_renewal_setup_cart_subscriptions',
array(
$this,
'after_renewal_setup_cart',
),
10,
2
);
add_filter(
'wcs_after_parent_order_setup_cart',
array(
$this,
'after_renewal_setup_cart',
),
10,
2
);
add_filter(
'wc_wallee_is_method_available',
array(
$this,
'method_available_for_cart_renewal',
),
10,
1
);
add_filter(
'wc_wallee_success_url',
array(
$this,
'update_success_url',
),
10,
2
);
add_filter(
'wc_wallee_checkout_failure_url',
array(
$this,
'update_failure_url',
),
10,
2
);
}
/**
* Enhance gateway.
*
* @param WC_Wallee_Gateway $gateway Gateway.
* @return WC_Wallee_Gateway
*/
public function enhance_gateway( WC_Wallee_Gateway $gateway ) {
$gateway->supports = array_merge(
$gateway->supports,
array(
'subscriptions',
'multiple_subscriptions',
'subscription_cancellation',
'subscription_suspension',
'subscription_reactivation',
'subscription_amount_changes',
'subscription_date_changes',
'subscription_payment_method_change',
'subscription_payment_method_change_customer',
'subscription_payment_method_change_admin',
'subscription_payment_method_delayed_change',
)
);
// Create Subscription gateway and register hooks/filters.
new WC_Wallee_Subscription_Gateway( $gateway );
return $gateway;
}
/**
* Fulfill in progress.
*
* @param \Wallee\Sdk\Model\Transaction $transaction Transaction.
* @param mixed $order Order.
* @return void
*/
public function fulfill_in_progress( \Wallee\Sdk\Model\Transaction $transaction, $order ) {
if ( wcs_order_contains_subscription( $order, array( 'parent', 'resubscribe', 'switch', 'renewal' ) ) ) {
$GLOBALS['_wc_wallee_subscription_fulfill'] = true;
}
}
/**
* Is transaction method change.
*
* @param \Wallee\Sdk\Model\Transaction $transaction Transaction.
* @return bool
*/
private function is_transaction_method_change( \Wallee\Sdk\Model\Transaction $transaction ) {
$line_items = $transaction->getLineItems();
if ( count( $line_items ) != 1 ) {
return false;
}
$line_item = current( $line_items );
/* @var \Wallee\Sdk\Model\LineItem $line_item */
if ( $line_item->getSku() == 'paymentmethodchange' ) {
return true;
}
return false;
}
/**
* Before renewal setup.
* Create a checkout instead of using the order to pay. Here wet our method as available and ensure we do not create transaction on such order.
*
* @param mixed $subscriptions Subscriptions.
* @param mixed $order Order.
* @return void
*/
public function before_renewal_setup_cart( $subscriptions, $order ) {
$GLOBALS['_wc_wallee_renewal_cart_setup'] = true;
}
/**
* After renewal setup.
*
* @param mixed $subscriptions Subscriptions.
* @param mixed $order Order.
* @return void
*/
public function after_renewal_setup_cart( $subscriptions, $order ) {
$GLOBALS['_wc_wallee_renewal_cart_setup'] = false;
}
/**
* Method available for cart renewal.
*
* @param mixed $available Available.
* @return bool|mixed
*/
public function method_available_for_cart_renewal( $available ) {
if ( isset( $GLOBALS['_wc_wallee_renewal_cart_setup'] ) && $GLOBALS['_wc_wallee_renewal_cart_setup'] ) {
return true;
}
return $available;
}
/**
* Update success url.
*
* @param mixed $url Url.
* @param mixed $order Order.
* @return mixed
*/
public function update_success_url( $url, $order ) {
if ( wcs_is_subscription( $order ) ) {
return $order->get_view_order_url();
}
return $url;
}
/**
* Update failure url.
*
* @param mixed $url Url.
* @param mixed $order Order.
* @return mixed
*/
public function update_failure_url( $url, $order ) {
if ( wcs_is_subscription( $order ) ) {
wc_clear_notices();
$msg = WC()->session->get( 'wallee_failure_message', null );
if ( ! empty( $msg ) ) {
WooCommerce_Wallee::instance()->add_notice( (string) $msg, 'error' );
WC()->session->set( 'wallee_failure_message', null );
}
return $order->get_view_order_url();
}
return $url;
}
/**
* Add valid order statuses for subscription completion.
*
* @param mixed $statuses Statuses.
* @param mixed $order Order.
* @return mixed
*/
public function add_valid_order_statuses_for_subscription_completion( $statuses, $order = null ) {
if ( isset( $GLOBALS['_wc_wallee_subscription_fulfill'] ) ) {
$statuses[] = 'wallee-waiting';
$statuses[] = 'wallee-manual';
}
return $statuses;
}
/**
* Update transaction from session.
*
* @param \Wallee\Sdk\Model\AbstractTransactionPending $transaction Transaction.
* @return \Wallee\Sdk\Model\AbstractTransactionPending
*/
public function update_transaction_from_session( \Wallee\Sdk\Model\AbstractTransactionPending $transaction ) {
if ( WC_Subscriptions_Cart::cart_contains_subscription() || wcs_cart_contains_failed_renewal_order_payment() ) {
$transaction->setTokenizationMode( \Wallee\Sdk\Model\TokenizationMode::FORCE_CREATION_WITH_ONE_CLICK_PAYMENT );
}
return $transaction;
}
/**
* Modify line item method change.
*
* @param array $line_items Line items.
* @param mixed $order Order.
* @return array|\Wallee\Sdk\Model\LineItemCreate[] LineItemCreate.
*/
public function modify_line_item_method_change( array $line_items, $order ) {
if ( WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment ) {
// It is a method change for a subscription -> zero transaction.
$line_item = new \Wallee\Sdk\Model\LineItemCreate();
$line_item->setAmountIncludingTax( 0 );
$line_item->setQuantity( 1 );
$line_item->setName( __( 'Payment Method Change', 'woo-wallee-subscription' ) );
$line_item->setShippingRequired( false );
$line_item->setSku( 'paymentmethodchange' );
$line_item->setTaxes( array() );
$line_item->setType( \Wallee\Sdk\Model\LineItemType::PRODUCT );
$line_item->setUniqueId( WC_Wallee_Unique_Id::get_uuid() );
return array( $line_item );
}
return $line_items;
}
/**
* Modify order total method change.
*
* @param mixed $total Total.
* @param mixed $order Order.
* @return int|mixed
*/
public function modify_order_total_method_change( $total, $order ) {
if ( WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment && wcs_is_subscription( $order ) ) {
$total = 0;
}
return $total;
}
/**
* Update transaction from order.
*
* @param \Wallee\Sdk\Model\AbstractTransactionPending $transaction Transaction.
* @param mixed $order Order.
* @return \Wallee\Sdk\Model\AbstractTransactionPending
*/
public function update_transaction_from_order( \Wallee\Sdk\Model\AbstractTransactionPending $transaction, $order ) {
if ( wcs_order_contains_subscription( $order, array( 'parent', 'resubscribe', 'switch', 'renewal' ) ) ) {
$transaction->setTokenizationMode( \Wallee\Sdk\Model\TokenizationMode::FORCE_CREATION_WITH_ONE_CLICK_PAYMENT );
}
if ( WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment ) {
$transaction->setTokenizationMode( \Wallee\Sdk\Model\TokenizationMode::FORCE_CREATION_WITH_ONE_CLICK_PAYMENT );
}
return $transaction;
}
/**
* Update subscription data.
*
* @param \Wallee\Sdk\Model\Transaction $transaction Transaction.
* @param mixed $order Order.
* @return void
*/
public function update_subscription_data( \Wallee\Sdk\Model\Transaction $transaction, $order ) {
if ( wcs_order_contains_subscription( $order, array( 'renewal', 'parent', 'resubscribe', 'switch' ) ) ) {
$order->add_meta_data( '_wallee_subscription_space_id', $transaction->getLinkedSpaceId(), true );
$order->add_meta_data( '_wallee_subscription_token_id', $transaction->getToken()->getId(), true );
$order->save();
$subscriptions = wcs_get_subscriptions_for_order( $order, array( 'parent', 'switch' ) );
foreach ( $subscriptions as $subscription ) {
$subscription->add_meta_data( '_wallee_subscription_space_id', $transaction->getLinkedSpaceId(), true );
$subscription->add_meta_data( '_wallee_subscription_token_id', $transaction->getToken()->getId(), true );
$subscription->save();
}
}
if ( wcs_is_subscription( $order->get_id() ) ) {
$order->add_meta_data( '_wallee_subscription_space_id', $transaction->getLinkedSpaceId(), true );
$order->add_meta_data( '_wallee_subscription_token_id', $transaction->getToken()->getId(), true );
$order->save();
}
if ( wcs_is_subscription( $order->get_id() ) && $this->is_transaction_method_change( $transaction ) ) {
$gateway_id = $order->get_meta( '_wallee_gateway_id', true, 'edit' );
$meta_data = array(
'post_meta' => array(
'_wallee_subscription_space_id' => array(
'value' => $transaction->getLinkedSpaceId(),
'label' => 'wallee Space Id',
),
'_wallee_subscription_token_id' => array(
'value' => $transaction->getToken()->getId(),
'label' => 'wallee Token Id',
),
),
);
WC_Subscriptions_Change_Payment_Gateway::update_payment_method( $order, $gateway_id, $meta_data );
if ( WC_Subscriptions_Change_Payment_Gateway::will_subscription_update_all_payment_methods( $order ) ) {
WC_Subscriptions_Change_Payment_Gateway::update_all_payment_methods_from_subscription( $order, $gateway_id );
}
}
}
/**
* Update transaction info.
*
* @param WC_Wallee_Entity_Transaction_Info $info Info.
* @param \Wallee\Sdk\Model\Transaction $transaction Transaction.
* @param WC_Order $order Oder.
* @return WC_Wallee_Entity_Transaction_Info
*/
public function update_transaction_info( WC_Wallee_Entity_Transaction_Info $info, \Wallee\Sdk\Model\Transaction $transaction, WC_Order $order ) {
if ( wcs_is_subscription( $order->get_id() ) ) {
if ( in_array( $transaction->getState(), array( \Wallee\Sdk\Model\TransactionState::FAILED, \Wallee\Sdk\Model\TransactionState::VOIDED, \Wallee\Sdk\Model\TransactionState::FULFILL, \Wallee\Sdk\Model\TransactionState::DECLINE ) ) ) {
$info->set_order_id( null );
}
}
return $info;
}
/**
* Ignore status update for subscription.
* Do not change the status for subscriptions, this is done by modifying the corresponding orders
*
* @param mixed $status Status.
* @param WC_Order $order Order.
* @return mixed
*/
public function ignore_status_update_for_subscription( $status, WC_Order $order ) {
if ( wcs_is_subscription( $order->get_id() ) ) {
$status = $order->get_status();
}
return $status;
}
/**
* Set transaction ids into session.
*
* @param mixed $subscription Subscription.
* @param WC_Order $order Order.
* @return void
*/
public function set_transaction_ids_into_session( $subscription, WC_Order $order ) {
$session_handler = WC()->session;
$existing_transaction = WC_Wallee_Entity_Transaction_Info::load_by_order_id( $order->get_id() );
if ( $existing_transaction->get_id() !== null && $existing_transaction->get_state() == \Wallee\Sdk\Model\TransactionState::PENDING ) {
$session_handler->set( 'wallee_transaction_id', $existing_transaction->get_transaction_id() );
$session_handler->set( 'wallee_space_id', $existing_transaction->get_space_id() );
}
}
/**
* Check failed renewal order.
*
* @param mixed $is_failed_renewal_order Is failed renewal order.
* @param mixed $order_id Order id.
* @param mixed $orders_old_status Orders old status.
* @return mixed
*/
public function check_failed_renewal_order( $is_failed_renewal_order, $order_id, $orders_old_status ) {
$order = WC_Order_Factory::get_order( $order_id );
if ( $order ) {
$gateway = wc_get_payment_gateway_by_order( $order );
if ( $gateway instanceof WC_Wallee_Gateway ) {
if ( 'failed' == $orders_old_status ) {
update_post_meta( $order_id, '_wallee_subscription_failed_renewal', true );
return $is_failed_renewal_order;
}
return get_post_meta( $order_id, '_wallee_subscription_failed_renewal', true );
}
}
return $is_failed_renewal_order;
}
}
}
WooCommerce_Wallee_Subscription::instance();