Completed
Push — master ( 9dc5b7...29458f )
by Stephen
10:13
created

PaystackBladeDirective   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 123
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
B paystackPopupCheckout() 0 55 1
B paystackEmbededCheckout() 0 55 4
1
<?php
2
3
namespace Stephenjude\PaystackLite\Blade;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\Facades\Config;
7
8
use function GuzzleHttp\json_encode;
9
10
class PaystackBladeDirective
11
{
12
    // Build your next great package.
13
14
    public static function register()
15
    {
16
        self::paystackPopupCheckout();
17
        self::paystackEmbededCheckout();
18
    }
19
20
    private static function paystackPopupCheckout()
21
    {
22
        Blade::directive('paystack', function () {
23
24
            $fallback_email = "\'" . Config::get('paystack-lite.customer_fallback_email') . "\'";
25
            $public_key = "\'" . Config::get('paystack-lite.public_key') . "\'";
26
            $paystack_js = '<script src="' . Config::get('paystack-lite.paystack_inline_js') . '"></script>';
27
            $script_open = "<script>";
28
            $script_closed = "</script>";
29
30
            return "<?php
31
            echo ' 
32
            $paystack_js
33
34
            $script_open
35
            function payWithPaystack(amount, email, meta, callback, onclose) {
36
               var meta_data = meta ? meta : {};
37
                var options = { 
38
                    key: $public_key,
39
                    email: email,
40
                    amount: amount+\'00\',
41
                    metadata: meta,
42
                    callback: function(response){
43
                        callback(response);
44
                    },
45
                    onClose:function(){
46
                        if(onclose){
47
                            onclose();
48
                        }
49
                    }
50
                };
51
52
                //check if email is valide else fallback
53
                if(!Boolean(options.email)){
54
                    options.email = $fallback_email;
55
                }else{
56
                    if(!validateEmail(email)){
57
                        options.email = $fallback_email;
58
                    }
59
                }
60
61
                var handler = PaystackPop.setup(options);
62
                handler.openIframe();
63
            }
64
65
            function validateEmail(email) {
66
                var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
67
                return re.test(email);
68
            }
69
            
70
            $script_closed
71
            ';
72
            ?>";
73
        });
74
    }
75
76
77
    private static function paystackEmbededCheckout()
78
    {
79
80
81
        Blade::directive('paystackEmbeded', function ($expression) {
82
83
            /**
84
             * @param $expression  contains 4 parameters
85
             * @param $params[0] amount
86
             * @param $params[1] callback
87
             * @param $params[2] email
88
             * @param $params[3] meta
89
             * 
90
             */
91
92
            eval("\$params = [$expression];");
93
94
            $amount = "\'" . $params[0] . "\'";
0 ignored issues
show
Bug introduced by
The variable $params does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
95
            $callback =  $params[1];
96
            $fallback_email = Config::get('paystack-lite.customer_fallback_email');
97
            $email =  isset($params[2]) ? filter_var($params[2], FILTER_VALIDATE_EMAIL) ? $params[2] : $fallback_email  : $fallback_email;
98
            $email = "\'" . $email . "\'";
99
            $meta = isset($params[3]) ? $params[3] : '{}';
100
101
            $public_key = "\'" . Config::get('paystack-lite.public_key') . "\'";
102
            $paystack_js = '<script src="' . Config::get('paystack-lite.paystack_inline_js') . '"></script>';
103
            $script_open = "<script>";
104
            $script_closed = "</script>";
105
106
            return "<?php
107
            
108
            echo ' $paystack_js
109
110
            <div id=\"paystackEmbedContainer\"></div>
111
            $script_open
112
            
113
                var options = { 
114
                    key: $public_key,
115
                    email: $email,
116
                    amount: $amount+\'00\',
117
                    metadata:  $meta,
118
                    container: \'paystackEmbedContainer\',
119
                    callback: function(response){
120
                        $callback(response);
121
                    }
122
                };
123
124
                var handler = PaystackPop.setup(options);
125
                handler.openIframe();
126
127
            $script_closed
128
            ';
129
            ?>";
130
        });
131
    }
132
}
133