WC_QuickPay_API

WC_QuickPay_API class.

Defined (1)

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

/classes/api/woocommerce-quickpay-api.php  
  1. class WC_QuickPay_API 
  2.  
  3. /** 
  4. * Contains cURL instance 
  5. * @access protected 
  6. */  
  7. protected $ch; 
  8.  
  9.  
  10. /** 
  11. * Contains the API url 
  12. * @access protected 
  13. */ 
  14. protected $api_url = 'https://api.quickpay.net/'; 
  15.  
  16.  
  17. /** 
  18. * Contains a resource data object 
  19. * @access protected 
  20. */ 
  21. protected $resource_data; 
  22.  
  23. protected $api_key = NULL; 
  24.  
  25. /** 
  26. * @var bool 
  27. */ 
  28. public $block_callback = FALSE; 
  29.  
  30.  
  31. /** 
  32. * __construct function. 
  33. * @access public 
  34. * @return void 
  35. */  
  36. public function __construct( $api_key = NULL ) 
  37. add_action('shutdown', array($this, 'shutdown')); 
  38.  
  39. if (empty($api_key)) { 
  40. $this->api_key = WC_QP()->s( 'quickpay_apikey' ); 
  41. } else { 
  42. $this->api_key = $api_key; 
  43.  
  44. // Instantiate an empty object ready for population 
  45. $this->resource_data = new stdClass(); 
  46.  
  47.  
  48. /** 
  49. * is_authorized_callback function. 
  50. * Performs a check on payment callbacks to see if it is legal or spoofed 
  51. * @access public 
  52. * @return boolean 
  53. */  
  54. public function is_authorized_callback( $response_body )  
  55. if( ! isset( $_SERVER["HTTP_QUICKPAY_CHECKSUM_SHA256"] ) )  
  56. return FALSE; 
  57.  
  58. return hash_hmac( 'sha256', $response_body, WC_QP()->s( 'quickpay_privatekey' ) ) == $_SERVER["HTTP_QUICKPAY_CHECKSUM_SHA256"]; 
  59.  
  60.  
  61. /** 
  62. * get function. 
  63. * Performs an API GET request 
  64. * @access public 
  65. * @return object 
  66. */  
  67. public function get( $path, $return_array = FALSE )  
  68. // Instantiate a new instance 
  69. $this->remote_instance(); 
  70.  
  71. // Set the request params 
  72. $this->set_url( $path ); 
  73.  
  74. // Start the request and return the response 
  75. return $this->execute('GET', $return_array); 
  76.  
  77.  
  78. /** 
  79. * post function. 
  80. * Performs an API POST request 
  81. * @access public 
  82. * @return object 
  83. */  
  84. public function post( $path, $form = array(), $return_array = FALSE )  
  85. // Instantiate a new instance 
  86. $this->remote_instance( $this->get_post_id_from_form_object( $form ) ); 
  87.  
  88. // Set the request params 
  89. $this->set_url( $path ); 
  90.  
  91. // Start the request and return the response 
  92. return $this->execute('POST', $form, $return_array); 
  93. }  
  94.  
  95.  
  96. /** 
  97. * put function. 
  98. * Performs an API PUT request 
  99. * @access public 
  100. * @return object 
  101. */  
  102. public function put( $path, $form = array(), $return_array = FALSE )  
  103. // Instantiate a new instance 
  104. $this->remote_instance( $this->get_post_id_from_form_object( $form ) ); 
  105.  
  106. // Set the request params 
  107. $this->set_url( $path ); 
  108.  
  109. // Start the request and return the response 
  110. return $this->execute('PUT', $form, $return_array); 
  111.  
  112.  
  113. /** 
  114. * execute function. 
  115. * Executes the API request 
  116. * @access public 
  117. * @param string $request_type 
  118. * @param array $form 
  119. * @param boolean $return_array - if we want to retrieve an array with additional  
  120. * @return object 
  121. * @throws QuickPay_API_Exception 
  122. */  
  123. public function execute( $request_type, $form = array(), $return_array = FALSE )  
  124. // Set the HTTP request type 
  125. curl_setopt( $this->ch, CURLOPT_CUSTOMREQUEST, $request_type ); 
  126.  
  127. // Prepare empty variable passed to any exception thrown 
  128. $request_form_data = ''; 
  129.  
  130. // If additional data is delivered, we will send it along with the API request 
  131. if( is_array( $form ) && ! empty( $form ) ) 
  132. // Build a string query based on the form array values 
  133. $request_form_data = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', http_build_query( $form, '', '&' )); 
  134.  
  135. // Prepare to post the data string 
  136. curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $request_form_data ); 
  137.  
  138. // Execute the request and decode the response to JSON 
  139. $this->resource_data = json_decode( curl_exec( $this->ch ) ); 
  140.  
  141. // Retrieve the HTTP response code 
  142. $response_code = (int) curl_getinfo( $this->ch, CURLINFO_HTTP_CODE ); 
  143. $response_data = json_encode( $this->resource_data ); 
  144. $curl_request_url = curl_getinfo( $this->ch, CURLINFO_EFFECTIVE_URL); 
  145.  
  146. // If the HTTP response code is higher than 299, the request failed. 
  147. // Throw an exception to handle the error 
  148. if( $response_code > 299 )  
  149. if( isset($this->resource_data->errors) )  
  150. $error_messages = ""; 
  151. foreach( $this->resource_data->errors as $error_field => $error_descriptions )  
  152. $error_messages .= "{$error_field}: "; 
  153.  
  154. foreach( $error_descriptions as $error_description ) 
  155. $error_messages .= "{$error_description}<br />\n"; 
  156.  
  157. throw new QuickPay_API_Exception( $error_messages, $response_code, NULL, $curl_request_url, $request_form_data, $response_data ); 
  158. else if( isset( $this->resource_data->message) )  
  159. throw new QuickPay_API_Exception( $this->resource_data->message, $response_code, NULL, $curl_request_url, $request_form_data, $response_data );  
  160. else  
  161. throw new QuickPay_API_Exception( (string) json_encode($this->resource_data), $response_code, NULL, $curl_request_url, $request_form_data, $response_data ); 
  162.  
  163.  
  164. // Everything went well, return the resource data object. 
  165. if( $return_array ) { 
  166. return array(  
  167. $this->resource_data,  
  168. $curl_request_url,  
  169. $request_form_data,  
  170. $response_data,  
  171. curl_getinfo( $this->ch ),  
  172. );  
  173. return $this->resource_data; 
  174.  
  175.  
  176. /** 
  177. * set_url function. 
  178. * Takes an API request string and appends it to the API url 
  179. * @access public 
  180. * @return void 
  181. */  
  182. public function set_url( $params )  
  183. curl_setopt( $this->ch, CURLOPT_URL, $this->api_url . trim( $params, '/' ) ); 
  184.  
  185.  
  186. /** 
  187. * remote_instance function. 
  188. * Create a cURL instance if none exists already 
  189. * @access public 
  190. * @return cURL object 
  191. */ 
  192. protected function remote_instance( $post_id = NULL )  
  193. if( $this->ch === NULL ) 
  194. $this->ch = curl_init(); 
  195. curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, TRUE ); 
  196. curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER , FALSE ); 
  197. curl_setopt( $this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);  
  198. curl_setopt( $this->ch, CURLOPT_HTTPHEADER, array( 
  199. 'Authorization: Basic ' . base64_encode(':' . $this->api_key ),  
  200. 'Accept-Version: v10',  
  201. 'Accept: application/json',  
  202. 'QuickPay-Callback-Url: ' . (!$this->block_callback) ? WC_QuickPay_Helper::get_callback_url( $post_id ) : NULL 
  203. )); 
  204.  
  205. return $this->ch;  
  206.  
  207.  
  208. /** 
  209. * shutdown function. 
  210. * Closes the current cURL connection 
  211. * @access public 
  212. * @return void 
  213. */ 
  214. public function shutdown()  
  215. if( ! empty( $this->ch ) )  
  216. curl_close( $this->ch ); 
  217. }  
  218.  
  219. /** 
  220. * Returns the POST ID if available in the form data object 
  221. * @param [type] $form_data [description] 
  222. * @return [type] [description] 
  223. */ 
  224. public function get_post_id_from_form_object( $form_data ) { 
  225. if( array_key_exists( 'order_post_id', $form_data ) ) { 
  226. return $form_data['order_post_id']; 
  227. return NULL;