created subscription ids */ public static function createFromOrder(\WC_Order $order, string $customerRef): array { global $wpdb; $ids = []; $now = current_time('mysql', true); foreach ($order->get_items() as $item) { if (!$item instanceof \WC_Order_Item_Product) { continue; } $productId = $item->get_product_id(); if (!SubscriptionProduct::isSubscription($productId)) { continue; } $terms = SubscriptionProduct::terms($productId); $isTrial = $terms['trial_days'] > 0; // First period end: trial end if trialing, else now + interval. $periodEnd = $isTrial ? gmdate('Y-m-d H:i:s', strtotime($now) + $terms['trial_days'] * DAY_IN_SECONDS) : self::addInterval($now, $terms['interval_unit'], $terms['interval_count']); $wpdb->insert(Subscription::table(), [ 'customer_id' => (int) $order->get_customer_id(), 'order_id' => (int) $order->get_id(), 'product_id' => $productId, 'status' => $isTrial ? Subscription::STATUS_TRIALING : Subscription::STATUS_ACTIVE, 'gateway' => 'stripe_sepa', 'gateway_ref' => $customerRef, // "cus_…"; PM appended by webhook → "cus|pm" 'amount' => (float) $item->get_total(), 'currency' => $order->get_currency(), 'interval_unit' => $terms['interval_unit'], 'interval_count' => $terms['interval_count'], 'trial_end' => $isTrial ? $periodEnd : null, 'current_period_start' => $now, 'current_period_end' => $periodEnd, ]); $ids[] = (int) $wpdb->insert_id; } return $ids; } private static function addInterval(string $from, string $unit, int $count): string { $ts = strtotime($from) ?: time(); return gmdate('Y-m-d H:i:s', strtotime("+{$count} {$unit}", $ts) ?: $ts); } }