@@ -81,6 +81,9 @@ public function add_support() {
81
81
// save token/customer ID to subscription objects
82
82
add_action ( 'wc_payment_gateway_ ' . $ this ->get_gateway ()->get_id () . '_add_transaction_data ' , array ( $ this , 'save_payment_meta ' ), 10 , 2 );
83
83
84
+ // add additional subscription details to orders along with payment data
85
+ add_action ( 'wc_payment_gateway_ ' . $ this ->get_gateway ()->get_id () . '_get_order_base ' , [ $ this , 'add_subscriptions_details_to_order ' ], 10 , 2 );
86
+
84
87
// process renewal payments
85
88
add_action ( 'woocommerce_scheduled_subscription_payment_ ' . $ this ->get_gateway ()->get_id (), array ( $ this , 'process_renewal_payment ' ), 10 , 2 );
86
89
@@ -193,6 +196,115 @@ public function save_payment_meta( $order ) {
193
196
}
194
197
195
198
199
+ /**
200
+ * Adds subscription details to order base data.
201
+ *
202
+ * Details are added to a \WC_Order::$payment->subscriptions[] property.
203
+ * @see SV_WC_Payment_Gateway_Integration_Subscriptions::add_subscription_details_to_order()
204
+ * @internal
205
+ *
206
+ * @since 5.4.2-dev.1
207
+ *
208
+ * @param \WC_Order $order order object
209
+ * @param SV_WC_Payment_Gateway $gateway payment gateway
210
+ * @return \WC_Order
211
+ */
212
+ public function add_subscriptions_details_to_order ( $ order , $ gateway ) {
213
+
214
+ if ( isset ( $ order ->payment ) ) {
215
+
216
+ // defaults
217
+ $ order ->payment ->subscriptions = [];
218
+ $ order ->payment ->recurring = ! empty ( $ order ->payment ->recurring ) ?: false ;
219
+
220
+ // if the order contains a subscription (but is not a renewal)
221
+ if ( wcs_order_contains_subscription ( $ order ) ) {
222
+
223
+ $ order ->payment ->recurring = true ;
224
+
225
+ $ subscriptions = wcs_get_subscriptions_for_order ( $ order );
226
+
227
+ if ( ! empty ( $ subscriptions ) ) {
228
+
229
+ foreach ( $ subscriptions as $ subscription ) {
230
+
231
+ if ( $ subscription instanceof \WC_Subscription ) {
232
+
233
+ $ order ->payment ->subscriptions [] = $ this ->add_subscription_details_to_order ( $ subscription , false );
234
+ }
235
+ }
236
+ }
237
+
238
+ // order is for a subscription renewal
239
+ } elseif ( wcs_order_contains_renewal ( $ order ) ) {
240
+
241
+ $ order ->payment ->recurring = true ;
242
+
243
+ $ subscriptions = wcs_get_subscriptions_for_renewal_order ( $ order );
244
+
245
+ if ( ! empty ( $ subscriptions ) ) {
246
+
247
+ foreach ( $ subscriptions as $ subscription ) {
248
+
249
+ if ( $ subscription instanceof \WC_Subscription ) {
250
+
251
+ $ order ->payment ->subscriptions [] = $ this ->add_subscription_details_to_order ( $ subscription , true );
252
+ }
253
+ }
254
+ }
255
+ }
256
+ }
257
+
258
+ return $ order ;
259
+ }
260
+
261
+
262
+ /**
263
+ * Builds and returns a subscription's details data.
264
+ *
265
+ * @see SV_WC_Payment_Gateway_Integration_Subscriptions::add_subscriptions_details_to_order()
266
+ *
267
+ * \stdClass {
268
+ * int $id the subscription's ID
269
+ * bool $is_renewal whether the order is for a subscription renewal
270
+ * bool $is_installment whether the subscription is for an installment
271
+ * bool $is_first whether it is the first payment for an installment series
272
+ * bool $is_last whether it is the last payment for an installment series
273
+ *}
274
+ *
275
+ * @since 5.4.2-dev.1
276
+ *
277
+ * @param \WC_Subscription $subscription subscription object
278
+ * @param bool $renewal whether the subscription is a renewal
279
+ * @return \stdClass subscription details data
280
+ */
281
+ protected function add_subscription_details_to_order ( $ subscription , $ renewal ) {
282
+
283
+ $ details = new \stdClass ;
284
+
285
+ $ details ->id = max ( 0 , (int ) SV_WC_Order_Compatibility::get_prop ( $ subscription , 'id ' ) );
286
+ $ details ->is_renewal = (bool ) $ renewal ;
287
+ $ details ->is_installment = (bool ) $ subscription ->get_date ( 'end ' );
288
+
289
+ $ details ->is_first = $ details ->is_last = false ;
290
+
291
+ if ( $ details ->is_installment ) {
292
+
293
+ // if this is not a renewal, but the subscription has an end date, then this must be the first installment
294
+ if ( ! $ details ->is_renewal ) {
295
+ $ details ->is_first = true ;
296
+ }
297
+
298
+ // if the subscription has an end date, but there is no next payment date set, this must be the last installment
299
+ if ( ! (bool ) $ subscription ->get_date ( 'next_payment ' ) ) {
300
+ $ details ->is_last = true ;
301
+ }
302
+ }
303
+
304
+ return $ details ;
305
+ }
306
+
307
+
196
308
/**
197
309
* Process a subscription renewal payment
198
310
*
0 commit comments