@@ -98,17 +98,11 @@ public function __construct( WC_Wallee_Gateway $gateway ) {
98
98
*/
99
99
public function process_scheduled_subscription_payment ( $ amount_to_charge , WC_Order $ order ) {
100
100
try {
101
- $ token_space_id = get_post_meta ( $ order ->get_id (), '_wallee_subscription_space_id ' , true );
102
- $ token_id = get_post_meta ( $ order ->get_id (), '_wallee_subscription_token_id ' , true );
101
+ $ token_data = $ this ->get_token_data_from_order ($ order );
102
+
103
+ $ token_space_id = $ token_data ['_wallee_subscription_space_id ' ];
104
+ $ token_id = $ token_data ['_wallee_subscription_token_id ' ];
103
105
104
- if ( empty ( $ token_space_id ) || get_option ( WooCommerce_Wallee::CK_SPACE_ID ) != $ token_space_id ) {
105
- $ order ->update_status ( 'failed ' , __ ( 'The token space and the configured space are not equal. ' , 'woo-wallee-subscription ' ) );
106
- return ;
107
- }
108
- if ( empty ( $ token_id ) ) {
109
- $ order ->update_status ( 'failed ' , __ ( 'There is no token associated with this subscription. ' , 'woo-wallee-subscription ' ) );
110
- return ;
111
- }
112
106
$ transaction_service = WC_Wallee_Subscription_Service_Transaction::instance ();
113
107
114
108
$ transaction_info = WC_Wallee_Entity_Transaction_Info::load_by_order_id ( $ order ->get_id () );
@@ -134,6 +128,57 @@ public function process_scheduled_subscription_payment( $amount_to_charge, WC_Or
134
128
}
135
129
}
136
130
131
+ /**
132
+ * Get token data from original order
133
+ *
134
+ * @param WC_Order $order Order.
135
+ * @return array
136
+ */
137
+ private function get_token_data_from_order ($ order )
138
+ {
139
+ $ order_id = $ order ->get_id ();
140
+ $ token_data = [];
141
+ $ token_data ['_wallee_subscription_space_id ' ] = get_post_meta ( $ order_id , '_wallee_subscription_space_id ' , true );
142
+ $ token_data ['_wallee_subscription_token_id ' ] = get_post_meta ( $ order_id , '_wallee_subscription_token_id ' , true );
143
+
144
+ if ( ! isset ($ token_data ['_wallee_subscription_space_id ' ]) || isset ($ token_data ['_wallee_subscription_token_id ' ]) ) {
145
+
146
+ $ subscriptions = wcs_get_subscriptions_for_renewal_order ( $ order_id );
147
+ // In theory, each of the array elements should contain the same token and space data
148
+ $ subscription_object = array_pop ($ subscriptions );
149
+ $ subscription_meta = $ subscription_object ->get_meta_data ();
150
+ $ token_data = [];
151
+ $ wallee_subscription_keys = [
152
+ '_wallee_subscription_space_id ' ,
153
+ '_wallee_subscription_token_id '
154
+ ];
155
+
156
+ foreach ( $ subscription_meta as $ meta_data ) {
157
+ $ contained_data = $ meta_data ->get_data ();
158
+ if ( in_array ($ contained_data ['key ' ], $ wallee_subscription_keys ) ) {
159
+ $ token_data [$ contained_data ['key ' ]] = $ contained_data ['value ' ];
160
+ }
161
+ }
162
+ }
163
+
164
+ if ( ! isset ($ token_data ['_wallee_subscription_space_id ' ]) ) {
165
+ $ order ->update_status ( 'failed ' , __ ( 'No space ID is found. ' , 'woo-wallee-subscription ' ) );
166
+ throw new Exception ('Missing space id details ' );
167
+ }
168
+
169
+ if ( ! isset ($ token_data ['_wallee_subscription_token_id ' ]) ) {
170
+ $ order ->update_status ( 'failed ' , __ ( 'No token ID is found. ' , 'woo-wallee-subscription ' ) );
171
+ throw new Exception ('Missing token id ' );
172
+ }
173
+
174
+ if ( get_option ( WooCommerce_Wallee::WALLEE_CK_SPACE_ID ) != $ token_data ['_wallee_subscription_space_id ' ] ) {
175
+ $ order ->update_status ( 'failed ' , __ ( 'The token space and the configured space are not equal. ' , 'woo-wallee-subscription ' ) );
176
+ throw new Exception ('Token space does not match configured space ' );
177
+ }
178
+
179
+ return $ token_data ;
180
+ }
181
+
137
182
138
183
/**
139
184
* Update payment method.
@@ -261,7 +306,7 @@ public function validate_subscription_payment_meta( $payment_method_id, $payment
261
306
if ( $ this ->gateway ->id === $ payment_method_id ) {
262
307
if ( ! isset ( $ payment_meta ['post_meta ' ]['_wallee_subscription_space_id ' ]['value ' ] ) || empty ( $ payment_meta ['post_meta ' ]['_wallee_subscription_space_id ' ]['value ' ] ) ) {
263
308
throw new Exception ( __ ( 'The wallee Space Id value is required. ' , 'woo-wallee-subscription ' ) );
264
- } elseif ( get_option ( WooCommerce_Wallee::CK_SPACE_ID ) != $ payment_meta ['post_meta ' ]['_wallee_subscription_space_id ' ]['value ' ] ) {
309
+ } elseif ( get_option ( WooCommerce_Wallee::WALLEE_CK_SPACE_ID ) != $ payment_meta ['post_meta ' ]['_wallee_subscription_space_id ' ]['value ' ] ) {
265
310
throw new Exception ( __ ( 'The wallee Space Id needs to be in the same space as configured in the main configuration. ' , 'woo-wallee-subscription ' ) );
266
311
}
267
312
if ( ! isset ( $ payment_meta ['post_meta ' ]['_wallee_subscription_token_id ' ]['value ' ] ) || empty ( $ payment_meta ['post_meta ' ]['_wallee_subscription_token_id ' ]['value ' ] ) ) {
0 commit comments