/**
  * Retreive the authorize_aim vars needed to send to the gateway to proceed with payment
  * @param EM_Booking $EM_Booking
  */
 function authorize_and_capture($EM_Booking)
 {
     global $EM_Notices;
     $sale = $this->get_api();
     //Get transaction ID for authorization/capture
     $sale->amount = $amount = $EM_Booking->get_price(false, false, true);
     $sale->exp_date = $_REQUEST['x_exp_date_month'] . '/' . $_REQUEST['x_exp_date_year'];
     $sale->card_num = $_REQUEST['x_card_num'];
     $sale->card_code = $_REQUEST['x_card_code'];
     //Email Info
     $sale->email_customer = get_option('em_' . $this->gateway . '_email_customer', 0) ? '1' : '0';
     //for later
     $sale->header_email_receipt = get_option('em_' . $this->gateway . '_header_email_receipt');
     $sale->footer_email_receipt = get_option('em_' . $this->gateway . '_footer_email_receipt');
     //Order Info
     $sale->invoice_num = $EM_Booking->booking_id;
     $sale->description = preg_replace('/[^a-zA-Z0-9\\s]/i', "", $EM_Booking->get_event()->event_name);
     //clean event name
     //Customer Info
     $sale->email = $EM_Booking->get_person()->user_email;
     $sale->customer_ip = $_SERVER['REMOTE_ADDR'];
     $sale->cust_id = get_option('dbem_bookings_registration_disable') ? 'booking-' . $EM_Booking->booking_id : 'user-' . $EM_Booking->get_person()->ID;
     //Address Info
     $names = explode(' ', $EM_Booking->get_person()->get_name());
     if (!empty($names[0])) {
         $sale->first_name = array_shift($names);
     }
     if (implode(' ', $names) != '') {
         $sale->last_name = implode(' ', $names);
     }
     //address slightly special address field
     $address = '';
     if (EM_Gateways::get_customer_field('address', $EM_Booking) != '') {
         $address = EM_Gateways::get_customer_field('address', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('address_2', $EM_Booking) != '') {
         $address .= ', ' . EM_Gateways::get_customer_field('address_2', $EM_Booking);
     }
     if (!empty($address)) {
         $sale->address = substr($address, 0, 60);
     }
     //cut off at 60 characters
     if (EM_Gateways::get_customer_field('city', $EM_Booking) != '') {
         $sale->city = EM_Gateways::get_customer_field('city', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('state', $EM_Booking) != '') {
         $sale->state = EM_Gateways::get_customer_field('state', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('zip', $EM_Booking) != '') {
         $sale->zip = EM_Gateways::get_customer_field('zip', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('country', $EM_Booking) != '') {
         $countries = em_get_countries();
         $sale->country = $countries[EM_Gateways::get_customer_field('country', $EM_Booking)];
     }
     if (EM_Gateways::get_customer_field('phone', $EM_Booking) != '') {
         $sale->phone = EM_Gateways::get_customer_field('phone', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('fax', $EM_Booking) != '') {
         $sale->fax = EM_Gateways::get_customer_field('fax', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('company', $EM_Booking) != '') {
         $sale->company = EM_Gateways::get_customer_field('company', $EM_Booking);
     }
     //Itemized Billing
     $tax_enabled = get_option('dbem_bookings_tax') > 0 ? 'Y' : 'N';
     foreach ($EM_Booking->get_tickets_bookings()->tickets_bookings as $EM_Ticket_Booking) {
         $price = round($EM_Ticket_Booking->get_price() / $EM_Ticket_Booking->get_spaces(), 2);
         if ($price > 0) {
             $ticket_name = substr($EM_Ticket_Booking->get_ticket()->ticket_name, 0, 31);
             $sale->addLineItem($EM_Ticket_Booking->get_ticket()->ticket_id, $ticket_name, $EM_Ticket_Booking->get_ticket()->ticket_description, $EM_Ticket_Booking->get_spaces(), $price, $tax_enabled);
         }
     }
     if ($tax_enabled == 'Y') {
         $sale->tax = number_format($EM_Booking->get_price_taxes(), 2);
     }
     //Add discounts to itemized billing
     $discount = $EM_Booking->get_price_discounts_amount('pre') + $EM_Booking->get_price_discounts_amount('post');
     if ($discount > 0) {
         $sale->addLineItem(0, __('Discount', 'em-pro'), '', 1, $discount, 'N');
     }
     //Get Payment
     $sale = apply_filters('em_gateawy_authorize_aim_sale_var', $sale, $EM_Booking, $this);
     $response = $sale->authorizeAndCapture();
     //Handle result
     $result = $response->approved == true;
     if ($result) {
         $EM_Booking->booking_meta[$this->gateway] = array('txn_id' => $response->transaction_id, 'amount' => $amount);
         $this->record_transaction($EM_Booking, $amount, 'USD', date('Y-m-d H:i:s', current_time('timestamp')), $response->transaction_id, 'Completed', '');
     } else {
         $EM_Booking->add_error($response->response_reason_text);
     }
     //Return transaction_id or false
     return apply_filters('em_gateway_authorize_aim_authorize', $result, $EM_Booking, $this);
 }
 /**
  * Retreive the paypal vars needed to send to the gatway to proceed with payment
  * @param EM_Booking $EM_Booking
  */
 function get_paypal_vars($EM_Booking)
 {
     global $wp_rewrite, $EM_Notices;
     $notify_url = $this->get_payment_return_url();
     $paypal_vars = array('business' => get_option('em_' . $this->gateway . "_email"), 'cmd' => '_cart', 'upload' => 1, 'currency_code' => get_option('dbem_bookings_currency', 'USD'), 'notify_url' => $notify_url, 'custom' => $EM_Booking->booking_id . ':' . $EM_Booking->event_id, 'charset' => 'UTF-8', 'bn' => 'NetWebLogic_SP');
     if (get_option('em_' . $this->gateway . "_lc")) {
         $paypal_vars['lc'] = get_option('em_' . $this->gateway . "_lc");
     }
     //address fields`and name/email fields to prefill on checkout page (if available)
     $paypal_vars['email'] = $EM_Booking->get_person()->user_email;
     $paypal_vars['first_name'] = $EM_Booking->get_person()->first_name;
     $paypal_vars['last_name'] = $EM_Booking->get_person()->last_name;
     if (EM_Gateways::get_customer_field('address', $EM_Booking) != '') {
         $paypal_vars['address1'] = EM_Gateways::get_customer_field('address', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('address_2', $EM_Booking) != '') {
         $paypal_vars['address2'] = EM_Gateways::get_customer_field('address_2', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('city', $EM_Booking) != '') {
         $paypal_vars['city'] = EM_Gateways::get_customer_field('city', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('state', $EM_Booking) != '') {
         $paypal_vars['state'] = EM_Gateways::get_customer_field('state', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('zip', $EM_Booking) != '') {
         $paypal_vars['zip'] = EM_Gateways::get_customer_field('zip', $EM_Booking);
     }
     if (EM_Gateways::get_customer_field('country', $EM_Booking) != '') {
         $paypal_vars['country'] = EM_Gateways::get_customer_field('country', $EM_Booking);
     }
     //tax is added regardless of whether included in ticket price, otherwise we can't calculate post/pre tax discounts
     if ($EM_Booking->get_price_taxes() > 0 && !get_option('em_' . $this->gateway . "_inc_tax")) {
         $paypal_vars['tax_cart'] = round($EM_Booking->get_price_taxes(), 2);
     }
     if (get_option('em_' . $this->gateway . "_return") != "") {
         $paypal_vars['return'] = get_option('em_' . $this->gateway . "_return");
     }
     if (get_option('em_' . $this->gateway . "_cancel_return") != "") {
         $paypal_vars['cancel_return'] = get_option('em_' . $this->gateway . "_cancel_return");
     }
     if (get_option('em_' . $this->gateway . "_format_logo") !== false) {
         $paypal_vars['cpp_logo_image'] = get_option('em_' . $this->gateway . "_format_logo");
     }
     if (get_option('em_' . $this->gateway . "_border_color") !== false) {
         $paypal_vars['cpp_cart_border_color'] = get_option('em_' . $this->gateway . "_format_border");
     }
     $count = 1;
     foreach ($EM_Booking->get_tickets_bookings()->tickets_bookings as $EM_Ticket_Booking) {
         /* @var $EM_Ticket_Booking EM_Ticket_Booking */
         //divide price by spaces for per-ticket price
         //we divide this way rather than by $EM_Ticket because that can be changed by user in future, yet $EM_Ticket_Booking will change if booking itself is saved.
         if (!get_option('em_' . $this->gateway . "_inc_tax")) {
             $price = $EM_Ticket_Booking->get_price() / $EM_Ticket_Booking->get_spaces();
         } else {
             $price = $EM_Ticket_Booking->get_price_with_taxes() / $EM_Ticket_Booking->get_spaces();
         }
         if ($price > 0) {
             $paypal_vars['item_name_' . $count] = wp_kses_data($EM_Ticket_Booking->get_ticket()->name);
             $paypal_vars['quantity_' . $count] = $EM_Ticket_Booking->get_spaces();
             $paypal_vars['amount_' . $count] = round($price, 2);
             $count++;
         }
     }
     //calculate discounts, if any:
     $discount = $EM_Booking->get_price_discounts_amount('pre') + $EM_Booking->get_price_discounts_amount('post');
     if ($discount > 0) {
         $paypal_vars['discount_amount_cart'] = $discount;
     }
     return apply_filters('em_gateway_paypal_get_paypal_vars', $paypal_vars, $EM_Booking, $this);
 }
	/**
	* attempt to process payment
	* @param EM_Booking $EM_Booking
	* @return boolean
	*/
	public function processPayment($EM_Booking){
		// process the payment
		$isLiveSite = !(get_option('em_' . EM_EWAY_GATEWAY . '_mode') == 'sandbox');
		if (!$isLiveSite && get_option('em_' . EM_EWAY_GATEWAY . '_test_force')) {
			$customerID = EWAY_PAYMENTS_TEST_CUSTOMER;
		}
		else {
			$customerID = get_option('em_' . EM_EWAY_GATEWAY . '_cust_id');
		}

		if (get_option('em_' . EM_EWAY_GATEWAY . '_stored')) {
			$eway = new EwayPaymentsStoredPayment($customerID, $isLiveSite);
		}
		else {
			$eway = new EwayPaymentsPayment($customerID, $isLiveSite);
		}

		$eway->invoiceDescription			= $EM_Booking->get_event()->event_name;
		//~ $eway->invoiceDescription		= $EM_Booking->output('#_BOOKINGTICKETDESCRIPTION');
		$eway->invoiceReference				= $EM_Booking->booking_id;						// customer invoice reference
		$eway->transactionNumber			= $EM_Booking->booking_id;						// transaction reference
		$eway->cardHoldersName				= self::getPostValue('x_card_name');
		$eway->cardNumber					= strtr(self::getPostValue('x_card_num'), array(' ' => '', '-' => ''));
		$eway->cardExpiryMonth				= self::getPostValue('x_exp_date_month');
		$eway->cardExpiryYear				= self::getPostValue('x_exp_date_year');
		$eway->cardVerificationNumber		= self::getPostValue('x_card_code');
		$eway->emailAddress					= $EM_Booking->get_person()->user_email;
		$eway->postcode						= self::getPostValue('zip');

		// for Beagle (free) security
		if (get_option('em_' . EM_EWAY_GATEWAY . '_beagle')) {
			$eway->customerCountryCode		= EM_Gateways::get_customer_field('country', $EM_Booking);
		}

		// attempt to split name into parts, and hope to not offend anyone!
		$names = explode(' ', $EM_Booking->get_person()->get_name());
		if (!empty($names[0])) {
			$eway->firstName				= array_shift($names);		// remove first name from array
		}
		$eway->lastName						= trim(implode(' ', $names));

		// use cardholder name for last name if no customer name entered
		if (empty($eway->firstName) && empty($eway->lastName)) {
			$eway->lastName = $eway->cardHoldersName;
		}

		// aggregate street, city, state, country into a single string
		$parts = array (
			EM_Gateways::get_customer_field('address', $EM_Booking),
			EM_Gateways::get_customer_field('address_2', $EM_Booking),
			EM_Gateways::get_customer_field('city', $EM_Booking),
			EM_Gateways::get_customer_field('state', $EM_Booking),
			self::getCountryName(EM_Gateways::get_customer_field('country', $EM_Booking)),
		);
		$eway->address						= implode(', ', array_filter($parts, 'strlen'));

		// if live, pass through amount exactly, but if using test site, round up to whole dollars or eWAY will fail
		$amount = $EM_Booking->get_price(false, false, true);
		$amount = apply_filters('em_eway_amount', $amount, $EM_Booking);
		$eway->amount						= $isLiveSite ? $amount : ceil($amount);

		// allow plugins/themes to modify invoice description and reference, and set option fields
		$eway->invoiceDescription			= apply_filters('em_eway_invoice_desc', $eway->invoiceDescription, $EM_Booking);
		$eway->invoiceReference				= apply_filters('em_eway_invoice_ref', $eway->invoiceReference, $EM_Booking);
		$eway->option1						= apply_filters('em_eway_option1', '', $EM_Booking);
		$eway->option2						= apply_filters('em_eway_option2', '', $EM_Booking);
		$eway->option3						= apply_filters('em_eway_option3', '', $EM_Booking);

		// Get Payment
		try {
			$result = false;
			$response = $eway->processPayment();

			if ($response->status) {
				// transaction was successful, so record transaction number and continue
				$EM_Booking->booking_meta[EM_EWAY_GATEWAY] = array(
					'txn_id'	=> $response->transactionNumber,
					'authcode'	=> $response->authCode,
					'amount'	=> $response->amount,
				);

				$notes = array();
				if (!empty($response->authCode)) {
					$notes[] = 'Authcode: ' . $response->authCode;
				}
				if (!empty($response->beagleScore)) {
					$notes[] = 'Beagle score: ' . $response->beagleScore;
				}
				$note = implode("\n", $notes);

				$status = get_option('em_' . EM_EWAY_GATEWAY . '_stored') ? 'Pending' : 'Completed';
				$this->record_transaction($EM_Booking, $response->amount, 'AUD', date('Y-m-d H:i:s', current_time('timestamp')), $response->transactionNumber, $status, $note);
				$result = true;
			}
			else {
				// transaction was unsuccessful, so record the error
				$EM_Booking->add_error($response->error);
			}
		}
		catch (Exception $e) {
			// an exception occured, so record the error
			$EM_Booking->add_error($e->getMessage());
			return;
		}

		// Return status
		return apply_filters('em_gateway_eway_authorize', $result, $EM_Booking, $this);
	}