Advertisement
Guest User

f

a guest
Aug 8th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.27 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2013 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2013 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26.  
  27. if (!defined('_PS_VERSION_'))
  28. exit;
  29.  
  30. /**
  31. * @deprecated : these defines are going to be deleted on 1.6 version of Prestashop
  32. * USE : Configuration::get() method in order to getting the id of order state
  33. */
  34. define('_PS_OS_CRYPTOCURRENCY_', Configuration::get('PS_OS_CRYPTOCURRENCY'));
  35.  
  36. class CryptoCurrency extends PaymentModule
  37. {
  38. private $_html = '';
  39. private $_postErrors = array();
  40.  
  41. public $details;
  42. public $owner;
  43. public $address;
  44. public $id_currency; //new, stores id of current currency used (it's lost otherwise!)
  45. public $extra_mail_vars;
  46. public function __construct()
  47. {
  48. $this->name = 'cryptocurrency';
  49. $this->tab = 'payments_gateways';
  50. $this->version = '1.0.3'; //based on bankwire '0.6'
  51. $this->author = 'PrestaShop & Victor Blanch';
  52.  
  53. $this->currencies = true;
  54. $this->currencies_mode = 'checkbox';
  55.  
  56. $config = Configuration::getMultiple(array
  57. ('CRYPTO_CURRENCY_DETAILS',
  58. 'CRYPTO_CURRENCY_OWNER',
  59. 'CRYPTO_CURRENCY_ADDRESS',
  60. 'CRYPTO_CURRENCY_ID_CURRENCY',
  61. 'CRYPTO_CURRENCY_NAME_CURRENCY',
  62. 'CRYPTO_CURRENCY_UPDATE_BTC',
  63. 'CRYPTO_CURRENCY_UPDATE_STELLAR',
  64. 'CRYPTO_CURRENCY_UPDATE_DOGE'));
  65.  
  66. if (isset($config['CRYPTO_CURRENCY_OWNER']))
  67. $this->owner = $config['CRYPTO_CURRENCY_OWNER'];
  68. if (isset($config['CRYPTO_CURRENCY_DETAILS']))
  69. $this->details = $config['CRYPTO_CURRENCY_DETAILS'];
  70. if (isset($config['CRYPTO_CURRENCY_ADDRESS'])){
  71. //format of addreses string: 1|ADDRESSOFBTC,2|ADDRESSOFLTC ... etc
  72. $sub_addresses = explode(',', $config['CRYPTO_CURRENCY_ADDRESS']);
  73. if(is_array($sub_addresses)){
  74. foreach($sub_addresses as $sub){
  75. $items = explode('|', $sub);
  76. $this->address[$items[0]] = $items[1];
  77. }
  78. }
  79. }
  80.  
  81. parent::__construct();
  82.  
  83. //no context before parent::_constuct()!
  84. $currency = $this->context->currency;
  85.  
  86. //update if there is a valid value in the currency
  87. if(isset($currency->id) && $currency->id!='' && $currency->id!=null && $currency->id!=false){
  88. Configuration::updateValue('CRYPTO_CURRENCY_ID_CURRENCY', $currency->id);
  89. Configuration::updateValue('CRYPTO_CURRENCY_NAME_CURRENCY', $currency->name);
  90. $this->id_currency = $config['CRYPTO_CURRENCY_ID_CURRENCY'];
  91. //save name also so we have it!
  92. $this->name_currency = $config['CRYPTO_CURRENCY_NAME_CURRENCY']; // //DEFINE NAME_CURRENCY
  93. }
  94.  
  95. $this->displayName = $this->l('Cryptocurrency');
  96. $this->description = $this->l('Accept payments for your products via cryptocurrencies.');
  97. $this->confirmUninstall = $this->l('Are you sure about removing these details?');
  98. if (!isset($this->owner) || !isset($this->details) || !isset($this->address))
  99. $this->warning = $this->l('Wallet owner and wallet details must be configured before using this module.');
  100. if (!count(Currency::checkPaymentCurrencies($this->id)))
  101. $this->warning = $this->l('No currency has been set for this module.');
  102.  
  103. $current_address = $this->address[$config['CRYPTO_CURRENCY_ID_CURRENCY']];
  104. $current_currency_name = $config['CRYPTO_CURRENCY_NAME_CURRENCY'];
  105.  
  106. $this->extra_mail_vars = array(
  107. '{wallet_owner}' => Configuration::get('CRYPTO_CURRENCY_OWNER'),
  108. '{wallet_details}' => nl2br(Configuration::get('CRYPTO_CURRENCY_DETAILS')),
  109. '{wallet_currency_name}' => $current_currency_name,
  110. '{wallet_address}' => $current_address
  111. );
  112.  
  113. /* For 1.4.3 and less compatibility */
  114. $updateConfig = array('PS_OS_CHEQUE', 'PS_OS_PAYMENT', 'PS_OS_PREPARATION', 'PS_OS_SHIPPING', 'PS_OS_CANCELED', 'PS_OS_REFUND', 'PS_OS_ERROR', 'PS_OS_OUTOFSTOCK', 'PS_OS_BANKWIRE', 'PS_OS_PAYPAL', 'PS_OS_WS_PAYMENT', 'PS_OS_CRYPTOCURRENCY');
  115. if (!Configuration::get('PS_OS_PAYMENT'))
  116. foreach ($updateConfig as $u)
  117. if (!Configuration::get($u) && defined('_'.$u.'_'))
  118. Configuration::updateValue($u, constant('_'.$u.'_'));
  119.  
  120. }
  121.  
  122. public function install()
  123. {
  124. if (!parent::install()
  125. || !$this->registerHook('payment')
  126. || !$this->registerHook('paymentReturn')
  127. || !$this->registerHook('header'))
  128. return false;
  129.  
  130. //register module in hook table
  131.  
  132. $results_config = Db::getInstance()->executeS('
  133. SELECT *
  134. FROM `'._DB_PREFIX_.'configuration`
  135. WHERE `name`=\'PS_OS_CRYPTOCURRENCY\'');
  136.  
  137. if(!$results_config)
  138. $return1 = Db::getInstance()->Execute('
  139. INSERT IGNORE INTO `'._DB_PREFIX_.'configuration` (`name`, `value`, `date_add`, `date_upd`) VALUES
  140. (\'PS_OS_CRYPTOCURRENCY\', \'14\', NOW(), NOW())');
  141. //14 = cryptos (1-13 are order states)
  142.  
  143. //set color and state for the crypto order state
  144.  
  145. $return2 = Db::getInstance()->Execute('
  146. INSERT IGNORE INTO `'._DB_PREFIX_.'order_state` (`id_order_state`, `invoice`, `send_email`, `module_name`, `color`, `unremovable`, `hidden`, `logable`, `delivery`, `shipped`, `paid`, `deleted`) VALUES
  147. (14, 0, 1, \'cryptocurrency\', \'RoyalBlue\', 1, 0, 0, 0, 0, 0, 0)');
  148.  
  149. $results_langs = Db::getInstance()->executeS('
  150. SELECT DISTINCT `id_lang`
  151. FROM `'._DB_PREFIX_.'order_state_lang`');
  152.  
  153. if ($results_langs)
  154. foreach ($results_langs as $result)
  155. //$compareProducts[] = $result['id_product'];
  156. $return3 = Db::getInstance()->Execute('
  157. INSERT IGNORE INTO `'._DB_PREFIX_.'order_state_lang` (`id_order_state`, `id_lang`, `name`, `template`) VALUES
  158. (14, '.$result['id_lang'].', \'Awaiting cryptocurrency transaction\', \'cryptocurrency\')');
  159.  
  160. //update "condition" table: set the same condition as bankwire or cheque for cryptocurrency
  161. $return2 = Db::getInstance()->Execute('
  162. UPDATE IGNORE `'._DB_PREFIX_.'condition`
  163. SET `request` = REPLACE (
  164. `request`,
  165. \'"bankwire", "cheque"\',
  166. \'"bankwire", "cryptocurrency", "cheque"\')');
  167.  
  168. //TODO: find a way to show warnings in install!
  169. //try copy mail templates in english to /mails/en if possible
  170. $ps_mailspath_en = dirname(__FILE__).'/../../mails/en';
  171. if(is_dir($ps_mailspath_en)){
  172. //try to copy html
  173. $file = dirname(__FILE__).'/mails/en/cryptocurrency.html';
  174. $newfile = dirname(__FILE__).'/../../mails/en/cryptocurrency.html';
  175.  
  176. //copy but dont overwrite files
  177. if(file_exists($newfile)){
  178. $this->warning = $this->l('File ').$newfile.$this->l(' already exists, copy aborted.');
  179. //$this->context->controller->errors[] = 'give an error regardless';
  180. }else{
  181. if (!copy($file, $newfile)) {
  182. $this->warning = $this->l('Failed to copy mail template file ').$newfile.'.';
  183. }
  184. }
  185.  
  186. //try to copy txt
  187. $file = dirname(__FILE__).'/mails/en/cryptocurrency.txt';
  188. $newfile = dirname(__FILE__).'/../../mails/en/cryptocurrency.txt';
  189.  
  190. //copy but dont overwrite files
  191. if(file_exists($newfile)){
  192. $this->warning = $this->l('File ').$newfile.$this->l(' already exists, copy aborted.');
  193. }else{
  194. if (!copy($file, $newfile)) {
  195. $this->warning = $this->l('Failed to copy mail template file ').$newfile.'.';
  196. }
  197. }
  198. }else{
  199. $this->warning = $this->l('Folder ').$ps_mailspath_en.$this->l(' not found, copy of mail templates aborted.');
  200. }
  201.  
  202. //try to copy coins icon
  203. $ps_img_os_dir = dirname(__FILE__).'/../../img/os';
  204. if(is_dir($ps_img_os_dir)){
  205. $file = dirname(__FILE__).'/img/os/14.gif';
  206. $newfile = dirname(__FILE__).'/../../img/os/14.gif';
  207.  
  208. //copy but dont overwrite files
  209. if(file_exists($newfile)){
  210. $this->warning = $this->l('File ').$newfile.$this->l(' already exists, copy aborted.');
  211. //$this->context->controller->errors[] = 'give an error regardless';
  212. }else{
  213. if (!copy($file, $newfile)) {
  214. $this->warning = $this->l('Failed to copy mail template file ').$newfile.'.';
  215. }
  216. }
  217. }
  218.  
  219. return true;
  220. }
  221.  
  222. public function uninstall()
  223. {
  224. if (!Configuration::deleteByName('CRYPTO_CURRENCY_DETAILS')
  225. || !Configuration::deleteByName('CRYPTO_CURRENCY_OWNER')
  226. || !Configuration::deleteByName('CRYPTO_CURRENCY_ADDRESS')
  227. || !Configuration::deleteByName('CRYPTO_CURRENCY_ID_CURRENCY')
  228. || !Configuration::deleteByName('CRYPTO_CURRENCY_NAME_CURRENCY')
  229. || !parent::uninstall())
  230. return false;
  231. return true;
  232. }
  233.  
  234. private function _postValidation()
  235. {
  236. if (Tools::isSubmit('btnSubmit'))
  237. {
  238. if (!Tools::getValue('details'))
  239. $this->_postErrors[] = $this->l('Wallet details are required.');
  240. elseif (!Tools::getValue('owner'))
  241. $this->_postErrors[] = $this->l('Wallet owner is required.');
  242. }
  243. }
  244.  
  245. private function _postProcess()
  246. {
  247. if (Tools::isSubmit('btnSubmit'))
  248. {
  249. Configuration::updateValue('CRYPTO_CURRENCY_DETAILS', Tools::getValue('details'));
  250. Configuration::updateValue('CRYPTO_CURRENCY_OWNER', Tools::getValue('owner'));
  251. Configuration::updateValue('CRYPTO_CURRENCY_UPDATE_BTC', Tools::getValue('update_btc'));
  252. Configuration::updateValue('CRYPTO_CURRENCY_UPDATE_DOGE', Tools::getValue('update_doge'));
  253. Configuration::updateValue('CRYPTO_CURRENCY_UPDATE_STELLAR', Tools::getValue('update_stellar'));
  254.  
  255. $crypto_addresses = array();
  256. $cart = $this->context->cart;
  257. $currencies_module = $this->getCurrency((int)$cart->id_currency);
  258.  
  259. foreach ($currencies_module as $currency_module){
  260. $caddress_id = $currency_module['id_currency'];
  261. $caddress = Tools::getValue('address_'.$currency_module['id_currency']);
  262. $crypto_addresses[]= $caddress_id.'|'.$caddress;
  263. }
  264. Configuration::updateValue('CRYPTO_CURRENCY_ADDRESS', implode(',', $crypto_addresses));
  265. }
  266. $this->_html .= '<div class="conf confirm"> '.$this->l('Settings updated').'</div>';
  267. }
  268.  
  269. private function _displayCryptoCurrency()
  270. {
  271. $this->_html .= '<img src="../modules/cryptocurrency/cryptocurrency.jpg" style="float:left; margin-right:15px;" width="86" height="49"><b>'.$this->l('This module allows you to accept payments by cryptocurrency transactions.').'</b><br /><br />
  272. '.$this->l('If the client chooses to pay with a cryptocurrency transaction, the order\'s status will change to "Waiting for Payment."').'<br />
  273. '.$this->l('That said, you must manually confirm the order upon receiving the cryptocurrency transaction. ').'<br /><br /><br />';
  274. }
  275.  
  276. private function _displayForm()
  277. {
  278. //get currencies attached to the module
  279. $cart = $this->context->cart;
  280. $currencies_module = $this->getCurrency((int)$cart->id_currency);
  281.  
  282. $this->_html .=
  283. '<form action="'.Tools::htmlentitiesUTF8($_SERVER['REQUEST_URI']).'" method="post">
  284. <fieldset>
  285. <legend><img src="../img/admin/contact.gif" />'.$this->l('Contact details').'</legend>
  286. <table border="0" width="500" cellpadding="0" cellspacing="0" id="form">
  287. <tr><td colspan="2">'.$this->l('Please specify the cryptocurrency wallet details for customers').'.<br /><br /></td></tr>
  288. <tr><td width="130" style="height: 35px;">'.$this->l('Wallet owner').'</td><td><input type="text" name="owner" value="'.htmlentities(Tools::getValue('owner', $this->owner), ENT_COMPAT, 'UTF-8').'" style="width: 300px;" /></td></tr>
  289. <tr>
  290. <td width="130" style="vertical-align: top;">'.$this->l('Details').'</td>
  291. <td style="padding-bottom:15px;">
  292. <textarea name="details" rows="4" cols="53">'.htmlentities(Tools::getValue('details', $this->details), ENT_COMPAT, 'UTF-8').'</textarea>
  293. <p class="clear">'.$this->l('Such as additional messages or instructions...').'</p>
  294. </td>
  295. </tr>';
  296.  
  297. foreach ($currencies_module as $currency_module){
  298. $this->_html .='
  299. <tr><td width="130" style="height: 35px;">'.$this->l('Wallet address for').' '.$currency_module['name'].'</td><td><input type="text" name="address_'.$currency_module['id_currency'].'" value="'.htmlentities(Tools::getValue('address_'.$currency_module['id_currency'], $this->address[$currency_module['id_currency']]), ENT_COMPAT, 'UTF-8').'" style="width: 300px;" /></td></tr>
  300. ';
  301. }
  302. $this->_html .='
  303.  
  304. <tr><td colspan="2" align="center"><input class="button" name="btnSubmit" value="'.$this->l('Update settings').'" type="submit" /></td></tr>
  305. </table>
  306. </fieldset>
  307.  
  308. <fieldset>
  309. <legend><img src="../img/admin/cog.gif" />'.$this->l('Conversion rate settings').'</legend>
  310. <table border="0" width="500" cellpadding="0" cellspacing="0" id="form">
  311. <tr><td colspan="2">'.$this->l('Please specify if this module should update automatically the conversion rates of your cryptocurrencies').'.<br /><br /></td></tr>
  312. <tr>
  313. <td width="130" style="vertical-align: top;">'.$this->l('Update Bitcoin conversion rate').'</td>
  314. <td style="padding-bottom:15px;">
  315. <input type="checkbox" name="update_btc" id="update_btc" value="1"'.((Configuration::get('CRYPTO_CURRENCY_UPDATE_BTC')) ? ' checked=""': '').'/>
  316. <p class="clear">'.$this->l('Update will be performed on every page load when cart currency is Bitcoin using https://blockchain.info/ticker last conversion rates. Make sure the ISO code of Bitcoin in your Currencies tab is \'BTC\'').'.</p>
  317. </td>
  318. </tr>
  319. <tr>
  320. <td width="130" style="vertical-align: top;">'.$this->l('Update Dogecoin conversion rate').'</td>
  321. <td style="padding-bottom:15px;">
  322. <input type="checkbox" name="update_doge" id="update_doge" value="1"'.((Configuration::get('CRYPTO_CURRENCY_UPDATE_DOGE')) ? ' checked=""': '').'/>
  323. <p class="clear">'.$this->l('Update will be performed on every page load when cart currency is Dogecoin using http://coinmarketcap-nexuist.rhcloud.com/api/doge last conversion rates. Make sure the ISO code of Dogecoin in your Currencies tab is \'DOGE\'').'.</p>
  324. </td>
  325. </tr>
  326. <tr>
  327. <td width="130" style="vertical-align: top;">'.$this->l('Update Stellar conversion rate').'</td>
  328. <td style="padding-bottom:15px;">
  329. <input type="checkbox" name="update_stellar" id="update_stellar" value="1"'.((Configuration::get('CRYPTO_CURRENCY_UPDATE_STELLAR')) ? ' checked=""': '').'/>
  330. <p class="clear">'.$this->l('Update will be performed on every page load when cart currency is Stellar using http://coinmarketcap-nexuist.rhcloud.com/api/str last conversion rates. Make sure the ISO code of Stellar in your Currencies tab is \'STELLAR\'').'.</p>
  331. </td>
  332. </tr>
  333. <tr><td colspan="2" align="center"><input class="button" name="btnSubmit" value="'.$this->l('Update settings').'" type="submit" /></td></tr>
  334. </table>
  335. </fieldset>
  336.  
  337. <fieldset class="fieldset-donate">
  338. <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Donate').'</legend>
  339. <div>'.$this->l('If you like this module, please consider making a donation to support the author using Paypal, Bitcoin, Litecoin or Dogecoin').':</div>
  340. <div class="margin-form"> </div>
  341. <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank">
  342. <input type="hidden" name="cmd" value="_donations">
  343. <input type="hidden" name="business" value="victor@vblanch.com">
  344. <input type="hidden" name="lc" value="US">
  345. <input type="hidden" name="item_name" value="Hookmanager PS Module">
  346. <input type="hidden" name="no_note" value="0">
  347. <input type="hidden" name="currency_code" value="EUR">
  348. <input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHostedGuest">
  349. <input type="image" src="https://www.paypalobjects.com/es_XC/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal, la forma más segura y rápida de pagar en línea.">
  350. <img alt="" border="0" src="https://www.paypalobjects.com/es_XC/i/scr/pixel.gif" width="1" height="1">
  351. </form>
  352. <div class="margin-form"> </div>
  353. <div class="pay-method">
  354. Bitcoin: 12NZncFaCSv5xE8GCVDFBMaCAoLDDmqhL4
  355. </div>
  356. <div class="margin-form"> </div>
  357. <div class="pay-method">
  358. Litecoin: LYqdGQ9Eu2XCva6kSHWJ3uSTxBivFyfDNM
  359. </div>
  360. <div class="margin-form"> </div>
  361. <div class="pay-method">
  362. Dogecoin: DShCGaE7c9Ur9N29kd7Wfs7xqTCQTKoLAg
  363. </div>
  364. </fieldset>
  365. </form>';
  366. }
  367.  
  368. public function getContent()
  369. {
  370. $this->_html = '<link type="text/css" rel="stylesheet" href="'.$this->_path.'css/styles.css"/>';
  371. $this->_html .= '<h2>'.$this->displayName.'</h2>';
  372.  
  373. if (Tools::isSubmit('btnSubmit'))
  374. {
  375. $this->_postValidation();
  376. if (!count($this->_postErrors))
  377. $this->_postProcess();
  378. else
  379. foreach ($this->_postErrors as $err)
  380. $this->_html .= '<div class="alert error">'.$err.'</div>';
  381. }
  382. else
  383. $this->_html .= '<br />';
  384.  
  385. $this->_displayCryptoCurrency();
  386. $this->_displayForm();
  387.  
  388. return $this->_html;
  389. }
  390.  
  391. public function hookPayment($params)
  392. {
  393. if (!$this->active)
  394. return;
  395. if (!$this->checkCurrency($params['cart']))
  396. return;
  397.  
  398.  
  399. $this->smarty->assign(array(
  400. 'this_path' => $this->_path,
  401. 'this_path_bw' => $this->_path,
  402. 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/'
  403. ));
  404. return $this->display(__FILE__, 'payment.tpl');
  405. }
  406.  
  407. public function hookPaymentReturn($params)
  408. {
  409. if (!$this->active)
  410. return;
  411.  
  412. $state = $params['objOrder']->getCurrentState();
  413. //if ($state == Configuration::get('PS_OS_BANKWIRE') || $state == Configuration::get('PS_OS_OUTOFSTOCK'))
  414. if ($state == Configuration::get('PS_OS_CRYPTOCURRENCY') || $state == Configuration::get('PS_OS_OUTOFSTOCK'))
  415. {
  416. $currency = $this->context->currency;
  417. $this->smarty->assign(array(
  418. 'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false),
  419. 'cryptocurrencyDetails' => Tools::nl2br($this->details),
  420. 'cryptocurrencyName' => Tools::nl2br($currency->name),
  421. 'cryptocurrencyAddress' => Tools::nl2br($this->address[$currency->id]),
  422. 'cryptocurrencyOwner' => $this->owner,
  423. 'status' => 'ok',
  424. 'id_order' => $params['objOrder']->id
  425. ));
  426. if (isset($params['objOrder']->reference) && !empty($params['objOrder']->reference))
  427. $this->smarty->assign('reference', $params['objOrder']->reference);
  428. }
  429. else
  430. $this->smarty->assign('status', 'failed');
  431. return $this->display(__FILE__, 'payment_return.tpl');
  432. }
  433.  
  434. public function checkCurrency($cart)
  435. {
  436. $currency_order = new Currency($cart->id_currency);
  437. $currencies_module = $this->getCurrency($cart->id_currency);
  438.  
  439. if (is_array($currencies_module))
  440. foreach ($currencies_module as $currency_module)
  441. if ($currency_order->id == $currency_module['id_currency'])
  442. return true;
  443. return false;
  444. }
  445.  
  446. //update btc conversion rate if the user loads a page and has btc as current currency
  447. public function hookHeader($params){
  448. if(Configuration::get('CRYPTO_CURRENCY_UPDATE_DOGE')){
  449. $doge_code = 'DOGE';
  450. $cart_currency = $this->context->cart->id_currency;
  451.  
  452. $doge_currency_id = Db::getInstance()->executeS('
  453. SELECT `id_currency`
  454. FROM `'._DB_PREFIX_.'currency`
  455. WHERE `iso_code`=\''.$doge_code.'\'');
  456.  
  457. if($doge_currency_id && $doge_currency_id[0]['id_currency']==$cart_currency){
  458. //some code snippets taken from bitcointicker module
  459. $default_currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  460. $response = file_get_contents('http://coinmarketcap-nexuist.rhcloud.com/api/doge');
  461. $object = json_decode($response);
  462.  
  463. if(is_object($object)){
  464. $curr = strtolower($default_currency->iso_code);//'USD';
  465. //$rate = $object->$curr->{'last'};
  466. $rate = $object->{'price'}->$curr;
  467. $rate_default = 1.0/floatval($rate);
  468. $rate_default_str = trim(sprintf("%.6f", $rate_default));
  469.  
  470. //update conversion_rate on currency table
  471. $query='UPDATE IGNORE `'._DB_PREFIX_.'currency`
  472. SET `conversion_rate` = \''.$rate_default_str.'\'
  473. WHERE `iso_code` = \''.$doge_code.'\'';
  474.  
  475. //print_r($query);
  476. $return = Db::getInstance()->Execute($query);
  477.  
  478. //update conversion_rate in currency_shop table too
  479. $curr_id = strval($doge_currency_id[0]['id_currency']);
  480. $query2='UPDATE IGNORE `'._DB_PREFIX_.'currency_shop`
  481. SET `conversion_rate` = \''.$rate_default_str.'\'
  482. WHERE `id_currency`=\''.$curr_id.'\'';
  483.  
  484. $return = Db::getInstance()->Execute($query2);
  485. }
  486. }
  487. }
  488.  
  489. if(Configuration::get('CRYPTO_CURRENCY_UPDATE_BTC')){
  490.  
  491. $btc_code = 'BTC';
  492. $cart_currency = $this->context->cart->id_currency;
  493.  
  494. $btc_currency_id = Db::getInstance()->executeS('
  495. SELECT `id_currency`
  496. FROM `'._DB_PREFIX_.'currency`
  497. WHERE `iso_code`=\''.$btc_code.'\'');
  498.  
  499. if($btc_currency_id && $btc_currency_id[0]['id_currency']==$cart_currency){
  500. //some code snippets taken from bitcointicker module
  501. //print_r("updating...");
  502. $default_currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  503. $response = file_get_contents('https://blockchain.info/ticker');
  504. $object = json_decode($response);
  505.  
  506. if(is_object($object)){
  507. $curr = $default_currency->iso_code;//'USD';
  508. $rate = $object->$curr->{'last'};
  509. $rate_default = 1.0/floatval($rate);
  510. $rate_default_str = trim(sprintf("%.6f", $rate_default));
  511.  
  512. //update conversion_rate on currency table
  513. $query='UPDATE IGNORE `'._DB_PREFIX_.'currency`
  514. SET `conversion_rate` = \''.$rate_default_str.'\'
  515. WHERE `iso_code` = \''.$btc_code.'\'';
  516.  
  517. //print_r($query);
  518. $return = Db::getInstance()->Execute($query);
  519.  
  520. //update conversion_rate in currency_shop table too
  521. $curr_id = strval($btc_currency_id[0]['id_currency']);
  522. $query2='UPDATE IGNORE `'._DB_PREFIX_.'currency_shop`
  523. SET `conversion_rate` = \''.$rate_default_str.'\'
  524. WHERE `id_currency`=\''.$curr_id.'\'';
  525.  
  526. $return = Db::getInstance()->Execute($query2);
  527. }
  528. }
  529. }
  530. return;
  531. }
  532. }
  533. //update btc conversion rate if the user loads a page and has btc as current currency
  534. public function hookHeader($params){
  535. if(Configuration::get('CRYPTO_CURRENCY_UPDATE_STELLAR')){
  536. $stellar_code = 'STELLAR';
  537. $cart_currency = $this->context->cart->id_currency;
  538.  
  539. $stellar_currency_id = Db::getInstance()->executeS('
  540. SELECT `id_currency`
  541. FROM `'._DB_PREFIX_.'currency`
  542. WHERE `iso_code`=\''.$stellar_code.'\'');
  543.  
  544. if($stellar_currency_id && $stellar_currency_id[0]['id_currency']==$cart_currency){
  545. //some code snippets taken from bitcointicker module
  546. $default_currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  547. $response = file_get_contents('http://coinmarketcap-nexuist.rhcloud.com/api/str');
  548. $object = json_decode($response);
  549.  
  550. if(is_object($object)){
  551. $curr = strtolower($default_currency->iso_code);//'USD';
  552. //$rate = $object->$curr->{'last'};
  553. $rate = $object->{'price'}->$curr;
  554. $rate_default = 1.0/floatval($rate);
  555. $rate_default_str = trim(sprintf("%.6f", $rate_default));
  556.  
  557. //update conversion_rate on currency table
  558. $query='UPDATE IGNORE `'._DB_PREFIX_.'currency`
  559. SET `conversion_rate` = \''.$rate_default_str.'\'
  560. WHERE `iso_code` = \''.$stellar_code.'\'';
  561.  
  562. //print_r($query);
  563. $return = Db::getInstance()->Execute($query);
  564.  
  565. //update conversion_rate in currency_shop table too
  566. $curr_id = strval($stellar_currency_id[0]['id_currency']);
  567. $query2='UPDATE IGNORE `'._DB_PREFIX_.'currency_shop`
  568. SET `conversion_rate` = \''.$rate_default_str.'\'
  569. WHERE `id_currency`=\''.$curr_id.'\'';
  570.  
  571. $return = Db::getInstance()->Execute($query2);
  572. }
  573. }
  574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement