laravel / cashier-stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services.
https://laravel.com/docs/billing
MIT License
2.37k stars 671 forks source link

Calculate prorate amount #45

Closed alexbowers closed 10 years ago

alexbowers commented 10 years ago

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?

bmadigan commented 10 years ago

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

taylorotwell commented 10 years ago

Stripe will handle this for you.

alexbowers commented 10 years ago

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" notifications@github.com 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 .

woodymendoza commented 5 years ago

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, '.', '');
    }
mateo2181 commented 4 years ago

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

shanka12 commented 4 years ago

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.