diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 678d2728ba77..6ecdc15ac08b 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -13,6 +13,7 @@ namespace App\PaymentDrivers; use App\Events\Invoice\InvoiceWasPaid; +use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Models\Client; @@ -147,6 +148,24 @@ class BaseDriver extends AbstractPaymentDriver $payment->currency_id = $this->client->getSetting('currency_id'); $payment->date = Carbon::now(); + $client_contact = $this->getContact(); + $client_contact_id = $client_contact ? $client_contact->id : null; + + $payment->amount = $data['amount']; + $payment->type_id = $data['payment_type']; + $payment->transaction_reference = $data['payment_method']; + $payment->client_contact_id = $client_contact_id; + $payment->save(); + + $this->payment_hash->payment_id = $payment->id; + $this->payment_hash->save(); + + $this->attachInvoices($payment, $this->payment_hash); + + $payment->service()->updateInvoicePayment($this->payment_hash); + + event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); + return $payment->service()->applyNumber()->save(); } diff --git a/app/PaymentDrivers/CheckoutCom/CreditCard.php b/app/PaymentDrivers/CheckoutCom/CreditCard.php index 13c6f1236363..14ffc720db3b 100644 --- a/app/PaymentDrivers/CheckoutCom/CreditCard.php +++ b/app/PaymentDrivers/CheckoutCom/CreditCard.php @@ -27,11 +27,6 @@ class CreditCard */ public $checkout; - /** - * @var \App\Models\PaymentHash - */ - private $payment_hash; - public function __construct(CheckoutComPaymentDriver $checkout) { $this->checkout = $checkout; @@ -94,7 +89,7 @@ class CreditCard $payment_hash->data = array_merge((array) $payment_hash->data, $state); $payment_hash->save(); - $this->payment_hash = $payment_hash; + $this->checkout->payment_hash = $payment_hash; if ($request->has('token') && !is_null($request->token)) { return $this->attemptPaymentUsingToken(); @@ -105,14 +100,14 @@ class CreditCard private function attemptPaymentUsingToken() { - $method = new IdSource($this->payment_hash->data->token); + $method = new IdSource($this->checkout->payment_hash->data->token); return $this->completePayment($method); } private function attemptPaymentUsingCreditCard() { - $checkout_response = $this->payment_hash->data->server_response; + $checkout_response = $this->checkout->payment_hash->data->server_response; $method = new TokenSource( $checkout_response->cardToken @@ -125,9 +120,9 @@ class CreditCard { // TODO: confirmGatewayFee & unwind - $payment = new Payment($method, $this->payment_hash->data->currency); - $payment->amount = $this->payment_hash->data->value; - $payment->reference = $this->payment_hash->data->reference; + $payment = new Payment($method, $this->checkout->payment_hash->data->currency); + $payment->amount = $this->checkout->payment_hash->data->value; + $payment->reference = $this->checkout->payment_hash->data->reference; if ($this->checkout->client->currency()->code === 'EUR' && $enable_3ds) { $payment->{'3ds'} = ['enabled' => true]; diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index 22e4a3678d4d..1deaf8b70840 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -12,12 +12,10 @@ namespace App\PaymentDrivers\CheckoutCom; -use App\Events\Payment\PaymentWasCreated; use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Util\SystemLogger; use App\Models\PaymentType; use App\Models\SystemLog; -use App\Utils\Ninja; trait Utilities { @@ -49,27 +47,18 @@ trait Utilities private function processSuccessfulPayment(\Checkout\Models\Payments\Payment $_payment) { - if ($this->payment_hash->data->store_card) { + if ($this->checkout->payment_hash->data->store_card) { // $this->saveCreditCard(); } $data = [ 'payment_method' => $_payment->source['id'], 'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])), - 'amount' => $this->payment_hash->data->value, + 'amount' => $this->checkout->payment_hash->data->value, ]; $payment = $this->checkout->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); - $this->payment_hash->payment_id = $payment->id; - $this->payment_hash->save(); - - $this->checkout->attachInvoices($payment, $this->payment_hash); - - $payment->service()->updateInvoicePayment($this->payment_hash); - - event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); - SystemLogger::dispatch( ['response' => $_payment, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, @@ -87,12 +76,12 @@ trait Utilities $this->checkout->client, $_payment, $this->checkout->client->company, - $this->payment_hash->data->value + $this->checkout->payment_hash->data->value ); $message = [ 'server_response' => $_payment, - 'data' => $this->payment_hash->data, + 'data' => $this->checkout->payment_hash->data, ]; SystemLogger::dispatch( @@ -114,20 +103,11 @@ trait Utilities $data = [ 'payment_method' => $_payment->source['id'], 'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])), - 'amount' => $this->payment_hash->data->value, + 'amount' => $this->checkout->payment_hash->data->value, ]; $payment = $this->checkout->createPayment($data, \App\Models\Payment::STATUS_PENDING); - $this->payment_hash->payment_id = $payment->id; - $this->payment_hash->save(); - - $this->checkout->attachInvoices($payment, $this->payment_hash); - - $payment->service()->updateInvoicePayment($this->payment_hash); - - event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); - SystemLogger::dispatch( ['response' => $_payment, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, @@ -157,11 +137,11 @@ trait Utilities $this->checkout->client, $error, $this->checkout->client->company, - $this->payment_hash->data->value + $this->checkout->payment_hash->data->value ); SystemLogger::dispatch( - $this->payment_hash, + $this->checkout->payment_hash, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_ERROR, SystemLog::TYPE_CHECKOUT, diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index d4afee61f9bb..9209d5731534 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -48,6 +48,11 @@ 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, ]; @@ -140,22 +145,6 @@ class CheckoutComPaymentDriver extends BaseDriver return $this->payment_method->paymentResponse($request); } - public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment - { - $payment = parent::createPayment($data, $status); - - $client_contact = $this->getContact(); - $client_contact_id = $client_contact ? $client_contact->id : null; - - $payment->amount = $data['amount']; - $payment->type_id = $data['payment_type']; - $payment->transaction_reference = $data['payment_method']; - $payment->client_contact_id = $client_contact_id; - $payment->save(); - - return $payment; - } - public function saveCard($state) { //some cards just can't be tokenized....