1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Working on credit payments

This commit is contained in:
David Bomba 2020-10-15 14:35:35 +11:00
parent c44e8330a5
commit 901f7c4117
3 changed files with 44 additions and 17 deletions

View File

@ -70,10 +70,13 @@ class PaymentController extends Controller
*/
public function process(Request $request)
{
if($request->input('company_gateway_id') == CompanyGateway::GATEWAY_CREDIT)
return $this->processCreditPayment($request);
$is_credit_payment = false;
$token = false;
$gateway = CompanyGateway::findOrFail(request()->input('company_gateway_id'));
if($request->input('company_gateway_id') == CompanyGateway::GATEWAY_CREDIT)
$is_credit_payment = true;
$gateway = CompanyGateway::find($request->input('company_gateway_id'));
//refactor from here!
@ -81,10 +84,9 @@ class PaymentController extends Controller
* find invoices
*
* ['invoice_id' => xxx, 'amount' => 22.00]
*
*/
$payable_invoices = collect(request()->payable_invoices);
$payable_invoices = collect($request->payable_invoices);
$invoices = Invoice::whereIn('id', $this->transformKeys($payable_invoices->pluck('invoice_id')->toArray()))->get();
/* pop non payable invoice from the $payable_invoices array */
@ -164,23 +166,20 @@ class PaymentController extends Controller
});
if ((bool) request()->signature) {
if ((bool) $request->signature) {
$invoices->each(function ($invoice) {
InjectSignature::dispatch($invoice, request()->signature);
InjectSignature::dispatch($invoice, $request->signature);
});
}
$payment_method_id = request()->input('payment_method_id');
$payment_method_id = $request->input('payment_method_id');
$invoice_totals = $payable_invoices->sum('amount');
$first_invoice = $invoices->first();
$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();
if($gateway)
$first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save();
/**
* Gateway fee is calculated
@ -189,6 +188,9 @@ class PaymentController extends Controller
*/
$fee_totals = $first_invoice->amount - $starting_invoice_amount;
if($gateway)
$token = auth()->user()->client->gateway_token($gateway->id, $payment_method_id);
$payment_hash = new PaymentHash;
$payment_hash->hash = Str::random(128);
$payment_hash->data = $payable_invoices->toArray();
@ -207,11 +209,14 @@ class PaymentController extends Controller
'payment_hash' => $payment_hash->hash,
'total' => $totals,
'invoices' => $payable_invoices,
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
'token' => $token,
'payment_method_id' => $payment_method_id,
'amount_with_fee' => $invoice_totals + $fee_totals,
];
if($is_credit_payment)
return $this->processCreditPayment($request, $data);
return $gateway
->driver(auth()->user()->client)
->setPaymentMethod($payment_method_id)
@ -287,8 +292,10 @@ class PaymentController extends Controller
}
public function processCreditPayment(Request $request)
public function processCreditPayment(Request $request, array $data)
{
return render('gateways.credit.index', $data);
}
}

View File

@ -42,7 +42,7 @@
<li class="list-group-item d-flex list-group-item-action justify-content-between align-items-center"><strong>{{ ctrans('texts.credit_amount')}}</strong>
<h3><span class="badge badge-primary badge-pill"><strong>{{ $credit_totals }}</strong></span></h3>
</li>
@endifs
@endif
@if($fee > 0)
<li class="list-group-item d-flex list-group-item-action justify-content-between align-items-center"><strong>{{ ctrans('texts.gateway_fee')}}</strong>
<h3><span class="badge badge-primary badge-pill"><strong>{{ $fee }}</strong></span></h3>

View File

@ -35,7 +35,7 @@ class AutoBillInvoiceTest extends TestCase
{
$this->company->use_credits_payment = 'always';
$this->company->save();
$this->assertEquals($this->client->balance, 10);
$this->assertEquals($this->client->paid_to_date, 0);
$this->assertEquals($this->client->credit_balance, 10);
@ -51,4 +51,24 @@ class AutoBillInvoiceTest extends TestCase
}
public function testAutoBillSetOffFunctionality()
{
$this->company->use_credits_payment = 'off';
$this->company->save();
$this->assertEquals($this->client->balance, 10);
$this->assertEquals($this->client->paid_to_date, 0);
$this->assertEquals($this->client->credit_balance, 10);
$this->invoice->service()->markSent()->autoBill()->save();
$this->assertNotNull($this->invoice->payments());
$this->assertEquals(0, $this->invoice->payments()->sum('payments.amount'));
$this->assertEquals($this->client->balance, 10);
$this->assertEquals($this->client->paid_to_date, 0);
$this->assertEquals($this->client->credit_balance, 10);
}
}