Apr 11, 2024
0
Level 1
Making subscription assignment from admin panel with Laravel Cashier
I want to manage subscriptions through my admin panel. I want to assign a subscription to the user I want, manage the subscription period, or cancel the subscription. (I use Cashier Stripe) But no matter what I did, I couldn't complete it and I kept getting different errors. I get the following error very often and cannot start a subscription.
The customer's location isn't recognized. Set a valid customer address in order to automatically calculate tax.
// Subscription and Plan
$newPlanId = $request->input('subscription');
$expiresAt = $request->input('expires_at') ? Carbon::parse($request->input('expires_at')) : null;
// Get stripe_price id
$stripePlanId = $newPlanId ? SubscriptionPlan::find($newPlanId)->stripe_id : null;
if ($newPlanId) {
if (!$user->hasStripeId()) {
$user->createAsStripeCustomer();
}
if ($user->subscribed('default')) {
if ($user->subscription('default')->stripe_plan != $stripePlanId) {
if ($user->subscription('default')->canceled()) {
$user->newSubscription('default', $stripePlanId)->trialUntil($expiresAt)->create(null);
} else {
$user->subscription('default')->swap($stripePlanId);
}
}
} else {
$user->newSubscription('default', $stripePlanId)->trialUntil($expiresAt)->create(null);
}
if ($expiresAt) {
$user->subscription('default')->update(['trial_ends_at' => $expiresAt]);
}
}
if (is_null($newPlanId) || $newPlanId === '0') {
$user->subscriptions()->each(function ($subscription) {
$subscription->cancelNow();
});
$user->subscription('default')->update(['trial_ends_at' => $expiresAt]);
}
Please or to participate in this conversation.