I'm testing Cashier with Stripe to manage plans with subscriptions, but I have an issue when I want to subscribe to a plan:
Stripe HTML code (Angular) :
this.stripe.confirmCardSetup(
this.intentToken.client_secret, {
payment_method: {
card: this.card,
billing_details: {
name: name
}
}
}
).then((result) => {
if(!result.error) {
const payment_method = result.setupIntent.payment_method
return this.http.post('/user/subscription', {
payment_method: payment_method,
plan: 'monthly',
}).toPromise()
}
})
Laravel API /user/subscription endpoint:
public function store(StoreSubscription $request)
{
$user = Auth::user();
$paymentMethod = $request->input('payment_method');
$planId = $request->input('plan');
$subscription = $user->newSubscription('premium', $planId)->create($paymentMethod);
return $subscription;
}
Here I have a first error:
Trying to get property 'default_payment_method' of non-object
So I tried to get the Stripe Customer by simply call $user->createAsStripeCustomer(), and then I got this error:
User is already a Stripe customer with ID cus_XXX.
The issue is this customer has been deleted from the Stripe Dashboard, and I can't recreate it. Cashier seems to find it, even if it's a deleted customer.
Raaah... 🤦🏻♂️
Just after I posted it I found out the webhook had some errors, I resend them and now it's working... Sorry!
Beware that if you delete the stripe_id manually from the database, there is a difference between setting it to EMTPY or NULL. If you just delete it, the DB will set it to EMPTY, and Cashier will request Stripe details with the following payload {customer: "", ...} which results in the following error: 'User is already a Stripe customer with ID .'
Notice the ID and a space after it, that's where the Stripe ID would normally go.
Most helpful comment
Beware that if you delete the stripe_id manually from the database, there is a difference between setting it to EMTPY or NULL. If you just delete it, the DB will set it to EMPTY, and Cashier will request Stripe details with the following payload {customer: "", ...} which results in the following error: 'User is already a Stripe customer with ID .'
Notice the ID and a space after it, that's where the Stripe ID would normally go.