From 028b20fcdbcc373ce608160a95d546c571859946 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 6 Jul 2017 23:12:09 +0300 Subject: [PATCH] Enable Mollie webhooks --- .../PaymentDrivers/MolliePaymentDriver.php | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/app/Ninja/PaymentDrivers/MolliePaymentDriver.php b/app/Ninja/PaymentDrivers/MolliePaymentDriver.php index 0c361b815c..af21c697f3 100644 --- a/app/Ninja/PaymentDrivers/MolliePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/MolliePaymentDriver.php @@ -3,6 +3,7 @@ namespace App\Ninja\PaymentDrivers; use Exception; +use App\Models\Invitation; class MolliePaymentDriver extends BasePaymentDriver { @@ -10,9 +11,8 @@ class MolliePaymentDriver extends BasePaymentDriver { $data = parent::paymentDetails($paymentMethod); - // Enable the webhooks - //$data['notifyUrl'] = $data['returnUrl']; - $data['notifyUrl'] = url('/payment_hook/'. $this->account->account_key . '/' . GATEWAY_MOLLIE); + // Enable webhooks + $data['notifyUrl'] = url('/payment_hook/'. $this->account()->account_key . '/' . GATEWAY_MOLLIE); return $data; } @@ -20,18 +20,12 @@ class MolliePaymentDriver extends BasePaymentDriver public function completeOffsitePurchase($input) { $details = $this->paymentDetails(); - $details['transactionReference'] = $this->invitation->transaction_reference; $response = $this->gateway()->fetchTransaction($details)->send(); - \Log::info('completeOffsitePurchase'); - \Log::info($response); - - if ($response->isCancelled()) { + if ($response->isCancelled() || ! $response->isSuccessful()) { return false; - } elseif (! $response->isSuccessful()) { - throw new Exception($response->getMessage()); } return $this->createPayment($response->getTransactionReference()); @@ -39,12 +33,29 @@ class MolliePaymentDriver extends BasePaymentDriver public function handleWebHook($input) { - //$paymentId = array_get($input, 'id'); - $response = $this->gateway()->fetchTransaction($input)->send(); + $ref = array_get($input, 'id'); + $invitation = Invitation::whereAccountId($this->accountGateway->account_id) + ->whereTransactionReference($ref) + ->first(); - \Log::info('handleWebHook'); - \Log::info($response); - return 'Processed successfully'; + 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; } }