Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static methods for accessing nested resources #383

Merged
merged 1 commit into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
*/
class Account extends ApiResource
{
const PATH_EXTERNAL_ACCOUNTS = '/external_accounts';
const PATH_LOGIN_LINKS = '/login_links';

public function instanceUrl()
{
if ($this['id'] === null) {
Expand Down Expand Up @@ -131,6 +134,12 @@ public static function all($params = null, $opts = null)
return self::_all($params, $opts);
}

/**
* @param array|null $clientId
* @param array|string|null $opts
*
* @return StripeObject Object containing the response from the API.
*/
public function deauthorize($clientId = null, $opts = null)
{
$params = array(
Expand All @@ -139,4 +148,79 @@ public function deauthorize($clientId = null, $opts = null)
);
OAuth::deauthorize($params, $opts);
}

/**
* @param array|null $id The ID of the account on which to create the external account.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function createExternalAccount($id, $params = null, $opts = null)
{
return self::_createNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $params, $opts);
}

/**
* @param array|null $id The ID of the account to which the external account belongs.
* @param array|null $externalAccountId The ID of the external account to retrieve.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function retrieveExternalAccount($id, $externalAccountId, $params = null, $opts = null)
{
return self::_retrieveNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
}

/**
* @param array|null $id The ID of the account to which the external account belongs.
* @param array|null $externalAccountId The ID of the external account to update.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function updateExternalAccount($id, $externalAccountId, $params = null, $opts = null)
{
return self::_updateNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
}

/**
* @param array|null $id The ID of the account to which the external account belongs.
* @param array|null $externalAccountId The ID of the external account to delete.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function deleteExternalAccount($id, $externalAccountId, $params = null, $opts = null)
{
return self::_deleteNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
}

/**
* @param array|null $id The ID of the account on which to retrieve the external accounts.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function allExternalAccounts($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_EXTERNAL_ACCOUNTS, $params, $opts);
}

/**
* @param array|null $id The ID of the account on which to create the login link.
* @param array|null $params
* @param array|string|null $opts
*
* @return LoginLink
*/
public static function createLoginLink($id, $params = null, $opts = null)
{
return self::_createNestedResource($id, static::PATH_LOGIN_LINKS, $params, $opts);
}
}
104 changes: 104 additions & 0 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,108 @@ protected function _delete($params = null, $options = null)
$this->refreshFrom($response, $opts);
return $this;
}

/**
* @param string $method
* @param string $url
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
*/
protected static function _nestedResourceOperation($method, $url, $params = null, $options = null)
{
self::_validateParams($params);

list($response, $opts) = static::_staticRequest($method, $url, $params, $options);
$obj = Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}

/**
* @param string $id
* @param string $nestedPath
* @param string|null $nestedId
*
* @return string
*/
protected static function _nestedResourceUrl($id, $nestedPath, $nestedId = null)
{
$url = static::resourceUrl($id) . $nestedPath;
if ($nestedId !== null) {
$url .= "/$nestedId";
}
return $url;
}

/**
* @param string $id
* @param string $nestedPath
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
*/
protected static function _createNestedResource($id, $nestedPath, $params = null, $options = null)
{
$url = static::_nestedResourceUrl($id, $nestedPath);
return self::_nestedResourceOperation('post', $url, $params, $options);
}

/**
* @param string $id
* @param string $nestedPath
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
*/
protected static function _retrieveNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
{
$url = static::_nestedResourceUrl($id, $nestedPath, $nestedId);
return self::_nestedResourceOperation('get', $url, $params, $options);
}

/**
* @param string $id
* @param string $nestedPath
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
*/
protected static function _updateNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
{
$url = static::_nestedResourceUrl($id, $nestedPath, $nestedId);
return self::_nestedResourceOperation('post', $url, $params, $options);
}

/**
* @param string $id
* @param string $nestedPath
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
*/
protected static function _deleteNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null)
{
$url = static::_nestedResourceUrl($id, $nestedPath, $nestedId);
return self::_nestedResourceOperation('delete', $url, $params, $options);
}

/**
* @param string $id
* @param string $nestedPath
* @param array|null $params
* @param array|string|null $options
*
* @return StripeObject
*/
protected static function _allNestedResources($id, $nestedPath, $params = null, $options = null)
{
$url = static::_nestedResourceUrl($id, $nestedPath);
return self::_nestedResourceOperation('get', $url, $params, $options);
}
}
52 changes: 52 additions & 0 deletions lib/ApplicationFee.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class ApplicationFee extends ApiResource
{
const PATH_REFUNDS = '/refunds';

/**
* This is a special case because the application fee endpoint has an
* underscore in it. The parent `className` function strips underscores.
Expand Down Expand Up @@ -67,4 +69,54 @@ public function refund($params = null, $opts = null)
$this->refresh();
return $this;
}

/**
* @param array|null $id The ID of the application fee on which to create the refund.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApplicationFeeRefund
*/
public static function createRefund($id, $params = null, $opts = null)
{
return self::_createNestedResource($id, static::PATH_REFUNDS, $params, $opts);
}

/**
* @param array|null $id The ID of the application fee to which the refund belongs.
* @param array|null $refundId The ID of the refund to retrieve.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApplicationFeeRefund
*/
public static function retrieveRefund($id, $refundId, $params = null, $opts = null)
{
return self::_retrieveNestedResource($id, static::PATH_REFUNDS, $refundId, $params, $opts);
}

/**
* @param array|null $id The ID of the application fee to which the refund belongs.
* @param array|null $refundId The ID of the refund to update.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApplicationFeeRefund
*/
public static function updateRefund($id, $refundId, $params = null, $opts = null)
{
return self::_updateNestedResource($id, static::PATH_REFUNDS, $refundId, $params, $opts);
}

/**
* @param array|null $id The ID of the application fee on which to retrieve the refunds.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApplicationFeeRefund
*/
public static function allRefunds($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_REFUNDS, $params, $opts);
}
}
65 changes: 65 additions & 0 deletions lib/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
class Customer extends ApiResource
{
const PATH_SOURCES = '/sources';

/**
* @param array|string $id The ID of the customer to retrieve, or an
* options array containing an `id` key.
Expand Down Expand Up @@ -188,4 +190,67 @@ public function deleteDiscount()
list($response, $opts) = $this->_request('delete', $url);
$this->refreshFrom(array('discount' => null), $opts, true);
}

/**
* @param array|null $id The ID of the customer on which to create the source.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function createSource($id, $params = null, $opts = null)
{
return self::_createNestedResource($id, static::PATH_SOURCES, $params, $opts);
}

/**
* @param array|null $id The ID of the customer to which the source belongs.
* @param array|null $sourceId The ID of the source to retrieve.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function retrieveSource($id, $sourceId, $params = null, $opts = null)
{
return self::_retrieveNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
}

/**
* @param array|null $id The ID of the customer to which the source belongs.
* @param array|null $sourceId The ID of the source to update.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function updateSource($id, $sourceId, $params = null, $opts = null)
{
return self::_updateNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
}

/**
* @param array|null $id The ID of the customer to which the source belongs.
* @param array|null $sourceId The ID of the source to delete.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function deleteSource($id, $sourceId, $params = null, $opts = null)
{
return self::_deleteNestedResource($id, static::PATH_SOURCES, $sourceId, $params, $opts);
}

/**
* @param array|null $id The ID of the customer on which to retrieve the sources.
* @param array|null $params
* @param array|string|null $opts
*
* @return ExternalAccount
*/
public static function allSources($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_SOURCES, $params, $opts);
}
}
Loading