コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
ファイル: Client.php プロジェクト: siparker/gocardless-whmcs
 /**
  * Send an HTTP request to confirm the creation of a new payment resource
  *
  * @param array $params Parameters to send with the request
  *
  * @return string The result of the HTTP request
  */
 public function confirm_resource($params)
 {
     // Define confirm endpoint
     $endpoint = '/confirm';
     // First validate signature
     // Then send confirm request
     // List of required params
     $required_params = array('resource_id', 'resource_type');
     // Loop through required params
     // Add to $data or throw exception if missing
     foreach ($required_params as $key => $value) {
         if (!isset($params[$value])) {
             throw new GoCardless_ArgumentsException("{$value} missing");
         }
         $data[$value] = $params[$value];
     }
     // state is optional
     if (isset($params['state'])) {
         $data['state'] = $params['state'];
     }
     // resource_uri is optional
     if (isset($params['resource_uri'])) {
         $data['resource_uri'] = $params['resource_uri'];
     }
     $sig_validation_data = array('data' => $data, 'secret' => $this->account_details['app_secret'], 'signature' => $params['signature']);
     if ($this->validate_signature($sig_validation_data) == false) {
         throw new GoCardless_SignatureException();
     }
     // Sig valid, now send confirm request
     $confirm_params = array('resource_id' => $params['resource_id'], 'resource_type' => $params['resource_type']);
     // Use HTTP Basic Authorization
     $confirm_params['http_authorization'] = $this->account_details['app_id'] . ':' . $this->account_details['app_secret'];
     // If no method-specific redirect sent, use class level if available
     if (!isset($params['redirect_uri']) && isset($this->redirect_uri)) {
         $confirm_params['redirect_uri'] = $this->redirect_uri;
     }
     // Do query
     $response = $this->request('post', $endpoint, $confirm_params);
     if ($response['success'] == true) {
         $endpoint = '/' . $params['resource_type'] . 's/' . $params['resource_id'];
         $class_name = 'GoCardless_' . GoCardless_Utils::camelize($params['resource_type']);
         return new $class_name($this, $this->request('get', $endpoint));
     } else {
         throw new GoCardless_ClientException('Failed to fetch the confirmed
     resource.');
     }
 }