问题
I am working on developing a subscription for a web application using Laravel Cashier and Stripe.
I'm using Stripe v3 JavaScript API and using the card elements to generate the Stripe token. The Stripe token is being generated, and if you look in the Stripe dashboard a customer is created. Additionally, a stripe id is being saved in the user database. However, when I try and subscribe the user to a plan with the following code:
$user->newSubscription($planId, $planId)->create($stripeToken, [
'email' => $user->email
]);
It fails with the error: "No such customer: cus_xxxxxx". The $planId variable is the ID of the plan in Stripe.
Like I said, the stripe token is being generated correctly, the customer is created in the Stripe dashboard, and the stripe id is being set in the database. I've done some digging into the Laravel Cashier code, and it seems to be getting the error when it tries to update the card info. More specifically, it fails at this function:
public function asStripeCustomer()
Which is found in the billable model.
I contacted Stripe support, and they said there are no further requests being made to their API after the initial customer is created.
My laravel version is 5.5.34, and I'm using the latest Cashier release. I've tried reinstalling Cashier and it still doesn't work. I've also refreshed my cache.
Any help is greatly appreciated.
回答1:
Just in case anyone else faces this issue in the future, I managed to get it fixed.
Essentially, the issue comes down to your database settings. For me, my database settings were saving everything lowercased. Stripe is case sensitive with its customer keys. After I changed my database to be case sensitive began to work again.
回答2:
This means that the current value of user's stripe customer id (stored in stripe_id
column) is invalid.
So, just remove it, and the next time when the newSubscription()
will be called, it will create the new customer, again.
Also, you can handle this by doing something like this:
try {
$user->newSubscription($subscription, $plan)->create($request->stripeToken);
} catch (\Exception $e) {
// No such customer. Invalid value in stripe_id. Clean it, for making the next request successfully
$user->stripe_id = NULL;
$user->save();
}
回答3:
Did you create your plan in stripe? if so you can call it like this
try {
$user->newSubscription('myplan','myplan')
->create($stripeToken);
}
} catch (\Exception $e) {
Session::flash('error_message', 'There has been an error processing your payment.');
return redirect('/');
}
来源:https://stackoverflow.com/questions/48811509/laravel-cashier-stripe-no-such-customer