diff --git a/blockonomics-woocommerce.php b/blockonomics-woocommerce.php index 2bb51017..fcf4f75b 100755 --- a/blockonomics-woocommerce.php +++ b/blockonomics-woocommerce.php @@ -282,22 +282,19 @@ private function checkForErrors($responseObj, $woocommerce) { if(!isset($responseObj->response_code)) { $error_str = __('Your webhost is blocking outgoing HTTPS connections. Blockonomics requires an outgoing HTTPS POST (port 443) to generate new address. Check with your webhosting provider to allow this.', 'blockonomics-bitcoin-payments'); - } elseif(!ini_get('allow_url_fopen')) { - $error_str = __('The allow_url_fopen is not enabled, please enable this option to allow address generation.', 'blockonomics-bitcoin-payments'); - } else { switch ($responseObj->response_code) { - case 'HTTP/1.1 200 OK': + case 200: break; - case 'HTTP/1.1 401 Unauthorized': { + case 401: { $error_str = __('API Key is incorrect. Make sure that the API key set in admin Blockonomics module configuration is correct.', 'blockonomics-bitcoin-payments'); break; } - case 'HTTP/1.1 500 Internal Server Error': { + case 500: { if(isset($responseObj->message)) { diff --git a/php/Blockonomics.php b/php/Blockonomics.php index 5788db02..02d4d432 100755 --- a/php/Blockonomics.php +++ b/php/Blockonomics.php @@ -10,35 +10,42 @@ public function __construct() { } - public function new_address($api_key, $secret) { - $options = array( - 'http' => array( - 'header' => 'Authorization: Bearer ' . $api_key, - 'method' => 'POST', - 'content' => '', - 'ignore_errors' => true - ) - ); - - $context = stream_context_create($options); - $contents = file_get_contents(Blockonomics::NEW_ADDRESS_URL."?match_callback=$secret", false, $context); - $responseObj = json_decode($contents); - - //Create response object if it does not exist - if (!isset($responseObj)) $responseObj = new stdClass(); - $responseObj->{'response_code'} = $http_response_header[0]; + $url = Blockonomics::NEW_ADDRESS_URL . "?match_callback=" . $secret; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, 60); + curl_setopt($ch, CURLOPT_POSTFIELDS, ""); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Authorization: Bearer ' . $api_key, + 'Content-type: application/x-www-form-urlencoded' + )); + $data = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + $responseObj = json_decode($data); + if (!isset($responseObj)) { + $responseObj = new stdClass(); + } + $responseObj->{'response_code'} = $httpcode; return $responseObj; } public function get_price($currency) { - $options = array( 'http' => array( 'method' => 'GET') ); - $context = stream_context_create($options); - $contents = file_get_contents(Blockonomics::PRICE_URL. "?currency=$currency", false, $context); - $price = json_decode($contents); + $url = Blockonomics::PRICE_URL. "?currency=$currency"; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, 60); + $data = curl_exec($ch); + curl_close($ch); + $price = json_decode($data); return $price->price; } }