where('order_id', $order->id)->first(); if ($existing !== null) { return $existing; } $start = now(); return Subscription::create(array_merge( // The version the order carries, when the checkout recorded one: // what the customer saw beats what happens to be on sale by the // time their payment reaches us. Subscription::snapshotFrom($order->plan, $term, $order->plan_version_id), [ 'customer_id' => $order->customer_id, 'order_id' => $order->id, 'started_at' => $start, 'current_period_start' => $start, 'current_period_end' => $term === Subscription::TERM_YEARLY ? $start->copy()->addYear() : $start->copy()->addMonth(), 'status' => 'active', ], )); } }