Skip to content

Commit

Permalink
Merge pull request #323 from spatie/fix/paddle-webhook-validation
Browse files Browse the repository at this point in the history
Add Paddle webhook validation
  • Loading branch information
AlexVanderbist authored Sep 25, 2024
2 parents 1b91beb + f13347f commit 40e1568
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
18 changes: 18 additions & 0 deletions app/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace App\Http\Controllers;

use App\Models\User;
use App\Support\Paddle\ProcessPaymentSucceededJob;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Laravel\Paddle\Exceptions\InvalidPassthroughPayload;
use Laravel\Paddle\Http\Controllers\WebhookController as CashierWebhookController;

class WebhookController extends CashierWebhookController
Expand All @@ -18,4 +22,18 @@ public function handlePaymentSucceeded($payload): void

dispatch(new ProcessPaymentSucceededJob($payload));
}

protected function findOrCreateCustomer(string $passthrough)

Check failure on line 26 in app/Http/Controllers/WebhookController.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Http\Controllers\WebhookController::findOrCreateCustomer() has invalid return type Laravel\Paddle\Billable.
{
$passthrough = json_decode($passthrough, true);

$morphAlias = Relation::getMorphAlias(User::class);

// The passthrough data comes from the shop front-end. We cannot trust it.
if (! is_array($passthrough) || $passthrough['billable_type'] !== $morphAlias) {
throw new InvalidPassthroughPayload;
}

return parent::findOrCreateCustomer($passthrough);
}
}
5 changes: 1 addition & 4 deletions app/Support/Paddle/ProcessPaymentSucceededJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Support\Paddle;

use App\Domain\Shop\Actions\HandlePurchaseAction;
use App\Domain\Shop\Exceptions\CouldNotHandlePaymentSucceeded;
use App\Domain\Shop\Models\Bundle;
use App\Domain\Shop\Models\Purchasable;
use App\Domain\Shop\Models\Purchase;
Expand Down Expand Up @@ -50,9 +49,7 @@ public function handle()
return;
}

if (! $user = (new $passthrough['billable_type']())->find($passthrough['billable_id'])) {
throw CouldNotHandlePaymentSucceeded::userNotFound($this->payload);
}
$user = $receipt->billable;

if ($purchasable) {
$purchaseForPurchasable = Purchase::where('user_id', $user->id)
Expand Down

0 comments on commit 40e1568

Please sign in to comment.