Fix and improve license webhook controller

This commit is contained in:
Julien Nahum 2023-11-01 20:36:03 +01:00
parent 04a367d120
commit 88a1e18055
1 changed files with 18 additions and 28 deletions

View File

@ -42,39 +42,30 @@ class AppSumoController extends Controller
private function handleActivateEvent($request)
{
$licence = License::firstOrNew([
'license_key' => $request->license_key,
'license_provider' => 'appsumo',
'status' => License::STATUS_ACTIVE,
]);
$licence->meta = $request->json()->all();
$licence->save();
Log::info('[APPSUMO] activating license', $request->toArray());
$this->createLicense($request->json()->all());
}
private function handleChangeEvent($request)
{
// Deactivate old license
$oldLicense = License::where([
'license_key' => $request->prev_license_key,
'license_provider' => 'appsumo',
])->firstOrFail();
$oldLicense->update([
'status' => License::STATUS_INACTIVE,
]);
$this->deactivateLicense($request->prev_license_key);
$this->createLicense($request->json()->all());
}
Log::info('[APPSUMO] De-activating license', [
'license_key' => $request->prev_license_key,
'license_id' => $oldLicense->id,
]);
private function handleDeactivateEvent($request)
{
$this->deactivateLicense($request->license_key);
}
// Create new license
$license = License::create([
'license_key' => $request->license_key,
private function createLicense(array $licenseData)
{
$license = License::firstOrNew([
'license_key' => $licenseData['license_key'],
'license_provider' => 'appsumo',
'status' => License::STATUS_ACTIVE,
'meta' => $request->json()->all(),
]);
$license->meta = $licenseData;
$license->save();
Log::info('[APPSUMO] creating new license',
[
'license_key' => $license->license_key,
@ -82,18 +73,17 @@ class AppSumoController extends Controller
]);
}
private function handleDeactivateEvent($request)
private function deactivateLicense(string $licenseKey)
{
// Deactivate old license
$oldLicense = License::where([
'license_key' => $request->prev_license_key,
'license_key' => $licenseKey,
'license_provider' => 'appsumo',
])->firstOrFail();
$oldLicense->update([
'status' => License::STATUS_INACTIVE,
]);
Log::info('[APPSUMO] De-activating license', [
'license_key' => $request->prev_license_key,
'license_key' => $licenseKey,
'license_id' => $oldLicense->id,
]);
}