· 7 years ago · Jul 11, 2018, 12:56 PM
1<?php
2/**
3 * @package Loc_payment Checkout Integration
4 * @category Payment Gateway for Booking Calendar
5 * @author wpdevelop
6 * @version 1.0
7 * @web-site https://wpbookingcalendar.com/
8 * @email info@wpbookingcalendar.com
9 *
10 * @modified 2017-10-08
11 * Integration based on Loc_payment PHP library 5.2.3 2017-09-27
12 * Based on guide: https://loc_payment.com/docs/checkout/php and https://loc_payment.com/docs/checkout
13 *
14 * Initially provided customization by: ingoratsdorf
15 */
16
17//FixIn:8.0.1.10
18
19/*
20 * Testing:
21 * Use test card number—4242 4242 4242 4242,
22 * any future month and year for the expiration,
23 * any three-digit number for the CVC, and any random ZIP code.
24
25 $loc_payment = array(
26 "secret_key" => "sk_test_BQokikJOvBiI2HlWgH4olfQ2", // booking_loc_payment_loc_payment_secret_test_key = sk_test_BQokikJOvBiI2HlWgH4olfQ2
27 "publishable_key" => "pk_test_6pRNASCoBOKtIshFeQd4XMUh" // booking_loc_payment_loc_payment_public_test_key = pk_test_6pRNASCoBOKtIshFeQd4XMUh
28 );
29 */
30
31
32if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
33
34if ( ! defined( 'WPBC_LOC_PAYMENT_GATEWAY_ID' ) ) define( 'WPBC_LOC_PAYMENT_GATEWAY_ID', 'loc_payment' );
35
36
37
38
39/** API for Payment Gateway */
40class WPBC_Gateway_API_LOC_PAYMENT extends WPBC_Gateway_API {
41
42
43 public function get_payment_form( $output, $params, $gateway_id = '' ) {
44
45
46
47 // Check if currently is showing this Gateway
48 if (
49 ( ( ! empty( $gateway_id ) ) && ( $gateway_id !== $this->get_id() ) ) // Does we need to show this Gateway
50 || ( ! $this->is_gateway_on() ) // Payment Gateway does NOT active
51 ) return $output ;
52
53
54 ////////////////////////////////////////////////////////////////////////
55 // Payment Options /////////////////////////////////////////////////////
56 $payment_options = array();
57
58
59 $payment_options['publishable_key'] = get_bk_option( 'booking_loc_payment_publishable_key' );
60
61
62 $loc_payment_charge_url = untrailingslashit( plugins_url( '', __FILE__ ) )
63 . '/loc_payment-charge.php?'
64 . 'payed_booking=' . $params['booking_id']
65 . '&wp_nonce=' . $params['__nonce']
66 . '&pay_sys=' . $this->get_id();
67
68 ////////////////////////////////////////////////////////////////////////
69 // Payment Form
70 ////////////////////////////////////////////////////////////////////////
71
72 ob_start();
73 $loc_payment_description = get_bk_option( 'booking_loc_payment_description' );
74 $loc_payment_description = apply_bk_filter('wpdev_check_for_active_language', $loc_payment_description );
75 $loc_payment_description = wpbc_replace_booking_shortcodes( $loc_payment_description, $params );
76
77 ?>
78
79
80
81
82
83 <div style="width:100%;clear:both;margin-top:20px;"></div><?php
84 ?><div class="loc_payment_div wpbc-replace-ajax wpbc-payment-form" style="text-align:left;clear:both;">
85 <?php
86
87 /**
88 * We need to open payment form in separate window, if this booking was made togather with other
89 * in booking form was used several calendars from different booking resources.
90 * So we are having several payment forms for each booked resource.
91 * System transfer this parameter $params['payment_form_target'] = ' target="_blank" ';
92 * otherwise $params['payment_form_target'] = '';
93 */
94 echo $loc_payment_description;
95 $x_amount = $params['cost_in_gateway'] * 100;
96
97
98 ?>
99
100
101
102
103
104
105 <form id="payment-form" action=<?php echo $loc_payment_charge_url; ?> <?php echo $params['payment_form_target']; ?>
106 method="POST" name="loc_paymentPayForm"
107 style="text-align:left;" class="booking_loc_paymentPayForm">
108 <div class="main">
109 <div class="visitor_info"><h2>Payment Information</h2>
110 <div class="inside">
111 <ol class="left">
112
113
114 <div class="form-row">
115
116
117 <label>
118 <li class="smth">Card Number</li>
119 <li class="smth"><input type="text" size="20" maxlength="16" name="txtCardNumber" autocomplete="off" data-vp="cardnumber"/></li>
120 </label>
121
122 </ol>
123 <ol class="right">
124 <label>
125 <li class="smth">Valid Through</li>
126 <li class="smth">
127 <select name="txtMonth" data-vp="month">
128 <option selected value=""></option>
129 <option value="01">01</option>
130 <option value="02">02</option>
131 <option value="03">03</option>
132 <option value="04">04</option>
133 <option value="05">05</option>
134 <option value="06">06</option>
135 <option value="07">07</option>
136 <option value="08">08</option>
137 <option value="09">09</option>
138 <option value="10">10</option>
139 <option value="11">11</option>
140 <option value="12">12</option>
141 </select> /
142 <select name=â€txtYear†data-vp="year">
143 <option selected value=""></option>
144 <option value="2018">2018</option>
145 <option value="2019">2019</option>
146 <option value="2020">2020</option>
147 <option value="2021">2021</option>
148 <option value="2022">2022</option>
149 <option value="2023">2023</option>
150 <option value="2024">2024</option>
151 <option value="2024">2025</option>
152 <option value="2024">2026</option>
153 <option value="2024">2027</option>
154 <option value="2024">2028</option>
155 </select>
156 </li>
157 </label>
158 </ol>
159
160 <ol class="left">
161 <label>
162 <li class="smth">Cardholder Name</li>
163 <li class="smth"><input type="text" size="20" name="txtCardHolder" autocomplete="off" data-vp="cardholder"/></li>
164 </label>
165 </ol
166 <ol class="right">
167 <label>
168 <li class="smth">Card Verification Code (CVV2)</li>
169 <li class="smth"><input type="text" maxlength="4" pattern="[0-9]*" name="txtCVV" inputmode="numeric" size="4" autocomplete="off" data-vp="cvv"/></li>
170 <li class="smth"><a class="modal-link" href="cvv" style="cursor:pointer; color:dodgerblue; text-decoration: underline;"><small>What is this?</small></a></li>
171 </label>
172 </ol>
173 </div>
174 </div>
175 </div>
176
177
178
179
180 <select id="drpInstallments" name="drpInstallments" style="display:none"></select>
181 <?php wp_nonce_field( 'wpbc_stripe', 'wpbc_stripe_payment' ); ?>
182 <input type="hidden" name="customer_reservation" value="Room: <?php echo $params['bookingtype']; ?>. Dates: <?php echo $params['dates']; ?> " />
183 <input type="hidden" name="customer_phone" value="<?php echo $params['phone']; ?>" />
184 <input type="hidden" name="customer_email" value="<?php echo $params['email']; ?>" />
185 <input type="hidden" name="customer_name" value="<?php echo $params['name']; ?> <?php echo $params['secondname']; ?>" />
186 <input type="hidden" name="x_amount" value="<?php echo $x_amount; ?>" />
187 <input type="hidden" name="hidToken" id="hidToken" /> <!--Hidden Field to hold the Generated Token-->
188
189
190 </div>
191 <div class="main" style="margin-top:6px;">
192 <div class="visitor_info">
193 <h2>Book Now</h2>
194 <div class="inside">
195 <ol class="smth">
196
197 <li><input type="button" value="Submit" onclick="VivaPayments.cards.requestToken();" /></li>
198 </ol>
199 </div>
200 </div>
201 </div>
202
203 </form>
204
205 </div>
206<script type="text/javascript" src="https://demo.vivapayments.com/web/checkout/js"></script>
207 <script type="text/javascript">
208 $(document).ready(function (){
209 VivaPayments.cards.setup({
210 publicKey: 'zz0Mt8J4MKe9ywwylJfcpJ0V837sm1aP8l4pHbvCdi4=',
211 baseURL: 'https://demo.vivapayments.com',
212 cardTokenHandler: function (response) {
213 if (!response.Error) {
214 $('#hidToken').val(response.Token);
215 $('#payment-form').submit();
216 return false;
217 }
218 else{
219 console.log(response);
220 alert(response.Error);
221 return false;
222 }
223 },
224 installmentsHandler: function (response) {
225 if (!response.Error) {
226 if (response.MaxInstallments == 0)
227 return;
228 $('#drpInstallments').show();
229 for(i=1; i<=response.MaxInstallments; i++){
230 $('#drpInstallments').append($("<option>").val(i).text(i));
231 }
232 }
233 else
234 alert(response.Error);
235 }
236 });
237 });
238</script>
239 <?php
240
241 $payment_form = ob_get_clean();
242
243 return $output . $payment_form;
244
245 }
246
247
248 /** Define settings Fields */
249 public function init_settings_fields() {
250
251 $this->fields = array();
252
253 // On | Off
254 $this->fields['is_active'] = array(
255 'type' => 'checkbox'
256 , 'default' => 'On'
257 , 'title' => __( 'Enable / Disable', 'booking' )
258 , 'label' => __( 'Enable this payment gateway', 'booking')
259 , 'description' => ''
260 , 'group' => 'general'
261
262 );
263
264
265 // Public Key
266 $this->fields['publishable_key'] = array(
267 'type' => 'text'
268 , 'default' => ( wpbc_is_this_demo() ? 'pk_test_6pRNASCoBOKtIshFeQd4XMUh' : '' )
269 //, 'placeholder' => ''
270 , 'title' => __('Publishable key', 'booking')
271 , 'description' => __('Required', 'booking') . '.<br/>'
272 . sprintf( __('This parameter have to assigned to you by %s' ,'booking'), 'Loc_payment' )
273 . ( ( wpbc_is_this_demo() ) ? wpbc_get_warning_text_in_demo_mode() : '' )
274 , 'description_tag' => 'span'
275 , 'css' => ''//'width:100%'
276 , 'group' => 'general'
277 , 'tr_class' => 'wpbc_sub_settings_grayed wpbc_sub_settings_mode_live'
278 //, 'validate_as' => array( 'required' )
279 );
280 $this->fields['description'] = array(
281 'type' => 'wp_textarea'
282 , 'default' => ''
283 , 'placeholder' => ''
284 , 'title' => __('Description', 'booking')
285 , 'description' => __( 'Payment method description that the customer will see on your payment page.' ,'booking')
286 , 'description_tag' => ''
287
288 , 'group' => 'general'
289 , 'tr_class' => ''
290 , 'rows' => 30
291 , 'show_in_2_cols' => true
292 , 'css' => 'width:100%'
293 // Default options:
294 , 'teeny' => true
295 , 'show_visual_tabs' => true
296 , 'default_editor' => 'tinymce' // 'tinymce' | 'html' // 'html' is used for the "Text" editor tab.
297 , 'drag_drop_upload' => false
298 );
299
300
301 }
302 public function get_gateway_info() {
303
304 $gateway_info = array(
305 'id' => $this->get_id()
306 , 'title' => 'Loc_payment'
307 , 'currency' => get_bk_option( 'booking_' . $this->get_id() . '_' . 'curency' )
308 , 'enabled' => $this->is_gateway_on()
309 );
310 return $gateway_info;
311 }
312
313
314 /**
315 * Get payment Statuses of gateway
316 *
317 * @return array
318 */
319 public function get_payment_status_array() {
320
321 return array(
322 'ok' => array( 'Loc_payment:OK' )
323 , 'pending' => array( 'Loc_payment:Pending' )
324 , 'unknown' => array( 'Loc_payment:Unknown' )
325 , 'error' => array( 'Loc_payment:Failed',
326 'Loc_payment:REJECTED',
327 'Loc_payment:NOTAUTHED',
328 'Loc_payment:MALFORMED',
329 'Loc_payment:INVALID',
330 'Loc_payment:ABORT',
331 'Loc_payment:ERROR' )
332 );
333 }
334
335
336}
337
338// </editor-fold>
339
340
341
342// <editor-fold defaultstate="collapsed" desc=" Settings Page " >
343
344/** Settings Page */
345class WPBC_Settings_Page_Gateway_LOC_PAYMENT extends WPBC_Page_Structure {
346
347 public $gateway_api = false;
348
349
350 /**
351 * Define interface for Gateway API
352 *
353 * @param string $selected_email_name - name of Email template
354 * @param array $init_fields_values - array of init form fields data - this array can ovveride "default" fields and loaded data.
355 * @return object Email API
356 */
357 public function get_api( $init_fields_values = array() ){
358
359 if ( $this->gateway_api === false ) {
360 $this->gateway_api = new WPBC_Gateway_API_LOC_PAYMENT( WPBC_LOC_PAYMENT_GATEWAY_ID , $init_fields_values );
361 }
362
363 return $this->gateway_api;
364 }
365
366
367 public function in_page() { // P a g e t a g
368 return 'wpbc-settings';
369 }
370
371
372 public function tabs() { // T a b s A r r a y
373
374 $tabs = array();
375
376 $subtabs = array();
377
378 // Checkbox Icon, for showing in toolbar panel does this payment system active
379 $is_data_exist = get_bk_option( 'booking_'. WPBC_LOC_PAYMENT_GATEWAY_ID .'_is_active' );
380 if ( ( ! empty( $is_data_exist ) ) && ( $is_data_exist == 'On' ) )
381 $icon = '<i class="menu_icon icon-1x glyphicon glyphicon-check"></i> ';
382 else
383 $icon = '<i class="menu_icon icon-1x glyphicon glyphicon-unchecked"></i> ';
384
385
386 $subtabs[ WPBC_LOC_PAYMENT_GATEWAY_ID ] = array(
387 'type' => 'subtab' // Required| Possible values: 'subtab' | 'separator' | 'button' | 'goto-link' | 'html'
388 , 'title' => $icon . 'Loc_payment' // Title of TAB
389 , 'page_title' => sprintf( __('%s Settings', 'booking'), 'Loc_payment' ) // Title of Page
390 , 'hint' => sprintf( __('Integration of %s payment system' ,'booking' ), 'Loc_payment' ) // Hint
391 , 'link' => '' // link
392 , 'position' => '' // 'left' || 'right' || ''
393 , 'css_classes' => '' // CSS class(es)
394 //, 'icon' => 'http://.../icon.png' // Icon - link to the real PNG img
395 //, 'font_icon' => 'glyphicon glyphicon-envelope' // CSS definition of Font Icon
396 , 'default' => false // Is this sub tab activated by default or not: true || false.
397 , 'disabled' => false // Is this sub tab deactivated: true || false.
398 , 'checkbox' => false // or definition array for specific checkbox: array( 'checked' => true, 'name' => 'feature1_active_status' ) //, 'checkbox' => array( 'checked' => $is_checked, 'name' => 'enabled_active_status' )
399 , 'content' => 'content' // Function to load as conten of this TAB
400 );
401
402 $tabs[ 'payment' ]['subtabs'] = $subtabs;
403
404 return $tabs;
405 }
406
407
408 /** Show Content of Settings page */
409 public function content() {
410
411
412 $this->css();
413
414 ////////////////////////////////////////////////////////////////////////
415 // Checking
416 ////////////////////////////////////////////////////////////////////////
417
418 do_action( 'wpbc_hook_settings_page_header', 'gateway_settings'); // Define Notices Section and show some static messages, if needed
419 do_action( 'wpbc_hook_settings_page_header', 'gateway_settings_' . WPBC_LOC_PAYMENT_GATEWAY_ID );
420
421 if ( ! wpbc_is_mu_user_can_be_here('activated_user') ) return false; // Check if MU user activated, otherwise show Warning message.
422
423 // if ( ! wpbc_is_mu_user_can_be_here('only_super_admin') ) return false; // User is not Super admin, so exit. Basically its was already checked at the bottom of the PHP file, just in case.
424
425
426 ////////////////////////////////////////////////////////////////////////
427 // Load Data
428 ////////////////////////////////////////////////////////////////////////
429
430 // $this->check_compatibility_with_older_7_ver();
431
432 $init_fields_values = array();
433
434 $this->get_api( $init_fields_values );
435
436
437 ////////////////////////////////////////////////////////////////////////
438 // S u b m i t Main Form
439 ////////////////////////////////////////////////////////////////////////
440
441 $submit_form_name = 'wpbc_gateway_' . WPBC_LOC_PAYMENT_GATEWAY_ID; // Define form name
442
443 $this->get_api()->validated_form_id = $submit_form_name; // Define ID of Form for ability to validate fields (like required field) before submit.
444
445 if ( isset( $_POST['is_form_sbmitted_'. $submit_form_name ] ) ) {
446
447 // Nonce checking {Return false if invalid, 1 if generated between, 0-12 hours ago, 2 if generated between 12-24 hours ago. }
448 $nonce_gen_time = check_admin_referer( 'wpbc_settings_page_' . $submit_form_name ); // Its stop show anything on submiting, if its not refear to the original page
449
450 // Save Changes
451 $this->update();
452 }
453
454
455 ////////////////////////////////////////////////////////////////////////
456 // JavaScript: Tooltips, Popover, Datepick (js & css)
457 ////////////////////////////////////////////////////////////////////////
458
459 echo '<span class="wpdevelop">';
460
461 wpbc_js_for_bookings_page();
462
463 echo '</span>';
464
465
466 ////////////////////////////////////////////////////////////////////////
467 // Content
468 ////////////////////////////////////////////////////////////////////////
469 ?>
470 <div class="clear" style="margin-bottom:10px;"></div>
471
472 <span class="metabox-holder">
473 <form name="<?php echo $submit_form_name; ?>" id="<?php echo $submit_form_name; ?>" action="" method="post" autocomplete="off">
474 <?php
475 // N o n c e field, and key for checking S u b m i t
476 wp_nonce_field( 'wpbc_settings_page_' . $submit_form_name );
477 ?><input type="hidden" name="is_form_sbmitted_<?php echo $submit_form_name; ?>" id="is_form_sbmitted_<?php echo $submit_form_name; ?>" value="1" />
478
479
480 <div class="clear" style="height:10px;"></div>
481
482 <div class="metabox-holder">
483
484 <div class="wpbc_settings_row wpbc_settings_row_left_NO" >
485 <?php
486 wpbc_open_meta_box_section( $submit_form_name . 'general', 'Loc_payment' );
487 $this->get_api()->show( 'general' );
488 wpbc_close_meta_box_section();
489 ?>
490 </div>
491 <div class="clear"></div>
492
493
494
495 </div>
496
497 <input type="submit" value="<?php _e('Save Changes', 'booking'); ?>" class="button button-primary" />
498 </form>
499 </span>
500 <?php
501
502
503 }
504
505
506 /** Update Email template to DB */
507 public function update() {
508
509 // Get Validated Email fields
510 $validated_fields = $this->get_api()->validate_post();
511
512 $validated_fields = apply_filters( 'wpbc_gateway_loc_payment_validate_fields_before_saving', $validated_fields ); //Hook for validated fields.
513
514 $this->get_api()->save_to_db( $validated_fields );
515
516 wpbc_show_message ( __('Settings saved.', 'booking'), 5 ); // Show Save message
517 }
518
519
520 // <editor-fold defaultstate="collapsed" desc=" CSS & JS " >
521
522 /** CSS for this page */
523 private function css() {
524 ?>
525 <style type="text/css">
526 .wpbc-help-message {
527 border:none;
528 margin:0 !important;
529 padding:0 !important;
530 }
531 @media (max-width: 399px) {
532 }
533 </style>
534 <?php
535 }
536
537
538
539
540}
541add_action('wpbc_menu_created', array( new WPBC_Settings_Page_Gateway_LOC_PAYMENT() , '__construct') ); // Executed after creation of Menu
542
543
544
545/**
546 * Override VALIDATED fields BEFORE saving to DB
547 * Description:
548 * Check "Return URLs" and "LOC_PAYMENT Email"m, etc...
549 *
550 * @param array $validated_fields
551 */
552function wpbc_gateway_loc_payment_validate_fields_before_saving__all( $validated_fields ) {
553
554 $validated_fields['order_successful'] = wpbc_make_link_relative( $validated_fields['order_successful'] );
555 $validated_fields['order_failed'] = wpbc_make_link_relative( $validated_fields['order_failed'] );
556
557
558
559 return $validated_fields;
560}
561add_filter( 'wpbc_gateway_loc_payment_validate_fields_before_saving', 'wpbc_gateway_loc_payment_validate_fields_before_saving__all', 10, 1 ); // Hook for validated fields.
562
563// </editor-fold>
564
565
566
567// <editor-fold defaultstate="collapsed" desc=" Activate | Deactivate " >
568
569////////////////////////////////////////////////////////////////////////////////
570// Activate | Deactivate
571////////////////////////////////////////////////////////////////////////////////
572
573/** A c t i v a t e */
574function wpbc_booking_activate_LOC_PAYMENT() {
575
576 $op_prefix = 'booking_' . WPBC_LOC_PAYMENT_GATEWAY_ID . '_';
577
578 add_bk_option( $op_prefix . 'is_active', ( wpbc_is_this_demo() ? 'On' : 'Off' ) );
579 add_bk_option( $op_prefix . 'account_mode', 'test' );
580 add_bk_option( $op_prefix . 'publishable_key', ( wpbc_is_this_demo() ? 'pk_test_6pRNASCoBOKtIshFeQd4XMUh' : '' ) );
581
582
583}
584add_bk_action( 'wpbc_other_versions_activation', 'wpbc_booking_activate_LOC_PAYMENT' );
585
586
587/** D e a c t i v a t e */
588function wpbc_booking_deactivate_LOC_PAYMENT() {
589
590 $op_prefix = 'booking_' . WPBC_LOC_PAYMENT_GATEWAY_ID . '_';
591
592 delete_bk_option( $op_prefix . 'is_active' );
593 delete_bk_option( $op_prefix . 'account_mode' );
594 delete_bk_option( $op_prefix . 'publishable_key' );
595
596
597}
598add_bk_action( 'wpbc_other_versions_deactivation', 'wpbc_booking_deactivate_LOC_PAYMENT' );
599
600
601
602// <script type="text/javascript" src="https://demo.vivapayments.com/web/checkout/js"></script>
603
604add_action('wp_enqueue_scripts', 'test');
605function test() {
606
607 wp_enqueue_script('jquery_now', '//code.jquery.com/jquery-1.11.2.min.js', array('jquery'), null, false);
608 //wp_enqueue_script('viva_payments', '//demo.vivapayments.com/web/checkout/js', array('jquery'), null, false);
609//wp_enqueue_script('viva_payments2', '/wp-content/plugins/booking.bl.multisite.8.3/inc/gateways/loc_payment/scripts.js', array(), null, true);
610
611 //wp_dequeue_script('jquery_now');
612 }
613
614
615// Hook for getting gateway payment form to show it after booking process, or for "payment request" after clicking on link in email.
616// Note, here we generate new Object for correctly getting payment fields data of specific WP User in WPBC MU version.
617add_filter( 'wpbc_get_gateway_payment_form', array( new WPBC_Gateway_API_LOC_PAYMENT( WPBC_LOC_PAYMENT_GATEWAY_ID ), 'get_payment_form' ), 10, 3 );
618
619?>