<p>I'm not terribly familiar with drupal, and I've never used the module you're talking about, but I'll take a shot at it:</p>

<pre><code>function uc_bitcoin_exchange_rate($order) {
  static $data;
  $rate = FALSE;
  $currency = $order-&gt;currency;
  $currency_code = 'btc_to_' . strtolower($currency);

  if ($currency == 'BTC') {
    // BTC 1:1
    return 1;
  }
  if (!isset($data) {
    if ($response = drupal_http_request('https://api.coinbase.com/v1/currencies/exchange_rates')) {
      if ($response-&gt;code == 200) {
        $data = json_decode($response-&gt;data);
        $rate = $data[$currency_code];
      }
    }
  }

  if (!is_numeric($rate)) {
    watchdog('uc_bitcoin', 'CoinBase lookup - Unable to get current exchange rate', WATCHDOG_ERROR);
    $rate = FALSE; // just to be super sure.
  }

  return $rate;
}
</code></pre>

CC BY-SA 3.0
2014-12-02T06:30:00.747Z
Getting this error: `syntax error, unexpected {` on line 7 of this code. I can't figure out why, everything looks fine.
John Tate
CC BY-SA 3.0
2014-12-02T06:35:39.320Z
Found the problem.
John Tate
CC BY-SA 3.0
2014-11-25T19:49:15.747Z
2014-11-25T19:55:27.033Z
2014-11-25T19:55:27.033Z
Nick ODell
<p>Does this help you?</p>
<p><a href="https://dgtlmoon.com/how_configuring_drupals_ubercart_bitcoin_payment_module" rel="nofollow noreferrer">https://dgtlmoon.com/how_configuring_drupals_ubercart_bitcoin_payment_module</a></p>
<p>unfortunately it needs to communicate with a real 'bitcoind' instance via JSON/XMLRPC, I'm not sure if blockchain.info will provide this?</p>

CC BY-SA 4.0
2022-01-07T13:36:17.127Z
2022-01-07T13:36:17.127Z
Allen Moss
<p>I need a simple hack to change the Drupal <code>uc_bitcoin</code> modules code so it gets the exchange rate from Coinbase, it is designed to use MtGox. I want to avoid using the entire coinbase API because I just want the exchange rate, not the kitchen sink. There is a lot of documentation there.</p>

<p>I have found the code in the <code>uc_bitcoin</code> drupal module which gets the information from MtGox. It needs some simple changes so it gets this data from Coinbase. Since both are so different, and I'm not fluent with PHP I thought someone might be able to help me with this.</p>

<p>Here is the code which gets the exchange rate from MtGox:</p>

<pre><code>function uc_bitcoin_exchange_rate($order) {

  static $rate;

  if(isset($rate)) {
    return $rate;
  }

  if ($order-&gt;currency == 'BTC') {
    // BTC 1:1
    return 1;
  }

  // Supported MtGox currencies, Dec 15 2012
  $mtGox_currencies = array(
    'USD',
    'EUR',
    'JPY',
    'CAD',
    'GBP',
    'CHF',
    'RUB',
    'AUD',
    'SEK',
    'DKK',
    'HKD',
    'PLN',
    'CNY',
    'SGD',
    'THB',
    'NZD',
    'NOK'
  );

  // @todo add cache
  if (in_array($order-&gt;currency, $mtGox_currencies)) {
    if ($response = drupal_http_request('https://data.mtgox.com/api/1/BTC' . $order-&gt;currency . '/ticker')) { 
      if ($response-&gt;code == 200) {
        $data = json_decode($response-&gt;data);
        if(!empty($data-&gt;return-&gt;avg-&gt;value)) {
          $rate = $data-&gt;return-&gt;avg-&gt;value;
        }
      }
    }
  }

// @todo add cache
  if (in_array($order-&gt;currency, $mtGox_currencies)) {
    if ($response = drupal_http_request('https://data.mtgox.com/api/1/BTC' . $order-&gt;currency . '/ticker')) { 
      if ($response-&gt;code == 200) {
        $data = json_decode($response-&gt;data);
        if(!empty($data-&gt;return-&gt;avg-&gt;value)) {
          $rate = $data-&gt;return-&gt;avg-&gt;value;
        }
      }
    }
  }

  if (!$rate || !is_numeric($rate)) {
    watchdog('uc_bitcoin', 'MtGox lookup - Unable to get current exchange rate', WATCHDOG_ERROR);
    $rate = FALSE; // just to be super sure.
  }

  return $rate;
}
</code></pre>

<exchange-rate><php>
using coinbase with uc_bitcoin drupal module
John Tate
# using coinbase with uc_bitcoin drupal module

(Asked by: username_0 on 2014-11-25)

I need a simple hack to change the Drupal uc_bitcoin modules code so it gets the exchange rate from Coinbase, it is designed to use MtGox. I want to avoid using the entire coinbase API because I just want the exchange rate, not the kitchen sink. There is a lot of documentation there.
I have found the code in the uc_bitcoin drupal module which gets the information from MtGox. It needs some simple changes so it gets this data from Coinbase. Since both are so different, and I'm not fluent with PHP I thought someone might be able to help me with this.
Here is the code which gets the exchange rate from MtGox:
function uc_bitcoin_exchange_rate($order) {

  static $rate;

  if(isset($rate)) {
    return $rate;
  }

  if ($order->currency == 'BTC') {
    // BTC 1:1
    return 1;
  }

  // Supported MtGox currencies, Dec 15 2012
  $mtGox_currencies = array(
    'USD',
    'EUR',
    'JPY',
    'CAD',
    'GBP',
    'CHF',
    'RUB',
    'AUD',
    'SEK',
    'DKK',
    'HKD',
    'PLN',
    'CNY',
    'SGD',
    'THB',
    'NZD',
    'NOK'
  );

  // @todo add cache
  if (in_array($order->currency, $mtGox_currencies)) {
    if ($response = drupal_http_request('https://data.mtgox.com/api/1/BTC' . $order->currency . '/ticker')) { 
      if ($response->code == 200) {
        $data = json_decode($response->data);
        if(!empty($data->return->avg->value)) {
          $rate = $data->return->avg->value;
        }
      }
    }
  }

// @todo add cache
  if (in_array($order->currency, $mtGox_currencies)) {
    if ($response = drupal_http_request('https://data.mtgox.com/api/1/BTC' . $order->currency . '/ticker')) { 
      if ($response->code == 200) {
        $data = json_decode($response->data);
        if(!empty($data->return->avg->value)) {
          $rate = $data->return->avg->value;
        }
      }
    }
  }

  if (!$rate || !is_numeric($rate)) {
    watchdog('uc_bitcoin', 'MtGox lookup - Unable to get current exchange rate', WATCHDOG_ERROR);
    $rate = FALSE; // just to be super sure.
  }

  return $rate;
}

**Tags:** exchange-rate, php


---

## 2 answers

### Answer by username_1 on 2014-11-25. Score: 1

I'm not terribly familiar with drupal, and I've never used the module you're talking about, but I'll take a shot at it:
function uc_bitcoin_exchange_rate($order) {
  static $data;
  $rate = FALSE;
  $currency = $order->currency;
  $currency_code = 'btc_to_' . strtolower($currency);

  if ($currency == 'BTC') {
    // BTC 1:1
    return 1;
  }
  if (!isset($data) {
    if ($response = drupal_http_request('https://api.coinbase.com/v1/currencies/exchange_rates')) {
      if ($response->code == 200) {
        $data = json_decode($response->data);
        $rate = $data[$currency_code];
      }
    }
  }

  if (!is_numeric($rate)) {
    watchdog('uc_bitcoin', 'CoinBase lookup - Unable to get current exchange rate', WATCHDOG_ERROR);
    $rate = FALSE; // just to be super sure.
  }

  return $rate;
}

> **Comments:**
> **username_0**: Getting this error: `syntax error, unexpected {` on line 7 of this code. I can't figure out why, everything looks fine.  
> **username_0**: Found the problem.  

---

### Answer by username_2 on 2022-01-07. Score: 0

Does this help you?

<https://dgtlmoon.com/how_configuring_drupals_ubercart_bitcoin_payment_module>

unfortunately it needs to communicate with a real 'bitcoind' instance via JSON/XMLRPC, I'm not sure if blockchain.info will provide this?


---

