メインコンテンツまでスキップ

テナントの作成

セルフサインアップができたら、テナントを作成します。

今回はテナント作成時はチュートリアルで作成したFreeプランを自動で適用するようになっています。

    // バリデーション済みデータの取得
$validated = $request->validated();

// SaaSusSDKの利用
$client = new ApiClient();
$authClient = $this->client->getAuthClient();
$pricingClient = $this->client->getPricingClient();

// 料金プランを検索
$pricingPlans = $pricingClient->getPricingPlans();
$nextPlanId = "";
foreach ($pricingPlans->getPricingPlans() as $pricingPlan) {
if ($pricingPlan['display_name'] == 'Freeプラン') {
$nextPlanId = $pricingPlan['id'];
}
}

// プランのidを取得できなかった場合エラーとする
if (empty($nextPlanId)) {
return response()->json(['detail' => 'プラン情報の取得に失敗しました。'], Response::HTTP_INTERNAL_SERVER_ERROR);
}

// テナント作成
// テナント名:画面で入力された名前
// バックオフィススタッフemail:ログインしている人のメールアドレス
$tenant = $authClient->createTenant((object)array(
'name' => $tenantName,
'back_office_staff_email' => $email,
));

// 作成したテナントのIDを取得
$tenantId = $tenant->getId();

// プラン変更時に現在の時刻から5分以上未来の時間を指定
$currentTimeWith5MinutesAfterUnixTime = Carbon::now('UTC')->addMinutes(5)->timestamp;

// プラン情報を更新
$authClient->updateTenantPlan($tenantId, (object)array(
'next_plan_id' => $nextPlanId,
'using_next_plan_from' => $currentTimeWith5MinutesAfterUnixTime,
));

APIを利用してテナントが正常に作成できたかの確認は、「SaaSus管理コンソール>SaaS 運用コンソール>テナント管理」よりご確認頂けます。

tenant