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) private function handleActivateEvent($request)
{ {
$licence = License::firstOrNew([ $this->createLicense($request->json()->all());
'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());
} }
private function handleChangeEvent($request) private function handleChangeEvent($request)
{ {
// Deactivate old license $this->deactivateLicense($request->prev_license_key);
$oldLicense = License::where([ $this->createLicense($request->json()->all());
'license_key' => $request->prev_license_key, }
'license_provider' => 'appsumo',
])->firstOrFail();
$oldLicense->update([
'status' => License::STATUS_INACTIVE,
]);
Log::info('[APPSUMO] De-activating license', [ private function handleDeactivateEvent($request)
'license_key' => $request->prev_license_key, {
'license_id' => $oldLicense->id, $this->deactivateLicense($request->license_key);
]); }
// Create new license private function createLicense(array $licenseData)
$license = License::create([ {
'license_key' => $request->license_key, $license = License::firstOrNew([
'license_key' => $licenseData['license_key'],
'license_provider' => 'appsumo', 'license_provider' => 'appsumo',
'status' => License::STATUS_ACTIVE, 'status' => License::STATUS_ACTIVE,
'meta' => $request->json()->all(),
]); ]);
$license->meta = $licenseData;
$license->save();
Log::info('[APPSUMO] creating new license', Log::info('[APPSUMO] creating new license',
[ [
'license_key' => $license->license_key, '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([ $oldLicense = License::where([
'license_key' => $request->prev_license_key, 'license_key' => $licenseKey,
'license_provider' => 'appsumo', 'license_provider' => 'appsumo',
])->firstOrFail(); ])->firstOrFail();
$oldLicense->update([ $oldLicense->update([
'status' => License::STATUS_INACTIVE, 'status' => License::STATUS_INACTIVE,
]); ]);
Log::info('[APPSUMO] De-activating license', [ Log::info('[APPSUMO] De-activating license', [
'license_key' => $request->prev_license_key, 'license_key' => $licenseKey,
'license_id' => $oldLicense->id, 'license_id' => $oldLicense->id,
]); ]);
} }