0

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;
}

2 Answers 2

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;
}
2
  • Getting this error: syntax error, unexpected { on line 7 of this code. I can't figure out why, everything looks fine.
    – John Tate
    Dec 2, 2014 at 6:30
  • Found the problem.
    – John Tate
    Dec 2, 2014 at 6:35
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.