/**
  * Get session ID.
  *
  * @return string
  */
 public function get_session_id()
 {
     if ('yes' == $this->gateway->debug) {
         $this->gateway->log->add($this->gateway->id, 'Requesting session ID...');
     }
     $url = add_query_arg(array('email' => $this->gateway->get_email(), 'token' => $this->gateway->get_token()), $this->get_sessions_url());
     $response = $this->do_request($url, 'POST');
     // Check to see if the request was valid.
     if (is_wp_error($response)) {
         if ('yes' == $this->gateway->debug) {
             $this->gateway->log->add($this->gateway->id, 'WP_Error requesting session ID: ' . $response->get_error_message());
         }
     } else {
         try {
             $session = $this->safe_load_xml($response['body'], LIBXML_NOCDATA);
         } catch (Exception $e) {
             $session = '';
             if ('yes' == $this->gateway->debug) {
                 $this->gateway->log->add($this->gateway->id, 'Error while parsing the PagSeguro session response: ' . print_r($e->getMessage(), true));
             }
         }
         if (isset($session->id)) {
             if ('yes' == $this->gateway->debug) {
                 $this->gateway->log->add($this->gateway->id, 'PagSeguro session is valid! The return is: ' . print_r($session, true));
             }
             return (string) $session->id;
         }
     }
     if ('yes' == $this->gateway->debug) {
         $this->gateway->log->add($this->gateway->id, 'Session Response: ' . print_r($response, true));
     }
     return false;
 }
 /**
  * Do payment request.
  *
  * @param  WC_Order $order  Order data.
  * @param  array    $posted Posted data.
  *
  * @return array
  */
 public function do_payment_request($order, $posted)
 {
     $payment_method = isset($posted['pagseguro_payment_method']) ? $posted['pagseguro_payment_method'] : '';
     /**
      * Validate if has selected a payment method.
      */
     if (!in_array($payment_method, $this->get_available_payment_methods())) {
         return array('url' => '', 'data' => '', 'error' => array('<strong>' . __('PagSeguro', 'woocommerce-pagseguro') . '</strong>: ' . __('Please, select a payment method.', 'woocommerce-pagseguro')));
     }
     // Sets the xml.
     $xml = $this->get_payment_xml($order, $posted);
     if ('yes' == $this->gateway->debug) {
         $this->gateway->log->add($this->gateway->id, 'Requesting direct payment for order ' . $order->get_order_number() . ' with the following data: ' . $xml);
     }
     $url = add_query_arg(array('email' => $this->gateway->email, 'token' => $this->gateway->token), $this->get_transactions_url());
     $response = $this->do_request($url, 'POST', $xml, array('Content-Type' => 'application/xml;charset=UTF-8'));
     if (is_wp_error($response)) {
         if ('yes' == $this->gateway->debug) {
             $this->gateway->log->add($this->gateway->id, 'WP_Error in requesting the direct payment: ' . $response->get_error_message());
         }
     } else {
         if (401 === $response['response']['code']) {
             if ('yes' == $this->gateway->debug) {
                 $this->gateway->log->add($this->gateway->id, 'The user does not have permissions to use the PagSeguro Transparent Checkout!');
             }
             return array('url' => '', 'data' => '', 'error' => array(__('You are not allowed to use the PagSeguro Transparent Checkout. Looks like you neglected to installation guide of this plugin. This is not pretty, do you know?', 'woocommerce-pagseguro')));
         } else {
             try {
                 $data = $this->safe_load_xml($response['body'], LIBXML_NOCDATA);
             } catch (Exception $e) {
                 $data = '';
                 if ('yes' == $this->gateway->debug) {
                     $this->gateway->log->add($this->gateway->id, 'Error while parsing the PagSeguro response: ' . print_r($e->getMessage(), true));
                 }
             }
             if (isset($data->code)) {
                 if ('yes' == $this->gateway->debug) {
                     $this->gateway->log->add($this->gateway->id, 'PagSeguro direct payment created successfully!');
                 }
                 return array('url' => $this->gateway->get_return_url($order), 'data' => $data, 'error' => '');
             }
             if (isset($data->error)) {
                 $errors = array();
                 if ('yes' == $this->gateway->debug) {
                     $this->gateway->log->add($this->gateway->id, 'An error occurred while generating the PagSeguro direct payment: ' . print_r($response, true));
                 }
                 foreach ($data->error as $error_key => $error) {
                     $errors[] = '<strong>' . __('PagSeguro', 'woocommerce-pagseguro') . '</strong>: ' . $this->get_error_message($error->code);
                 }
                 return array('url' => '', 'data' => '', 'error' => $errors);
             }
         }
     }
     if ('yes' == $this->gateway->debug) {
         $this->gateway->log->add($this->gateway->id, 'An error occurred while generating the PagSeguro direct payment: ' . print_r($response, true));
     }
     // Return error message.
     return array('url' => '', 'data' => '', 'error' => array('<strong>' . __('PagSeguro', 'woocommerce-pagseguro') . '</strong>: ' . __('An error has occurred while processing your payment, please try again. Or contact us for assistance.', 'woocommerce-pagseguro')));
 }