mollie / laravel-mollie

Mollie API client wrapper for Laravel & Mollie Connect provider for Laravel Socialite
https://www.mollie.com/
MIT License
322 stars 62 forks source link

Can't access Metadata in handleWebhookNotification #234

Closed PatricGithub closed 10 months ago

PatricGithub commented 11 months ago

My function results in Verbindung zum Webhook ist mit Status-Code 500 ist fehlgeschlagen (Internal Server Error). the original lines of code worked, however this one not. I would be happy for a helping hand.

public function handleWebhookNotification(Request $request) {
    $paymentId = $request->input('id');   
    $payment = Mollie::api()->payments->get($paymentId);
    $metadata = $payment->metadata;
    $rechnungsnummer = $metadata['rechnungsnummer'];

    if ($payment->isPaid()) { 
        $payload = json_decode($request->getContent(), true);

        if (isset($payload['metadata']['rechnungsnummer'])) {
            $rechnungsnummer = $payload['metadata']['rechnungsnummer'];
            DB::table('appointments')
            ->where('rechnungsnummer', '=', $rechnungsnummer) 
            ->update(['paid' => 'yes']);

            DB::table('bills')
            ->where('rechnungsnummer', $rechnungsnummer)
            ->update(['paid' => 'yes']);
        } else {
            // Handle the case where "rechnungsnummer" is not present in the payload
        }
    } 
}
Baspa commented 11 months ago

Can you check your laravel.log file? It probably contains the error message.

PatricGithub commented 11 months ago

`[2023-10-14 09:33:32] local.ERROR: Cannot use object of type stdClass as array {"exception":"[object] (Error(code: 0): Cannot use object of type stdClass as array at /home/vito/hallopsychologe.de/app/Http/Controllers/ThankYouController.php:158) [stacktrace]

0 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\Http\Controllers\ThankYouController->handleWebhookNotification()

1 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(43): Illuminate\Routing\Controller->callAction()

2 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Routing/Route.php(259): Illuminate\Routing\ControllerDispatcher->dispatch()

3 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Routing/Route.php(205): Illuminate\Routing\Route->runController()

4 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Routing/Router.php(799): Illuminate\Routing\Route->run()

5 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\Routing\Router->Illuminate\Routing\{closure}()

6 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()

7 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\Routing\Middleware\SubstituteBindings->handle()

8 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(78): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()

9 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle()

10 /home/vito/hallopsychologe.de/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()

`

Baspa commented 11 months ago

@Psychology4me the payment is an object and not an array. I guess the data in the payload variable is an object instead of an array. Can you verify this?

PatricGithub commented 11 months ago

I can confirm this was indeed the mistake. Here is the final code for future references:

public function handleWebhookNotification(Request $request) {
        $paymentId = $request->input('id');
        $payment = Mollie::api()->payments->get($paymentId);
        $metadata = $payment->metadata;
        $rechnungsnummer = $metadata->rechnungsnummer;

        if ($payment->isPaid()) {
                 DB::table('appointments')
                ->where('rechnungsnummer', '=', $rechnungsnummer)
                ->update(['paid' => 'paid']);
        } else{
}
PatricGithub commented 11 months ago

Maybe provide additional examples in your example section for "coders new to mollie and laravel". (like me) I had to switch from svelte to laravel to make mollie work :) Anyhow, thank you for the great tip with the logs!

Baspa commented 11 months ago

I'm happy that I could help you. Feel free to ask any questions if you need help @Psychology4me.