From b144d1e031ce95d8d73e969c97e78c5d80def456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 2 Aug 2021 14:53:44 +0200 Subject: [PATCH] Mollie webhook implementation --- app/PaymentDrivers/Mollie/CreditCard.php | 4 +-- app/PaymentDrivers/MolliePaymentDriver.php | 34 +++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/PaymentDrivers/Mollie/CreditCard.php b/app/PaymentDrivers/Mollie/CreditCard.php index c223d1392277..16741b1c7226 100644 --- a/app/PaymentDrivers/Mollie/CreditCard.php +++ b/app/PaymentDrivers/Mollie/CreditCard.php @@ -71,7 +71,7 @@ class CreditCard 'customerId' => $cgt->gateway_customer_reference, 'sequenceType' => 'recurring', 'description' => \sprintf('Hash: %s', $this->mollie->payment_hash->hash), - 'webhookUrl' => 'https://invoiceninja.com', + 'webhookUrl' => $this->mollie->company_gateway->webhookUrl(), ]); if ($payment->status === 'paid') { @@ -105,7 +105,7 @@ class CreditCard 'company_gateway_id' => $this->mollie->company_gateway->hashed_id, 'hash' => $this->mollie->payment_hash->hash, ]), - 'webhookUrl' => 'https://invoiceninja.com', + 'webhookUrl' => $this->mollie->company_gateway->webhookUrl(), 'cardToken' => $request->gateway_response, ]; diff --git a/app/PaymentDrivers/MolliePaymentDriver.php b/app/PaymentDrivers/MolliePaymentDriver.php index 863d9738a8d2..60c27321e8ed 100644 --- a/app/PaymentDrivers/MolliePaymentDriver.php +++ b/app/PaymentDrivers/MolliePaymentDriver.php @@ -22,6 +22,8 @@ use App\Models\PaymentHash; use App\Models\SystemLog; use App\PaymentDrivers\Mollie\CreditCard; use App\Utils\Traits\MakesHash; +use Illuminate\Support\Facades\Validator; +use Mollie\Api\Exceptions\ApiException; use Mollie\Api\MollieApiClient; class MolliePaymentDriver extends BaseDriver @@ -121,8 +123,38 @@ class MolliePaymentDriver extends BaseDriver return $this->payment_method->yourTokenBillingImplmentation(); } - public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null) + public function processWebhookRequest(PaymentWebhookRequest $request) { + $validator = Validator::make($request->all(), [ + 'id' => ['required', 'starts_with:tr'], + ]); + + if ($validator->fails()) { + return response()->json($validator->errors(), 422); + } + + $this->init(); + + $codes = [ + 'open' => Payment::STATUS_PENDING, + 'canceled' => Payment::STATUS_CANCELLED, + 'pending' => Payment::STATUS_PENDING, + 'expired' => Payment::STATUS_CANCELLED, + 'failed' => Payment::STATUS_FAILED, + 'paid' => Payment::STATUS_COMPLETED, + ]; + + try { + $payment = $this->gateway->payments->get($request->id); + + $record = Payment::where('transaction_reference', $payment->id)->firstOrFail(); + $record->status_id = $codes[$payment->status]; + $record->save(); + + return response()->json([], 200); + } catch(ApiException $e) { + return response()->json(['message' => $e->getMessage(), 'gatewayStatusCode' => $e->getCode()], 500); + } } public function process3dsConfirmation(Mollie3dsRequest $request)