InvoiceXpressRequest

The WooCommerce InvoiceXpress Extension InvoiceXpressRequest class.

Defined (1)

The class is defined in the following location(s).

/InvoiceXpressRequest-PHP-API/lib/InvoiceXpressRequest.php  
  1. class InvoiceXpressRequest { 
  2. /** 
  3. * The domain you need when making a request 
  4. */ 
  5. protected static $_domain = ''; 
  6.  
  7. /** 
  8. * The API token you need when making a request 
  9. */ 
  10. protected static $_token = ''; 
  11.  
  12. /** 
  13. * The API url we're hitting. {{ DOMAIN }} will get replaced with $domain 
  14. * when you set InvoiceXpressRequest::init($domain, $token) 
  15. */ 
  16. protected $_api_url = 'https://{{ DOMAIN }}.invoicexpress.com/{{ CLASS }}.xml'; 
  17.  
  18. /** 
  19. * Stores the current method we're using. Example: 
  20. * new InvoiceXpressRequest('client.create'), 'client.create' would be the method 
  21. */ 
  22. protected $_method = ''; 
  23.  
  24. /** 
  25. * Any arguments to pass to the request 
  26. */ 
  27. protected $_args = array(); 
  28.  
  29. /** 
  30. * Determines whether or not the request was successful 
  31. */ 
  32. protected $_success = false; 
  33.  
  34. /** 
  35. * Holds the error returned from our request 
  36. */ 
  37. protected $_error = ''; 
  38.  
  39. /** 
  40. * Holds the response after our request 
  41. */ 
  42. protected $_response = array(); 
  43.  
  44. /** 
  45. * Initialize the and store the domain/token for making requests 
  46. * @param string $domain The subdomain like 'yoursite'.freshbooks.com 
  47. * @param string $token The token found in your account settings area 
  48. * @return null 
  49. */ 
  50. public static function init($domain, $token) 
  51. self::$_domain = $domain; 
  52. self::$_token = $token; 
  53.  
  54. /** 
  55. * Set up the request object and assign a method name 
  56. * @param array $method The method name from the API, like 'client.update' etc 
  57. * @return null 
  58. */ 
  59. public function __construct($method) 
  60. $this->_method = $method; 
  61.  
  62. /** 
  63. * Set the data/arguments we're about to request with 
  64. * @return null 
  65. */ 
  66. public function post($data) 
  67. $this->_args = $data; 
  68.  
  69. /** 
  70. * Determine whether or not it was successful 
  71. * @return bool 
  72. */ 
  73. public function success() 
  74. return $this->_success; 
  75.  
  76. /** 
  77. * Get the error (if there was one returned from the request) 
  78. * @return string 
  79. */ 
  80. public function getError() 
  81. return $this->_error; 
  82.  
  83. /** 
  84. * Get the response from the request we made 
  85. * @return array 
  86. */ 
  87. public function getResponse() 
  88. return $this->_response; 
  89.  
  90. /** 
  91. * Get the generated XML to view. This is useful for debugging 
  92. * to see what you're actually sending over the wire. Call this 
  93. * after $ie->post() but before your make your $ie->request() 
  94. * @return array 
  95. */ 
  96. public function getGeneratedXML() 
  97.  
  98. $dom = new XmlDomConstruct('1.0', 'utf-8'); 
  99. $dom->fromMixed($this->_args); 
  100. $post_data = $dom->saveXML(); 
  101.  
  102. $post_data = str_replace('<?xml version="1.0" encoding="utf-8"?>', '', $post_data); 
  103.  
  104. return $post_data; 
  105.  
  106.  
  107. /** 
  108. * Send the request over the wire 
  109. * @return array 
  110. */ 
  111. public function request($id = '') 
  112. {  
  113. if(!self::$_domain || !self::$_token) 
  114. throw new InvoiceXpressRequestException('You need to call InvoiceXpressRequest::init($domain, $token) with your domain and token.'); 
  115.  
  116. $post_data = $this->getGeneratedXML(); 
  117. $p = print_r($post_data, true); 
  118. error_log("post = ".$p); 
  119.  
  120. $url = str_replace('{{ DOMAIN }}', self::$_domain, $this->_api_url); 
  121.  
  122. $class = explode(".", $this->_method); 
  123.  
  124. $ch = curl_init(); // initialize curl handle 
  125.  
  126. if ($class[1] == "change-state" || $class[1] == "email-invoice") 
  127. $url = str_replace('{{ CLASS }}', "invoice/".$id."/".$class[1], $url); 
  128. elseif ($class[0] == "clients" && $class[1] == "get") { 
  129. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
  130. $url = str_replace('{{ CLASS }}', "clients/".$id, $url); 
  131. } elseif ($class[0] == "simplified_invoices" && $class[1] == "get") { 
  132. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
  133. $url = str_replace('{{ CLASS }}', "simplified_invoices/".$id, $url); 
  134. } else { 
  135. $url = str_replace('{{ CLASS }}', $class[0], $url); 
  136. curl_setopt($ch, CURLOPT_POST, 1); 
  137. error_log("POST Request = true"); 
  138.  
  139. $url .= "?api_key=".self::$_token; 
  140.  
  141.  
  142. error_log("URL = ".$url); 
  143. curl_setopt($ch, CURLOPT_URL, $url); // set url to post to 
  144. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable 
  145. curl_setopt($ch, CURLOPT_TIMEOUT, 40); // times out after 40s 
  146. if ($class[1] != "get") 
  147. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // add POST fields 
  148. curl_setopt($ch, CURLOPT_USERPWD, self::$_token . ':X'); 
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
  150. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml; charset=utf-8")); 
  151.  
  152. $result = curl_exec($ch); 
  153. $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  154.  
  155. if(curl_errno($ch)) 
  156. $this->_error = 'A cURL error occured: ' . curl_error($ch); 
  157. return; 
  158. else 
  159. curl_close($ch); 
  160.  
  161. // if weird simplexml error then you may have the a user with 
  162. // a user_meta wc_ie_client_id defined that not exists in InvoiceXpress 
  163. if ($result && $result != " ") { 
  164. $res = print_r($result, true); 
  165. error_log("result = {".$res."}"); 
  166.  
  167. $response = json_decode(json_encode(simplexml_load_string($result)), true); 
  168. $r = print_r($response, true); 
  169. error_log("response = ".$r);  
  170.  
  171. $this->_response = $response; 
  172.  
  173. $this->_success = (($http_status == '201 Created') || ($http_status == '200 OK')); 
  174. error_log("http status = ".$http_status); 
  175.  
  176. if(isset($response['error'])) 
  177. $this->_error = $response['error']; 
  178.