1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Services/Invoice/AutoBillInvoice.php

207 lines
6.0 KiB
PHP
Raw Normal View History

2020-07-07 14:33:11 +02:00
<?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\Services\Invoice;
2020-07-08 04:20:44 +02:00
use App\DataMapper\InvoiceItem;
2020-07-07 14:33:11 +02:00
use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory;
use App\Models\Client;
use App\Models\Invoice;
use App\Models\Payment;
use App\Services\AbstractService;
use App\Services\Client\ClientService;
use App\Services\Payment\PaymentService;
use App\Utils\Traits\GeneratesCounter;
class AutoBillInvoice extends AbstractService
{
private $invoice;
private $client;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
$this->client = $invoice->client;
}
public function run()
{
2020-07-08 04:20:44 +02:00
if(!$this->invoice->isPayable())
return $this->invoice;
2020-07-07 14:33:11 +02:00
2020-07-15 08:08:57 +02:00
$this->invoice = $this->invoice->service()->markSent()->save();
2020-07-08 04:20:44 +02:00
if($this->invoice->balance > 0)
2020-07-13 01:29:44 +02:00
$gateway_token = $this->getGateway($this->invoice->balance);
else
return $this->invoice->service()->markPaid()->save();
2020-07-13 00:28:19 +02:00
2020-07-14 14:50:16 +02:00
if(!$gateway_token || !$gateway_token->gateway->driver($this->client)->token_billing){
2020-07-13 00:28:19 +02:00
return $this->invoice;
2020-07-14 14:50:16 +02:00
}
2020-07-07 14:33:11 +02:00
2020-07-15 07:05:02 +02:00
if($this->invoice->partial > 0){
2020-07-13 05:35:28 +02:00
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->partial);
$amount = $this->invoice->partial + $fee;
}
else{
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance);
$amount = $this->invoice->balance + $fee;
}
2020-07-08 04:20:44 +02:00
2020-07-13 06:46:16 +02:00
/* Make sure we remove any stale fees*/
$this->purgeStaleGatewayFees();
2020-07-13 05:35:28 +02:00
2020-07-13 06:46:16 +02:00
if($fee > 0)
$this->addFeeToInvoice($fee);
2020-07-13 05:35:28 +02:00
2020-07-15 07:05:02 +02:00
$payment = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount, $this->invoice);
2020-07-08 04:20:44 +02:00
2020-07-15 07:05:02 +02:00
if($payment){
2020-07-15 08:08:57 +02:00
$this->invoice = $this->invoice->service()->toggleFeesPaid()->save();
2020-07-15 07:05:02 +02:00
}
else
{
2020-07-15 08:08:57 +02:00
//TODO autobill failed
2020-07-15 07:05:02 +02:00
}
return $this->invoice;
2020-07-07 14:33:11 +02:00
}
2020-07-15 07:05:02 +02:00
/**
* Harvests a client gateway token which passes the
* necessary filters for an $amount
*
* @param float $amount The amount to charge
* @return ClientGatewayToken The client gateway token
*/
2020-07-07 14:33:11 +02:00
private function getGateway($amount)
{
2020-07-13 00:28:19 +02:00
$gateway_tokens = $this->client->gateway_tokens()->orderBy('is_default', 'DESC')->get();
foreach($gateway_tokens as $gateway_token)
{
2020-07-15 07:05:02 +02:00
if($this->validGatewayLimits($gateway_token, $amount)){
2020-07-13 00:28:19 +02:00
return $gateway_token;
2020-07-15 07:05:02 +02:00
}
2020-07-13 00:28:19 +02:00
}
2020-07-07 16:50:51 +02:00
2020-07-08 02:18:13 +02:00
}
2020-07-07 16:50:51 +02:00
2020-07-08 05:07:07 +02:00
/**
* Adds a gateway fee to the invoice
*
* @param float $fee The fee amount.
*/
2020-07-08 04:20:44 +02:00
private function addFeeToInvoice(float $fee)
{
2020-07-08 05:07:07 +02:00
//todo if we increase the invoice balance here, we will also need to adjust UP the client balance and ledger?
$starting_amount = $this->invoice->amount;
2020-07-08 04:20:44 +02:00
$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();
2020-07-08 05:07:07 +02:00
if($starting_amount != $this->invoice->amount && $this->invoice->status_id != Invoice::STATUS_DRAFT){
$this->invoice->client->service()->updateBalance($this->invoice->amount - $starting_amount)->save();
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount - $starting_amount, 'Invoice balance updated after stale gateway fee removed')->save();
}
2020-07-08 08:54:16 +02:00
2020-07-08 05:07:07 +02:00
return $this;
}
/**
* Removes any existing unpaid gateway fees
* due to previous payment failure.
*
* @return $this
*/
private function purgeStaleGatewayFees()
{
$starting_amount = $this->invoice->amount;
$line_items = $this->invoice->line_items;
$new_items = [];
foreach($line_items as $item)
{
if($item->type_id != 3)
$new_items[] = $item;
}
$this->invoice->line_items = $new_items;
$this->invoice->save();
2020-07-15 07:05:02 +02:00
$this->invoice = $this->invoice->calc()->getInvoice();
2020-07-08 05:07:07 +02:00
if($starting_amount != $this->invoice->amount && $this->invoice->status_id != Invoice::STATUS_DRAFT){
$this->invoice->client->service()->updateBalance($this->invoice->amount - $starting_amount)->save();
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->amount - $starting_amount, 'Invoice balance updated after stale gateway fee removed')->save();
}
2020-07-08 04:20:44 +02:00
return $this;
}
2020-07-08 02:18:13 +02:00
/**
* Checks whether a given gateway token is able
* to process the payment after passing through the
* fees and limits check
*
* @param CompanyGateway $cg The CompanyGateway instance
* @param float $amount The amount to be paid
* @return bool
*/
public function validGatewayLimits($cg, $amount) : bool
{
if(isset($cg->fees_and_limits))
$fees_and_limits = $cg->fees_and_limits->{"1"};
else
2020-07-15 07:05:02 +02:00
return true;
2020-07-08 02:18:13 +02:00
if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) {
info("amount {$amount} less than ". $fees_and_limits->min_limit);
$passes = false;
}
else if ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->max_limit){
info("amount {$amount} greater than ". $fees_and_limits->max_limit);
$passes = false;
}
else
$passes = true;
return $passes;
2020-07-07 14:33:11 +02:00
}
2020-07-07 16:50:51 +02:00
2020-07-07 14:33:11 +02:00
}