diff --git a/app/Http/Controllers/PaymentWebhookController.php b/app/Http/Controllers/PaymentWebhookController.php index f024a282d32d..bdb9a26f8b8f 100644 --- a/app/Http/Controllers/PaymentWebhookController.php +++ b/app/Http/Controllers/PaymentWebhookController.php @@ -16,11 +16,6 @@ use App\Http\Requests\Payments\PaymentWebhookRequest; class PaymentWebhookController extends Controller { - public function __construct() - { - $this->middleware('guest'); - } - public function __invoke(PaymentWebhookRequest $request, string $company_key, string $company_gateway_id) { $payment = $request->getPayment(); diff --git a/app/Http/Requests/Payments/PaymentWebhookRequest.php b/app/Http/Requests/Payments/PaymentWebhookRequest.php index 445e2d08bb18..2ea39f713485 100644 --- a/app/Http/Requests/Payments/PaymentWebhookRequest.php +++ b/app/Http/Requests/Payments/PaymentWebhookRequest.php @@ -44,7 +44,7 @@ class PaymentWebhookRequest extends Request */ public function getCompanyGateway(): ?CompanyGateway { - return CompanyGateway::find($this->decodePrimaryKey($this->company_gateway_id))->firstOrFail(); + return CompanyGateway::findOrFail($this->decodePrimaryKey($this->company_gateway_id)); } /** @@ -67,16 +67,30 @@ class PaymentWebhookRequest extends Request * * @return null|\App\Models\Payment */ - public function getPayment(): ?Payment + public function getPayment() { - /** - * Some gateways, like Checkout, we can dynamically pass payment hash, - * which we will resolve here and get payment information from it. - */ + // For testing purposes we'll slow down the webhook processing by 2 seconds + // to make sure webhook request doesn't came before our processing. + if (app()->environment() !== 'production') { + sleep(2); + } + + // Some gateways, like Checkout, we can dynamically pass payment hash, + // which we will resolve here and get payment information from it. if ($this->getPaymentHash()) { return $this->getPaymentHash()->payment; } + // While for some gateways, we need to extract the payment source/reference from the webhook request. + // Gateways like this: Stripe + if ($this->has('api_version') && $this->has('type') && $this->has('data')) { + $src = $this->data['object']['id']; + + return Payment::where('transaction_reference', $src)->firstOrFail(); + } + + // If none of previously done logics is correct, we'll just display + // not found page. abort(404); } diff --git a/routes/api.php b/routes/api.php index ab9635e2ef8c..2b4fa6e7c7c6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -185,6 +185,8 @@ Route::group(['middleware' => ['api_db', 'token_auth', 'locale'], 'prefix' => 'a Route::post('support/messages/send', 'Support\Messages\SendingController'); }); -Route::match(['get', 'post'], 'payment_webhook/{company_key}/{company_gateway_id}', 'PaymentWebhookController')->name('payment_webhook'); +Route::match(['get', 'post'], 'payment_webhook/{company_key}/{company_gateway_id}', 'PaymentWebhookController') + ->middleware(['guest', 'api_db']) + ->name('payment_webhook'); Route::fallback('BaseController@notFound');