Search code examples
laravellaravel-5laravel-5.3stripe-paymentslaravel-cashier

Laravel Cashier -How to Modify cashier to store additional parameter in user database


I'm using Laravel Cashier along with Stripe to manage subscriptions. The user will put their credit card information when signing up and subscribes to a plan,but i also want them to put some credits into their account when they subscribing for a specific plan.

For example if a user subscribe for Bronze package i want to put 300 credits (lets say) into their account once the subscription is completed successfully. How can i do that?? any advice.

So far my subscription code is like this

Subscription Controller

// create the users subscription
        // grab the credit card token
        $ccToken = $request->input('cc_token');
        $plan = $request->input('plan');

        // create the subscription
        try {
            $user->newSubscription('main', 'plan_CWtS4yCNvo5vAj')->create($ccToken, [
                'email' => $user->email
            ]);
        } catch (\Exception $e) {
            return back()->withErrors(['message' => 'Error creating subscription.'.$e->getMessage()]);
        }

        return redirect()->back()->with('success','Successfully subscribed');

Since user database is populated by laravel cashier itself once the subscription is done i have no idea how can i alter so. Any help is appreciated. Thanks.


Solution

  • The create() method returns an instance of Cashier Subscribtion model of the created row, so you can collect the returned value from create() in a ( $subscribtion ) variable then save whatever you want on this instance :

    $subscribtion = $user->newSubscription('main', 'plan_CWtS4yCNvo5vAj')
                         ->create($ccToken, [
                             'email' => $user->email
                         ]);
    $subscribtion->credits = 300;
    //some other stuff
    $subscribtion->save();