Cashier-stripe: Calculate prorate amount

Created on 12 Apr 2014  Â·  6Comments  Â·  Source: laravel/cashier-stripe

Is there any part of the current API which helps to calculate the prorate amount.

For example, if you are upgrading from a monthly to a yearly plan, then the difference gets paid immediately (Say, from $10 to $100), then there would be a $90 fee taken immediately. Is there a way already built in which calculates this based on Stripe plans?

Most helpful comment

This is how I solved this issue - hope this helps, open to better suggestions.

use Stripe\Invoice as StripeInvoice;
    /**
     * @param  string  $plan_id
     * @param  \App\User $user
     * @return string
     */
    private function previewProrate(string $plan_id, $user) {

        $proration_date = time();
        $subscription = $user->subscription('main')->asStripeSubscription();

        $items = [
            [
                'id' => $subscription->items->data[0]->id,
                'plan' => $plan_id,
            ],
        ];

        $invoice = StripeInvoice::upcoming([
            'customer' => $subscription->customer,
            'subscription' => $subscription->id,
            'subscription_items' => $items,
            'subscription_proration_date' => $proration_date,
        ],['api_key' => $user->getStripeKey()]);

        $cost = 0;
        $current_prorations = [];
        foreach ($invoice->lines->data as $line) {
            if ($line->period->start == $proration_date) {
                array_push($current_prorations, $line);
                $cost += $line->amount;
            }
        }

        return number_format((float)$cost, 2, '.', '');
    }

All 6 comments

I believe it does it for you. If you look at this video (you will need a monthly subscription to view) at the 18min mark he shows how it works.
https://laracasts.com/lessons/painless-subscriptions-with-laravel-and-stripe

Stripe will handle this for you.

It was more to find the value to output to the user. Such as, if you
upgrade today, this is how much you will be paying. Not to send it to
stripe, but to fetch from them
On Apr 16, 2014 4:05 AM, "Taylor Otwell" [email protected] wrote:

Closed #45 https://github.com/laravel/cashier/issues/45.

—
Reply to this email directly or view it on GitHubhttps://github.com/laravel/cashier/issues/45
.

This is how I solved this issue - hope this helps, open to better suggestions.

use Stripe\Invoice as StripeInvoice;
    /**
     * @param  string  $plan_id
     * @param  \App\User $user
     * @return string
     */
    private function previewProrate(string $plan_id, $user) {

        $proration_date = time();
        $subscription = $user->subscription('main')->asStripeSubscription();

        $items = [
            [
                'id' => $subscription->items->data[0]->id,
                'plan' => $plan_id,
            ],
        ];

        $invoice = StripeInvoice::upcoming([
            'customer' => $subscription->customer,
            'subscription' => $subscription->id,
            'subscription_items' => $items,
            'subscription_proration_date' => $proration_date,
        ],['api_key' => $user->getStripeKey()]);

        $cost = 0;
        $current_prorations = [];
        foreach ($invoice->lines->data as $line) {
            if ($line->period->start == $proration_date) {
                array_push($current_prorations, $line);
                $cost += $line->amount;
            }
        }

        return number_format((float)$cost, 2, '.', '');
    }

there is a way to get the price with prorate before the user has subscribed to plan? I'm using cashier 9.3.5

This is how I solved this issue - hope this helps, open to better suggestions.

use Stripe\Invoice as StripeInvoice;
    /**
     * @param  string  $plan_id
     * @param  \App\User $user
     * @return string
     */
    private function previewProrate(string $plan_id, $user) {

        $proration_date = time();
        $subscription = $user->subscription('main')->asStripeSubscription();

        $items = [
            [
                'id' => $subscription->items->data[0]->id,
                'plan' => $plan_id,
            ],
        ];

        $invoice = StripeInvoice::upcoming([
            'customer' => $subscription->customer,
            'subscription' => $subscription->id,
            'subscription_items' => $items,
            'subscription_proration_date' => $proration_date,
        ],['api_key' => $user->getStripeKey()]);

        $cost = 0;
        $current_prorations = [];
        foreach ($invoice->lines->data as $line) {
            if ($line->period->start == $proration_date) {
                array_push($current_prorations, $line);
                $cost += $line->amount;
            }
        }

        return number_format((float)$cost, 2, '.', '');
    }

This is not work with during subscription cancel.
I got this error

Stripe\Exception\InvalidRequestException
You cannot preview the upcoming invoice for a canceled subscription.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

tania-pets picture tania-pets  Â·  3Comments

andyrobertscarpediem picture andyrobertscarpediem  Â·  4Comments

MarGul picture MarGul  Â·  5Comments

VirtualDesignFactory picture VirtualDesignFactory  Â·  4Comments

borutjures picture borutjures  Â·  3Comments