PaystackBladeDirective   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A paystackPopupCheckout() 0 33 1
A paystackEmbededCheckout() 0 43 2
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
        self::paystackPopupCheckout();
15
        self::paystackEmbededCheckout();
16
    }
17
18
    private static function paystackPopupCheckout()
19
    {
20
        Blade::directive('paystack', function () {
21
            $public_key = Config::get('paystack-lite.public_key');
22
            $cdn = Config::get('paystack-lite.paystack_inline_js');
23
24
            return <<<EOD
25
<script src="$cdn"></script>
26
<script>
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
    var handler = PaystackPop.setup(options);
45
    handler.openIframe();
46
}
47
</script>
48
EOD;
49
        });
50
    }
51
52
    private static function paystackEmbededCheckout()
53
    {
54
        /**
55
             * $expression contains 4 parameters.
56
             * @param $params[0] amount
57
             * @param $params[1] callback
58
             * @param $params[2] email
59
             * @param $params[3] meta
60
             *
61
             */
62
63
        Blade::directive('paystackEmbeded', function ($expression) {
64
            eval("\$params = [$expression];");
65
66
            $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...
67
            $callback = $params[1];
68
            $email = $params[2];
69
            $meta = isset($params[3]) ? $params[3] : '{}';
70
71
            $public_key = Config::get('paystack-lite.public_key');
72
            $cdn = Config::get('paystack-lite.paystack_inline_js');
73
74
            return <<<EOD
75
<div id="paystackEmbedContainer"></div>
76
<script src="$cdn"></script>
77
<script>
78
    var options = {
79
        key: "$public_key",
80
        email: "$email",
81
        amount: "$amount"+'00',
82
        metadata:  $meta,
83
        container: 'paystackEmbedContainer',
84
        callback: function(response){
85
            $callback(response);
86
        }
87
    };
88
89
    var handler = PaystackPop.setup(options);
90
    handler.openIframe();
91
</script>
92
EOD;
93
        });
94
    }
95
}
96