Completed
Pull Request — master (#5)
by Stephen
01:09
created

PaystackBladeDirective   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 107
rs 10
c 0
b 0
f 0
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 <<<PAYSTACK
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
            PAYSTACK;
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];
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 <<<PAYSTACK
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);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected EOF
Loading history...
86
                    }
87
                };
88
89
                var handler = PaystackPop.setup(options);
90
                handler.openIframe();
91
            </script>
92
            PAYSTACK;
93
        });
94
    }
95
}
96