AeliaWCEU_VAT_AssistantExchange_Rates_BitPay_Model

Retrieves the exchange rates from BitPay.

Defined (1)

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

/src/lib/classes/exchange_rates/aelia-wc-exchangerates-bitpay.php  
  1. class Exchange_Rates_BitPay_Model extends \Aelia\WC\ExchangeRatesModel { 
  2. // @var string The URL template to use to query BitPay 
  3. private $bitpay_api_rates_url = 'https://bitpay.com/api/rates'; 
  4.  
  5. /** 
  6. * Tranforms the exchange rates received from BitPay into an array of 
  7. * currency code => exchange rate pairs. 
  8. * @param string bitpay_rates The JSON received from BitPay. 
  9. * @retur array 
  10. */ 
  11. protected function decode_rates($bitpay_rates) { 
  12. $exchange_rates = array(); 
  13. foreach($bitpay_rates as $rates_obj) { 
  14. $exchange_rates[$rates_obj->code] = $rates_obj->rate; 
  15.  
  16. return $exchange_rates; 
  17.  
  18. /** 
  19. * Fetches all exchange rates from BitPay API. 
  20. * @return object|bool An object containing the response from Open Exchange, or 
  21. * False in case of failure. 
  22. */ 
  23. private function fetch_all_rates() { 
  24. try { 
  25. $response = \Httpful\Request::get($this->bitpay_api_rates_url) 
  26. ->expectsJson() 
  27. ->send(); 
  28.  
  29. // Debug 
  30. //var_dump("BITPAY RATES RESPONSE:", $response); die(); 
  31. if($response->hasErrors()) { 
  32. // OpenExchangeRates sends error details in response body 
  33. if($response->hasBody()) { 
  34. $response_data = $response->body; 
  35.  
  36. $this->add_error(self::ERR_ERROR_RETURNED,  
  37. sprintf(__('Error returned by BitPay. ' . 
  38. 'Error code: %s. Error message: %s - %s.',  
  39. Definitions::TEXT_DOMAIN),  
  40. $response_data->status,  
  41. $response_data->message,  
  42. $response_data->description)); 
  43. return false; 
  44. return $response->body; 
  45. catch(Exception $e) { 
  46. $this->add_error(self::ERR_EXCEPTION_OCCURRED,  
  47. sprintf(__('Exception occurred while retrieving the exchange rates from BitPay. ' . 
  48. 'Error message: %s.',  
  49. Definitions::TEXT_DOMAIN),  
  50. $e->getMessage())); 
  51. return null; 
  52.  
  53. /** 
  54. * Returns current exchange rates for the specified currency. 
  55. * @param string base_currency The base currency. 
  56. * @return array An array of Currency => Exchange Rate pairs. 
  57. */ 
  58. private function current_rates($base_currency) { 
  59. if(empty($this->_current_rates) || 
  60. $this->_base_currency != $base_currency) { 
  61.  
  62. $cache_key = get_class($this); 
  63. // Try to get the cached rates for the specified base currency, if any 
  64. $this->_current_rates = $this->get_cached_exchange_rates($cache_key); 
  65. if(!empty($this->_current_rates)) { 
  66. return $this->_current_rates; 
  67.  
  68. // Fetch exchange rates 
  69. $bitpay_exchange_rates = $this->fetch_all_rates(); 
  70. if(empty($bitpay_exchange_rates)) { 
  71. return null; 
  72.  
  73. // BitPay rates are returned as JSON representation of an array of objects. 
  74. // We need to transform it into an array of currency => rate pairs 
  75. $exchange_rates = $this->decode_rates($bitpay_exchange_rates); 
  76. if(!is_array($exchange_rates)) { 
  77. $this->add_error(self::ERR_UNEXPECTED_ERROR_FETCHING_EXCHANGE_RATES,  
  78. __('An unexpected error occurred while fetching exchange rates ' . 
  79. 'from BitPay. The most common cause of this issue is the ' . 
  80. 'absence of PHP CURL extension. Please make sure that ' . 
  81. 'PHP CURL is installed and configured in your system.',  
  82. Definitions::TEXT_DOMAIN)); 
  83. return array(); 
  84.  
  85. // Cache the exchange rates 
  86. $this->cache_exchange_rates($cache_key, $exchange_rates); 
  87.  
  88. // Recalculate exchange rates against the base currency we would like to use 
  89. $this->_current_rates = $this->rebase_rates($exchange_rates, $base_currency); 
  90. $this->_base_currency = $base_currency; 
  91. return $this->_current_rates; 
  92.  
  93. /** 
  94. * Recaculates the exchange rates using another base currency. This method 
  95. * is invoked because the rates fetched from BitPay are relative to BitCoin,  
  96. * but another currency is most likely is used by WooCommerce. 
  97. * @param array exchange_rates The exchange rates retrieved from BitPay. 
  98. * @param string base_currency The base currency against which the rates should 
  99. * be recalculated. 
  100. * @return array An array of currency => exchange rate pairs. 
  101. */ 
  102. private function rebase_rates(array $exchange_rates, $base_currency) { 
  103. $recalc_rate = get_value($base_currency, $exchange_rates); 
  104. //var_dump($base_currency, $exchange_rates); 
  105.  
  106. if(empty($recalc_rate)) { 
  107. $this->add_error(self::ERR_BASE_CURRENCY_NOT_FOUND,  
  108. sprintf(__('Could not rebase rates against base currency "%s". ' . 
  109. 'Currency not found in data returned by BitPay.',  
  110. Definitions::TEXT_DOMAIN),  
  111. $base_currency)); 
  112. return null; 
  113.  
  114. $result = array(); 
  115. foreach($exchange_rates as $currency => $rate) { 
  116. $result[$currency] = $rate / $recalc_rate; 
  117.  
  118. //var_dump($result); die(); 
  119.  
  120. return $result; 
  121.  
  122. /** 
  123. * Returns the exchange rate of a currency in respect to a base currency. 
  124. * @param string base_currency The code of the base currency. 
  125. * @param string currency The code of the currency for which to find the 
  126. * Exchange Rate. 
  127. * @return float 
  128. */ 
  129. protected function get_rate($base_currency, $currency) { 
  130. $current_rates = $this->current_rates($base_currency); 
  131. return get_value($currency, $current_rates); 
  132.  
  133. /** 
  134. * Class constructor. 
  135. * @param array An array of Settings that can be used to override the ones 
  136. * currently saved in the configuration. 
  137. * @return Exchange_Rates_BitPay_Model. 
  138. */ 
  139. public function __construct($settings = null) { 
  140. parent::__construct($settings);