|
15 | 15 | use Laravel\Paddle\Events\SubscriptionUpdated;
|
16 | 16 | use Laravel\Paddle\Events\WebhookHandled;
|
17 | 17 | use Laravel\Paddle\Events\WebhookReceived;
|
| 18 | +use Laravel\Paddle\Exceptions\InvalidPassthroughPayload; |
18 | 19 | use Laravel\Paddle\Http\Middleware\VerifyWebhookSignature;
|
19 | 20 | use Laravel\Paddle\Receipt;
|
20 | 21 | use Laravel\Paddle\Subscription;
|
@@ -53,7 +54,11 @@ public function __invoke(Request $request)
|
53 | 54 | WebhookReceived::dispatch($payload);
|
54 | 55 |
|
55 | 56 | if (method_exists($this, $method)) {
|
56 |
| - $this->{$method}($payload); |
| 57 | + try { |
| 58 | + $this->{$method}($payload); |
| 59 | + } catch (InvalidPassthroughPayload $e) { |
| 60 | + return new Response('Webhook Skipped'); |
| 61 | + } |
57 | 62 |
|
58 | 63 | WebhookHandled::dispatch($payload);
|
59 | 64 |
|
@@ -142,17 +147,23 @@ protected function handleSubscriptionPaymentFailed(array $payload)
|
142 | 147 | *
|
143 | 148 | * @param array $payload
|
144 | 149 | * @return void
|
| 150 | + * |
| 151 | + * @throws \Laravel\Paddle\Exceptions\InvalidPassthroughPayload |
145 | 152 | */
|
146 | 153 | protected function handleSubscriptionCreated(array $payload)
|
147 | 154 | {
|
148 | 155 | $passthrough = json_decode($payload['passthrough'], true);
|
149 | 156 |
|
| 157 | + if (! is_array($passthrough) || ! isset($passthrough['subscription_name'])) { |
| 158 | + throw new InvalidPassthroughPayload; |
| 159 | + } |
| 160 | + |
| 161 | + $customer = $this->findOrCreateCustomer($payload['passthrough']); |
| 162 | + |
150 | 163 | $trialEndsAt = $payload['status'] === Subscription::STATUS_TRIALING
|
151 | 164 | ? Carbon::createFromFormat('Y-m-d', $payload['next_bill_date'], 'UTC')->startOfDay()
|
152 | 165 | : null;
|
153 | 166 |
|
154 |
| - $customer = $this->findOrCreateCustomer($payload['passthrough']); |
155 |
| - |
156 | 167 | $subscription = $customer->subscriptions()->create([
|
157 | 168 | 'name' => $passthrough['subscription_name'],
|
158 | 169 | 'paddle_id' => $payload['subscription_id'],
|
@@ -238,11 +249,17 @@ protected function handleSubscriptionCancelled(array $payload)
|
238 | 249 | *
|
239 | 250 | * @param string $passthrough
|
240 | 251 | * @return \Laravel\Paddle\Billable
|
| 252 | + * |
| 253 | + * @throws \Laravel\Paddle\Exceptions\InvalidPassthroughPayload |
241 | 254 | */
|
242 | 255 | protected function findOrCreateCustomer(string $passthrough)
|
243 | 256 | {
|
244 | 257 | $passthrough = json_decode($passthrough, true);
|
245 | 258 |
|
| 259 | + if (! is_array($passthrough) || ! isset($passthrough['billable_id'], $passthrough['billable_type'])) { |
| 260 | + throw new InvalidPassthroughPayload; |
| 261 | + } |
| 262 | + |
246 | 263 | return Customer::firstOrCreate([
|
247 | 264 | 'billable_id' => $passthrough['billable_id'],
|
248 | 265 | 'billable_type' => $passthrough['billable_type'],
|
|
0 commit comments