From 2d824e4d1ece5b09a5052272cb81a802e5144e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 30 Sep 2021 15:55:34 +0200 Subject: [PATCH] Handling webhooks --- .../GoCardlessPaymentDriver.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/app/PaymentDrivers/GoCardlessPaymentDriver.php b/app/PaymentDrivers/GoCardlessPaymentDriver.php index 2e8990177d3f..770a272c7d37 100644 --- a/app/PaymentDrivers/GoCardlessPaymentDriver.php +++ b/app/PaymentDrivers/GoCardlessPaymentDriver.php @@ -11,6 +11,7 @@ namespace App\PaymentDrivers; +use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; @@ -211,4 +212,42 @@ class GoCardlessPaymentDriver extends BaseDriver ); } } + + public function processWebhookRequest(PaymentWebhookRequest $request) + { + // Allow app to catch up with webhook request. + sleep(2); + + $this->init(); + + foreach ($request->events as $event) { + if ($event['action'] === 'confirmed') { + $payment = Payment::query() + ->where('transaction_reference', $event['links']['payment']) + ->where('company_id', $request->getCompany()->id) + ->first(); + + if ($payment) { + $payment->status_id = Payment::STATUS_COMPLETED; + $payment->save(); + } + } + + if ($event['action'] === 'failed') { + // Update invoices, etc? + + $payment = Payment::query() + ->where('transaction_reference', $event['links']['payment']) + ->where('company_id', $request->getCompany()->id) + ->first(); + + if ($payment) { + $payment->status_id = Payment::STATUS_FAILED; + $payment->save(); + } + } + } + + return response()->json([], 200); + } }