Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Derockypeter's avatar

Stripe Casher webhook returns 500 on the dashboard

Ok, I am trying to make an API call to create a hosted zone and update its DNS in AWS after the stripe webhook returns payment_succeeded. The API call is always successful, but Stripe fails with a 500 error and a retry. I've tried to use Jobs but still can't get it going. This is from My StripeEventListener.php

<?php
namespace App\Listeners;

use GuzzleHttp\Client;
use App\Mail\WebsiteLive;
use GuzzleHttp\Middleware;
use App\Events\StripeEvent;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Handler\CurlHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Validator;
use Laravel\Cashier\Http\WebhookResponse;
use App\Mail\MailInvoiceOnSuccesfulPayment;
use Illuminate\Contracts\Queue\ShouldQueue;
use Laravel\Cashier\Events\WebhookReceived;
use App\Jobs\UpdateAwsJob;
class StripeEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\StripeEvent  $event
     * @return void
     */
    public function handle(WebhookReceived $event) :void
    {
        if ($event->payload['type'] === 'invoice.payment_succeeded') {
            $data = $event->payload['data']['object'];
            $response = new Response('Webhook received.', 200);
            $response->send();
            UpdateAwsJob::dispatch($data)->onQueue('webhooks');
            // Handle the incoming event...  || $event->payload['type'] === 'customer.subscription.updated' || $event->payload['type'] === 'customer.subscription.created'
            
            return;
            // return response()->json(['message' => 'Webhook processing started']);

            // Make the API call to your server to update AWS
            // This could take some time...
            // $client = new Client();
            // $url = route('api.domain.register', ['stripe_id' => $data['customer']]);
            // $client->request('GET', $url);
            // echo $response->getBody();
            // $response = WebhookResponse::create('Webhook received successfully.');
            // $response->send();
            // return WebhookResponse::create('Webhook received successfully.');
        }
    }

    
}

And this is my job

<?php

namespace App\Jobs;

use App\Models\User;
use GuzzleHttp\Client;
use GuzzleHttp\Middleware;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\DB;
use GuzzleHttp\Handler\CurlHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;

class UpdateAwsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;
    /**
     * Create a new job instance.
     *
     * @param string $data
     */
    public function __construct(Array $data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // Make the API call to your server to update AWS
        // This could take some time...
        $amountPaid = number_format($this->data['amount_paid']/100, 2, '.', '');
        $discount = $this->data['discount'] === null ? json_encode(['value' => null]) : json_encode(['id' => $this->data['discount']['coupon']['id'], 'percentage' => $this->data['discount']['coupon']['percent_off'], 'amount_off' => $this->data['discount']['coupon']['amount_off']]) ;
            
        DB::table('payments')->insert([
            'customer_id' => $this->data['customer'],
            'collection_method' => $this->data['collection_method'],
            'amount_paid' => $amountPaid,
            'currency' => $this->data['currency'],
            'created' => $this->data['created'],
            'billing_reason' => $this->data['billing_reason'],
            'amount_remaining' => $this->data['amount_remaining'],
            'account_country' => $this->data['account_country'],
            'discount' => $discount,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        // $client = new Client();
        // $url = route('api.domain.register', ['stripe_id' => $this->data['customer']]);
        // $response = $client->request('GET', $url);
        $this->registerDomain($this->data['customer']);
        // Log::info($response);

        // http_response_code(200);
        //  $response->getBody();
    }

    public function registerDomain($stripeId) {
        $user = User::where('stripe_id', $stripeId)->first();
        // $tenant = Tenant::where('user_id', $user->id)->first();
        // $adminClientOrder = AdminClientOrder::where('tenant_id', $tenant->id)->first();
        // $tenantPassTB = DB::table('tenant_password_tables')->where('tenant_id', $tenant->id)->first();
        // if (!empty($tenant)) {
            $domain = 'purityvendor';
            $detail = [
                'email' => $user->email,
                'password' => 'hahkeil',
                'domain' => $domain,
                'name' => $user->firstname.' '.$user->lastname,
            ];
            $domainDotCom = $domain.'.com';
            $years = 1;
            $key = env('NAMESILO_SANDBOX_API_KEY');
            $api = env('NAMESILO_SANDBOX_API_URL');
            // try {
            //     $URL = "{$api}/registerDomain?version=1&type=xml&key={$key}&domain={$domainDotCom}&years={$years}&private=1&auto_renew=1";
            //     $client = new \GuzzleHttp\Client();
            
            //     $response = $client->request('GET', $URL);
            //     $body = $response->getBody(); 
            //     $xml = simplexml_load_string($body);
            //     if (htmlentities((string)$xml->reply->code) == 300) { // && htmlentities((string)$xml->reply->detail) == 'success'
                    return $this->runAWSUtility($domainDotCom, $detail);
                    // return 200;
            //     }
            //     else {
            //         return response()->json(['message' => htmlentities((string)$xml->reply->detail), 'status' => htmlentities((string) $xml->reply->code)]);
            //     }
            // } catch (\Throwable $th) {
            //     echo $th->getMessage();
            //     exit;
            // }
        // }
        // else{
        //     return response()->json(['status' => 404, 'message' => 'Payment made against a physician that does not exist']);
        // }
        
    }

    public function configureEmail($domainName, $key, $email, $forward1) {
        $api = env('NAMESILO_API_URL');
        try {
            $URL = "{$api}/configureEmailForward?version=1&type=xml&key={$key}&domain={$domainName}&email={$email}&forward1={$forward1}";
            $handler = new CurlHandler();
            $client = new \GuzzleHttp\Client();
            $tapMiddleware = Middleware::tap(function ($request) {});
            $response = $client->request('GET', $URL, [
                'handler' => $tapMiddleware($handler)
            ]);
            $body = $response->getBody(); 
            $xml = simplexml_load_string($body);
            if (htmlentities((string)$xml->reply->detail) === 'success' && htmlentities((string)$xml->reply->code) === 300) {
                // Request AWS Facility
                // return 
                echo response()->json(["message" => htmlentities((string)$xml->reply->detail), "status" =>  htmlentities((string)$xml->reply->code)]);
            }
            else {
                echo response()->json(['message' => htmlentities((string)$xml->reply->detail), 'status' => htmlentities((string) $xml->reply->code)]);
                exit;
            }
        } catch (\Throwable $th) {
            echo $th->getMessage();
            exit;
        }
    }

    public function runAWSUtility($domain, $detail){
        try {
            $handler = new CurlHandler();
            $client = new \GuzzleHttp\Client();
            $tapMiddleware = Middleware::tap(function ($request) {});
            $data = ["DomainName" => $domain];
            $response = $client->request('POST', env('AWS_UTILITY_URL'), [
                'json' => $data,
                'handler' => $tapMiddleware($handler)
            ]);
            
            $body = $response->getBody();
            $data = json_decode($body, true);
            if ($data['status'] === 200) {
                // Send mail and run the aws sendcommand api
                // $this->sendMail($detail);
                // return response()->json(['status' => 200], 200);

                return $this->runAWSUtilityCommand($domain);
            }
            else {
                echo 'Not available:'. $response->getStatusCode();
            }
        } catch (\Throwable $th) {
            return $th->getMessage();
        }
    }

    public function runAWSUtilityCommand($domain){
        try {
            $handler = new CurlHandler();
            $client = new \GuzzleHttp\Client();
            $tapMiddleware = Middleware::tap(function ($request) {});
            $data = ["DomainName" => $domain];

            $response = $client->request('POST', env('AWS_UTILITY_URL_SENDCOMMAND'), [
                'json' => $data,
                'handler' => $tapMiddleware($handler)
            ]);

            $body = $response->getBody();
            $data = json_decode($body, true);
            if ($data['status'] === 200) {
                return response()->json(['message' => 'Success', 'status' => $data['status'], 'commandID' => $data['commandID']]);
            }
            else {
                echo 'Not available:'. $response->getStatusCode();
            }
        } catch (\Throwable $th) {
            return $th->getMessage();
        }
    }
}

Please any help will be really appreciated. Thanks.

0 likes
9 replies
LaryAI's avatar
Level 58

The 500 error suggests that there is an issue with the server-side code. One possible solution is to check the server logs to see if there are any error messages that can provide more information about the issue. Additionally, it may be helpful to try simplifying the code to isolate the issue.

In the provided code, it looks like the UpdateAwsJob is being dispatched to a queue. It may be helpful to check the queue worker logs to see if there are any errors being thrown when the job is being processed.

Another thing to check is the Stripe dashboard to see if there are any error messages or logs that can provide more information about the 500 error.

Without more information about the specific error message or logs, it is difficult to provide a more specific solution.

Derockypeter's avatar

@LaryAI Stripe replies err The webhook delivery failed. Stripe will try to resend.... And no response body.

Snapey's avatar

check your laravel log file. It will show the reason for the 500 error

Derockypeter's avatar

Stripe replies err The webhook delivery failed. Stripe will try to resend.... And no response body. There's nothing on my log file.

Derockypeter's avatar

@Snapey I don't understand. It does run the logic within the payment_succeeded. So I don't understand please.

Snapey's avatar

@Derockypeter you said Stripe sees a 500 error from your webhook - in which case it should be logged in your app

Derockypeter's avatar

@Snapey Ok, the 500 error was from my Job dispatch, fixed it. But now it doesn't deliver the webhook. And no response body.

martinbean's avatar

@derockypeter This isn’t an issue with Stripe. It’s an issue with your code.

If Stripe is receiving a 500 then it’s receiving that status from your server. So you need to look into your log files as see what error is causing a 500 response to be returned, as a 500 means your application is throwing an exception.

Please or to participate in this conversation.