Inconsistency when trying to calculate the cost of proration in stripe

15 Views Asked by At

I have setup stripe subscription with recurring payment. I have two plans 1.Basic plan(30$), 2.Premium plan(40$). Basically i have subscribed with basic plan with 30$ and after few days i have switch to premium plan. For that process first i cancel the current subscription and create new subscription. i have added proration date when create new subscription but it not calculate proper price. here is my code.

if(auth()->check()){ 
    $authUserPlan = SubscribedUser::where('user_id',auth()->user()->id)->first();            } 
if(isset($authUserPlan) && !empty($authUserPlan)){
    // Retrieve user's current subscription
    $currentSubscription = $stripe->subscriptions->retrieve(
        $authUserPlan->stripe_subscription_id,
        []
    );      
    // Calculate the remaining time in the current billing period
    $currentPeriodEnd = Carbon::createFromTimestamp($currentSubscription->current_period_end);
    $remainingTime = now()->diffInHours($currentPeriodEnd);

    // Calculate the proration date 
        $prorationDate = $currentPeriodEnd->subHours($remainingTime); 
        $currentTimestamp = time(); 
        $new_date = strtotime('+2 minutes', $currentTimestamp);

        // Cancel the current subscription immediately
         $stripe->subscriptions->update( 
            $currentSubscription->id, ['cancel_at_period_end' => true] 
         );

    // Create a new subscription for the user 
    $newSubscription = $stripe->subscriptions->create([ 
        'customer' => auth()->user()->stripe_id, 
        'items' => [ 
            ['price' => $plan->stripe_plan], // Replace 'price' with the new price ID 
         ],              
        'billing_cycle_anchor' => $new_date,     
        'proration_behavior' => 'always_invoice', 
    ]); 
} 
0

There are 0 best solutions below