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 672 forks source link

The second argument to Stripe API method calls is an optional per-request apiKey #243

Closed Yorchi closed 7 years ago

Yorchi commented 8 years ago

I having this error at try to use this.

The second argument to Stripe API method calls is an optional per-request apiKey, 
which must be a string, or per-request options, which must be an array. 
(HINT: you can set a global apiKey by "Stripe::setApiKey(<apiKey>)") 
(View: /home/vagrant/Documents/Code/Homestead/facturas/resources/views/dashboard/cuenta/partials/_facturacion.blade.php) 
(View: /home/vagrant/Documents/Code/Homestead/facturas/resources/views/dashboard/cuenta/partials/_facturacion.blade.php)
jsmmth commented 8 years ago

Did you manage to fix this issue. I am still having it?

Yorchi commented 8 years ago

hi @JosephSmith127, yes still having this issue, what am I doing wrong?

jsmmth commented 8 years ago

@andradedev Wish I knew! Literally have just been faced with this issue. Will make sure I post a solution when I find one!

jsmmth commented 8 years ago

@andradedev I managed to fix. Make sure in your config > services.php you also include the key as well as the secret:

'stripe' => [ 'model' => App\User::class, 'key' => env('STRIPE_KEY'), 'secret' => env('STRIPE_SECRET'), ],

Yorchi commented 8 years ago

hi @JosephSmith127, I already have the API SECRET, but where do I find the API KEY?

sorry, but im new with cashier and stripe :(

jsmmth commented 8 years ago

@andradedev if you go to stipe.com and open up your settings, under API keys

Publishable key = API KEY Secret Key = API SECRET

Hope this helps :)

paulocastellano commented 8 years ago

+1

staticwave commented 8 years ago

I am having the same issue with fresh install of latest stable release even with both KEY and SECRET set.

paulocastellano commented 8 years ago

The issue is only in live mode, in test its ok!

staticwave commented 8 years ago

@paulocastellano You mean the Stripe modes test/live? I have Stripe in Test mode and am experiencing this issue.

paulocastellano commented 8 years ago

For me issue is only in live mode, in test its ok.

But i using laravel 5.2, cashier 6.0

staticwave commented 8 years ago

@paulocastellano Hmm, I have the same setup but it's not working in either mode. Which Stripe API version are you using?

paulocastellano commented 8 years ago

@staticwave "laravel/cashier": "~6.0"

staticwave commented 8 years ago

@paulocastellano I was asking about your Stripe API version. Mine is '2015-10-16 (latest)' in Stripe account settings.

paulocastellano commented 8 years ago

@staticwave me too!

staticwave commented 8 years ago

@paulocastellano Thanks for the info. Hope to get this resolved soon.

paulocastellano commented 8 years ago

@staticwave i finding on web the solution and trying find the core code

paulocastellano commented 8 years ago

@staticwave i found the solution, set us key in .env!

if u set in services not work

staticwave commented 8 years ago

@paulocastellano That did it! Thanks.

MicroDreamIT commented 8 years ago

@paulocastellano @staticwave i having the same trouble, i have set the STRIPE_API_SECRET in .env and call it in services.php, now its giving me error like "The second argument to Stripe API method calls is an optional per-request apiKey". any help??

staticwave commented 8 years ago

@MicroDreamIT Make sure you have both stripe key and stripe secret in .env

MicroDreamIT commented 8 years ago

@staticwave i dont understand the difference, between stripe key and stripe secret. I have only "test secret key" and "test publishable key". Please help me... :)

staticwave commented 8 years ago

@MicroDreamIT stripe key = publishable key and stripe secret = secret key. Make sure they're both set in .env and use them in services config

MicroDreamIT commented 8 years ago

@staticwave my services.php is look like this brother,

  'stripe' => [
        'model'  => App\User::class,
        'key'    => env('STRIPE_PUBLISHABLE_KEY'),
        'secret' => env('STRIPE_API_SECRET'),
    ],

I am using cashier 6.0 in laravel 5.2

staticwave commented 8 years ago

@MicroDreamIT That looks right. In Stripe dashboard where the API keys are, there's a publishable key and a secret key. Set them accordingly in .env

staticwave commented 8 years ago

@MicroDreamIT Here's what mine look like .env

STRIPE_KEY=pk_test_....
STRIPE_SECRET=sk_test_...

config/services.php

'stripe' => [
        'model'  => App\User::class,
        'key'    => env('STRIPE_KEY'), //publishable key
        'secret' => env('STRIPE_SECRET'), //secret key
    ],
MicroDreamIT commented 8 years ago

@staticwave lets see brother, you are helping me a lot to develop this things, its really helping me a lot... :+1:

MicroDreamIT commented 8 years ago

@staticwave Brother, almost there, now in my stripe account,

  1. customer generated,
  2. test payment is also made,

in my app,

  1. user table populated with stripe detail

***but subscriptions table is not updated in DB, now its telling me

InvalidRequest in ApiRequestor.php line 103: No such plan: small; one exists with a name of small, but its ID is 1.

here code in my controller

$creditCardToken=$request->input('stripe-token');
        Auth::user()->newSubscription('main', $request->input('plan'))->create($creditCardToken);
        return "done";
MicroDreamIT commented 8 years ago

@staticwave please check http://stackoverflow.com/questions/34669826/invalidrequest-in-apirequestor-php-line-103-no-such-plan-small-one-exists-wit

sjaved193 commented 8 years ago

@staticwave I have done every thing stated above but it isn't working. My .env file looks like

STRIPE_API_KEY=sk_test_mytestkey
STRIPE_API_PUBLISHABLE=pk_test_mypublish_key

And my services.php file looks like

'stripe' => [
    'model'  => 'User',
            'key'   => env('STRIPE_API_PUBLISHABLE'),
    'secret' => env('STRIPE_API_KEY'),

]

And this is my controller

 $user = Sentinel::getUser();
 $user->newSubscription('main', 'plan_monthly')->create(Input::get('_token'));

And it is always throwing error

The second argument to Stripe API method calls is an optional per-request apiKey, which must be a string, or per-request options, which must be an array. (HINT: you can set a global apiKey by "Stripe::setApiKey()")

One more thing my strip account isn't activated yet. Is it the reason? And second I am using laravel cashier 6.0 of laravel 5.2 in my laravel 5.1

I have also tried

Stripe::setApiKey('sk_test_mytestkey');

by setting in controlller and in appserviceprovider file but every time I am getting same.

MicroDreamIT commented 8 years ago

@sjaved193 step by step

  1. Goto .env and set
STRIPE_SECRET = stripe secret key
STRIPE_KEY= stripe publishable key
  1. goto services.php
 'stripe' => [
        'model'  => App\User::class,
        'secret' => env('STRIPE_SECRET'),
        'key'    => env('STRIPE_KEY')
    ],
  1. in js, declare publishable key
Stripe.setPublishableKey('your_stripe_publishable_key');

important two line, make sure you are getting stripe token

 form.append($('<input type="hidden" name="stripe-token" value="response.id">').val(token));
            form.submit();
  1. in controller
$creditCardToken=$request->input('stripe-token');
        Auth::user()->newSubscription('main', $request->input('plan'))->create($creditCardToken, ['email'=>Auth::user()->email]);

let us know how you are doing, if you would it better ways, please share with us. Thank you

paulocastellano commented 8 years ago

The problem back with me, and i dont make nothing :/

MicroDreamIT commented 8 years ago

@paulocastellano where is the problem?

paulocastellano commented 8 years ago

The problem is:

The second argument to Stripe API method calls is an optional per-request apiKey, which must be a string, or per-request options, which must be an array. (HINT: you can set a global apiKey by "Stripe::setApiKey()").

The problem back but i have keys in .env!

The problem is only in live mode, in testing its ok!

MicroDreamIT commented 8 years ago

publishable key has to be on scripts as well

like

Stripe.setPublishableKey('pk_test_****);
paulocastellano commented 8 years ago

In my view i have:

Stripe.setPublishableKey('pk_live_MYKEY');

And in controller i receive this token!

MicroDreamIT commented 8 years ago

strange !!!

paulocastellano commented 8 years ago

Much bro!

I thinking in remove cashier and use pure -> https://github.com/stripe/stripe-php

MicroDreamIT commented 8 years ago

@paulocastellano that will be great

paulocastellano commented 8 years ago

@MicroDreamIT, using stripe-php pure, and same error! WTF?

westcrosby commented 8 years ago

Thanks @MicroDreamIT , setting up my .env was the missing link. The 5.2 documentation doesn't seem to reference configuring .env at all. It's a little confusing because it says setup services.php with 'secret' => env('STRIPE_API_SECRET') and doesn't reference a key.

On top of that, when (Billable)::newSubscription(str, str)->create(stripeToken) makes a new customer to auto sync this information with a subscriptions database, it uses \Laravel\Cashier\Billable::getStripeKey which is actually looking for getenv('STRIPE_SECRET') not STRIPE_API_SECRET.

services.php

'stripe' => [
        'model'  => App\Account::class,
        'secret' => env('STRIPE_SECRET'),
        'key'    => env('STRIPE_KEY')
    ],

Note: I'm using App\Account as my Billable.

.env

STRIPE_SECRET=sk_test_(blah)
STRIPE_KEY=pk_test_(blah)

.js (handling credit card stripe token generator) via https://stripe.com/docs/tutorials/forms with validation support from https://github.com/stripe/jquery.payment

With the subscription table intact, I ran

$account->newSubscription($data['name'], $data['package'])->create($creditCardToken, [
            'description' => $data['description']
        ]);

And it successfully added a record to my subscriptions table, and the desired POST request showed up on my stripe logs. I'm no where near done, but it's progress. I hope some of this information helps, good luck!

dubcanada commented 8 years ago

For me the issue was I was manually setting the key in the services.php file while the method getStripeKey requires it to be set as an ENV variable or set manually using setStripeKey.

This function getStripeKey should really just use the Laravel Config facade to get the details from services.php.

dambridge commented 8 years ago

I just tweaked my .env keys to match what @westcrosby suggested and this fixed it for me. Annoying.

dambridge commented 8 years ago

Well, not exactly. Now in my seeder I get a step further. With the exact same code above I get: Could not determine which URL to request: Stripe\Token instance has invalid ID:

eckberg commented 8 years ago

I agree with @dubcanada. The Billable trait should primarily reference the Laravel config file. The solution for me was to override the getStripeKey() function in my Billable model.

cklmercer commented 8 years ago

I began receiving the following error after migration a development site to production The second argument to Stripe API method calls is an optional per-request apiKey, which must be a string, or per-request options, which must be an array.

I read through this issue, and did some manual testing. What I found, is that getenv('STRIPE_SECRET') and env('STRIPE_SECRET') both returned null, while config('services.stripe.secret') would return my key.

Given that the Cashier method getStripeKey() uses getenv('STRIPE_SECRET') I decided to overwrite it directly on my User model to instead use config('services.stripe.secret'). After doing such, I stopped receiving the aforementioned error message and stripe went back to working as intended.

arfanmukhtar commented 8 years ago

A simple php artisan config:clear should fix your problem.

aaronhuisinga commented 8 years ago

@cklmercer Really surprised that isn't the default behavior. Using the getenv() function when using the built in Laravel route caching seems to break Cashier. Disabling route caching fixes it right away.

cvcv01 commented 7 years ago

Having the same problem in Stripe test. Everything used to work in test, when I created a new plan in Stripe, now I get the error.

Error

No such plan: monthly; one exists with a name of monthly, but its ID is primary.

Controller

$user->newSubscription('primary', 'monthly')->create($token, [ ]);

Plan details

ID: primary
Name: monthly
Price: $19.99 USD/month
Trial period: No trial

php artisan config:clear doesn't help

arfanmukhtar commented 7 years ago

@cvcv01 Use this : $user->newSubscription('primary', 'primary')->create($token, [ ]);