public static function generate_query_string($params, &$pairs = array(),
    $namespace = null) {

    if (is_array($params)) {

      foreach ($params as $k => $v) {

        if (is_int($k)) {
          GoCardless_Utils::generate_query_string($v, $pairs, $namespace .
            '[]');
        } else {
          GoCardless_Utils::generate_query_string($v, $pairs,
            $namespace !== null ? $namespace . "[$k]" : $k);
        }

      }

      if ($namespace !== null) {
        return $pairs;
      }

      if (empty($pairs)) {
        return '';
      }

      usort($pairs, array(__CLASS__, 'sortPairs'));

      $strs = array();
      foreach ($pairs as $pair) {
        $strs[] = $pair[0] . '=' . $pair[1];
      }

      return implode('&', $strs);

    } else {

      $pairs[] = array(rawurlencode($namespace), rawurlencode($params));

    }

  }