diff --git a/app/Http/Controllers/LicenseController.php b/app/Http/Controllers/LicenseController.php index 88e04574d928..72543ef1f1c7 100644 --- a/app/Http/Controllers/LicenseController.php +++ b/app/Http/Controllers/LicenseController.php @@ -11,11 +11,13 @@ namespace App\Http\Controllers; +use stdClass; +use Carbon\Carbon; use App\Models\Account; use App\Utils\CurlUtils; -use Carbon\Carbon; use Illuminate\Http\Response; -use stdClass; +use Illuminate\Support\Facades\Http; +use Illuminate\Http\Request; class LicenseController extends BaseController { @@ -78,7 +80,7 @@ class LicenseController extends BaseController * ), * ) */ - public function index() + public function indexx() { $this->checkLicense(); @@ -147,6 +149,57 @@ class LicenseController extends BaseController return response()->json($error, 400); } + public function index(Request $request) + { + $this->checkLicense(); + + /* Catch claim license requests */ + if (config('ninja.environment') == 'selfhost' && request()->has('license_key')) { + + $response = Http::get( config('ninja.license_url')."/claim_license", [ + 'license_key' => $request->input('license_key'), + 'product_id' => 3, + ]); + + if($response->successful()) { + + $payload = $response->json(); + + $account = auth()->user()->account; + + $account->plan_term = Account::PLAN_TERM_YEARLY; + $account->plan_expires = Carbon::parse($payload?->expires)->format('Y-m-d'); + $account->plan = Account::PLAN_WHITE_LABEL; + $account->save(); + + $error = [ + 'message' => trans('texts.bought_white_label'), + 'errors' => new \stdClass, + ]; + + return response()->json($error, 200); + }else { + + $error = [ + 'message' => trans('texts.white_label_license_error'), + 'errors' => new stdClass, + ]; + + return response()->json($error, 400); + } + + } + + $error = [ + 'message' => ctrans('texts.invoice_license_or_environment', ['environment' => config('ninja.environment')]), + 'errors' => new stdClass, + ]; + + return response()->json($error, 400); + + } + + private function checkLicense() { $account = auth()->user()->account; @@ -156,5 +209,6 @@ class LicenseController extends BaseController $account->plan_expires = null; $account->save(); } + } } diff --git a/app/Http/Livewire/BillingPortalPurchase.php b/app/Http/Livewire/BillingPortalPurchase.php index 289dc7bdd39e..b0df3019ca18 100644 --- a/app/Http/Livewire/BillingPortalPurchase.php +++ b/app/Http/Livewire/BillingPortalPurchase.php @@ -28,6 +28,7 @@ use Illuminate\Support\Facades\Cache; use App\Mail\ContactPasswordlessLogin; use App\Repositories\ClientRepository; use App\Repositories\ClientContactRepository; +use App\Services\Subscription\SubscriptionService; class BillingPortalPurchase extends Component { @@ -399,8 +400,8 @@ class BillingPortalPurchase extends Component $context = 'purchase'; - // if(Ninja::isHosted() && $this->subscription->service()->recurring_products()->first()->product_key == 'whitelabel') { - if($this->subscription->service()->recurring_products()->first()?->product_key == 'whitelabel') { + // if(Ninja::isHosted() && $this->subscription->service()->recurring_products()->first()?->id == SubscriptionService::WHITE_LABEL) { + if(Ninja::isHosted() && $this->subscription->service()->recurring_products()->first()?->product_key == 'whitelabel') { $context = 'whitelabel'; } diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 32c5514f1f10..2bc4060aadb0 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -58,7 +58,7 @@ class SubscriptionService /** @var subscription */ private $subscription; - private const WHITE_LABEL = 4316; + public const WHITE_LABEL = 4316; private float $credit_payments = 0; @@ -203,7 +203,7 @@ class SubscriptionService $license->save(); $invitation = $invoice->invitations()->first(); - + $email_object = new EmailObject; $email_object->to = [$contact->email]; $email_object->subject = ctrans('texts.white_label_link') . " " .ctrans('texts.payment_subject'); diff --git a/tests/Feature/Ninja/PlanTest.php b/tests/Feature/Ninja/PlanTest.php index 1253166cc250..20f15a333cdb 100644 --- a/tests/Feature/Ninja/PlanTest.php +++ b/tests/Feature/Ninja/PlanTest.php @@ -11,16 +11,18 @@ namespace Tests\Feature\Ninja; -use App\Factory\SubscriptionFactory; -use App\Models\Account; -use App\Models\RecurringInvoice; -use App\Utils\Traits\MakesHash; use Carbon\Carbon; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Foundation\Testing\DatabaseTransactions; -use Illuminate\Support\Facades\Session; -use Tests\MockAccountData; use Tests\TestCase; +use App\Models\Account; +use App\Models\License; +use Tests\MockAccountData; +use App\Utils\Traits\MakesHash; +use App\Models\RecurringInvoice; +use App\Factory\SubscriptionFactory; +use Illuminate\Support\Facades\Http; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\Session; +use Illuminate\Foundation\Testing\DatabaseTransactions; /** * @test @@ -87,4 +89,23 @@ class PlanTest extends TestCase $this->assertEquals($date->addMonthNoOverflow()->startOfDay(), $next_date->startOfDay()); } + + public function testLicense() + { + $license = new License; + $license->license_key = "1234"; + $license->product_id = "3"; + $license->email = 'test@gmail.com'; + $license->is_claimed = 1; + $license->save(); + + $license->fresh(); + + $response = $this->get("/claim_license?license_key=1234&product_id=3") + ->assertStatus(200); + + $response = $this->get("/claim_license?license_key=12345&product_id=3") + ->assertStatus(400); + + } }