diff --git a/app/Http/Controllers/AccountApiController.php b/app/Http/Controllers/AccountApiController.php index e963bac585..3ca39b8c50 100644 --- a/app/Http/Controllers/AccountApiController.php +++ b/app/Http/Controllers/AccountApiController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Events\UserSignedUp; use App\Http\Requests\RegisterRequest; use App\Http\Requests\UpdateAccountRequest; +use App\Models\Company; use App\Models\Account; use App\Models\User; use App\Ninja\OAuth\OAuth; @@ -14,6 +15,7 @@ use App\Ninja\Transformers\UserAccountTransformer; use App\Services\AuthService; use Auth; use Cache; +use Carbon; use Exception; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; @@ -287,4 +289,53 @@ class AccountApiController extends BaseAPIController } + public function upgrade(Request $request) + { + $user = Auth::user(); + $account = $user->account; + $company = $account->company; + $orderId = $request->order_id; + $timestamp = $request->timestamp; + $productId = $request->product_id; + + if (Company::where('app_store_order_id', '=', $orderId)->first()) { + return 'Order Id has already been used'; + } + + if (Carbon::createFromTimestamp($timestamp) < Carbon::now()->subYear()) { + return 'The order is expired'; + } + + if ($productId == 'v1_pro_yearly') { + $company->plan = PLAN_PRO; + $company->num_users = 1; + $company->plan_price = PLAN_PRICE_PRO_MONTHLY * 10; + } else if ($productId == 'v1_enterprise_2_yearly') { + $company->plan = PLAN_ENTERPRISE; + $company->num_users = 2; + $company->plan_price = PLAN_PRICE_ENTERPRISE_MONTHLY_2 * 10; + } else if ($productId == 'v1_enterprise_5_yearly') { + $company->plan = PLAN_ENTERPRISE; + $company->num_users = 5; + $company->plan_price = PLAN_PRICE_ENTERPRISE_MONTHLY_5 * 10; + } else if ($productId == 'v1_enterprise_10_yearly') { + $company->plan = PLAN_ENTERPRISE; + $company->num_users = 10; + $company->plan_price = PLAN_PRICE_ENTERPRISE_MONTHLY_10 * 10; + } else if ($productId == 'v1_enterprise_20_yearly') { + $company->plan = PLAN_ENTERPRISE; + $company->num_users = 20; + $company->plan_price = PLAN_PRICE_ENTERPRISE_MONTHLY_20 * 10; + } + + $company->app_store_order_id = $orderId; + $company->plan_term = PLAN_TERM_YEARLY; + $company->plan_started = $company->plan_started ?: date('Y-m-d'); + $company->plan_paid = date('Y-m-d'); + $company->plan_expires = Carbon::createFromTimestamp($timestamp)->addYear()->format('Y-m-d'); + $company->trial_plan = null; + $company->save(); + + return RESULT_SUCCESS; + } } diff --git a/database/migrations/2019_08_27_074711_add_app_store_order_id.php b/database/migrations/2019_08_27_074711_add_app_store_order_id.php new file mode 100644 index 0000000000..e4d892ee4a --- /dev/null +++ b/database/migrations/2019_08_27_074711_add_app_store_order_id.php @@ -0,0 +1,32 @@ +string('app_store_order_id')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('companies', function ($table) { + $table->dropColumn('app_store_order_id'); + }); + } +} diff --git a/routes/api.php b/routes/api.php index 3c675c1767..bb8ea037b0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -8,6 +8,7 @@ Route::get('static', 'AccountApiController@getStaticData'); Route::get('accounts', 'AccountApiController@show'); Route::put('accounts', 'AccountApiController@update'); Route::post('refresh', 'AccountApiController@refresh'); +Route::post('upgrade', 'AccountApiController@upgrade'); Route::resource('clients', 'ClientApiController'); Route::resource('contacts', 'ContactApiController');