From d40c222253f45d68dd8e48fdfac8d9d37660b7f3 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 6 Jul 2017 23:17:17 +0300 Subject: [PATCH] Enable Mollie webhooks --- .../PaymentDrivers/BasePaymentDriver.php | 3 ++ .../PaymentDrivers/MolliePaymentDriver.php | 44 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 1571f3b68570..583664482dfc 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -618,6 +618,9 @@ class BasePaymentDriver $account = $this->account(); $invitation = $this->invitation; $invoice = $this->invoice(); + if (! $invoice->canBePaid()) { + return false; + } $invoice->markSentIfUnsent(); $payment = Payment::createNew($invitation); diff --git a/app/Ninja/PaymentDrivers/MolliePaymentDriver.php b/app/Ninja/PaymentDrivers/MolliePaymentDriver.php index 2181cd4d6d77..af21c697f37e 100644 --- a/app/Ninja/PaymentDrivers/MolliePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/MolliePaymentDriver.php @@ -3,23 +3,59 @@ namespace App\Ninja\PaymentDrivers; use Exception; +use App\Models\Invitation; class MolliePaymentDriver extends BasePaymentDriver { + protected function paymentDetails($paymentMethod = false) + { + $data = parent::paymentDetails($paymentMethod); + + // Enable webhooks + $data['notifyUrl'] = url('/payment_hook/'. $this->account()->account_key . '/' . GATEWAY_MOLLIE); + + return $data; + } + public function completeOffsitePurchase($input) { $details = $this->paymentDetails(); - $details['transactionReference'] = $this->invitation->transaction_reference; $response = $this->gateway()->fetchTransaction($details)->send(); - if ($response->isCancelled()) { + if ($response->isCancelled() || ! $response->isSuccessful()) { return false; - } elseif (! $response->isSuccessful()) { - throw new Exception($response->getMessage()); } return $this->createPayment($response->getTransactionReference()); } + + public function handleWebHook($input) + { + $ref = array_get($input, 'id'); + $invitation = Invitation::whereAccountId($this->accountGateway->account_id) + ->whereTransactionReference($ref) + ->first(); + + if ($invitation) { + $this->invitation = $invitation; + } else { + return false; + } + + $data = [ + 'transactionReference' => $ref + ]; + $response = $this->gateway()->fetchTransaction($data)->send(); + + if ($response->isCancelled() || ! $response->isSuccessful()) { + return false; + } + + $this->createPayment($ref); + + return RESULT_SUCCESS; + } + }