2023-11-01 15:58:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Webhook;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Models\License;
|
|
|
|
use Illuminate\Http\Request;
|
2024-02-23 10:54:12 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2023-11-01 15:58:10 +00:00
|
|
|
use Illuminate\Validation\UnauthorizedException;
|
|
|
|
|
|
|
|
class AppSumoController extends Controller
|
|
|
|
{
|
|
|
|
public function handle(Request $request)
|
|
|
|
{
|
|
|
|
$this->validateSignature($request);
|
|
|
|
|
|
|
|
if ($request->test) {
|
2023-11-01 19:17:39 +00:00
|
|
|
Log::info('[APPSUMO] test request received', $request->toArray());
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2023-11-01 15:58:10 +00:00
|
|
|
return $this->success([
|
|
|
|
'message' => 'Webhook received.',
|
|
|
|
'event' => $request->event,
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-11-01 19:17:39 +00:00
|
|
|
Log::info('[APPSUMO] request received', $request->toArray());
|
|
|
|
|
2023-11-01 15:58:10 +00:00
|
|
|
// Call the right function depending on the event using match()
|
|
|
|
match ($request->event) {
|
|
|
|
'activate' => $this->handleActivateEvent($request),
|
|
|
|
'upgrade', 'downgrade' => $this->handleChangeEvent($request),
|
|
|
|
'deactivate' => $this->handleDeactivateEvent($request),
|
|
|
|
default => null,
|
|
|
|
};
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
'message' => 'Webhook received.',
|
|
|
|
'event' => $request->event,
|
|
|
|
'success' => true,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleActivateEvent($request)
|
|
|
|
{
|
2023-11-01 19:36:03 +00:00
|
|
|
$this->createLicense($request->json()->all());
|
2023-11-01 15:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function handleChangeEvent($request)
|
|
|
|
{
|
2023-11-08 20:56:24 +00:00
|
|
|
$license = $this->deactivateLicense($request->prev_license_key);
|
|
|
|
$this->createLicense(array_merge($request->json()->all(), [
|
|
|
|
'user_id' => $license->user_id,
|
|
|
|
]));
|
2023-11-01 19:36:03 +00:00
|
|
|
}
|
2023-11-01 15:58:10 +00:00
|
|
|
|
2023-11-01 19:36:03 +00:00
|
|
|
private function handleDeactivateEvent($request)
|
|
|
|
{
|
|
|
|
$this->deactivateLicense($request->license_key);
|
|
|
|
}
|
2023-11-01 19:17:39 +00:00
|
|
|
|
2023-11-08 20:56:24 +00:00
|
|
|
private function createLicense(array $licenseData): License
|
2023-11-01 19:36:03 +00:00
|
|
|
{
|
|
|
|
$license = License::firstOrNew([
|
|
|
|
'license_key' => $licenseData['license_key'],
|
2023-11-01 15:58:10 +00:00
|
|
|
'license_provider' => 'appsumo',
|
|
|
|
'status' => License::STATUS_ACTIVE,
|
|
|
|
]);
|
2023-11-01 19:36:03 +00:00
|
|
|
$license->meta = $licenseData;
|
2023-11-08 21:18:22 +00:00
|
|
|
$license->user_id = $licenseData['user_id'] ?? null;
|
2023-11-01 19:36:03 +00:00
|
|
|
$license->save();
|
|
|
|
|
2024-02-23 10:54:12 +00:00
|
|
|
Log::info(
|
|
|
|
'[APPSUMO] creating new license',
|
2023-11-01 19:17:39 +00:00
|
|
|
[
|
|
|
|
'license_key' => $license->license_key,
|
|
|
|
'license_id' => $license->id,
|
2024-02-23 10:54:12 +00:00
|
|
|
]
|
|
|
|
);
|
2023-11-08 20:56:24 +00:00
|
|
|
|
|
|
|
return $license;
|
2023-11-01 15:58:10 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 20:56:24 +00:00
|
|
|
private function deactivateLicense(string $licenseKey): License
|
2023-11-01 15:58:10 +00:00
|
|
|
{
|
2023-11-08 20:56:24 +00:00
|
|
|
$license = License::where([
|
2023-11-01 19:36:03 +00:00
|
|
|
'license_key' => $licenseKey,
|
2023-11-01 15:58:10 +00:00
|
|
|
'license_provider' => 'appsumo',
|
|
|
|
])->firstOrFail();
|
2023-11-08 20:56:24 +00:00
|
|
|
$license->update([
|
2023-11-01 15:58:10 +00:00
|
|
|
'status' => License::STATUS_INACTIVE,
|
|
|
|
]);
|
2023-11-01 19:17:39 +00:00
|
|
|
Log::info('[APPSUMO] De-activating license', [
|
2023-11-01 19:36:03 +00:00
|
|
|
'license_key' => $licenseKey,
|
2023-11-08 20:56:24 +00:00
|
|
|
'license_id' => $license->id,
|
2023-11-01 19:17:39 +00:00
|
|
|
]);
|
2023-11-08 20:56:24 +00:00
|
|
|
|
|
|
|
return $license;
|
2023-11-01 15:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function validateSignature(Request $request)
|
|
|
|
{
|
|
|
|
$signature = $request->header('x-appsumo-signature');
|
|
|
|
$payload = $request->getContent();
|
|
|
|
|
|
|
|
if ($signature === hash_hmac('sha256', $payload, config('services.appsumo.api_key'))) {
|
|
|
|
throw new UnauthorizedException('Invalid signature.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|