1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Refactor payments for credits

This commit is contained in:
David Bomba 2020-10-15 11:37:16 +11:00
parent 4ca112e61e
commit 0d17b299a0
8 changed files with 30 additions and 25 deletions

View File

@ -115,8 +115,6 @@ class InvoiceController extends Controller
'total' => $total,
];
//REFACTOR entry point for online payments starts here
return $this->render('invoices.payment', $data);
}

View File

@ -68,10 +68,15 @@ class PaymentController extends Controller
*
* @return \Illuminate\Http\RedirectResponse|mixed
*/
public function process()
public function process(Request $request)
{
if($request->input('company_gateway_id') == CompanyGateway::GATEWAY_CREDIT)
return $this->processCreditPayment($request);
$gateway = CompanyGateway::findOrFail(request()->input('company_gateway_id'));
//refactor from here!
/**
* find invoices
*
@ -171,17 +176,16 @@ class PaymentController extends Controller
$first_invoice = $invoices->first();
$credit_totals = $first_invoice->client->service()->getCreditBalance();
$credit_totals = $first_invoice->company->use_credits_payment == 'off' ? 0 : $first_invoice->client->service()->getCreditBalance();
$starting_invoice_amount = $first_invoice->amount;
$first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save();
/**
*
* The best way to determine the exact gateway fee is to not calculate it in isolation (due to rounding)
* but to simply add it as a line item, and then subtract the starting and finishing amounts of
* the invoice.
* Gateway fee is calculated
* by adding it as a line item, and then subtract
* the starting and finishing amounts of the invoice.
*/
$fee_totals = $first_invoice->amount - $starting_invoice_amount;
@ -282,4 +286,9 @@ class PaymentController extends Controller
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
public function processCreditPayment(Request $request)
{
}
}

View File

@ -524,6 +524,14 @@ class Client extends BaseModel implements HasLocalePreference
}
}
if($this->company->use_credit_payment = 'optin' && $this->service()->getCreditBalance() > 0) {
$payment_urls[] = [
'label' => ctrans('texts.apply_credit'),
'company_gateway_id' => CompanyGateway::GATEWAY_CREDIT,
'gateway_type_id' => GatewayType::CREDIT,
];
}
return $payment_urls;
}

View File

@ -66,22 +66,6 @@ class Company extends BaseModel
const ENTITY_RECURRING_TASK = 'task';
const ENTITY_RECURRING_QUOTE = 'recurring_quote';
// const int kModuleRecurringInvoices = 1;
// const int kModuleCredits = 2;
// const int kModuleQuotes = 4;
// const int kModuleTasks = 8;
// const int kModuleExpenses = 16;
// const int kModuleProjects = 32;
// const int kModuleVendors = 64;
// const int kModuleTickets = 128;
// const int kModuleProposals = 256;
// const int kModuleRecurringExpenses = 512;
// const int kModuleRecurringTasks = 1024;
// const int kModuleRecurringQuotes = 2048;
// kModuleInvoices = 4096;
// kModulePayments = 8192;
// 16383
protected $presenter = \App\Models\Presenters\CompanyPresenter::class;
protected $fillable = [

View File

@ -23,6 +23,8 @@ class CompanyGateway extends BaseModel
{
use SoftDeletes;
public const GATEWAY_CREDIT = 10000000;
protected $casts = [
'fees_and_limits' => 'object',
'updated_at' => 'timestamp',

View File

@ -27,6 +27,7 @@ class GatewayType extends StaticModel
const SOFORT = 7;
const APPLE_PAY = 8;
const SEPA = 9;
const CREDIT = 10;
public function gateway()
{

View File

@ -47,6 +47,7 @@ class ClientService
public function getCreditBalance() :float
{
$credits = $this->client->credits
->where('is_deleted', false)
->where('balance', '>', 0)

View File

@ -55,7 +55,9 @@ class AutoBillInvoice extends AbstractService
return $this->invoice->service()->markPaid()->save();
//if the credits cover the payments, we stop here, build the payment with credits and exit early
$this->applyCreditPayment();
if($this->invoice->company->use_credits_payment == 'always' || $this->invoice->company->use_credits_payment == 'optin')
$this->applyCreditPayment();
info("partial = {$this->invoice->partial}");
info("balance = {$this->invoice->balance}");