diff --git a/app/Events/PaymentGatewayConfirm.php b/app/Events/InvoicePaid.php similarity index 53% rename from app/Events/PaymentGatewayConfirm.php rename to app/Events/InvoicePaid.php index 1106fa22f..33fab7741 100644 --- a/app/Events/PaymentGatewayConfirm.php +++ b/app/Events/InvoicePaid.php @@ -2,21 +2,20 @@ namespace App\Events; -class PaymentGatewayConfirm +class InvoicePaid { - public $gateway; - public $invoice; + public $request; + /** * Create a new event instance. * - * @param $gateway * @param $invoice */ - public function __construct($gateway, $invoice) + public function __construct($invoice, $request) { - $this->gateway = $gateway; $this->invoice = $invoice; + $this->request = $request; } } diff --git a/app/Http/Controllers/Customers/Invoices.php b/app/Http/Controllers/Customers/Invoices.php index a6c4490c5..756edf00a 100644 --- a/app/Http/Controllers/Customers/Invoices.php +++ b/app/Http/Controllers/Customers/Invoices.php @@ -3,13 +3,9 @@ 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; @@ -17,9 +13,6 @@ use App\Traits\Currencies; use App\Traits\DateTime; use App\Traits\Uploads; use Auth; -use Date; - -use App\Events\PaymentGatewayConfirm; use App\Utilities\Modules; @@ -168,84 +161,4 @@ class Invoices extends Controller 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, - ]); - } } diff --git a/app/Http/Requests/Customer/InvoicePayment.php b/app/Http/Requests/Customer/InvoicePayment.php index e56126d0e..263a75fa0 100644 --- a/app/Http/Requests/Customer/InvoicePayment.php +++ b/app/Http/Requests/Customer/InvoicePayment.php @@ -24,7 +24,6 @@ class InvoicePayment extends Request public function rules() { return [ - 'invoice_id' => 'required|integer', 'payment_method' => 'required|string', ]; } diff --git a/app/Listeners/Incomes/Invoice/Paid.php b/app/Listeners/Incomes/Invoice/Paid.php new file mode 100644 index 000000000..30d38ae40 --- /dev/null +++ b/app/Listeners/Incomes/Invoice/Paid.php @@ -0,0 +1,67 @@ +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, + ]); + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 427406871..7cfcad577 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -24,6 +24,9 @@ class EventServiceProvider extends ServiceProvider 'Illuminate\Auth\Events\Logout' => [ 'App\Listeners\Auth\Logout', ], + 'App\Events\Events\InvoicePaid' => [ + 'App\Listeners\Incomes\Invoice\Paid', + ], ]; /** diff --git a/modules/OfflinePayment/Database/Migrations/2017_09_19_delete_offline_file.php b/modules/OfflinePayment/Database/Migrations/2017_09_19_delete_offline_file.php index d46cbfed9..0afa77ad2 100644 --- a/modules/OfflinePayment/Database/Migrations/2017_09_19_delete_offline_file.php +++ b/modules/OfflinePayment/Database/Migrations/2017_09_19_delete_offline_file.php @@ -28,6 +28,7 @@ class OfflineFile extends Migration $offlinepayment[] = array( 'code' => 'offlinepayment.' . $code[1] . '.' . $code[2], 'name' => $offline_payment['name'], + 'customer' => 0, 'order' => $offline_payment['order'], 'description' => $offline_payment['description'] ); diff --git a/modules/OfflinePayment/Database/Seeders/OfflinePaymentDatabaseSeeder.php b/modules/OfflinePayment/Database/Seeders/OfflinePaymentDatabaseSeeder.php index e308919f9..8cd0986ca 100644 --- a/modules/OfflinePayment/Database/Seeders/OfflinePaymentDatabaseSeeder.php +++ b/modules/OfflinePayment/Database/Seeders/OfflinePaymentDatabaseSeeder.php @@ -29,6 +29,7 @@ class OfflinePaymentDatabaseSeeder extends Seeder $methods[] = array( 'code' => 'offlinepayment.cash.1', 'name' => 'Cash', + 'customer' => '0', 'order' => '1', 'description' => null, ); @@ -36,6 +37,7 @@ class OfflinePaymentDatabaseSeeder extends Seeder $methods[] = array( 'code' => 'offlinepayment.bank_transfer.2', 'name' => 'Bank Transfer', + 'customer' => '0', 'order' => '2', 'description' => null, ); diff --git a/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php b/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php deleted file mode 100644 index db82996cc..000000000 --- a/modules/OfflinePayment/Events/Handlers/OfflinePaymentConfirm.php +++ /dev/null @@ -1,45 +0,0 @@ -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, - ]; - } -} diff --git a/modules/OfflinePayment/Http/Controllers/OfflinePayment.php b/modules/OfflinePayment/Http/Controllers/OfflinePayment.php new file mode 100644 index 000000000..c2f4b326a --- /dev/null +++ b/modules/OfflinePayment/Http/Controllers/OfflinePayment.php @@ -0,0 +1,54 @@ +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); + } +} diff --git a/modules/OfflinePayment/Http/Requests/Show.php b/modules/OfflinePayment/Http/Requests/Show.php new file mode 100644 index 000000000..bfe6ffc18 --- /dev/null +++ b/modules/OfflinePayment/Http/Requests/Show.php @@ -0,0 +1,30 @@ + 'required|string', + ]; + } +} diff --git a/modules/OfflinePayment/Http/routes.php b/modules/OfflinePayment/Http/routes.php index 1c81fc5a8..d5cfba100 100644 --- a/modules/OfflinePayment/Http/routes.php +++ b/modules/OfflinePayment/Http/routes.php @@ -6,3 +6,8 @@ Route::group(['middleware' => ['web', 'auth', 'language', 'adminmenu', 'permissi Route::post('settings/get', 'Settings@get'); 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'); +}); diff --git a/modules/OfflinePayment/Providers/OfflinePaymentServiceProvider.php b/modules/OfflinePayment/Providers/OfflinePaymentServiceProvider.php index 2bd748c61..9ebaac98e 100644 --- a/modules/OfflinePayment/Providers/OfflinePaymentServiceProvider.php +++ b/modules/OfflinePayment/Providers/OfflinePaymentServiceProvider.php @@ -11,9 +11,6 @@ use Modules\OfflinePayment\Events\Handlers\OfflinePaymentAdminMenu; use App\Events\PaymentGatewayListing; use Modules\OfflinePayment\Events\Handlers\OfflinePaymentGateway; -use App\Events\PaymentGatewayConfirm; -use Modules\OfflinePayment\Events\Handlers\OfflinePaymentConfirm; - class OfflinePaymentServiceProvider extends ServiceProvider { /** @@ -39,7 +36,6 @@ class OfflinePaymentServiceProvider extends ServiceProvider $this->app['events']->listen(AdminMenuCreated::class, OfflinePaymentAdminMenu::class); $this->app['events']->listen(PaymentGatewayListing::class, OfflinePaymentGateway::class); - $this->app['events']->listen(PaymentGatewayConfirm::class, OfflinePaymentConfirm::class); } /** diff --git a/modules/OfflinePayment/Resources/views/show.blade.php b/modules/OfflinePayment/Resources/views/show.blade.php new file mode 100644 index 000000000..07a691b1d --- /dev/null +++ b/modules/OfflinePayment/Resources/views/show.blade.php @@ -0,0 +1,39 @@ +