protonemedia / laravel-paddle

Paddle.com API integration for Laravel with support for webhooks/events
https://protone.media/en/blog/a-new-laravel-package-to-handle-payments-and-subscriptions-with-paddle
MIT License
203 stars 18 forks source link

Undefined index: alert_name #3

Closed sandulat closed 4 years ago

sandulat commented 4 years ago

Hello! Thank you for building this awesome package!

When I receive a "payment_succeeded" webhook from Paddle, an exception log is created:

[2020-01-27 22:58:09] production.ERROR: Undefined index: alert_name {"exception":"[object] (ErrorException(code: 0): Undefined index: alert_name at .../vendor/protonemedia/laravel-paddle/src/Events/Event.php:59)

However, everything works excellently. My listener catches the event and does the business logic as expected. I don't understand why this exception is happening while everything works as expected. It feels like Paddle is dispatching 2 requests, instead of 1. But the Alert History from Paddle shows only 1 request, and the payload does contain alert_name.

Should I submit a PR which checks if "alert_name" exists in the payload, inside the fire method from the Event base class? Like this:

    public static function fire(array $data)
    {
        if (isset($data['alert_name'])) {
            $event = Str::studly($data['alert_name']);

            $eventClass = __NAMESPACE__ . '\\' . $event;

            event(new $eventClass($data));
        }
    }

Thank you very much!

sandulat commented 4 years ago

Wow Paddle began to spam the webhook endpoint: image

sandulat commented 4 years ago

My bad! I've configured the one time product to additionally request the webhook endpoint once purchased. Sorry for bothering you!

zoltanszogyenyi commented 4 years ago

@sandulat I'm facing the same problem. I have already created a webhook for the PaymentSuccess, but I'm not sure how to disable the other ProductFulfillment webhook on Paddle. I must choose an option from there: download, license or webhook. Thanks!

sandulat commented 4 years ago

@zoltanszogyenyi I've chosen the "webhook" option and ended up pointing it to the homepage/landing page of my app so that the webhook would just receive a 200 response. Very dirty, I know. But hey, it works 😄! I'd also love to find out a better way.

zoltanszogyenyi commented 4 years ago

@sandulat Oh that's dirty alright lol. Here's what I managed to do to still use the library:

When setting the main webhook (the one you must choose as fulfillment) I added a static value with "alert_name" as a label and "payment_suceeded" as the value. Then I would basically hackishly use the PaymentSuceeded event as a hook to get the data from the main webhook.

I contacted the Paddle team and they recommend using the main webhook comparative to the PaymentSucceeded one because they also make security checks after the payment, so there are some rare occasions when your code may be faulty.

Anyways, I hope the author of this library gives support to this fulfillment webhook. It's a great library!

pascalbaljet commented 4 years ago

@zoltanszogyenyi will look into it!

pascalbaljet commented 4 years ago

I've just tagged v1.1.2, which has a new GenericWebhook event. This event gets fired when the alert_name is missing. It also brings support for accessing the request:

<?php

namespace App\Listeners;

use ProtoneMedia\LaravelPaddle\Events\GenericWebhook;

class HandleProductFulfillment
{
    public function handle(GenericWebhook $event)
    {
        $webhookData = $event->all();
        $request = $event->getRequest();
    }
}