6

I am trying to create a subscription but get the error "The resource ID cannot be null or whitespace". I have stripe and cashier installed and migrated.

<?php
namespace App\Http\Controllers;
require_once('../vendor/autoload.php');
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Cashier\Cashier;
use \Stripe\Stripe;
use Exception;
use Stripe_Error;
class SubscriptionController extends Controller {
   public function __construct() {
     $this->middleware('auth');
}
public function retrievePlans() {
   $key = \config('services.stripe.secret');
   $stripe = new \Stripe\StripeClient($key);
   $plansraw = $stripe->plans->all();
   $plans = $plansraw->data;
   
   foreach($plans as $plan) {
       $prod = $stripe->products->retrieve(
           $plan->product,[]
       );
       $plan->product = $prod;
   }
   return $plans;
 }
 public function showSubscription() {
   $plans = $this->retrievePlans();
   $user = Auth::user();
   
   return view('subscribe', [
       'user'=>$user,
       'intent' => $user->createSetupIntent(),
       'plans' => $plans
   ]);
}
public function processSubscription(Request $request)
{
   $user = Auth::user();
   $paymentMethod = $request->input('payment_method');
               
   $user->createOrGetStripeCustomer();
   $user->addPaymentMethod($paymentMethod);
   $plan = $request->input('plan');
   try {
       $user->newSubscription('default', $plan)->create($paymentMethod, [
           'email' => $user->email
       ]);
   } catch (\Exception $e) {
       return back()->withErrors(['message' => 'Error creating subscription. ' . $e->getMessage()]);
   }
   
   return redirect('dashboard');
}
}

I keep getting the error when creating a user $user->createOrGetStripeCustomer();

Matthew Foster
  • 167
  • 1
  • 10
  • I'm wondering if this is bad documentation on Stripe, as I run into this error even following Stripe's own tutorials (with or without Cashier). – Adam Sep 29 '21 at 22:19

4 Answers4

9

In the user table, while creating a new user just kept the stripe-id field as NULL. It should be either a stripe id or must be set as NULL. If you just keep it blank will get the error

Another important point, stripe_id filed is highly case sensitive so keep it as utf8mb4_ci encoding

Asha V
  • 91
  • 1
1

I had that same error. And in my case was because the following:

  1. Subscribe user 1 (and thus was created as customer in Stripe)
  2. Delete customer from the Stripe dashboard
  3. Try to subscribe user 1 again (in the next 10 minutes or so after delete from the customers Stripe list)

This stop happening when I subscribe a different user that has never been in the customers list.

Perphaps and this is just me doing a long shot: Stripe needs a bit of time between delete and re-subscribe.

I also looked in docs but nothing came up about this error.

only this: https://stripe.com/docs/error-codes#resource-missing

Rocam
  • 31
  • 4
0

I was trying to integrate subscription with stripe using cashier in Laravel 8 but I was getting the same error. For me, I have already stripe_id field as NULL by default and kept utf8mb4_ci encoding.

After debugging many things, I found that "payment_method" was not coming in post and found issue with below code.

<button id="card-button" data-secret="{{ $intent->client_secret }}" class="btn btn-lg btn-success btn-block">SUBMIT</button>

It was submitting the form before JavaScript code get fully executed to get the payment method and payment method was not getting posted. So adding type="button" resolved my issue. Below is the submit button code that resolved my issue.

<button type="button" id="card-button" data-secret="{{ $intent->client_secret }}" class="btn btn-lg btn-success btn-block">SUBMIT</button>
0

Modify processSubscription function to check if the user has a payment method already before creating one to him like this:

if(! $user->hasPaymentMethod())
    $user->addPaymentMethod($paymentMethod);