Create driver template

This commit is contained in:
David Bomba 2020-11-26 08:07:09 +11:00
parent 137231c95e
commit b2c005a1aa
3 changed files with 155 additions and 41 deletions

View File

@ -11,15 +11,24 @@
namespace App\PaymentDrivers;
use App\Models\ClientGatewayToken;
use App\Models\Payment;
use App\Models\PaymentHash;
use Illuminate\Http\Request;
abstract class AbstractPaymentDriver
{
abstract public function authorize($payment_method);
abstract public function authorizeView(array $data);
abstract public function purchase($amount, $return_client_response = false);
abstract public function authorizeResponse(Request $request);
abstract public function processPaymentView(array $data);
abstract public function processPaymentResponse(Request $request);
abstract public function refund(Payment $payment, $refund_amount, $return_client_response = false);
abstract public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash);
abstract public function setPaymentMethod($payment_method_id);
}

View File

@ -60,22 +60,19 @@ class BaseDriver extends AbstractPaymentDriver
/* The client */
public $client;
/* The initiated gateway driver class*/
/* The initialized gateway driver class*/
public $payment_method;
/**
* @var PaymentHash
*/
/* PaymentHash */
public $payment_hash;
/* Array of payment methods */
public static $methods = [];
public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false)
{
$this->company_gateway = $company_gateway;
$this->invitation = $invitation;
$this->client = $client;
}
@ -83,23 +80,35 @@ class BaseDriver extends AbstractPaymentDriver
* Authorize a payment method.
*
* Returns a reusable token for storage for future payments
* @param const $payment_method The GatewayType::constant
* @return void Return a view for collecting payment method information
*
* @param array $data
* @return mixed Return a view for collecting payment method information
*/
public function authorize($payment_method)
{
}
public function authorizeView(array $data) {}
/**
* Executes purchase attempt for a given amount.
* The payment authorization response
*
* @param float $amount The amount to be collected
* @param bool $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
* @return mixed
* @param Request $request
* @return mixed Return a response for collecting payment method information
*/
public function purchase($amount, $return_client_response = false)
{
}
public function authorizeResponse(Request $request) {}
/**
* Process a payment
*
* @param array $data
* @return mixed Return a view for the payment
*/
public function processPaymentView(array $data) {}
/**
* Process payment response
*
* @param Request $request
* @return mixed Return a response for the payment
*/
public function processPaymentResponse(Request $request) {}
/**
* Executes a refund attempt for a given amount with a transaction_reference.
@ -109,23 +118,30 @@ class BaseDriver extends AbstractPaymentDriver
* @param bool $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
* @return mixed
*/
public function refund(Payment $payment, $amount, $return_client_response = false)
{
}
public function refund(Payment $payment, $amount, $return_client_response = false) {}
/**
* Process an unattended payment.
*
* @param ClientGatewayToken $cgt The client gateway token object
* @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @return void The payment response
*/
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash){}
/**
* Set the inbound request payment method type for access.
*
* @param int $payment_method_id The Payment Method ID
*/
public function setPaymentMethod($payment_method_id)
{
}
public function setPaymentMethod($payment_method_id){}
/************************** Helper methods *************************************/
public function setPaymentHash(PaymentHash $payment_hash)
{
$this->payment_hash = $payment_hash;
return $this;
}
@ -186,17 +202,6 @@ class BaseDriver extends AbstractPaymentDriver
return $payment->service()->applyNumber()->save();
}
/**
* Process an unattended payment.
*
* @param ClientGatewayToken $cgt The client gateway token object
* @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @return void The payment response
*/
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
}
/**
* When a successful payment is made, we need to append the gateway fee
* to an invoice.
@ -212,7 +217,7 @@ class BaseDriver extends AbstractPaymentDriver
/*Payment invoices*/
$payment_invoices = $payment_hash->invoices();
// /*Fee charged at gateway*/
/*Fee charged at gateway*/
$fee_total = $payment_hash->fee_total;
// Sum of invoice amounts
@ -262,7 +267,6 @@ class BaseDriver extends AbstractPaymentDriver
}
}
/**
* Store payment method as company gateway token.
*

View File

@ -0,0 +1,101 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers;
use App\Utils\Traits\MakesHash;
class YourGatewayPaymentDriver extends BaseDriver
{
use MakesHash;
public $refundable = true; //does this gateway support refunds?
public $token_billing = true; //does this gateway support token billing?
public $can_authorise_credit_card = true; //does this gateway support authorizations?
public $gateway; //initialized gateway
public $payment_method; //initialized payment method
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class, //maps GatewayType => Implementation class
];
const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE;
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
return $this;
}
public function authorizeView(array $data)
{
return $this->payment_method->authorizeView($data); //this is your custom implementation from here
}
public function authorizeResponse($request)
{
return $this->payment_method->authorizeResponse($request); //this is your custom implementation from here
}
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data); //this is your custom implementation from here
}
public function processPaymentResponse($request)
{
return $this->payment_method->paymentResponse($request); //this is your custom implementation from here
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
return $this->payment_method->yourRefundImplementationHere();
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
return $this->payment_method->yourTokenBillingImplmentation();
}
/**
* Creates a payment record for the given
* data array.
*
* @param array $data An array of payment attributes
* @param float $amount The amount of the payment
* @return Payment The payment object
*/
public function createPaymentRecord($data, $amount): ?Payment
{
$payment = PaymentFactory::create($this->client->company_id, $this->client->user_id);
$payment->client_id = $this->client->id;
$payment->company_gateway_id = $this->company_gateway->id;
$payment->status_id = Payment::STATUS_COMPLETED;
$payment->gateway_type_id = $data['gateway_type_id'];
$payment->type_id = $data['type_id'];
$payment->currency_id = $this->client->getSetting('currency_id');
$payment->date = Carbon::now();
$payment->transaction_reference = $data['transaction_reference'];
$payment->amount = $amount;
$payment->save();
return $payment->service()->applyNumber()->save();
}
}