/**
  * @param boolean $result
  * @param EM_Booking $EM_Booking
  * @return boolean
  */
 function em_booking_validate($result, $EM_Booking)
 {
     if (empty($EM_Booking->booking_id) && self::$validate) {
         //only run if taking post data, because validation could fail elsewhere
         $EM_Form = self::get_form($EM_Booking->event_id);
         if (!$EM_Form->get_post()) {
             $EM_Booking->add_error($EM_Form->get_errors());
             return false;
         }
     }
     return $result;
 }
 /**
  * Validates a booking against the attendee fields provided
  * @param boolean $result
  * @param EM_Booking $EM_Booking
  * @return boolean
  */
 public static function em_booking_validate($result, $EM_Booking)
 {
     //going through each ticket type booked
     $EM_Form = self::get_form($EM_Booking->event_id);
     if (self::$form_id > 0) {
         foreach ($EM_Booking->get_tickets_bookings()->tickets_bookings as $EM_Ticket_Booking) {
             //get original field labels for replacement of #NUM#
             $original_fields = array();
             foreach ($EM_Form->form_fields as $key => $field) {
                 $original_fields[$key] = $EM_Form->form_fields[$key]['label'];
             }
             //validate a form for each space booked
             for ($i = 0; $i < $EM_Ticket_Booking->ticket_booking_spaces; $i++) {
                 if (isset($EM_Booking->booking_meta['attendees'][$EM_Ticket_Booking->ticket_id][$i])) {
                     //unlike post values each attendee has an array within the array of a ticket attendee info
                     $EM_Form->field_values = $EM_Booking->booking_meta['attendees'][$EM_Ticket_Booking->ticket_id][$i];
                     $EM_Form->errors = array();
                     //change the field labels in case of #NUM#
                     foreach ($EM_Form->form_fields as $key => $field) {
                         $EM_Form->form_fields[$key]['label'] = str_replace('#NUM#', $i + 1, $original_fields[$key]);
                     }
                     //validate and save errors within this ticket user
                     if (!$EM_Form->validate()) {
                         $title = $EM_Ticket_Booking->get_ticket()->ticket_name . " - " . sprintf(__('Attendee %s', 'em-pro'), $i + 1);
                         $error = array($title => $EM_Form->get_errors());
                         $EM_Booking->add_error($error);
                         $result = false;
                     }
                 }
             }
         }
     }
     return $result;
 }
 /**
  * Retreive the paypal pro vars needed to send to the gateway to proceed with payment
  * @param EM_Booking $EM_Booking
  */
 function processStripe($EM_Booking)
 {
     global $EM_Notices;
     if (empty($_POST['stripe_card_num'])) {
         $EM_Booking->add_error(__('Please enter credit card number', 'em-pro') . '"');
         return false;
     }
     if (empty($_POST['stripe_exp_date_month'])) {
         $EM_Booking->add_error(__('Please select expire month', 'em-pro') . '"');
         return false;
     }
     if (empty($_POST['stripe_exp_date_year'])) {
         $EM_Booking->add_error(__('Please select expire year', 'em-pro') . '"');
         return false;
     }
     if (empty($_POST['stripe_card_code'])) {
         $EM_Booking->add_error(__('Please enter CVV number', 'em-pro') . '"');
         return false;
     }
     if ($this->debug == 'yes') {
         // Send request to paypal
         EM_Pro::log(sprintf(__('Payment Processing Start here', 'emp_stripe')));
     }
     // Get the credit card details submitted by the form
     include "lib/Stripe.php";
     if ($this->debug == 'yes') {
         EM_Pro::log(sprintf(__('Payment Processing start after include library', 'emp_stripe')));
     }
     Stripe::setApiKey($this->SecretKey);
     if ($this->debug == 'yes') {
         EM_Pro::log(sprintf(__('Set Secret Key', 'emp_stripe')));
     }
     try {
         $amount = $EM_Booking->get_price(false, false, true);
         if ($this->debug == 'yes') {
             EM_Pro::log(sprintf(__('Credit Card token create', 'emp_stripe')));
         }
         $token_id = Stripe_Token::create(array("card" => array("number" => $_POST['stripe_card_num'], "exp_month" => $_POST['stripe_exp_date_month'], "exp_year" => $_POST['stripe_exp_date_year'], "cvc" => $_POST['stripe_card_code'])));
         if ($this->debug == 'yes') {
             EM_Pro::log(sprintf(__('Token genreated ID : %s', 'emp_stripe'), print_r($token_id->id, true)));
         }
         //Email Info
         $email_customer = get_option('em_' . $this->gateway . '_header_email_customer', 0) ? '1' : '0';
         //for later
         $header_email_receipt = get_option('em_' . $this->gateway . '_header_email_receipt');
         $footer_email_receipt = get_option('em_' . $this->gateway . '_footer_email_receipt');
         //Order Info
         $booking_id = $EM_Booking->booking_id;
         $booking_description = preg_replace('/[^a-zA-Z0-9\\s]/i', "", $EM_Booking->get_event()->event_name);
         //clean event name
         $charge = Stripe_Charge::create(array("amount" => $amount * 100, "currency" => get_option('dbem_bookings_currency', 'USD'), "card" => $token_id->id, "metadata" => array("order_id" => $booking_id), "description" => $booking_description));
         if ($this->debug == 'yes') {
             EM_Pro::log(sprintf(__('Return Response from Stripe: %s', 'emp_stripe'), print_r($charge, true)));
         }
         if ($token_id->id != '') {
             if ($charge->paid == true) {
                 if ($this->debug == 'yes') {
                     EM_Pro::log(sprintf(__('Payment Received...', 'emp_stripe')));
                 }
                 $EM_Booking->booking_meta[$this->gateway] = array('txn_id' => $charge->id, 'amount' => $amount);
                 $this->record_transaction($EM_Booking, $amount, get_option('dbem_bookings_currency', 'USD'), date('Y-m-d H:i:s', current_time('timestamp')), $charge->id, 'Completed', '');
                 $result = true;
             } else {
                 if ($this->debug == 'yes') {
                     EM_Pro::log(sprintf(__('Stripe payment failed. Payment declined.', 'emp_stripe')));
                 }
                 $EM_Booking->add_error('Stripe payment failed. Payment declined.');
                 $result = false;
             }
         } else {
             if ($this->debug == 'yes') {
                 EM_Pro::log(sprintf(__('Stripe payment failed. Payment declined. Please Check your Admin settings', 'emp_stripe')));
             }
             $EM_Booking->add_error('Stripe payment failed. Payment declined. Please Check your Admin settings');
         }
         //Return transaction_id or false
         return apply_filters('em_gateway_stripe_capture', $result, $EM_Booking, $this);
     } catch (Exception $e) {
         $EM_Booking->add_error(__('Connection error:', 'em-pro') . ': "' . $e->getMessage() . '"');
         return false;
     }
 }
 /**
  * Modifies the booking status if the event isn't free and also adds a filter to modify user feedback returned.
  * Triggered by the em_booking_add_yourgateway action.
  * @param EM_Event $EM_Event
  * @param EM_Booking $EM_Booking
  * @param boolean $post_validation
  */
 function booking_add($EM_Event, $EM_Booking, $post_validation = false)
 {
     global $wpdb, $wp_rewrite, $EM_Notices;
     //manual bookings
     if (!empty($_REQUEST['manual_booking']) && wp_verify_nonce($_REQUEST['manual_booking'], 'em_manual_booking_' . $_REQUEST['event_id'])) {
         //validate post
         if (!empty($_REQUEST['payment_amount']) && !is_numeric($_REQUEST['payment_amount'])) {
             $EM_Booking->add_error('Invalid payment amount, please provide a number only.', 'em-pro');
         }
         //add em_event_save filter to log transactions etc.
         add_filter('em_booking_save', array(&$this, 'em_booking_save'), 10, 2);
         //set flag that we're manually booking here, and set gateway to offline
         if (empty($_REQUEST['person_id']) || $_REQUEST['person_id'] < 0) {
             EM_Bookings::$force_registration = EM_Bookings::$disable_restrictions = true;
         }
     }
     parent::booking_add($EM_Event, $EM_Booking, $post_validation);
 }