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

v10 - Use different API key for different user models #751

Closed garygreen closed 5 years ago

garygreen commented 5 years ago

In the old version of Cashier v9 it allowed you to specify a different API key for each user. In v10, it doesn't seem this is easily possible because everything has switched to using Cashier::stripeOptions() function which always returns the cashier.secret config value and isn't specific to a particular user.

For our application, we actually split our stripe account into two - one for males and a seperate account for females. It doesn't seem easily possible to convert our codebase over to Cashier v10 for this reason.

Was there any design consideration for removing the functionality of having different stripe keys per-user model? https://github.com/laravel/cashier/blob/9.0/src/Billable.php#L624

driesvints commented 5 years ago

Hmm I see how that can be a concern for you. Multiple Stripe accounts were never really supported in Cashier as the cashier secret on the billable trait was only meant for the only model it should be set on (usually the default user model in your Laravel app). But I can definitely see why you'd use it like this.

Basically what you're asking is support for multiple Stripe accounts. We could provide support for this. I'll take a look at this real soon.

garygreen commented 5 years ago

Thanks Dries! The old version of Cashier did definitely support multiple different keys for users though, so the way v10 works now is a step backwards and makes it hard to have seperate keys.

This is how we used it in Cashier v9, you just override the getStripeKey method and Cashier will use it for all API calls.

<?php

class Member extends Eloquent {

    /**
     * Get stripe key to use for the member.
     * Female and Male accounts use a different Stripe account
     * This is used by Laravel Cashier.
     */
    public function getStripeKey()
    {
        return $this->isFemale() ?
               config('services.stripe-female.secret') :
               config('services.stripe-male.secret');
    }

}
driesvints commented 5 years ago

@garygreen that helped! I've sent in a PR here: https://github.com/laravel/cashier/pull/754

you can overwrite the stripeOptions method there to do any customization you want. Please do note the concern I had there.