Completed
Push — master ( 76d1d9...e9130a )
by Stephen
02:33
created

PaystackBladeDirective   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 2
dl 0
loc 60
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 55 1
1
<?php
2
3
namespace Stephenjude\PaystackLite\Blade;
4
5
use Illuminate\Support\Facades\Blade;
6
use Illuminate\Support\Facades\Config;
7
8
class PaystackBladeDirective
9
{
10
    // Build your next great package.
11
12
    public static function register()
13
    {
14
        Blade::directive('paystack', function () {
15
16
            $fallback_email = "\'" . Config::get('paystack-lite.customer_fallback_email') . "\'";
17
            $public_key = "\'" . Config::get('paystack-lite.public_key') . "\'";
18
            $paystack_js = '<script src="' . Config::get('paystack-lite.paystack_inline_js') . '"></script>';
19
            $script_open = "<script>";
20
            $script_closed = "</script>";
21
22
            return "<?php
23
            echo ' 
24
            $paystack_js
25
26
            $script_open
27
            function payWithPaystack(amount, email, meta, callback, onclose) {
28
               var meta_data = meta ? meta : {};
29
                var options = { 
30
                    key: $public_key,
31
                    email: email,
32
                    amount: amount+\'00\',
33
                    metadata: meta,
34
                    callback: function(response){
35
                        callback(response);
36
                    },
37
                    onClose:function(){
38
                        if(onclose){
39
                            onclose();
40
                        }
41
                    }
42
                };
43
44
                //check if email is valide else fallback
45
                if(!Boolean(options.email)){
46
                    options.email = $fallback_email;
47
                }else{
48
                    if(!validateEmail(email)){
49
                        options.email = $fallback_email;
50
                    }
51
                }
52
53
                var handler = PaystackPop.setup(options);
54
                handler.openIframe();
55
            }
56
57
            function validateEmail(email) {
58
                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,}))$/;
59
                return re.test(email);
60
            }
61
            
62
            $script_closed
63
            ';
64
            ?>";
65
        });
66
    }
67
}
68