0

Is there any way to check if there are any pending events, from the controller?

I've created two listeners for handling Stripe webhook. One for 'WebhookHandled' and another for 'WebhookReceived'.
when i register a user (and subscribe / make payment as part of registration), these webhook will trigger accordingly. i would like to know if the above events are completed or not before redirecting the user to dashboard. Because, dashboard page contains many values which are only updated from the webhook.

This is my code after the stripe successfully confirms the payment as part of regisration.

    public function paymentVerificationUser(Request $request)
    {
        try {
            $data = $request->all();

            User::where('email', $data['user']['email'])->update([
                'status' => 'A' // activated user
            ]);
            $user = User::with(['prefectures', 'files'])->where(['email' => $data['user']['email'], 'status' => 'A'])->first();

            $data = [
                "message" => '"ユーザー登録が完了しました。ログイン画面よりログインしてください。"',
                'user' => new UserResource($user),
            ];

            $loginUrl = env('USER_FRONTEND_URL') . 'login';

            $user->notify(new UserSignUpCompleteNotification($loginUrl));

            $admin = User::where('email', env('ADMIN_EMAILS'))->first();
            if($admin) {
                $loginUrl = env('ADMIN_FRONTEND_URL') . 'login';
                $admin->notify(new AdminSignUpCompleteNotification($loginUrl, $user));
            }

            /* check here for event completion
             * check if all the stripe events are completed, if not , wait for it to complete. 
             * I'm planning to add a loading button in the front end until the webhooks are completed 
             * OR, if there are other better way to achieve the same goal, please share them.
             */            

            return $this->success($data);
        } catch (\Exception $th) {
            return $this->error($th);
        }
    }
Nadiya
  • 382
  • 1
  • 7
  • 18

1 Answers1

0

Try following code

    $hasPendingEvents = Event::hasListeners('WebhookHandled') || Event::hasListeners('WebhookReceived');
if ($hasPendingEvents) { 
  //Your code
}
Nehal
  • 1,004
  • 1
  • 10
  • 33