Home>
A recurring payment service using a Stripe subscription is being built at Laravel.
When recurring customers change their plans, they want to show the difference at the time of the change in advance.

I tried to customize for Laravel with reference to the following site
It was not possible due to lack of knowledge.

If i know someone
If i teach me, it will be very helpful.

specifically

There are two types of monthly payment plans: Light (1,000 yen) and Standard (5,000 yen).
If a customer who joined the Light Plan on November 1 changes to the Standard Plan on the 15th of the middle of the month
I want to automatically calculate the difference at the next billing.

* The daily bill calculation method follows the basic concept of periodic payments in Stripe Billing (see the URL below).
* Invoice issued on the first day of every month.
* I want to calculate automatically when downgrading from Standard to Light.
* Table configuration is the same as Cashier migration.

[Reference URL] Stripe Billing-Proportional distribution in recurring payments
https://qiita.com/y_toku/items/404b3c99632161f18579

Applicable source code

[Reference URL] https://stripe.com/docs/billing/subscriptions/prorations#prorations

// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\ Stripe \ Stripe :: setApiKey ('sk_test_Ar66eiYNVgxtbd07ig6vIugp009e1S3qwk');
// Set proration date to this moment:
$proration_date = time ();
$subscription = \ Stripe \ Subscription :: retrieve ('sub_49ty4767H20z6a');
// See what the next invoice would look like with a plan switch
// and proration set:
$items = [
    [
        'id' =>$subscription->items->data [0]->id,
        'plan' =>'plan_CBb6IXqvTLXp3f', # Switch to new plan
    ],
];
$invoice = \ Stripe \ Invoice :: upcoming ([
    'customer' =>'cus_4fdAW5ftNQow1a',
    'subscription' =>'sub_49ty4767H20z6a',
    'subscription_items' =>$items,
    'subscription_proration_date' =>$proration_date,
]);
// Calculate the proration cost:
$cost = 0;
$current_prorations = [];
foreach ($invoice->lines->data as $line) {
    if ($line->period->start == $proration_date) {
        array_push ($current_prorations, $line);
        $cost + = $line->amount;
    }
}
Error code
No API key provided. (HINT: set your API key using "Stripe :: setApiKey ()". You can generate API keys from the Stripe web interface.See https: // stripe .com/api for details, or email [email protected] if you have any questions.

The above code
$subscription = \ Stripe \ Subscription :: retrieve ('sub_49ty4767H20z6a');
Replaced with the following code
$subscription = $user->subscription ('main');

Error code
Trying to get property 'data' of non-object
// $subscription->items->data [0]->id is missing
Development environment

Laravel 6.0.3
laravel/cashier 9.3.5
PHP 7.3.5
xampp
windows 10

I would be happy if you could answer.

  • Answer # 1

    Thank you.
    I will describe the cause of the error.

    \ Stripe \ Stripe :: setApiKey ();
    The value of the Stripe secret key in this code was incorrect.

    The error code was "No API key provided"
    I should have obediently obeyed.

    Thank you for your advice.

    Success code
    use App \ Subscription;
    $user = \ Auth :: user ();
    // Set your secret key: remember to change this to your live secret key in production
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \ Stripe \ Stripe :: setApiKey (env ('STRIPE_SECRET'));
    // Set proration date to this moment:
    $proration_date = time ();
    $subscription_id = Subscription :: where ('user_id', $user->id)->value ('stripe_id');
    $subscription = \ Stripe \ Subscription :: retrieve ($subscription_id);
    // See what the next invoice would look like with a plan switch
    // and proration set:
    $items = [
        [
            'id' =>$subscription->items->data [0]->id,
            'plan' =>env ('STRIPE_STANDARD_ID'), # Switch to new plan
        ],
    ];
    $invoice = \ Stripe \ Invoice :: upcoming ([
        'customer' =>$user->stripe_id,
        'subscription' =>$subscription_id,
        'subscription_items' =>$items,
        'subscription_proration_date' =>$proration_date,
    ]);
    // Calculate the proration cost:
    $cost = 0;
    $current_prorations = [];
    foreach ($invoice->lines->data as $line) {
        if ($line->period->start == $proration_date) {
            array_push ($current_prorations, $line);
            $cost + = $line->amount;
        }
    }
    echo $cost;// Difference on next billing