PaymentServiceProvider::loadNormalRedirectForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Shetabit\Payment\Provider;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Support\Facades\Blade;
8
use Illuminate\Support\ServiceProvider;
9
use Illuminate\View\View;
10
use Shetabit\Multipay\Payment;
11
use Shetabit\Multipay\Request;
12
use Shetabit\Payment\Events\InvoicePurchasedEvent;
13
use Shetabit\Payment\Events\InvoiceVerifiedEvent;
14
15
class PaymentServiceProvider extends ServiceProvider
16
{
17
    /**
18
     * Perform post-registration booting of services.
19
     *
20
     * @return void
21
     */
22
    public function boot()
23
    {
24
        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'shetabitPayment');
25
26
        /**
27
         * Configurations that needs to be done by user.
28
         */
29
        $this->publishes(
30
            [
31
                Payment::getDefaultConfigPath() => config_path('payment.php'),
32
            ],
33
            'payment-config'
34
        );
35
36
        /**
37
         * Views that needs to be modified by user.
38
         */
39
        $this->publishes(
40
            [
41
                __DIR__ . '/../../resources/views' => resource_path('views/vendor/shetabitPayment'),
42
            ],
43
            'payment-views'
44
        );
45
    }
46
47
    /**
48
     * Register any package services.
49
     *
50
     * @return void
51
     */
52
    public function register()
53
    {
54
        // Merge default config with user's config
55
        $this->mergeConfigFrom(Payment::getDefaultConfigPath(), 'payment');
56
57
        Request::overwrite('input', function ($key) {
58
            return \request($key);
59
        });
60
61
        /**
62
         * Bind to service container.
63
         */
64
        $this->app->bind('shetabit-payment', function () {
65
            $config = config('payment') ?? [];
66
67
            return new Payment($config);
68
        });
69
70
        $this->registerEvents();
71
72
        // use blade to render redirection form
73
        Payment::setRedirectionFormViewRenderer(function ($view, $action, $inputs, $method) {
74
            if ($this->existCustomRedirectFormView()) {
75
                return $this->loadNormalRedirectForm($action, $inputs, $method);
76
            }
77
            return Blade::render(
78
                str_replace('</form>', '@csrf</form>', file_get_contents($view)),
79
                [
80
                    'action' => $action,
81
                    'inputs' => $inputs,
82
                    'method' => $method,
83
                ]
84
            );
85
        });
86
    }
87
88
    /**
89
     * Register Laravel events.
90
     *
91
     * @return void
92
     */
93
    public function registerEvents()
94
    {
95
        Payment::addPurchaseListener(function ($driver, $invoice) {
96
            event(new InvoicePurchasedEvent($driver, $invoice));
97
        });
98
99
        Payment::addVerifyListener(function ($reciept, $driver, $invoice) {
100
            event(new InvoiceVerifiedEvent($reciept, $driver, $invoice));
101
        });
102
    }
103
104
    /**
105
     * Checks whether the user has customized the view file called `redirectForm.blade.php` or not
106
     *
107
     * @return bool
108
     */
109
    private function existCustomRedirectFormView()
110
    {
111
        return file_exists(resource_path('views/vendor/shetabitPayment') . '/redirectForm.blade.php');
112
    }
113
114
    /**
115
     * @param $action
116
     * @param $inputs
117
     * @param $method
118
     * @return Application|Factory|View
119
     */
120
    private function loadNormalRedirectForm($action, $inputs, $method)
121
    {
122
        return view('shetabitPayment::redirectForm')->with(
123
            [
124
                'action' => $action,
125
                'inputs' => $inputs,
126
                'method' => $method,
127
            ]
128
        );
129
    }
130
}
131