benpva16
benpva16

Reputation: 454

Laravel Cashier 10 - Errors trying to show a Stripe element

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.

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.

Upvotes: 3

Views: 1978

Answers (1)

developerjack
developerjack

Reputation: 1213

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

Upvotes: 1

Related Questions