1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Http/Controllers/ClientPortal/PaymentController.php

152 lines
4.9 KiB
PHP
Raw Normal View History

2019-08-16 07:20:28 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Http\Controllers\ClientPortal;
use App\Filters\PaymentFilters;
use App\Http\Controllers\Controller;
2019-09-13 00:33:48 +02:00
use App\Models\CompanyGateway;
2019-09-25 04:07:33 +02:00
use App\Models\Invoice;
2019-08-16 07:20:28 +02:00
use App\Models\Payment;
2019-09-25 04:07:33 +02:00
use App\Utils\Number;
use App\Utils\Traits\MakesDates;
2019-08-16 07:20:28 +02:00
use App\Utils\Traits\MakesHash;
2019-09-25 04:07:33 +02:00
use Cache;
2019-08-16 07:20:28 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Yajra\DataTables\Facades\DataTables;
use Yajra\DataTables\Html\Builder;
/**
2019-09-25 04:07:33 +02:00
* Class PaymentController
* @package App\Http\Controllers\ClientPortal\PaymentController
2019-08-16 07:20:28 +02:00
*/
class PaymentController extends Controller
{
use MakesHash;
2019-09-25 04:07:33 +02:00
use MakesDates;
2019-08-16 07:20:28 +02:00
/**
* Show the list of Invoices
*
* @param \App\Filters\InvoiceFilters $filters The filters
*
* @return \Illuminate\Http\Response
*/
public function index(PaymentFilters $filters, Builder $builder)
{
//$payments = Payment::filter($filters);
$payments = Payment::with('type', 'client');
2019-09-18 14:43:37 +02:00
2019-08-16 07:20:28 +02:00
if (request()->ajax()) {
return DataTables::of($payments)->addColumn('action', function ($payment) {
return '<a href="/client/payments/'. $payment->hashed_id .'" class="btn btn-xs btn-primary"><i class="glyphicon glyphicon-edit"></i>'.ctrans('texts.view').'</a>';
})->editColumn('type_id', function ($payment) {
return $payment->type->name;
})
->editColumn('status_id', function ($payment) {
return Payment::badgeForStatus($payment->status_id);
2019-08-16 07:20:28 +02:00
})
->editColumn('date', function ($payment) {
//return $payment->date;
return $payment->formatDate($payment->date, $payment->client->date_format());
})
->editColumn('amount', function ($payment) {
return Number::formatMoney($payment->amount, $payment->client);
})
->rawColumns(['action', 'status_id','type_id'])
2019-08-16 07:20:28 +02:00
->make(true);
}
$data['html'] = $builder;
return view('portal.default.payments.index', $data);
}
/**
* Display the specified resource.
*
* @param \App\Models\Invoice $invoice The invoice
*
* @return \Illuminate\Http\Response
*/
public function show(Request $request, Payment $payment)
2019-08-16 07:20:28 +02:00
{
$payment->load('invoices');
2019-08-16 07:20:28 +02:00
$data['payment'] = $payment;
2019-10-03 23:51:54 +02:00
return view('portal.default.payments.show', $data);
2019-08-16 07:20:28 +02:00
}
2019-09-12 08:10:21 +02:00
/**
* Presents the payment screen for a given
* gateway and payment method.
* The request will also contain the amount
2019-09-12 13:46:09 +02:00
* and invoice ids for reference.
*
* @return void
2019-09-12 08:10:21 +02:00
*/
2019-09-25 04:07:33 +02:00
public function process()
2019-09-12 08:10:21 +02:00
{
$invoices = Invoice::whereIn('id', $this->transformKeys(explode(",", request()->input('hashed_ids'))))
2019-09-12 13:46:09 +02:00
->whereClientId(auth()->user()->client->id)
->get();
$amount = $invoices->sum('balance');
2019-09-12 08:10:21 +02:00
$invoices = $invoices->filter(function ($invoice) {
2019-09-25 04:07:33 +02:00
return $invoice->isPayable();
});
if ($invoices->count() == 0) {
return back()->with(['warning' => 'No payable invoices selected']);
}
$invoices->map(function ($invoice) {
2019-09-25 04:07:33 +02:00
$invoice->balance = Number::formatMoney($invoice->balance, $invoice->client);
$invoice->due_date = $this->formatDate($invoice->due_date, $invoice->client->date_format());
return $invoice;
});
2019-09-12 13:46:09 +02:00
2019-09-25 04:07:33 +02:00
$payment_methods = auth()->user()->client->getPaymentMethods($amount);
2019-09-12 08:10:21 +02:00
//boot the payment gateway
2019-09-25 04:07:33 +02:00
$gateway = CompanyGateway::find(request()->input('company_gateway_id'));
$payment_method_id = request()->input('payment_method_id');
2019-09-12 08:10:21 +02:00
//if there is a gateway fee, now is the time to calculate it
2019-09-13 00:33:48 +02:00
//and add it to the invoice
2019-09-12 08:10:21 +02:00
$data = [
2019-09-12 13:46:09 +02:00
'invoices' => $invoices,
'amount' => $amount,
2019-09-13 00:33:48 +02:00
'fee' => $gateway->calcGatewayFee($amount),
2019-09-25 04:07:33 +02:00
'amount_with_fee' => $amount + $gateway->calcGatewayFee($amount),
'token' => auth()->user()->client->gateway_token($gateway->id, $payment_method_id),
'payment_method_id' => $payment_method_id,
'hashed_ids' => explode(",", request()->input('hashed_ids')),
2019-09-12 08:10:21 +02:00
];
2019-09-25 04:07:33 +02:00
return $gateway->driver(auth()->user()->client)->processPaymentView($data);
2019-09-12 08:10:21 +02:00
}
public function response(Request $request)
{
$gateway = CompanyGateway::find($request->input('company_gateway_id'));
return $gateway->driver(auth()->user()->client)->processPaymentResponse($request);
}
2019-08-16 07:20:28 +02:00
}