/**
  * Process payment
  *
  * @param int $order_id
  *
  * @return array
  */
 public function process_payment($order_id)
 {
     $order = wc_get_order($order_id);
     $config = $this->settings;
     try {
         $payment_api = $this->get_api();
         $request = new WC_LUUP_API_Transaction_Request($config, $order);
         // Only accept specific currencies
         if ($this->get_option('currencies_accepted')) {
             $request->set_currencies_accepted($this->get_option('currencies_accepted'));
         }
         $request->set_payment_brand(self::PAYMENT_BRAND)->set_payment_type($this->get_option('auth_mode'))->set_shopper_result_url(WC()->api_request_url('WC_LUUP_PayPal'));
         $payment_response = $payment_api->send_payment($request);
     } catch (Exception $e) {
         //todo - log exception
         wc_add_notice('Sorry there was a problem connecting to PayPal, please try again.', 'error');
         return false;
     }
     return array('result' => 'success', 'redirect' => $payment_response->process_async_redirect());
 }
 /**
  * Make a LUUP request
  *
  * @param $endpoint
  * @param WC_LUUP_API_Transaction_Request $transaction
  *
  * @return mixed
  */
 protected function request($endpoint, WC_LUUP_API_Transaction_Request $transaction = null, $method = 'POST')
 {
     $headers = null;
     $data = array('authentication.userId' => $this->get_api_user_id(), 'authentication.password' => $this->get_api_password(), 'authentication.entityId' => $this->get_api_entity_id());
     if ($transaction) {
         $data = array_merge($data, $transaction->to_array());
     }
     if ('POST' == $method) {
         $headers = array('Content-Type' => 'application/x-www-form-urlencoded');
     }
     if ('GET' == $method) {
         $endpoint .= '?' . http_build_query($data);
     }
     $http_request = $this->get_http_client();
     $response = $http_request->request($endpoint, array('method' => $method, 'body' => http_build_query($data), 'headers' => $headers));
     $luup_response = new WC_LUUP_API_Transaction_Response($response['body']);
     if ($response instanceof WP_Error) {
         throw new WC_LUUP_API_Exception('Unexpected HTTP client error - ' . $response->get_error_message());
     } elseif ($response['response']['code'] == '200') {
         return $luup_response;
     } else {
         throw new WC_LUUP_API_Exception('Unexpected HTTP status code - Code:' . $response['response']['code']);
     }
 }
 /**
  * Processes a tokenised payment
  *
  * @param $original_order_id
  *
  * @return WC_Order|bool
  */
 protected function create_registration_payment($original_order_id)
 {
     $nonce = isset($_REQUEST['_wpnonce']) ? $_REQUEST['_wpnonce'] : '';
     if (!wp_verify_nonce($nonce, 'buy_postsale_offer')) {
         return false;
     }
     $order = $this->create_postsale_order($original_order_id);
     try {
         $payment_api = $this->get_api();
         $request = new WC_LUUP_API_Transaction_Request($this->settings, $order);
         $request->set_payment_type($this->get_option('auth_mode'));
         $payment_response = $payment_api->send_registration_payment(get_post_meta($original_order_id, self::TOKEN_NAME, true), $request);
     } catch (Exception $e) {
         //todo - log exception
         return false;
     }
     $order = $this->process_luup_payment_response($payment_response, $order);
     if ('processing' == $order->get_status()) {
         if (!add_post_meta($original_order_id, self::POSTSALE_ORDER_META_NAME, $order->id, true)) {
             update_post_meta($original_order_id, self::POSTSALE_ORDER_META_NAME, $order->id);
         }
         $_SESSION['luup']['can_postsale'] = false;
         unset($_SESSION['luup']['can_postsale']);
         return $order;
     }
     return false;
 }