2020-03-17 09:40:10 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-17 09:40:10 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Utils\CurlUtils;
|
|
|
|
use Illuminate\Http\Request;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use stdClass;
|
2020-03-17 09:40:10 +01:00
|
|
|
|
|
|
|
class LicenseController extends BaseController
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Claim a white label license.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @return Response
|
2020-03-17 09:40:10 +01:00
|
|
|
*
|
|
|
|
* @OA\Get(
|
|
|
|
* path="/api/v1/claim_license",
|
|
|
|
* operationId="getClaimLicense",
|
|
|
|
* tags={"claim_license"},
|
|
|
|
* summary="Attempts to claim a white label license",
|
|
|
|
* description="Attempts to claim a white label license",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="license_key",
|
2020-07-30 05:27:00 +02:00
|
|
|
* in="query",
|
2020-03-17 09:40:10 +01:00
|
|
|
* description="The license hash",
|
|
|
|
* example="d87sh-s755s-s7d76-sdsd8",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Parameter(
|
|
|
|
* name="product_id",
|
2020-07-30 05:27:00 +02:00
|
|
|
* in="query",
|
2020-03-17 09:40:10 +01:00
|
|
|
* description="The ID of the product purchased.",
|
|
|
|
* example="1",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Success!",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2020-03-17 09:40:10 +01:00
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Catch claim license requests */
|
2020-03-23 18:15:56 +01:00
|
|
|
if (config('ninja.environment') == 'selfhost' && request()->has('license_key')) {
|
|
|
|
$license_key = request()->input('license_key');
|
2020-03-18 10:40:15 +01:00
|
|
|
$product_id = 3;
|
2020-03-17 09:40:10 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$url = config('ninja.license_url')."/claim_license?license_key={$license_key}&product_id={$product_id}&get_date=true";
|
2020-03-17 09:40:10 +01:00
|
|
|
$data = trim(CurlUtils::get($url));
|
|
|
|
|
|
|
|
if ($data == Account::RESULT_FAILURE) {
|
|
|
|
$error = [
|
|
|
|
'message' => trans('texts.invalid_white_label_license'),
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2020-03-17 09:40:10 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return response()->json($error, 400);
|
|
|
|
} elseif ($data) {
|
|
|
|
$date = date_create($data)->modify('+1 year');
|
|
|
|
|
|
|
|
if ($date < date_create()) {
|
|
|
|
$error = [
|
|
|
|
'message' => trans('texts.invalid_white_label_license'),
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2020-03-17 09:40:10 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return response()->json($error, 400);
|
|
|
|
} else {
|
|
|
|
$account = auth()->user()->company()->account;
|
|
|
|
|
|
|
|
$account->plan_term = Account::PLAN_TERM_YEARLY;
|
|
|
|
$account->plan_paid = $data;
|
|
|
|
$account->plan_expires = $date->format('Y-m-d');
|
|
|
|
$account->plan = Account::PLAN_WHITE_LABEL;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
$error = [
|
|
|
|
'message' => trans('texts.bought_white_label'),
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2020-03-17 09:40:10 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return response()->json($error, 200);
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-21 06:37:30 +01:00
|
|
|
$error = [
|
2020-03-17 09:40:10 +01:00
|
|
|
'message' => trans('texts.white_label_license_error'),
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2020-03-17 09:40:10 +01:00
|
|
|
];
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return response()->json($error, 400);
|
2020-03-17 09:40:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$error = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'message' => 'Invalid license, or invalid environment '.config('ninja.environment'),
|
2020-10-28 11:10:49 +01:00
|
|
|
'errors' => new stdClass,
|
2020-03-17 09:40:10 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return response()->json($error, 400);
|
|
|
|
}
|
|
|
|
}
|