Offline payment method for customer

This commit is contained in:
cuneytsenturk 2017-11-18 16:57:55 +03:00
parent 6e7b9d6964
commit 994998dc0b
8 changed files with 180 additions and 21 deletions

View File

@ -4,9 +4,12 @@ namespace App\Http\Controllers\Customers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\InvoicePayment as PaymentRequest;
use App\Http\Requests\Customer\InvoiceConfirm as ConfirmRequest;
use App\Models\Banking\Account;
use App\Models\Income\Customer;
use App\Models\Income\Invoice;
use App\Models\Income\InvoicePayment;
use App\Models\Income\InvoiceHistory;
use App\Models\Income\InvoiceStatus;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
@ -14,7 +17,7 @@ use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Uploads;
use Auth;
use Jenssegers\Date\Date;
use Date;
use App\Events\PaymentGatewayConfirm;
@ -176,13 +179,73 @@ class Invoices extends Controller
public function payment(Invoice $invoice, PaymentRequest $request)
{
if (!$invoice) {
return response()->json(
);
return response()->json([
'error' => trans('You can not pay this invoice. Because it is not yours')
]);
}
// Fire the event to extend the menu
$result = event(new PaymentGatewayConfirm($request['payment_method'], $invoice));
$responses = event(new PaymentGatewayConfirm($request['payment_method'], $invoice));
$result = [
'name' => null,
'code' => null,
'description' => null,
'redirect' => false,
'html' => null,
];
foreach ($responses as $response) {
if ($response) {
$result = $response;
}
}
return response()->json($result);
}
public function confirm(Invoice $invoice, ConfirmRequest $request)
{
$request['invoice_id'] = $invoice->id;
$request['account_id'] = setting('general.default_account');
if (!isset($request['amount'])) {
$request['amount'] = $invoice->amount;
}
$request['currency_code'] = $invoice->currency_code;
$request['currency_rate'] = $invoice->currency_rate;
$request['paid_at'] = Date::parse('now')->format('Y-m-d');
if ($request['amount'] > $invoice->amount) {
$message = trans('messages.error.added', ['type' => trans_choice('general.payment', 1)]);
return response()->json($message);
} elseif ($request['amount'] == $invoice->amount) {
$invoice->invoice_status_code = 'paid';
} else {
$invoice->invoice_status_code = 'partial';
}
$invoice->save();
InvoicePayment::create($request->input());
$request['status_code'] = $invoice->invoice_status_code;
$request['notify'] = 0;
$desc_date = Date::parse($request['paid_at'])->format($this->getCompanyDateFormat());
$desc_amount = money((float) $request['amount'], $request['currency_code'], true)->format();
$request['description'] = $desc_date . ' ' . $desc_amount;
InvoiceHistory::create($request->input());
return response()->json([
'success' => true,
]);
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Requests\Customer;
use App\Http\Requests\Request;
class InvoiceConfirm extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'payment_method' => 'required|string',
];
}
}

View File

@ -18,11 +18,28 @@ class OfflinePaymentConfirm
return [];
}
$invoice = $event->invoice;
$gateway = [];
$payment_methods = json_decode(setting('offlinepayment.methods'), true);
foreach ($payment_methods as $payment_method) {
if ($payment_method['code'] == $event->gateway) {
$gateway = $payment_method;
break;
}
}
$html = view('offlinepayment::confirm', compact('gateway', 'invoice'))->render();
return [
'code' => $event->gateway,
'name' => $event->gateway,
'code' => $gateway['code'],
'name' => $gateway['name'],
'description' => $gateway['description'],
'redirect' => false,
'html' => true,
'html' => $html,
];
}
}

View File

@ -10,4 +10,7 @@ return [
'order' => 'Order',
'payment_gateways' => 'Offline Payment Methods',
'confirm' => 'Confirm',
'loading' => 'Loading',
];

View File

@ -0,0 +1,35 @@
<h2>{{ $gateway['name'] }}</h2>
@if ($gateway['description'])
<div class="well well-sm">
{{ $gateway['description'] }}
</div>
@endif
<div class="buttons">
<div class="pull-right">
<input type="button" value="{{ trans('offlinepayment::offlinepayment.confirm') }}" id="button-confirm" class="btn btn-success" data-loading-text="{{ trans('offlinepayment::offlinepayment.loading') }}" />
</div>
</div>
<script type="text/javascript"><!--
$('#button-confirm').on('click', function() {
$.ajax({
url: '{{ url("customers/invoices/" . $invoice->id . "/confirm") }}',
type: 'POST',
dataType: 'JSON',
data: {payment_method: '{{ $gateway['code'] }}'},
cache: false,
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
beforeSend: function() {
$('#button-confirm').button('loading');
},
complete: function() {
$('#button-confirm').button('reset');
},
success: function(data) {
if (data['success']) {
location.reload();
}
}
});
});
//--></script>

4
public/css/app.css vendored
View File

@ -241,10 +241,10 @@ div.required .control-label:not(span):after, td.required:after {
left: 40%;
}
.confirm #loading {
#confirm #loading {
position: inherit;
top: 40%;
left: 40%;
left: inherit;
}
#payment-modal .form-group.col-md-6 .help-block {

View File

@ -151,15 +151,17 @@
</a>
</div>
<div class="col-sm-2">
@if ($payment_methods)
{!! Form::select('payment_method', $payment_methods, null, array_merge(['id' => 'payment-method', 'class' => 'form-control', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.payment_methods', 1)])])) !!}
{!! Form::hidden('invoice_id', $invoice->id, []) !!}
@else
<div class="col-md-2">
@if($invoice->invoice_status_code != 'paid')
@if ($payment_methods)
{!! Form::select('payment_method', $payment_methods, null, array_merge(['id' => 'payment-method', 'class' => 'form-control', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.payment_methods', 1)])])) !!}
{!! Form::hidden('invoice_id', $invoice->id, []) !!}
@else
@endif
@endif
</div>
<div class="confirm"></div>
<div id="confirm" class="col-md-12"></div>
</div>
</section>
</div>
@ -170,21 +172,29 @@
$(document).ready(function(){
$(document).on('change', '#payment-method', function (e) {
$.ajax({
url: '{{ url("customers/invoices/" . $invoice->id . "payment") }}',
url: '{{ url("customers/invoices/" . $invoice->id . "/payment") }}',
type: 'POST',
dataType: 'JSON',
data: $('.box-footer input, .box-footer select'),
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
beforeSend: function() {
$('.confirm').append('<div id="loading" class="text-center"><i class="fa fa-spinner fa-spin fa-5x checkout-spin"></i></div>');
$('#confirm').append('<div id="loading" class="text-center"><i class="fa fa-spinner fa-spin fa-5x checkout-spin"></i></div>');
},
complete: function() {
$('#loading').remove();
},
success: function(data) {
$("#payment-modal").modal('hide');
if (data['erorr']) {
//location.reload();
}
if (data['redirect']) {
location = data['redirect'];
}
if (data['html']) {
$('#confirm').append(data['html']);
}
},
error: function(data){

View File

@ -123,6 +123,7 @@ Route::group(['middleware' => 'language'], function () {
Route::get('invoices/{invoice}/print', 'Customers\Invoices@printInvoice');
Route::get('invoices/{invoice}/pdf', 'Customers\Invoices@pdfInvoice');
Route::post('invoices/{invoice}/payment', 'Customers\Invoices@payment');
Route::post('invoices/{invoice}/confirm', 'Customers\Invoices@confirm');
Route::resource('invoices', 'Customers\Invoices');
Route::resource('payments', 'Customers\Payments');
Route::resource('transactions', 'Customers\Transactions');
@ -161,4 +162,4 @@ Route::group(['middleware' => 'language'], function () {
});
});
});
});
});