11<?php
22
3+ use Monei \Features \Subscriptions \SubscriptionService ;
4+ use Monei \Features \Subscriptions \WooCommerceSubscriptionsHandler ;
5+ use Monei \Features \Subscriptions \YithSubscriptionPluginHandler ;
36use Monei \Services \ApiKeyService ;
47use Monei \Services \payment \MoneiPaymentServices ;
58use Monei \Services \sdk \MoneiSdkClientFactory ;
1619 */
1720class WC_Monei_Addons_Redirect_Hooks {
1821
19- /**
20- * Use Subscription trait.
21- */
22- use WC_Monei_Subscriptions_Trait;
23-
2422 private MoneiPaymentServices $ moneiPaymentServices ;
2523
2624 /**
@@ -32,7 +30,10 @@ public function __construct() {
3230 //TODO use the container
3331 $ apiKeyService = new ApiKeyService ();
3432 $ sdkClient = new MoneiSdkClientFactory ( $ apiKeyService );
33+ $ wooHandler = new WooCommerceSubscriptionsHandler ( $ sdkClient );
34+ $ yithHandler = new YithSubscriptionPluginHandler ( $ sdkClient );
3535 $ this ->moneiPaymentServices = new MoneiPaymentServices ( $ sdkClient );
36+ $ this ->subscriptionService = new SubscriptionService ( $ wooHandler , $ yithHandler );
3637 }
3738
3839 /**
@@ -48,6 +49,7 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
4849 if ( ! isset ( $ _GET ['id ' ] ) ) {
4950 return ;
5051 }
52+ WC_Monei_Logger::log ( 'Changing the method, updating the sequence id for subscriptions ' );
5153
5254 $ payment_id = filter_input ( INPUT_GET , 'id ' , FILTER_CALLBACK , array ( 'options ' => 'sanitize_text_field ' ) );
5355 $ order_id = filter_input ( INPUT_GET , 'orderId ' , FILTER_CALLBACK , array ( 'options ' => 'sanitize_text_field ' ) );
@@ -60,7 +62,8 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
6062 }
6163
6264 $ order_id = $ verification_order_id [0 ];
63- if ( ! $ this ->is_order_subscription ( $ order_id ) ) {
65+ $ handler = $ this ->subscriptionService ->getHandler ();
66+ if ( ! $ handler || ! $ handler ->is_subscription_order ( $ order_id ) ) {
6467 return ;
6568 }
6669
@@ -69,12 +72,9 @@ public function subscriptions_save_sequence_id_on_payment_method_change() {
6972 * We need to update parent from subscription, where sequence id is stored.
7073 */
7174 $ payment = $ this ->moneiPaymentServices ->get_payment ( $ payment_id );
72- $ subscription = new WC_Subscription ( $ order_id );
75+ $ subscriptions = $ handler ->get_subscriptions_for_order ( $ order_id );
76+ $ handler ->update_subscription_meta_data ($ subscriptions , $ payment );
7377
74- $ subscription ->update_meta_data ( '_monei_sequence_id ' , $ payment ->getSequenceId () );
75- $ subscription ->update_meta_data ( '_monei_payment_method_brand ' , $ payment ->getPaymentMethod ()->getCard ()->getBrand () );
76- $ subscription ->update_meta_data ( '_monei_payment_method_4_last_digits ' , $ payment ->getPaymentMethod ()->getCard ()->getLast4 () );
77- $ subscription ->save_meta_data ();
7878 } catch ( Exception $ e ) {
7979 wc_add_notice ( __ ( 'Error while saving sequence id. Please contact admin. Payment ID: ' , 'monei ' ) . $ payment_id , 'error ' );
8080 WC_Monei_Logger::log ( $ e ->getMessage (), 'error ' );
@@ -100,27 +100,19 @@ public function subscriptions_save_sequence_id() {
100100 /**
101101 * Bail when not subscription.
102102 */
103- if ( ! $ this ->is_order_subscription ( $ order_id ) ) {
103+ $ handler = $ this ->subscriptionService ->getHandler ();
104+ if ( ! $ handler || ! $ handler ->is_subscription_order ( $ order_id ) ) {
104105 return ;
105106 }
106107
107108 try {
108-
109- $ subscriptions = wcs_get_subscriptions_for_order ( $ order_id , array ( 'order_type ' => array ( 'any ' ) ) );
109+ $ subscriptions = $ handler ->get_subscriptions_for_order ( $ order_id );
110110 if ( ! $ subscriptions ) {
111111 return ;
112112 }
113113
114114 $ payment = $ this ->moneiPaymentServices ->get_payment ( $ payment_id );
115- /**
116- * Iterate all subscriptions contained in the order, and add sequence id and cc data individually.
117- */
118- foreach ( $ subscriptions as $ subscription_id => $ subscription ) {
119- $ subscription ->update_meta_data ( '_monei_sequence_id ' , $ payment ->getSequenceId () );
120- $ subscription ->update_meta_data ( '_monei_payment_method_brand ' , $ payment ->getPaymentMethod ()->getCard ()->getBrand () );
121- $ subscription ->update_meta_data ( '_monei_payment_method_4_last_digits ' , $ payment ->getPaymentMethod ()->getCard ()->getLast4 () );
122- $ subscription ->save_meta_data ();
123- }
115+ $ handler ->update_subscription_meta_data ( $ subscriptions , $ payment );
124116 } catch ( Exception $ e ) {
125117 wc_add_notice ( __ ( 'Error while saving sequence id. Please contact admin. Payment ID: ' , 'monei ' ) . $ payment_id , 'error ' );
126118 WC_Monei_Logger::log ( $ e ->getMessage (), 'error ' );
0 commit comments