From 869df15ebc112bdbe6ff6264154fbaaf107d73be Mon Sep 17 00:00:00 2001 From: Mohammad Date: Sun, 5 Mar 2023 15:51:33 +0330 Subject: [PATCH] fixed bug in loading view file `redirectForm.blade.php` (#270) * fixed bug in loading view file `redirectForm.blade.php` && update README.md file and added docs for publishing vendor files * Code compression in method register from service provider * change style code --- README.md | 14 ++++++-- src/Provider/PaymentServiceProvider.php | 43 ++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3c98ce2..2e7bf70 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,18 @@ Via Composer $ composer require shetabit/payment ``` +## Publish Vendor Files + +- **publish configuration files:** +``` bash +php artisan vendor:publish --tag=payment-config +``` + + - **publish views for customization:** +``` bash +php artisan vendor:publish --tag=payment-views +``` + ## Configure If you are using `Laravel 5.5` or higher then you don't need to add the provider and alias. (Skip to b) @@ -115,8 +127,6 @@ a. In your `config/app.php` file add these two lines. ], ``` -b. then run `php artisan vendor:publish` to publish `config/payment.php` file in your config directory. - In the config file you can set the `default driver` to use for all your payments. But you can also change the driver at runtime. Choose what gateway you would like to use in your application. Then make that as default driver so that you don't have to specify that everywhere. But, you can also use multiple gateways in a project. diff --git a/src/Provider/PaymentServiceProvider.php b/src/Provider/PaymentServiceProvider.php index 6b34bd9..d63f2ef 100644 --- a/src/Provider/PaymentServiceProvider.php +++ b/src/Provider/PaymentServiceProvider.php @@ -2,9 +2,12 @@ namespace Shetabit\Payment\Provider; -use Shetabit\Multipay\Payment; -use Illuminate\Support\ServiceProvider; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Contracts\View\Factory; use Illuminate\Support\Facades\Blade; +use Illuminate\Support\ServiceProvider; +use Illuminate\View\View; +use Shetabit\Multipay\Payment; use Shetabit\Multipay\Request; use Shetabit\Payment\Events\InvoicePurchasedEvent; use Shetabit\Payment\Events\InvoiceVerifiedEvent; @@ -18,7 +21,7 @@ class PaymentServiceProvider extends ServiceProvider */ public function boot() { - $this->loadViewsFrom(__DIR__.'/../../resources/views', 'shetabitPayment'); + $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'shetabitPayment'); /** * Configurations that needs to be done by user. @@ -35,9 +38,9 @@ public function boot() */ $this->publishes( [ - __DIR__.'/../../resources/views' => resource_path('views/vendor/shetabitPayment') + __DIR__ . '/../../resources/views' => resource_path('views/vendor/shetabitPayment'), ], - 'views' + 'payment-views' ); } @@ -65,6 +68,9 @@ public function register() // use blade to render redirection form Payment::setRedirectionFormViewRenderer(function ($view, $action, $inputs, $method) { + if ($this->existCustomRedirectFormView()) { + return $this->loadNormalRedirectForm($action, $inputs, $method); + } return Blade::render( str_replace('', '@csrf', file_get_contents($view)), [ @@ -91,4 +97,31 @@ public function registerEvents() event(new InvoiceVerifiedEvent($reciept, $driver, $invoice)); }); } + + /** + * Checks whether the user has customized the view file called `redirectForm.blade.php` or not + * + * @return bool + */ + private function existCustomRedirectFormView() + { + return file_exists(resource_path('views/vendor/shetabitPayment') . '/redirectForm.blade.php'); + } + + /** + * @param $action + * @param $inputs + * @param $method + * @return Application|Factory|View + */ + private function loadNormalRedirectForm($action, $inputs, $method) + { + return view('shetabitPayment::redirectForm')->with( + [ + 'action' => $action, + 'inputs' => $inputs, + 'method' => $method, + ] + ); + } }