From 994998dc0bdb1f9269cdadbe50d2cf6171e1a537 Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Sat, 18 Nov 2017 16:57:55 +0300 Subject: [PATCH] Offline payment method for customer --- app/Http/Controllers/Customers/Invoices.php | 73 +++++++++++++++++-- app/Http/Requests/Customer/InvoiceConfirm.php | 30 ++++++++ .../Events/Handlers/OfflinePaymentConfirm.php | 23 +++++- .../Resources/lang/en-GB/offlinepayment.php | 3 + .../Resources/views/confirm.blade.php | 35 +++++++++ public/css/app.css | 4 +- .../views/customers/invoices/show.blade.php | 30 +++++--- routes/web.php | 3 +- 8 files changed, 180 insertions(+), 21 deletions(-) create mode 100644 app/Http/Requests/Customer/InvoiceConfirm.php create mode 100644 modules/OfflinePayment/Resources/views/confirm.blade.php diff --git a/app/Http/Controllers/Customers/Invoices.php b/app/Http/Controllers/Customers/Invoices.php index 014f80262..a6c4490c5 100644 --- a/app/Http/Controllers/Customers/Invoices.php +++ b/app/Http/Controllers/Customers/Invoices.php @@ -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, + ]); + } } diff --git a/app/Http/Requests/Customer/InvoiceConfirm.php b/app/Http/Requests/Customer/InvoiceConfirm.php new file mode 100644 index 000000000..0199f6216 --- /dev/null +++ b/app/Http/Requests/Customer/InvoiceConfirm.php @@ -0,0 +1,30 @@ + 'required|string', + ]; + } +} diff --git a/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php b/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php index 9f2b1637d..db82996cc 100644 --- a/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php +++ b/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php @@ -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, ]; } } diff --git a/modules/OfflinePayment/Resources/lang/en-GB/offlinepayment.php b/modules/OfflinePayment/Resources/lang/en-GB/offlinepayment.php index 9b702ed82..2b8d6a925 100644 --- a/modules/OfflinePayment/Resources/lang/en-GB/offlinepayment.php +++ b/modules/OfflinePayment/Resources/lang/en-GB/offlinepayment.php @@ -10,4 +10,7 @@ return [ 'order' => 'Order', 'payment_gateways' => 'Offline Payment Methods', + 'confirm' => 'Confirm', + 'loading' => 'Loading', + ]; diff --git a/modules/OfflinePayment/Resources/views/confirm.blade.php b/modules/OfflinePayment/Resources/views/confirm.blade.php new file mode 100644 index 000000000..695763dae --- /dev/null +++ b/modules/OfflinePayment/Resources/views/confirm.blade.php @@ -0,0 +1,35 @@ +

{{ $gateway['name'] }}

+ +@if ($gateway['description']) +
+ {{ $gateway['description'] }} +
+@endif +
+
+ +
+
+ diff --git a/public/css/app.css b/public/css/app.css index d1cd31211..26a2151b6 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -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 { diff --git a/resources/views/customers/invoices/show.blade.php b/resources/views/customers/invoices/show.blade.php index ecfb44baf..2cabd6e8c 100644 --- a/resources/views/customers/invoices/show.blade.php +++ b/resources/views/customers/invoices/show.blade.php @@ -151,15 +151,17 @@ -
- @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 +
+ @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
-
+
@@ -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('
'); + $('#confirm').append('
'); }, 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){ diff --git a/routes/web.php b/routes/web.php index 47df97b4a..3a774955c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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 () { }); }); }); -}); \ No newline at end of file +});