Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
<?php
declare(strict_types=1);
namespace App\Actions\Billing;
use App\Enums\Billing\CheckoutOrigin;
use App\Enums\Billing\Interval;
use App\Models\Account;
use App\Models\Plan;
use App\Support\Billing\SubscriptionPlanSync;
use Illuminate\Support\Facades\Gate;
use Laravel\Cashier\Exceptions\IncompletePayment;
use Symfony\Component\HttpFoundation\Response;
class ChangePlan
{
public function __construct(private StartSubscriptionCheckout $checkout) {}
/**
* Moves the account to a plan's price, once `swapPlan` allows it. A
* subscription swaps its price; a generic trial subscribes through Stripe
* Checkout, charged at once, with a coupon offer the owner claimed meanwhile.
* A swap to a yearly price drops the subscription's discount: an offer is
* written for Socials monthly, and Stripe would take it off a whole year.
*/
public function execute(Account $account, Plan $plan, Interval $interval): Response
{
$authorization = Gate::inspect('swapPlan', [$account, $plan]);
if ($authorization->denied()) {
return back()->with('flash.error', $authorization->message());
}
$priceId = $interval->priceIdFor($plan);
abort_if(blank($priceId), Response::HTTP_UNPROCESSABLE_ENTITY, 'No price configured for this interval');
if (! $account->subscribed(Account::SUBSCRIPTION_NAME)) {
return $this->checkout->redirect(
$account,
$priceId,
route('app.billing.index'),
$plan,
$account->currentOffer(withTrial: false),
CheckoutOrigin::Billing,
);
}
$subscription = $account->subscription(Account::SUBSCRIPTION_NAME);
try {
if ($subscription->stripe_price !== $priceId) {
$subscription->swap($priceId, $interval === Interval::Yearly ? ['discounts' => ''] : []);
}
} catch (IncompletePayment $exception) {
return redirect()->route('cashier.payment', [
$exception->payment->id,
'redirect' => route('app.billing.index'),
]);
}
$subscription->refresh();
if (! SubscriptionPlanSync::allows($subscription->stripe_status)) {
return redirect()->route('app.billing.index');
}
$account->update(['plan_id' => $plan->id]);
return redirect()->route('app.billing.index')
->with('flash.success', __('billing.flash.plan_changed', ['plan' => $plan->name]));
}
}