77use WC_Order ;
88
99class YithSubscriptionPluginHandler implements SubscriptionHandlerInterface {
10- private $ moneiPaymentServices ;
10+ private $ moneiPaymentServices ;
1111
12- public function __construct ( MoneiSdkClientFactory $ sdkClient ) {
13- $ this ->moneiPaymentServices = new MoneiPaymentServices ( $ sdkClient );
12+ public function __construct ( MoneiSdkClientFactory $ sdkClient ) {
13+ $ this ->moneiPaymentServices = new MoneiPaymentServices ( $ sdkClient );
1414 add_action (
1515 'ywsbs_pay_renew_order_with_ ' . MONEI_GATEWAY_ID ,
1616 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 ();
2121 $ this ->scheduled_subscription_payment ( $ amount_to_charge , $ renew_order );
2222 },
2323 10 ,
2424 1
2525 );
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+ );
2637 }
2738
2839 /**
@@ -59,24 +70,24 @@ public function is_subscription_change_payment_page() {
5970 return ( isset ( $ _GET ['pay_for_order ' ] ) && isset ( $ _GET ['change_payment_method ' ] ) ); // phpcs:ignore
6071 }
6172
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 );
6475 return $ order ->get_meta ( 'subscriptions ' );
6576 }
6677
6778 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+ }
8091 }
8192
8293
@@ -185,16 +196,15 @@ public function add_extra_info_to_subscriptions_payment_method_title( $payment_m
185196 * @param WC_Order $renewal_order
186197 */
187198 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+ }
198208 $ payload = array (
199209 'orderId ' => (string ) $ renewal_order ->get_id (),
200210 'amount ' => monei_price_format ( $ amount_to_charge ),
@@ -236,18 +246,18 @@ public function scheduled_subscription_payment( $amount_to_charge, $renewal_orde
236246 }
237247 }
238248
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.
248258
249- return $ subscription_id ? ywsbs_get_subscription ( $ subscription_id ) : false ;
250- }
259+ return $ subscription_id ? ywsbs_get_subscription ( $ subscription_id ) : false ;
260+ }
251261
252262 public function init_subscriptions ( array $ supports , string $ gateway_id ): array {
253263 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
267277 /**
268278 * From renewal order, get monei sequence id.
269279 *
270- * @param $renewal_order
280+ * @param $subscription
271281 *
272282 * @return string|false
273283 */
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 ' );
276286 }
277287
278288 /**
0 commit comments