diff --git a/app/Models/ClientGatewayToken.php b/app/Models/ClientGatewayToken.php index d27183e931e0..ab65290ed5d3 100644 --- a/app/Models/ClientGatewayToken.php +++ b/app/Models/ClientGatewayToken.php @@ -44,7 +44,7 @@ class ClientGatewayToken extends BaseModel public function gateway() { - return $this->belongsTo(CompanyGateway::class); + return $this->hasOne(CompanyGateway::class, 'id', 'company_gateway_id'); } public function gateway_type() diff --git a/app/PaymentDrivers/AuthorizePaymentDriver.php b/app/PaymentDrivers/AuthorizePaymentDriver.php index d9a60dd5cdb5..69a704c8e37a 100644 --- a/app/PaymentDrivers/AuthorizePaymentDriver.php +++ b/app/PaymentDrivers/AuthorizePaymentDriver.php @@ -136,4 +136,7 @@ class AuthorizePaymentDriver extends BaseDriver ->where('company_gateway_id', $this->company_gateway->id) ->first(); } + + public function tokenBilling(ClientGatewayToken $cgt, float $amount) {} + } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index eaa32f860fda..bfd11084a628 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -13,6 +13,7 @@ namespace App\PaymentDrivers; use App\Models\Client; +use App\Models\ClientGatewayToken; use App\Models\CompanyGateway; use App\Models\Invoice; use App\Models\Payment; @@ -48,6 +49,7 @@ class BaseDriver extends AbstractPaymentDriver /* The client */ public $client; + /* The initiated gateway driver class*/ public $payment_method; public static $methods = []; @@ -117,4 +119,13 @@ class BaseDriver extends AbstractPaymentDriver return $payment; } + + /** + * Process an unattended payment + * + * @param ClientGatewayToken $cgt the client gateway token object + * @param float $amount the amount to bill + * @return Response The payment response + */ + public function tokenBilling(ClientGatewayToken $cgt, float $amount) {} } diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 7ea15c1a3f1f..5096ed941084 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -311,4 +311,7 @@ class CheckoutComPaymentDriver extends BasePaymentDriver ]; } } + + public function tokenBilling(ClientGatewayToken $cgt, float $amount) {} + } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index bbb82246fae4..d0c96407d0d1 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -366,5 +366,7 @@ class StripePaymentDriver extends BasePaymentDriver return response([], 200); } - /************************************** Omnipay API methods **********************************************************/ + public function tokenBilling(ClientGatewayToken $cgt, float $amount) {} + + } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 2cd4b0632948..676fc79c7002 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -11,6 +11,7 @@ namespace App\Services\Invoice; +use App\DataMapper\InvoiceItem; use App\Events\Payment\PaymentWasCreated; use App\Factory\PaymentFactory; use App\Models\Client; @@ -38,20 +39,30 @@ class AutoBillInvoice extends AbstractService public function run() { - if(!$invoice->isPayable()) - return $invoice; + if(!$this->invoice->isPayable()) + return $this->invoice; + if($this->invoice->balance > 0) + $gateway_token = $this->getGateway($this->invoice->balance); + else + return $this->invoice->service()->markPaid()->save(); + $fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance); + + if($fee > 0) + $this->addFeeToInvoice($fee); + + $response = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount); + + //if response was successful, toggle the fee type_id to paid } private function getGateway($amount) { - - $gateway_tokens = $this->client->gateway_tokens->orderBy('is_default', 'DESC'); - $billing_gateway_token = null; + $gateway_tokens = $this->client->gateway_tokens()->orderBy('is_default', 'DESC'); - $gateway_tokens->filter(function ($token) use ($amount){ + return $gateway_tokens->filter(function ($token) use ($amount){ return $this->validGatewayLimits($token, $amount); @@ -59,6 +70,25 @@ class AutoBillInvoice extends AbstractService } + private function addFeeToInvoice(float $fee) + { + $item = new InvoiceItem; + $item->quantity = 1; + $item->cost = $fee; + $item->notes = ctrans('texts.online_payment_surcharge'); + $item->type_id = 3; + + $items = (array)$this->invoice->line_items; + $items[] = $item; + + $this->invoice->line_items = $items; + $this->invoice->save(); + + $this->invoice = $this->invoice->calc()->getInvoice()->save(); + + return $this; + } + /** * Checks whether a given gateway token is able * to process the payment after passing through the