2
votes

I am using this tutorial to integrate Stripe into my Laravel site using Cashier: https://appdividend.com/2018/12/05/laravel-stripe-payment-gateway-integration-tutorial-with-example/

This tutorial was written for Cashier 9, so it does not work out of the box with Cashier 10. However, it does work making the adjustments in this SO answer: https://stackoverflow.com/a/57812759/2002457

Except, it only works for existing Stripe customers. When I register a brand new user and try to view a plan, it gives this error: User is not a Stripe customer. See the createAsStripeCustomer method.

So, I try to do just that:

public function show(Plan $plan, Request $request)
    {
        if($request->user()->stripe_id === null)
        {
            $request->user()->createAsStripeCustomer();
        }
        $paymentMethods = $request->user()->paymentMethods();

        $intent = $request->user()->createSetupIntent();
        return view('plans.show', compact('plan', 'intent'));
    }

Which yields this error: No API key provided. (HINT: set your API key using "Stripe::setApiKey(<API-KEY>)". 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.

This SO answer addresses this problem: https://stackoverflow.com/a/34508056/2002457

But the solution only works in Cashier 9, because Billable changed, so it's not clear how to set the API key.

What am I doing wrong here to create a new customer if they're not a Stripe customer already?

EDIT - I am using the default cashier config, and I've confirmed it is pointing at the .env vars.

  • I put in a dd(config('cashier.key')); to confirm that config is working

  • I removed the old services.php config parts

  • The env vars are set correctly

Here's the show method:

    public function show(Plan $plan, Request $request)
    {
        $paymentMethods = $request->user()->paymentMethods();

        $intent = $request->user()->createSetupIntent();
        return view('plans.show', compact('plan', 'intent'));
    }

And here's the error now: User is not a Stripe customer. See the createAsStripeCustomer method.

1

1 Answers

1
votes

Cashier 10 introduced some changes to the configuration including setting up the cashier.php configuration file. The upgrade guide details how, this pull request commit shows the file.

Few things to debug this:

  • make sure you've setup the config for cashier 10 correctly.
  • make sure that the config key cashier.key is available (e.g. ddd(config('cashier.key'));
  • double check that that your .env var's are setup correctly for stripe's API key