7
7
use WC_Order ;
8
8
9
9
class YithSubscriptionPluginHandler implements SubscriptionHandlerInterface {
10
- private $ moneiPaymentServices ;
10
+ private $ moneiPaymentServices ;
11
11
12
- public function __construct ( MoneiSdkClientFactory $ sdkClient ) {
13
- $ this ->moneiPaymentServices = new MoneiPaymentServices ( $ sdkClient );
12
+ public function __construct ( MoneiSdkClientFactory $ sdkClient ) {
13
+ $ this ->moneiPaymentServices = new MoneiPaymentServices ( $ sdkClient );
14
14
add_action (
15
15
'ywsbs_pay_renew_order_with_ ' . MONEI_GATEWAY_ID ,
16
16
function ( $ renew_order ) {
17
- if ( ! $ renew_order instanceof \WC_Order ) {
18
- return false ;
19
- }
20
- $ amount_to_charge = $ renew_order ->get_total ();
17
+ if ( ! $ renew_order instanceof \WC_Order ) {
18
+ return false ;
19
+ }
20
+ $ amount_to_charge = $ renew_order ->get_total ();
21
21
$ this ->scheduled_subscription_payment ( $ amount_to_charge , $ renew_order );
22
22
},
23
23
10 ,
24
24
1
25
25
);
26
+ //whenever it creates a renew order it will check for this meta, as is just created we put to 0
27
+ add_action (
28
+ 'ywsbs_renew_subscription ' ,
29
+ function ( $ order_id , $ subscription_id ) {
30
+ $ order = wc_get_order ( $ order_id );
31
+ $ order ->update_meta_data ( 'failed_attemps ' , '0 ' );
32
+ $ order ->save ();
33
+ },
34
+ 10 ,
35
+ 2
36
+ );
26
37
}
27
38
28
39
/**
@@ -59,24 +70,24 @@ public function is_subscription_change_payment_page() {
59
70
return ( isset ( $ _GET ['pay_for_order ' ] ) && isset ( $ _GET ['change_payment_method ' ] ) ); // phpcs:ignore
60
71
}
61
72
62
- public function get_subscriptions_for_order ( int $ order_id ):array {
63
- $ order = wc_get_order ( $ order_id );
73
+ public function get_subscriptions_for_order ( int $ order_id ): array {
74
+ $ order = wc_get_order ( $ order_id );
64
75
return $ order ->get_meta ( 'subscriptions ' );
65
76
}
66
77
67
78
public function update_subscription_meta_data ( $ subscriptions , $ payment ): void {
68
- /**
69
- * Iterate all subscriptions contained in the order, and add sequence id and cc data individually.
70
- */
71
- foreach ( $ subscriptions as $ subscription ) {
72
- $ subscription = ywsbs_get_subscription ( $ subscription );
73
- $ meta = [
74
- ' _monei_sequence_id ' => $ payment ->getSequenceId (),
75
- ' _monei_payment_method_brand ' => $ payment ->getPaymentMethod ()->getCard ()->getBrand (),
76
- '_monei_payment_method_4_last_digits ' => $ payment ->getPaymentMethod ()->getCard ()->getLast4 ()
77
- ] ;
78
- $ subscription ->update_subscription_meta ( $ meta );
79
- }
79
+ /**
80
+ * Iterate all subscriptions contained in the order, and add sequence id and cc data individually.
81
+ */
82
+ foreach ( $ subscriptions as $ subscription ) {
83
+ $ subscription = ywsbs_get_subscription ( $ subscription );
84
+ $ meta = array (
85
+ ' _monei_sequence_id ' => $ payment ->getSequenceId (),
86
+ ' _monei_payment_method_brand ' => $ payment ->getPaymentMethod ()->getCard ()->getBrand (),
87
+ '_monei_payment_method_4_last_digits ' => $ payment ->getPaymentMethod ()->getCard ()->getLast4 (),
88
+ ) ;
89
+ $ subscription ->update_subscription_meta ( $ meta );
90
+ }
80
91
}
81
92
82
93
@@ -185,16 +196,15 @@ public function add_extra_info_to_subscriptions_payment_method_title( $payment_m
185
196
* @param WC_Order $renewal_order
186
197
*/
187
198
public function scheduled_subscription_payment ( $ amount_to_charge , $ renewal_order ): void {
188
- $ description = get_bloginfo ( 'name ' ) . ' - # ' . (string ) $ renewal_order ->get_id () . ' - Subscription Renewal ' ;
189
- $ order_id = $ renewal_order ->get_id ();
190
- $ is_a_renew = $ renewal_order ->get_meta ( 'is_a_renew ' );
191
- $ subscription = $ this ->get_subscription_from_renew_order ( $ renewal_order );
192
- $ sequence_id = $ this ->get_sequence_id_from_renewal_order ( $ subscription ->get_id () );
193
-
194
-
195
- if ( ! $ subscription || 'yes ' !== $ is_a_renew ) {
196
- WC_Monei_Logger::log ( sprintf ( 'Sorry, any subscription was found for order #%s or order is not a renew. ' , $ order_id ), 'subscription_payment ' );
197
- }
199
+ $ description = get_bloginfo ( 'name ' ) . ' - # ' . (string ) $ renewal_order ->get_id () . ' - Subscription Renewal ' ;
200
+ $ order_id = $ renewal_order ->get_id ();
201
+ $ is_a_renew = $ renewal_order ->get_meta ( 'is_a_renew ' );
202
+ $ subscription = $ this ->get_subscription_from_renew_order ( $ renewal_order );
203
+ $ sequence_id = $ this ->get_sequence_id_from_subscription ( $ subscription );
204
+
205
+ if ( ! $ subscription || 'yes ' !== $ is_a_renew ) {
206
+ WC_Monei_Logger::log ( sprintf ( 'Sorry, any subscription was found for order #%s or order is not a renew. ' , $ order_id ), 'subscription_payment ' );
207
+ }
198
208
$ payload = array (
199
209
'orderId ' => (string ) $ renewal_order ->get_id (),
200
210
'amount ' => monei_price_format ( $ amount_to_charge ),
@@ -236,18 +246,18 @@ public function scheduled_subscription_payment( $amount_to_charge, $renewal_orde
236
246
}
237
247
}
238
248
239
- /**
240
- * Get subscription object from renew order
241
- *
242
- * @param \WC_Order $renewal_order The WooCommerce order.
243
- * @return YWSBS_Subscription|bool
244
- */
245
- private function get_subscription_from_renew_order ( \WC_Order $ renewal_order ) {
246
- $ subscriptions = $ renewal_order ->get_meta ( 'subscriptions ' );
247
- $ subscription_id = ! empty ( $ subscriptions ) ? array_shift ( $ subscriptions ) : false ; // $subscriptions is always an array of 1 element.
249
+ /**
250
+ * Get subscription object from renew order
251
+ *
252
+ * @param \WC_Order $renewal_order The WooCommerce order.
253
+ * @return YWSBS_Subscription|bool
254
+ */
255
+ private function get_subscription_from_renew_order ( \WC_Order $ renewal_order ) {
256
+ $ subscriptions = $ renewal_order ->get_meta ( 'subscriptions ' );
257
+ $ subscription_id = ! empty ( $ subscriptions ) ? array_shift ( $ subscriptions ) : false ; // $subscriptions is always an array of 1 element.
248
258
249
- return $ subscription_id ? ywsbs_get_subscription ( $ subscription_id ) : false ;
250
- }
259
+ return $ subscription_id ? ywsbs_get_subscription ( $ subscription_id ) : false ;
260
+ }
251
261
252
262
public function init_subscriptions ( array $ supports , string $ gateway_id ): array {
253
263
add_action ( 'wc_gateway_monei_create_payment_success ' , array ( $ this , 'subscription_after_payment_success ' ), 1 , 3 );
@@ -267,12 +277,12 @@ public function init_subscriptions( array $supports, string $gateway_id ): array
267
277
/**
268
278
* From renewal order, get monei sequence id.
269
279
*
270
- * @param $renewal_order
280
+ * @param $subscription
271
281
*
272
282
* @return string|false
273
283
*/
274
- public function get_sequence_id_from_renewal_order ( $ renewal_order ) {
275
- return $ renewal_order -> get_meta ( '_monei_sequence_id ' , true );
284
+ public function get_sequence_id_from_subscription ( $ subscription ) {
285
+ return $ subscription -> get ( '_monei_sequence_id ' );
276
286
}
277
287
278
288
/**
0 commit comments