Fixed Offline Payment method for Customer
This commit is contained in:
parent
994998dc0b
commit
021b853127
@ -2,21 +2,20 @@
|
|||||||
|
|
||||||
namespace App\Events;
|
namespace App\Events;
|
||||||
|
|
||||||
class PaymentGatewayConfirm
|
class InvoicePaid
|
||||||
{
|
{
|
||||||
public $gateway;
|
|
||||||
|
|
||||||
public $invoice;
|
public $invoice;
|
||||||
|
|
||||||
|
public $request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*
|
*
|
||||||
* @param $gateway
|
|
||||||
* @param $invoice
|
* @param $invoice
|
||||||
*/
|
*/
|
||||||
public function __construct($gateway, $invoice)
|
public function __construct($invoice, $request)
|
||||||
{
|
{
|
||||||
$this->gateway = $gateway;
|
|
||||||
$this->invoice = $invoice;
|
$this->invoice = $invoice;
|
||||||
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,13 +3,9 @@
|
|||||||
namespace App\Http\Controllers\Customers;
|
namespace App\Http\Controllers\Customers;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
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\Banking\Account;
|
||||||
use App\Models\Income\Customer;
|
use App\Models\Income\Customer;
|
||||||
use App\Models\Income\Invoice;
|
use App\Models\Income\Invoice;
|
||||||
use App\Models\Income\InvoicePayment;
|
|
||||||
use App\Models\Income\InvoiceHistory;
|
|
||||||
use App\Models\Income\InvoiceStatus;
|
use App\Models\Income\InvoiceStatus;
|
||||||
use App\Models\Setting\Category;
|
use App\Models\Setting\Category;
|
||||||
use App\Models\Setting\Currency;
|
use App\Models\Setting\Currency;
|
||||||
@ -17,9 +13,6 @@ use App\Traits\Currencies;
|
|||||||
use App\Traits\DateTime;
|
use App\Traits\DateTime;
|
||||||
use App\Traits\Uploads;
|
use App\Traits\Uploads;
|
||||||
use Auth;
|
use Auth;
|
||||||
use Date;
|
|
||||||
|
|
||||||
use App\Events\PaymentGatewayConfirm;
|
|
||||||
|
|
||||||
use App\Utilities\Modules;
|
use App\Utilities\Modules;
|
||||||
|
|
||||||
@ -168,84 +161,4 @@ class Invoices extends Controller
|
|||||||
|
|
||||||
return $pdf->download($file_name);
|
return $pdf->download($file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the form for viewing the specified resource.
|
|
||||||
*
|
|
||||||
* @param PaymentRequest $request
|
|
||||||
*
|
|
||||||
* @return Response
|
|
||||||
*/
|
|
||||||
public function payment(Invoice $invoice, PaymentRequest $request)
|
|
||||||
{
|
|
||||||
if (!$invoice) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => trans('You can not pay this invoice. Because it is not yours')
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fire the event to extend the menu
|
|
||||||
$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,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,6 @@ class InvoicePayment extends Request
|
|||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'invoice_id' => 'required|integer',
|
|
||||||
'payment_method' => 'required|string',
|
'payment_method' => 'required|string',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
67
app/Listeners/Incomes/Invoice/Paid.php
Normal file
67
app/Listeners/Incomes/Invoice/Paid.php
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Listeners\Incomes\Invoice;
|
||||||
|
|
||||||
|
use App\Events\InvoicePaid;
|
||||||
|
|
||||||
|
class Paid extends Listener
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the event.
|
||||||
|
*
|
||||||
|
* @param $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle(InvoicePaid $event)
|
||||||
|
{
|
||||||
|
$invoice = $event->invoice;
|
||||||
|
$request = $event->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([
|
||||||
|
'success' => false,
|
||||||
|
'error' => $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,
|
||||||
|
'error' => false,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,9 @@ class EventServiceProvider extends ServiceProvider
|
|||||||
'Illuminate\Auth\Events\Logout' => [
|
'Illuminate\Auth\Events\Logout' => [
|
||||||
'App\Listeners\Auth\Logout',
|
'App\Listeners\Auth\Logout',
|
||||||
],
|
],
|
||||||
|
'App\Events\Events\InvoicePaid' => [
|
||||||
|
'App\Listeners\Incomes\Invoice\Paid',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,6 +28,7 @@ class OfflineFile extends Migration
|
|||||||
$offlinepayment[] = array(
|
$offlinepayment[] = array(
|
||||||
'code' => 'offlinepayment.' . $code[1] . '.' . $code[2],
|
'code' => 'offlinepayment.' . $code[1] . '.' . $code[2],
|
||||||
'name' => $offline_payment['name'],
|
'name' => $offline_payment['name'],
|
||||||
|
'customer' => 0,
|
||||||
'order' => $offline_payment['order'],
|
'order' => $offline_payment['order'],
|
||||||
'description' => $offline_payment['description']
|
'description' => $offline_payment['description']
|
||||||
);
|
);
|
||||||
|
@ -29,6 +29,7 @@ class OfflinePaymentDatabaseSeeder extends Seeder
|
|||||||
$methods[] = array(
|
$methods[] = array(
|
||||||
'code' => 'offlinepayment.cash.1',
|
'code' => 'offlinepayment.cash.1',
|
||||||
'name' => 'Cash',
|
'name' => 'Cash',
|
||||||
|
'customer' => '0',
|
||||||
'order' => '1',
|
'order' => '1',
|
||||||
'description' => null,
|
'description' => null,
|
||||||
);
|
);
|
||||||
@ -36,6 +37,7 @@ class OfflinePaymentDatabaseSeeder extends Seeder
|
|||||||
$methods[] = array(
|
$methods[] = array(
|
||||||
'code' => 'offlinepayment.bank_transfer.2',
|
'code' => 'offlinepayment.bank_transfer.2',
|
||||||
'name' => 'Bank Transfer',
|
'name' => 'Bank Transfer',
|
||||||
|
'customer' => '0',
|
||||||
'order' => '2',
|
'order' => '2',
|
||||||
'description' => null,
|
'description' => null,
|
||||||
);
|
);
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Modules\OfflinePayment\Events\Handlers;
|
|
||||||
|
|
||||||
use App\Events\PaymentGatewayConfirm;
|
|
||||||
|
|
||||||
class OfflinePaymentConfirm
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Handle the event.
|
|
||||||
*
|
|
||||||
* @param PaymentGatewayConfirm $event
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function handle(PaymentGatewayConfirm $event)
|
|
||||||
{
|
|
||||||
if (strpos($event->gateway, 'offlinepayment') === false) {
|
|
||||||
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' => $gateway['code'],
|
|
||||||
'name' => $gateway['name'],
|
|
||||||
'description' => $gateway['description'],
|
|
||||||
'redirect' => false,
|
|
||||||
'html' => $html,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
54
modules/OfflinePayment/Http/Controllers/OfflinePayment.php
Normal file
54
modules/OfflinePayment/Http/Controllers/OfflinePayment.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\OfflinePayment\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Events\InvoicePaid;
|
||||||
|
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
|
||||||
|
use App\Http\Requests\Customer\InvoicePayment as PaymentRequest;
|
||||||
|
use App\Http\Requests\Customer\InvoiceConfirm as ConfirmRequest;
|
||||||
|
|
||||||
|
use App\Models\Income\Invoice;
|
||||||
|
|
||||||
|
class OfflinePayment extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Show the form for editing the specified resource.
|
||||||
|
* @param Invoice
|
||||||
|
* @param PaymentRequest
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public function show(Invoice $invoice, PaymentRequest $request)
|
||||||
|
{
|
||||||
|
$gateway = [];
|
||||||
|
|
||||||
|
$payment_methods = json_decode(setting('offlinepayment.methods'), true);
|
||||||
|
|
||||||
|
foreach ($payment_methods as $payment_method) {
|
||||||
|
if ($payment_method['code'] == $request['payment_method']) {
|
||||||
|
$gateway = $payment_method;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$html = view('offlinepayment::show', compact('gateway', 'invoice'))->render();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'code' => $gateway['code'],
|
||||||
|
'name' => $gateway['name'],
|
||||||
|
'description' => $gateway['description'],
|
||||||
|
'redirect' => false,
|
||||||
|
'html' => $html,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function confirm(Invoice $invoice, ConfirmRequest $request)
|
||||||
|
{
|
||||||
|
$result = event(new InvoicePaid($invoice, $request));
|
||||||
|
|
||||||
|
return response()->json($result);
|
||||||
|
}
|
||||||
|
}
|
30
modules/OfflinePayment/Http/Requests/Show.php
Normal file
30
modules/OfflinePayment/Http/Requests/Show.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Modules\OfflinePayment\Http\Requests;
|
||||||
|
|
||||||
|
use App\Http\Requests\Request;
|
||||||
|
|
||||||
|
class Show 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',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -6,3 +6,8 @@ Route::group(['middleware' => ['web', 'auth', 'language', 'adminmenu', 'permissi
|
|||||||
Route::post('settings/get', 'Settings@get');
|
Route::post('settings/get', 'Settings@get');
|
||||||
Route::post('settings/delete', 'Settings@delete');
|
Route::post('settings/delete', 'Settings@delete');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Route::group(['prefix' => 'customers', 'namespace' => 'Modules\OfflinePayment\Http\Controllers'], function () {
|
||||||
|
Route::get('invoices/{invoice}/offlinepayment', 'OfflinePayment@show');
|
||||||
|
Route::post('invoices/{invoice}/offlinepayment/confirm', 'OfflinePayment@confirm');
|
||||||
|
});
|
||||||
|
@ -11,9 +11,6 @@ use Modules\OfflinePayment\Events\Handlers\OfflinePaymentAdminMenu;
|
|||||||
use App\Events\PaymentGatewayListing;
|
use App\Events\PaymentGatewayListing;
|
||||||
use Modules\OfflinePayment\Events\Handlers\OfflinePaymentGateway;
|
use Modules\OfflinePayment\Events\Handlers\OfflinePaymentGateway;
|
||||||
|
|
||||||
use App\Events\PaymentGatewayConfirm;
|
|
||||||
use Modules\OfflinePayment\Events\Handlers\OfflinePaymentConfirm;
|
|
||||||
|
|
||||||
class OfflinePaymentServiceProvider extends ServiceProvider
|
class OfflinePaymentServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -39,7 +36,6 @@ class OfflinePaymentServiceProvider extends ServiceProvider
|
|||||||
|
|
||||||
$this->app['events']->listen(AdminMenuCreated::class, OfflinePaymentAdminMenu::class);
|
$this->app['events']->listen(AdminMenuCreated::class, OfflinePaymentAdminMenu::class);
|
||||||
$this->app['events']->listen(PaymentGatewayListing::class, OfflinePaymentGateway::class);
|
$this->app['events']->listen(PaymentGatewayListing::class, OfflinePaymentGateway::class);
|
||||||
$this->app['events']->listen(PaymentGatewayConfirm::class, OfflinePaymentConfirm::class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
39
modules/OfflinePayment/Resources/views/show.blade.php
Normal file
39
modules/OfflinePayment/Resources/views/show.blade.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<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 . "/offlinepayment/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['error']) {
|
||||||
|
alert(data['error']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data['success']) {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
//--></script>
|
@ -155,7 +155,6 @@
|
|||||||
@if($invoice->invoice_status_code != 'paid')
|
@if($invoice->invoice_status_code != 'paid')
|
||||||
@if ($payment_methods)
|
@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::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
|
@else
|
||||||
|
|
||||||
@endif
|
@endif
|
||||||
@ -171,9 +170,13 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
$(document).on('change', '#payment-method', function (e) {
|
$(document).on('change', '#payment-method', function (e) {
|
||||||
|
var payment_method = $(this).val();
|
||||||
|
|
||||||
|
gateway = payment_method.split('.');
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '{{ url("customers/invoices/" . $invoice->id . "/payment") }}',
|
url: '{{ url("customers/invoices/" . $invoice->id) }}/' + gateway[0],
|
||||||
type: 'POST',
|
type: 'GET',
|
||||||
dataType: 'JSON',
|
dataType: 'JSON',
|
||||||
data: $('.box-footer input, .box-footer select'),
|
data: $('.box-footer input, .box-footer select'),
|
||||||
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
|
headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user