Move after-payment logic into BaseDriver

This commit is contained in:
Benjamin Beganović 2020-10-25 18:51:26 +01:00
parent dcb48297fb
commit f73ad4bef7
4 changed files with 37 additions and 54 deletions

View File

@ -13,6 +13,7 @@
namespace App\PaymentDrivers; namespace App\PaymentDrivers;
use App\Events\Invoice\InvoiceWasPaid; use App\Events\Invoice\InvoiceWasPaid;
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory; use App\Factory\PaymentFactory;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Models\Client; use App\Models\Client;
@ -147,6 +148,24 @@ class BaseDriver extends AbstractPaymentDriver
$payment->currency_id = $this->client->getSetting('currency_id'); $payment->currency_id = $this->client->getSetting('currency_id');
$payment->date = Carbon::now(); $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(); return $payment->service()->applyNumber()->save();
} }

View File

@ -27,11 +27,6 @@ class CreditCard
*/ */
public $checkout; public $checkout;
/**
* @var \App\Models\PaymentHash
*/
private $payment_hash;
public function __construct(CheckoutComPaymentDriver $checkout) public function __construct(CheckoutComPaymentDriver $checkout)
{ {
$this->checkout = $checkout; $this->checkout = $checkout;
@ -94,7 +89,7 @@ class CreditCard
$payment_hash->data = array_merge((array) $payment_hash->data, $state); $payment_hash->data = array_merge((array) $payment_hash->data, $state);
$payment_hash->save(); $payment_hash->save();
$this->payment_hash = $payment_hash; $this->checkout->payment_hash = $payment_hash;
if ($request->has('token') && !is_null($request->token)) { if ($request->has('token') && !is_null($request->token)) {
return $this->attemptPaymentUsingToken(); return $this->attemptPaymentUsingToken();
@ -105,14 +100,14 @@ class CreditCard
private function attemptPaymentUsingToken() private function attemptPaymentUsingToken()
{ {
$method = new IdSource($this->payment_hash->data->token); $method = new IdSource($this->checkout->payment_hash->data->token);
return $this->completePayment($method); return $this->completePayment($method);
} }
private function attemptPaymentUsingCreditCard() private function attemptPaymentUsingCreditCard()
{ {
$checkout_response = $this->payment_hash->data->server_response; $checkout_response = $this->checkout->payment_hash->data->server_response;
$method = new TokenSource( $method = new TokenSource(
$checkout_response->cardToken $checkout_response->cardToken
@ -125,9 +120,9 @@ class CreditCard
{ {
// TODO: confirmGatewayFee & unwind // TODO: confirmGatewayFee & unwind
$payment = new Payment($method, $this->payment_hash->data->currency); $payment = new Payment($method, $this->checkout->payment_hash->data->currency);
$payment->amount = $this->payment_hash->data->value; $payment->amount = $this->checkout->payment_hash->data->value;
$payment->reference = $this->payment_hash->data->reference; $payment->reference = $this->checkout->payment_hash->data->reference;
if ($this->checkout->client->currency()->code === 'EUR' && $enable_3ds) { if ($this->checkout->client->currency()->code === 'EUR' && $enable_3ds) {
$payment->{'3ds'} = ['enabled' => true]; $payment->{'3ds'} = ['enabled' => true];

View File

@ -12,12 +12,10 @@
namespace App\PaymentDrivers\CheckoutCom; namespace App\PaymentDrivers\CheckoutCom;
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Models\PaymentType; use App\Models\PaymentType;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Utils\Ninja;
trait Utilities trait Utilities
{ {
@ -49,27 +47,18 @@ trait Utilities
private function processSuccessfulPayment(\Checkout\Models\Payments\Payment $_payment) 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(); // $this->saveCreditCard();
} }
$data = [ $data = [
'payment_method' => $_payment->source['id'], 'payment_method' => $_payment->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])), '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); $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( SystemLogger::dispatch(
['response' => $_payment, 'data' => $data], ['response' => $_payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::CATEGORY_GATEWAY_RESPONSE,
@ -87,12 +76,12 @@ trait Utilities
$this->checkout->client, $this->checkout->client,
$_payment, $_payment,
$this->checkout->client->company, $this->checkout->client->company,
$this->payment_hash->data->value $this->checkout->payment_hash->data->value
); );
$message = [ $message = [
'server_response' => $_payment, 'server_response' => $_payment,
'data' => $this->payment_hash->data, 'data' => $this->checkout->payment_hash->data,
]; ];
SystemLogger::dispatch( SystemLogger::dispatch(
@ -114,20 +103,11 @@ trait Utilities
$data = [ $data = [
'payment_method' => $_payment->source['id'], 'payment_method' => $_payment->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])), '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); $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( SystemLogger::dispatch(
['response' => $_payment, 'data' => $data], ['response' => $_payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::CATEGORY_GATEWAY_RESPONSE,
@ -157,11 +137,11 @@ trait Utilities
$this->checkout->client, $this->checkout->client,
$error, $error,
$this->checkout->client->company, $this->checkout->client->company,
$this->payment_hash->data->value $this->checkout->payment_hash->data->value
); );
SystemLogger::dispatch( SystemLogger::dispatch(
$this->payment_hash, $this->checkout->payment_hash,
SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR, SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT, SystemLog::TYPE_CHECKOUT,

View File

@ -48,6 +48,11 @@ class CheckoutComPaymentDriver extends BaseDriver
public $payment_method; //the gateway type id public $payment_method; //the gateway type id
/**
* @var \App\Models\PaymentHash
*/
public $payment_hash;
public static $methods = [ public static $methods = [
GatewayType::CREDIT_CARD => \App\PaymentDrivers\CheckoutCom\CreditCard::class, GatewayType::CREDIT_CARD => \App\PaymentDrivers\CheckoutCom\CreditCard::class,
]; ];
@ -140,22 +145,6 @@ class CheckoutComPaymentDriver extends BaseDriver
return $this->payment_method->paymentResponse($request); 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) public function saveCard($state)
{ {
//some cards just can't be tokenized.... //some cards just can't be tokenized....