mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 21:22:58 +01:00
Add gateway fees to invoice
This commit is contained in:
parent
4b81e209c4
commit
6718915a9c
@ -50,7 +50,7 @@ class AutoBillInvoice extends AbstractService
|
||||
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance);
|
||||
|
||||
if($fee > 0)
|
||||
$this->addFeeToInvoice($fee);
|
||||
$this->purgeStaleGatewayFees()->addFeeToInvoice($fee);
|
||||
|
||||
$response = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount);
|
||||
|
||||
@ -70,8 +70,17 @@ class AutoBillInvoice extends AbstractService
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a gateway fee to the invoice
|
||||
*
|
||||
* @param float $fee The fee amount.
|
||||
*/
|
||||
private function addFeeToInvoice(float $fee)
|
||||
{
|
||||
|
||||
//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;
|
||||
|
||||
$item = new InvoiceItem;
|
||||
$item->quantity = 1;
|
||||
$item->cost = $fee;
|
||||
@ -86,6 +95,46 @@ class AutoBillInvoice extends AbstractService
|
||||
|
||||
$this->invoice = $this->invoice->calc()->getInvoice()->save();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
$this->invoice = $this->invoice->calc()->getInvoice()->save();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -48,12 +48,8 @@ class MarkSent extends AbstractService
|
||||
->setDueDate()
|
||||
->save();
|
||||
|
||||
info("marking invoice sent currently client balance = {$this->client->balance}");
|
||||
|
||||
$this->client->service()->updateBalance($this->invoice->balance)->save();
|
||||
|
||||
info("after marking invoice sent currently client balance = {$this->client->balance}");
|
||||
|
||||
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance);
|
||||
|
||||
return $this->invoice->fresh();
|
||||
|
Loading…
Reference in New Issue
Block a user