diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index a918ef9605..22c8e34351 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -220,17 +220,19 @@ class PaymentController extends Controller return $gateway ->driver(auth()->user()->client) ->setPaymentMethod($payment_method_id) + ->setPaymentHash($payment_hash) ->processPaymentView($data); } public function response(PaymentResponseRequest $request) { - /*Payment Gateway*/ $gateway = CompanyGateway::find($request->input('company_gateway_id'))->firstOrFail(); + $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first(); return $gateway ->driver(auth()->user()->client) ->setPaymentMethod($request->input('payment_method_id')) + ->setPaymentHash($payment_hash) ->processPaymentResponse($request); } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 6e7006918e..ff5ccc51f1 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -14,8 +14,11 @@ namespace App\PaymentDrivers; use App\Events\Invoice\InvoiceWasPaid; use App\Events\Payment\PaymentWasCreated; +use App\Exceptions\PaymentFailed; use App\Factory\PaymentFactory; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; +use App\Jobs\Mail\PaymentFailureMailer; +use App\Jobs\Util\SystemLogger; use App\Models\Client; use App\Models\ClientContact; use App\Models\ClientGatewayToken; @@ -23,6 +26,7 @@ use App\Models\CompanyGateway; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; +use App\Models\SystemLog; use App\PaymentDrivers\AbstractPaymentDriver; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; @@ -58,6 +62,11 @@ class BaseDriver extends AbstractPaymentDriver /* The initiated gateway driver class*/ public $payment_method; + /** + * @var \App\Models\PaymentHash + */ + public $payment_hash; + public static $methods = []; public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false) @@ -112,6 +121,13 @@ class BaseDriver extends AbstractPaymentDriver { } + public function setPaymentHash(PaymentHash $payment_hash) + { + $this->payment_hash = $payment_hash; + + return $this; + } + /** * Helper method to attach invoices to a payment. * @@ -274,4 +290,32 @@ class BaseDriver extends AbstractPaymentDriver return $company_gateway_token; } + + public function processInternallyFailedPayment($gateway, $e) + { + if ($e instanceof \Exception) { + $error = $e->getMessage(); + } + + if ($e instanceof \Checkout\Library\Exceptions\CheckoutHttpException) { + $error = $e->getBody(); + } + + PaymentFailureMailer::dispatch( + $gateway->client, + $error, + $gateway->client->company, + $this->payment_hash->data->value + ); + + SystemLogger::dispatch( + $this->checkout->payment_hash, + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_ERROR, + $gateway::SYSTEM_LOG_TYPE, + $this->checkout->client, + ); + + throw new PaymentFailed($error, $e->getCode()); + } } diff --git a/app/PaymentDrivers/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index c3314eecb5..ca94675071 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -85,12 +85,8 @@ class CreditCard $state = array_merge($state, $request->all()); $state['store_card'] = boolval($state['store_card']); - $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$request->payment_hash])->first(); - - $payment_hash->data = array_merge((array) $payment_hash->data, $state); - $payment_hash->save(); - - $this->checkout->payment_hash = $payment_hash; + $this->checkout->payment_hash->data = array_merge((array) $this->checkout->payment_hash->data, $state); + $this->checkout->payment_hash->save(); if ($request->has('token') && !is_null($request->token)) { return $this->attemptPaymentUsingToken($request); diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index dd334d91fd..5f24015e07 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -118,38 +118,10 @@ trait Utilities try { return redirect($_payment->_links['redirect']['href']); } catch (\Exception $e) { - return $this->processInternallyFailedPayment($e); + return $this->processInternallyFailedPayment($this->checkout, $e); } } - private function processInternallyFailedPayment($e) - { - if ($e instanceof \Checkout\Library\Exceptions\CheckoutHttpException) { - $error = $e->getBody(); - } - - if ($e instanceof \Exception) { - $error = $e->getMessage(); - } - - PaymentFailureMailer::dispatch( - $this->checkout->client, - $error, - $this->checkout->client->company, - $this->checkout->payment_hash->data->value - ); - - SystemLogger::dispatch( - $this->checkout->payment_hash, - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_ERROR, - SystemLog::TYPE_CHECKOUT, - $this->checkout->client, - ); - - throw new PaymentFailed($error, $e->getCode()); - } - private function storePaymentMethod(\Checkout\Models\Payments\Payment $response) { try { diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 1312ad8693..f479ab1db6 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -16,6 +16,7 @@ use App\Models\ClientGatewayToken; use App\Models\GatewayType; use App\Models\Payment; use App\Models\PaymentHash; +use App\Models\SystemLog; use App\PaymentDrivers\BaseDriver; use App\PaymentDrivers\CheckoutCom\Utilities; use App\Utils\Traits\SystemLogTrait; @@ -48,15 +49,12 @@ class CheckoutComPaymentDriver extends BaseDriver public $payment_method; //the gateway type id - /** - * @var \App\Models\PaymentHash - */ - public $payment_hash; - public static $methods = [ GatewayType::CREDIT_CARD => \App\PaymentDrivers\CheckoutCom\CreditCard::class, ]; + const SYSTEM_LOG_TYPE = SystemLog::TYPE_CHECKOUT; + /** * Returns the default gateway type. */