Completed
Push — master ( 6abae4...8c7895 )
by Megh
02:56
created

mun::webhook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
class mun extends Controller {
4
5
    public function __construct() {
6
        $this->load_library('auth_lib', 'auth');
7
        $this->auth->force_authentication();
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist on mun. Did you maybe forget to declare it?
Loading history...
8
        $this->load_model('contest_model', 'model');
9
        load_helper('validations');
10
    }
11
12
    private function get_mun_payment_url($nick, $phone, $team_size, $needs_accomodation) {
13
        global $payment_cfg;
14
15
        $proxy = 'http://proxy.iiit.ac.in:8080';
16
        $user_details = $this->auth->get_user_details();
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist on mun. Did you maybe forget to declare it?
Loading history...
17
18
        $name  = $user_details['name'];
19
        $email = $user_details['mail'];
20
21
        $url            = $payment_cfg['mun']['api_url'];
22
        $api_headers    = $payment_cfg['mun']['api_headers'];
23
        $purpose    = $payment_cfg['mun']['purpose'];
24
        $amount     = $payment_cfg['mun']['amount'] * $team_size;
25
        if ($needs_accomodation)
26
            $amount    = $amount + $payment_cfg['mun']['accomodation_amount'] * $team_size;
27
        $redirect_url    = $payment_cfg['mun']['redirect_url'];
28
        $webhook    = $payment_cfg['mun']['webhook'];
0 ignored issues
show
Unused Code introduced by
The assignment to $webhook is dead and can be removed.
Loading history...
29
30
        $ch = curl_init();
31
32
        curl_setopt($ch, CURLOPT_URL, $url);
33
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
34
        curl_setopt($ch, CURLOPT_HEADER, false);
35
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
36
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
37
        curl_setopt($ch, CURLOPT_HTTPHEADER, $api_headers);
38
        $payload = array(
39
            'purpose' => $purpose,
40
            'amount' => $amount,
41
            'phone' => $phone,
42
            'buyer_name' => $name,
43
            'redirect_url' => base_url() . $redirect_url,
44
            //'webhook' => base_url() . $webhook,
45
            'email' => $email,
46
            'allow_repeated_payments' => false
47
        );
48
        curl_setopt($ch, CURLOPT_POST, true);
49
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
50
        $response = curl_exec($ch);
51
        curl_close($ch);
52
53
        $response_array = json_decode($response, true);
54
        return $response_array['payment_request']['longurl'];
55
    }
56
57
    public function register() {
58
        $user_nick = $this->auth->get_user();
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist on mun. Did you maybe forget to declare it?
Loading history...
59
        global $payment_cfg;
60
        $user_details = $this->model->is_registered_for_mun($user_nick);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on mun. Did you maybe forget to declare it?
Loading history...
61
        $errors = [];
62
        if ($_SERVER['REQUEST_METHOD'] === 'POST' && !$user_details) {
63
            required_post_params(['contact_number', 'institute', 'team_size'], $errors);
64 View Code Duplication
            if (!empty($_POST['contact_number']) && !is_valid_phone_number($_POST['contact_number']) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
                $errors['contact_number'] = 'Please enter a valid phone number';
66
            }
67
            if (!ctype_digit($_POST['team_size'])) {
68
                $errors['team_size'] = 'Please enter a number';
69
            }
70
71
            $team_size = intval($_POST['team_size']);
72
            $needs_accomodation = (int) (isset($_POST['needs_accomodation']) && $_POST['needs_accomodation'] == 'yes');
73
74
            if (!$errors) {
75
                $user_details = [
76
                    'nick'                => $user_nick,
77
                    'contact_number'      => $_POST['contact_number'],
78
                    'institute'           => $_POST['institute'],
79
                    'team_size'           => $team_size,
80
                    'needs_accomodation'  => $needs_accomodation,
81
                ];
82
                if ($this->model->register_for_mun($user_details)) {
83
                    $redirect_url = $this->get_mun_payment_url($user_nick, $_POST['contact_number'], $team_size, $needs_accomodation);
84
                    $this->load_library('http_lib', 'http');
85
                    $this->http->redirect( $redirect_url );
0 ignored issues
show
Bug Best Practice introduced by
The property http does not exist on mun. Did you maybe forget to declare it?
Loading history...
86
                } else {
87
                    $errors['common'] = __('Some unexpected error occurred');
88
                }
89
            }
90
        }
91
        $this->load_view('skeleton_template/header', [
92
            'title'             => __('Register').' · '.__('MUN'),
93
            'is_authenticated'  => true,
94
            'user_nick'         => $user_nick,
95
        ]);
96
97
        $this->load_view('contest/mun', [
98
            'user_nick'     => $user_nick,
99
            'user_details'  => $user_details,
100
            'errors'        => $errors
101
        ]);
102
        $this->load_view('skeleton_template/footer');
103
        $this->load_view('skeleton_template/buttons_hide');
104
    }
105
106 View Code Duplication
    public function pay_again() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
        $user_nick = $this->auth->get_user();
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist on mun. Did you maybe forget to declare it?
Loading history...
108
        global $payment_cfg;
109
        $user_details = $this->model->is_registered_for_mun($user_nick);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on mun. Did you maybe forget to declare it?
Loading history...
110
        if ($user_details && $user_details['payment_status'] != 'success') {
111
            $redirect_url = $this->get_mun_payment_url($user_nick, $user_details['contact_number'],
112
                                                       $user_details['team_size'], $user_details['needs_accomodation']);
113
            $this->load_library('http_lib', 'http');
114
            $this->http->redirect($redirect_url);
0 ignored issues
show
Bug Best Practice introduced by
The property http does not exist on mun. Did you maybe forget to declare it?
Loading history...
115
        } else {
116
            $this->load_library('http_lib', 'http');
117
            $this->http->redirect(base_url() . "talks-and-workshops/mun/register/");
118
        }
119
    }
120
121 View Code Duplication
    public function success() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
        $this->load_library('http_lib', 'http');
123
        if (!isset($_GET['payment_id'])) {
124
            $this->http->response_code( 400 );
0 ignored issues
show
Bug Best Practice introduced by
The property http does not exist on mun. Did you maybe forget to declare it?
Loading history...
125
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
126
        }
127
        global $payment_cfg;
128
        $proxy = 'http://proxy.iiit.ac.in:8080';
129
130
        $nick      = $this->auth->get_user();
0 ignored issues
show
Bug Best Practice introduced by
The property auth does not exist on mun. Did you maybe forget to declare it?
Loading history...
131
        $id             = urlencode($_GET['payment_request_id']);
132
        $payment_id     = urlencode($_GET['payment_id']);
133
        $url            = $payment_cfg['mun']['api_url'] . $id . '/' . $payment_id . '/';
134
        $api_headers    = $payment_cfg['mun']['api_headers'];
135
136
        $ch = curl_init();
137
138
        curl_setopt($ch, CURLOPT_URL, $url);
139
        curl_setopt($ch, CURLOPT_PROXY, $proxy);
140
        curl_setopt($ch, CURLOPT_HEADER, false);
141
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
142
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
143
        curl_setopt($ch, CURLOPT_HTTPHEADER, $api_headers);
144
        $response = curl_exec($ch);
145
        curl_close($ch);
146
        $response_array = json_decode($response, true);
147
148
        if (@$response_array['success']) {
149
            $status = $response_array['payment_request']['payment']['status'];
150
            $payment_id = $_GET['payment_id'];
151
            $payment_data = $response;
152
            $this->model->mun_payment_success($payment_id, $nick, $status == 'Credit' ? 'success' : 'failed', $payment_data);
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on mun. Did you maybe forget to declare it?
Loading history...
153
        } else {
154
            if ( is_array( $response_array ) ) {
155
                $this->model->mun_dump_data('unknown', 'callback', json_encode( [ '$_GET' => $_GET, 'response' => $response_array ] ));
156
            } else {
157
                $this->model->mun_dump_data('unknown', 'callback', json_encode($_GET));
158
            }
159
        }
160
        $this->http->redirect(base_url() . "talks-and-workshops/mun/register/");
161
    }
162
163 View Code Duplication
    public function webhook() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
        global $payment_cfg;
165
166
        if (isset($_POST['custom_fields'][$nick_field])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nick_field seems to be never defined.
Loading history...
167
            $nick = $_POST['custom_fields'][$nick_field]['value'];
168
            $this->model->mun_dump_data($nick, 'webhook', json_encode($_POST));
0 ignored issues
show
Bug Best Practice introduced by
The property model does not exist on mun. Did you maybe forget to declare it?
Loading history...
169
        }
170
    }
171
}
172