forked from thedevdojo/wave
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'thedevdojo:main' into main
- Loading branch information
Showing
6 changed files
with
98 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Wave\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use TCG\Voyager\Models\Role; | ||
use Wave\PaddleSubscription; | ||
use Illuminate\Support\Carbon; | ||
use App\Http\Controllers\Controller; | ||
use Wave\Http\Middleware\VerifyWebhook; | ||
|
||
class WebhookController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
if (config('wave.paddle.public_key')) { | ||
$this->middleware(VerifyWebhook::class); | ||
} | ||
} | ||
|
||
public function __invoke(Request $request) | ||
{ | ||
$method = match ($request->get('alert_name', null)) { | ||
'subscription_cancelled', | ||
'subscription_payment_failed' => 'subscriptionCancelled', | ||
default => null, | ||
}; | ||
|
||
if (method_exists($this, $method)) { | ||
try { | ||
$this->{$method}($request); | ||
} catch (\Exception $e) { | ||
return response('Webhook failed'); | ||
} | ||
} | ||
|
||
return response('Webhook handled'); | ||
} | ||
|
||
protected function subscriptionCancelled(Request $request) | ||
{ | ||
$subscription = PaddleSubscription::where('subscription_id', $request->subscription_id)->firstOrFail(); | ||
$subscription->cancelled_at = Carbon::now(); | ||
$subscription->status = 'cancelled'; | ||
$subscription->save(); | ||
$user = config('wave.user_model')::find($subscription->user_id); | ||
$cancelledRole = Role::where('name', '=', 'cancelled')->first(); | ||
$user->role_id = $cancelledRole->id; | ||
$user->save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Wave\Http\Middleware; | ||
|
||
use Closure; | ||
use InvalidArgumentException; | ||
|
||
class VerifyWebhook | ||
{ | ||
/** | ||
* Handle an incoming webhook request. | ||
* | ||
* @see https://developer.paddle.com/webhook-reference/ZG9jOjI1MzUzOTg2-verifying-webhooks | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
$signature = $request->get('p_signature'); | ||
$fields = $request->except('p_signature'); | ||
|
||
ksort($fields); | ||
|
||
foreach ($fields as $k => $v) { | ||
if (!in_array(gettype($v), array('object', 'array'))) { | ||
$fields[$k] = "$v"; | ||
} | ||
} | ||
|
||
if (openssl_verify( | ||
serialize($fields), | ||
base64_decode($signature), | ||
openssl_get_publickey(config('wave.paddle.public_key')), | ||
OPENSSL_ALGO_SHA1 | ||
) !== 1) { | ||
throw new InvalidArgumentException('Webhook signature is invalid.'); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |