Skip to content

Commit

Permalink
Added license webhook logs
Browse files Browse the repository at this point in the history
  • Loading branch information
JhumanJ committed Nov 1, 2023
1 parent dc5a828 commit 04a367d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/Http/Controllers/Webhook/AppSumoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Models\License;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use Illuminate\Validation\UnauthorizedException;

Expand All @@ -14,13 +15,16 @@ public function handle(Request $request)
$this->validateSignature($request);

if ($request->test) {
Log::info('[APPSUMO] test request received', $request->toArray());
return $this->success([
'message' => 'Webhook received.',
'event' => $request->event,
'success' => true,
]);
}

Log::info('[APPSUMO] request received', $request->toArray());

// Call the right function depending on the event using match()
match ($request->event) {
'activate' => $this->handleActivateEvent($request),
Expand All @@ -45,6 +49,7 @@ private function handleActivateEvent($request)
]);
$licence->meta = $request->json()->all();
$licence->save();
Log::info('[APPSUMO] activating license', $request->toArray());
}

private function handleChangeEvent($request)
Expand All @@ -58,13 +63,23 @@ private function handleChangeEvent($request)
'status' => License::STATUS_INACTIVE,
]);

Log::info('[APPSUMO] De-activating license', [
'license_key' => $request->prev_license_key,
'license_id' => $oldLicense->id,
]);

// Create new license
License::create([
$license = License::create([
'license_key' => $request->license_key,
'license_provider' => 'appsumo',
'status' => License::STATUS_ACTIVE,
'meta' => $request->json()->all(),
]);
Log::info('[APPSUMO] creating new license',
[
'license_key' => $license->license_key,
'license_id' => $license->id,
]);
}

private function handleDeactivateEvent($request)
Expand All @@ -77,6 +92,10 @@ private function handleDeactivateEvent($request)
$oldLicense->update([
'status' => License::STATUS_INACTIVE,
]);
Log::info('[APPSUMO] De-activating license', [
'license_key' => $request->prev_license_key,
'license_id' => $oldLicense->id,
]);
}

private function validateSignature(Request $request)
Expand Down

0 comments on commit 04a367d

Please sign in to comment.