· 7 years ago · Mar 01, 2019, 05:58 PM
1<?php
2/**
3 * Plugin Name: VKL Registrations
4 * Description: Plugin providing registration to events
5 * Version: 0.2
6 * Author: edast
7 * Author URI: http://irir.lt
8 * License: GPL2
9 */
10
11
12require_once(__DIR__ . '/libwebtopay/WebToPay.php');
13require_once(__DIR__ . '/vkl-registration-options.php');
14
15add_action('wp_ajax_lkkf_get_member', 'lkkfAjaxCallback');
16add_action('wp_ajax_nopriv_lkkf_get_member', 'lkkfAjaxCallback');
17
18function lkkfAjaxCallback()
19{
20 if (!isset($_POST['lkkf_csrf']) ||
21 !wp_verify_nonce($_POST['lkkf_csrf'], 'lkkf_get_member')
22 ) {
23 print 'Sorry, your nonce did not verify.';
24 exit;
25 } else {
26 global $wpdb;
27 $name = $_POST['name'];
28 $surname = $_POST['surname'];
29 $members = null;
30
31 if (!empty($name) || !empty($surname)) {
32 $sql = 'SELECT `id`, `Vardas` as name, `Pavarde` as surname, `GimimoMetai` as dob, ' .
33 ' `KYU` as kyu, `Klubas` as club FROM `' .
34 $wpdb->prefix . 'lkk_lkkf_nariai` WHERE `Vardas` = %s AND `Pavarde` = %s ' .
35 "AND `Aktyvus` = 'TAIP' ";
36
37 $members = $wpdb->get_results($wpdb->prepare($sql, $name, $surname));
38 }
39 if ($members) {
40 wp_send_json(array(
41 'nounce' => wp_nonce_field('lkkf_register', 'lkkf_reg_csrf', true, false),
42 'members' => $members));
43 }
44 }
45}
46
47add_action('wp_ajax_lkkf_club_register', 'lkkfRegisterClubCallback');
48add_action('wp_ajax_nopriv_lkkf_club_register', 'lkkfRegisterClubCallback');
49function lkkfRegisterClubCallback()
50{
51 if (!isset($_POST['lkkf_reg_csrf']) ||
52 !wp_verify_nonce($_POST['lkkf_reg_csrf'], 'lkkf_register')
53 ) {
54 print 'Sorry, your nonce did not verify.';
55 exit;
56 } else {
57 $participants = array();
58 foreach ($_POST['member'] as $key => $val) {
59 if (is_array($val) && array_key_exists("weight", $val) && !empty($val["weight"])) {
60 $participants[$key] = $val;
61 }
62 }
63 global $wpdb;
64 $result = $wpdb->insert(
65 $wpdb->prefix . 'vkl_registrations',
66 array(
67 'event_id' => $_POST['event_id'],
68 'member_id' => 0,
69 'status' => 'unpaid',
70 'email' => $_POST['email'],
71 'reg_type' => 'club',
72 'participants' => count($participants),
73 'info' => $_POST['name'] . ' ' . $_POST['surname'] . ', ' . $_POST['club'],
74 'json_data' => json_encode($participants)
75 ),
76 array('%d', '%d', '%s', '%s', '%s', '%d', '%s', '%s')
77 );
78
79 if ($result) {
80 $data = array(
81 'name' => $_POST['name'],
82 'surname' => $_POST['surname'],
83 'email' => $_POST['email'],
84 'event_id' => $_POST['event_id'],
85 'order_id' => $wpdb->insert_id,
86 'participants' => count($participants),
87 //'testing' => true
88 );
89 redirect_to_webtopay($data);
90 }
91 }
92
93}
94
95add_action('wp_ajax_lkkf_register', 'lkkfRegisterCallback');
96add_action('wp_ajax_nopriv_lkkf_register', 'lkkfRegisterCallback');
97function lkkfRegisterCallback()
98{
99 if (!isset($_POST['lkkf_csrf']) ||
100 !wp_verify_nonce($_POST['lkkf_reg_csrf'], 'lkkf_register')
101 ) {
102 print 'Sorry, your nonce did not verify.';
103 exit;
104 } else {
105 // nounce is ok
106
107 global $wpdb;
108 $reg_type = ('bulk_registration' == $_POST['vkl_reg_type']) ? 'bulk' : 'single';
109 $participants = (isset($_POST['participants'])) ? $_POST['participants'] : 1;
110 if ('bulk' == $reg_type) {
111 $info = $_POST['name'] . ' ' . $_POST['surname'] . ', ' . $_POST['club'];
112 } else {
113 $info = NULL;
114 }
115
116 $info_json = array(
117 'name' => $_POST['name'],
118 'surname' => $_POST['surname'],
119 'club' => $_POST['club']
120 );
121 /*
122 if (isset($_POST['comp_kata'])) {
123 $info_json['comp_kata'] = $_POST['comp_kata'];
124 }
125 if (isset($_POST['comp_kumite'])) {
126 $info_json['comp_kumite'] = $_POST['comp_kumite'];
127 }
128 */
129 if (isset($_POST['comp_katakumite']) && $_POST['comp_katakumite'] === 'katakumite') {
130 $info_json['comp_kata'] = 'kata';
131 $info_json['comp_kumite'] = 'kumite';
132 //$info_json['comp_katakumite'] = $_POST['comp_katakumite'];
133 } else if (isset($_POST['comp_katakumite']) && $_POST['comp_katakumite'] === 'kata') {
134 $info_json['comp_kata'] = $_POST['comp_katakumite'];
135 } else if (isset($_POST['comp_katakumite']) && $_POST['comp_katakumite'] === 'kumite') {
136 $info_json['comp_kumite'] = $_POST['comp_katakumite'];
137 }
138
139 if (isset($_POST['bus_needed'])) {
140 $info_json['bus_needed'] = $_POST['bus_needed'];
141 }
142
143 if (!empty($_FILES)) {
144 // These files need to be included as dependencies when on the front end.
145 require_once( ABSPATH . 'wp-admin/includes/image.php' );
146 require_once( ABSPATH . 'wp-admin/includes/file.php' );
147 require_once( ABSPATH . 'wp-admin/includes/media.php' );
148 if (isset($_FILES['agreement_form'])) {
149 $info_json['agreement_form'] = media_handle_upload('agreement_form', $_POST['event_id']);
150 }
151 if (isset($_FILES['health_form'])) {
152 $info_json['health_form'] = media_handle_upload('health_form', $_POST['event_id']);
153 }
154 }
155
156 $result = $wpdb->insert(
157 $wpdb->prefix . 'vkl_registrations',
158 array(
159 'event_id' => $_POST['event_id'],
160 'member_id' => $_POST['member_id'],
161 'status' => 'unpaid',
162 'email' => $_POST['email'],
163 'weight' => $_POST['weight'],
164 'reg_type' => $reg_type,
165 'participants' => $participants,
166 'info' => $info,
167 'json_data' => json_encode($info_json)
168 ),
169 array('%d', '%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s')
170 );
171 if ($result) {
172 $data = array(
173 'name' => $_POST['name'],
174 'surname' => $_POST['surname'],
175 'email' => $_POST['email'],
176 'event_id' => $_POST['event_id'],
177 'order_id' => $wpdb->insert_id,
178 'participants' => $participants
179 );
180 redirect_to_webtopay($data);
181 }
182 }
183}
184
185function get_self_url()
186{
187 $s = substr(strtolower($_SERVER['SERVER_PROTOCOL']), 0,
188 strpos($_SERVER['SERVER_PROTOCOL'], '/'));
189
190 if (!empty($_SERVER["HTTPS"])) {
191 $s .= ($_SERVER["HTTPS"] == "on") ? "s" : "";
192 }
193
194 $s .= '://' . $_SERVER['HTTP_HOST'];
195
196 if (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != '80') {
197 $s .= ':' . $_SERVER['SERVER_PORT'];
198 }
199
200 $s .= dirname($_SERVER['SCRIPT_NAME']);
201
202 return $s;
203}
204
205function redirect_to_webtopay($data)
206{
207 try {
208 $self_url = get_self_url();
209 $testing = (isset($data['testing'])) ? $data['testing'] : $opts['test_payment'];
210 $opts = get_option('vkl_registration_options');
211 $cost = get_post_meta($data['event_id'], 'post-option-blog-cost', true) * 100 * $data['participants'];
212 if ($_POST['comp_katakumite'] === 'katakumite') {
213 $cost = $cost + 500;
214 //echo 'true;'
215 }
216 $request_data = array(
217 'projectid' => $opts['mokejimai_project'],
218 'sign_password' => $opts['mokejimai_key'],//'d4298550f994b2be0d435ca9e31bda07',
219 'orderid' => $data['order_id'],
220 'amount' => $cost,
221 'currency' => 'EUR',
222 'country' => 'LT',
223 'accepturl' => admin_url('admin-ajax.php?action=vkl_webpay_success'),
224 'cancelurl' => admin_url('admin-ajax.php?action=vkl_webpay_cancel'),
225 'callbackurl' => admin_url('admin-ajax.php?action=vkl_webpay_callback'),
226 'test' => $testing,
227 'p_firstname' => $data['name'],
228 'p_lastname' => $data['surname'],
229 'p_email' => $data['email'],
230 'log' => __DIR__ . '/webtopay.log'
231 );
232
233 $request = WebToPay::redirectToPayment($request_data);
234 } catch (WebToPayException $e) {
235 // handle exception
236 }
237
238}
239
240
241add_action('init', 'enqueue_assets');
242function enqueue_assets()
243{
244 wp_enqueue_script('underscore');
245 wp_enqueue_script('lkkf-check', plugins_url('vkl-registration/js/lkkf-check.js'), array('jquery'), '0.2');
246 wp_localize_script('lkkf-check', 'lkkfAjax', array('ajaxurl' => admin_url('admin-ajax.php')));
247}
248
249
250add_action('admin_menu', 'vkl_plugin_menu');
251function vkl_plugin_menu()
252{
253 add_menu_page('VKL Registrations', 'vkl-registrations', 'read', 'vkl-registration/admin/list-events.php');
254 add_submenu_page('vkl-registration/admin/list-events.php', 'club list', 'club list', 'read', 'vkl-registration/admin/list-clubs.php');
255 add_submenu_page('', 'event details', 'event details', 'read', 'vkl-registration/admin/event-details.php');
256 add_submenu_page('', 'event details', 'event details', 'read', 'vkl-registration/admin/event-categories.php');
257 add_submenu_page('', 'event details', 'event details', 'read', 'vkl-registration/admin/run-sync.php');
258}
259
260
261function vkl_create_registration_table()
262{
263 global $wpdb;
264
265 $table_name = $wpdb->prefix . "vkl_registrations";
266 $sql = "CREATE TABLE IF NOT EXISTS `{$table_name}` (
267 `id` int(11) NOT NULL AUTO_INCREMENT,
268 `event_id` int(11) NOT NULL,
269 `member_id` int(11) NOT NULL,
270 `status` varchar(255) NOT NULL,
271 `email` varchar(255) NOT NULL,
272 `reg_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
273 `weight` varchar(255) NOT NULL,
274 PRIMARY KEY (`id`)
275 ) ENGINE=InnoDB DEFAULT CHARSET=utf8
276 ";
277 $wpdb->query($sql);
278}
279
280register_activation_hook(__FILE__, 'vkl_create_registration_table');
281
282
283function vkl_drop_registration_table()
284{
285 global $wpdb;
286 $table_name = $wpdb->prefix . "vkl_registrations";
287 $sql = "DROP TABLE IF EXISTS `$table_name`";
288 $wpdb->query($sql);
289}
290
291register_deactivation_hook(__FILE__, 'vkl_drop_registration_table');
292
293
294add_action('wp_ajax_vkl_webpay_success', 'vkl_webpay_success');
295add_action('wp_ajax_nopriv_vkl_webpay_success', 'vkl_webpay_success');
296function vkl_webpay_success()
297{
298 $opts = get_option('vkl_registration_options');
299 wp_redirect($opts['success_url']);
300// echo 'should redirect to success page';
301}
302
303add_action('wp_ajax_vkl_webpay_cancel', 'vkl_webpay_cancel');
304add_action('wp_ajax_nopriv_vkl_webpay_cancel', 'vkl_webpay_cancel');
305function vkl_webpay_cancel()
306{
307 $opts = get_option('vkl_registration_options');
308 wp_redirect($opts['cancel_url']);
309// echo 'should redirect to cancel page';
310}
311
312
313add_action('wp_ajax_vkl_webpay_callback', 'vkl_webpay_callback');
314add_action('wp_ajax_nopriv_vkl_webpay_callback', 'vkl_webpay_callback');
315function vkl_webpay_callback()
316{
317 try {
318 $opts = get_option('vkl_registration_options');
319 $response = WebToPay::checkResponse($_GET, array(
320 'projectid' => $opts['mokejimai_project'],
321 'sign_password' => $opts['mokejimai_key'],
322 ));
323
324 $orderId = $response['orderid'];
325 $amount = $response['amount'];
326 $currency = $response['currency'];
327
328 if ($response['type'] !== 'macro') {
329 throw new Exception('Only macro payment callbacks are accepted');
330 }
331
332// print_r($response);
333 if ($response['test'] !== '0') {
334// throw new Exception('Testing, real payment was not made');
335 vkl_update_registration_status($orderId, 'test');
336 } else {
337 vkl_update_registration_status($orderId, 'paid');
338 }
339
340 //@todo: patikrinti, ar užsakymas su $orderId dar nepatvirtintas (callback gali būti pakartotas kelis kartus)
341 //@todo: patikrinti, ar užsakymo suma ir valiuta atitinka $amount ir $currency
342 //@todo: patvirtinti užsakymą
343
344// echo 'OK';
345 } catch (Exception $e) {
346 echo get_class($e) . ': ' . $e->getMessage();
347 }
348 die('OK');
349}
350
351function vkl_update_registration_status($reg_id, $status)
352{
353 global $wpdb;
354
355 if ('paid' == $status || 'test' == $status) {
356// $sql = 'SELECT * FROM ' . $wpdb->prefix . ' WHERE id = %d';
357 $row = $wpdb->get_row('SELECT * FROM ' . $wpdb->prefix . 'vkl_registrations WHERE id = ' . $reg_id, ARRAY_A);
358
359// die(print_r($row));
360 if ('club' == $row['reg_type']) {
361
362 $email = $row['email'];
363 $event_id = $row['event_id'];
364
365 $json_data = $row['json_data'];
366 $data = json_decode($json_data, true);
367 foreach ($data as $key => $val) {
368
369 $sql = 'SELECT `id`, `Vardas` as name, `Pavarde` as surname, `GimimoMetai` as dob, ' .
370 ' `KYU` as kyu, `Klubas` as club FROM `' .
371 $wpdb->prefix . 'lkk_lkkf_nariai` WHERE id = %d ';
372 $member = $wpdb->get_row($wpdb->prepare($sql, $key), ARRAY_A);
373
374 $jdata = array();
375 $jdata['name'] = $member['name'];
376 $jdata['surname'] = $member['surname'];
377 $jdata['club'] = $member['club'];
378
379 $wpdb->insert(
380 $wpdb->prefix . 'vkl_registrations', array(
381 'event_id' => $event_id,
382 'member_id' => $key,
383 'status' => $status,
384 'email' => $email,
385 'weight' => $val['weight'],
386 'reg_type' => 'single',
387 'participants' => 1,
388 'info' => '',
389 'json_data' => json_encode($jdata)
390 ), array('%d', '%d', '%s', '%s', '%s', '%s', '%d', '%s', '%s')
391 );
392 }
393 }
394 }
395
396 $wpdb->update(
397 $wpdb->prefix . "vkl_registrations",
398 array('status' => $status),
399 array('id' => $reg_id),
400 array('%s'),
401 array('%d')
402 );
403}
404
405function vkl_add_query_vars_filter($vars)
406{
407 $vars[] = "cns";
408 $vars[] = "event_id";
409 return $vars;
410}
411
412add_filter('query_vars', 'vkl_add_query_vars_filter');
413
414if (is_admin())
415 $my_settings_page = new VKLSettings();