/** * Fetch an object's subresource from the API * * @param string $type The subresource to fetch * @param array $params The params for the subresource query * * @return object The subresource object */ public function fetch_sub_resource($type, $params = array()) { // Generate subresource endpoint by snipping out the // right part of the sub_resource_uri $endpoint = preg_replace('/api\\/v[0-9]+\\//', '', parse_url($this->sub_resource_uris[$type], PHP_URL_PATH)); $sub_resource_params = array(); // Extract params from subresource uri if available and create // sub_resource_params array if ($param_string = parse_url($this->sub_resource_uris[$type], PHP_URL_QUERY)) { $split_params = explode('&', $param_string); foreach ($split_params as $split_param) { $parts = explode('=', $split_param); $sub_resource_params[$parts[0]] = $parts[1]; } } // Overwrite params from subresource uri with passed params, if found $params = array_merge($params, $sub_resource_params); // Get class name $class = 'GoCardless_' . GoCardless_Utils::camelize(GoCardless_Utils::singularize($type)); $objects = array(); // Create an array of objects foreach ($this->client->request('get', $endpoint, $params) as $value) { $objects[] = new $class($this->client, $value); } // Return the array of objects return $objects; }
/** * Generates, encodes, re-orders variables for the query string. * * @param array $params The specific parameters for this payment * @param array $pairs Pairs * @param string $namespace The namespace * * @return string An encoded string of parameters */ 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)); } }
/** * Confirm whether a signature is valid * * @param array $params Should include data, secret and signature * * @return boolean True or false */ public function validate_signature($params) { $new_sig = GoCardless_Utils::generate_signature($params['data'], $params['secret']); return $new_sig === $params['signature']; }