dolibarr/htdocs/includes/stripe/lib/Account.php

394 lines
13 KiB
PHP
Raw Normal View History

2017-04-18 05:44:08 +02:00
<?php
namespace Stripe;
/**
* Class Account
*
* @property string $id
* @property string $object
2019-05-03 02:22:27 +02:00
* @property mixed $business_profile
* @property string $business_type
* @property mixed $capabilities
2017-04-18 05:44:08 +02:00
* @property bool $charges_enabled
2019-05-03 02:22:27 +02:00
* @property mixed $company
2017-04-18 05:44:08 +02:00
* @property string $country
2018-03-06 13:52:56 +01:00
* @property int $created
2017-04-18 05:44:08 +02:00
* @property string $default_currency
* @property bool $details_submitted
* @property string $email
2019-05-03 02:22:27 +02:00
* @property Collection $external_accounts
* @property mixed $individual
2018-03-06 13:52:56 +01:00
* @property StripeObject $metadata
2017-04-18 05:44:08 +02:00
* @property bool $payouts_enabled
2019-05-03 02:22:27 +02:00
* @property mixed $requirements
* @property mixed $settings
2017-04-18 05:44:08 +02:00
* @property mixed $tos_acceptance
2019-05-03 02:22:27 +02:00
* @property string $type
2017-04-18 05:44:08 +02:00
*
* @package Stripe
*/
class Account extends ApiResource
{
2019-05-03 02:22:27 +02:00
const OBJECT_NAME = "account";
2018-03-06 13:52:56 +01:00
use ApiOperations\All;
use ApiOperations\Create;
use ApiOperations\Delete;
use ApiOperations\NestedResource;
use ApiOperations\Retrieve {
retrieve as protected _retrieve;
}
use ApiOperations\Update;
2019-05-03 02:22:27 +02:00
/**
* Possible string representations of an account's business type.
* @link https://stripe.com/docs/api/accounts/object#account_object-business_type
*/
const BUSINESS_TYPE_COMPANY = 'company';
const BUSINESS_TYPE_INDIVIDUAL = 'individual';
/**
* Possible string representations of an account's capabilities.
* @link https://stripe.com/docs/api/accounts/object#account_object-capabilities
*/
const CAPABILITY_CARD_PAYMENTS = 'card_payments';
const CAPABILITY_LEGACY_PAYMENTS = 'legacy_payments';
const CAPABILITY_PLATFORM_PAYMENTS = 'platform_payments';
/**
* Possible string representations of an account's capability status.
* @link https://stripe.com/docs/api/accounts/object#account_object-capabilities
*/
const CAPABILITY_STATUS_ACTIVE = 'active';
const CAPABILITY_STATUS_INACTIVE = 'inactive';
const CAPABILITY_STATUS_PENDING = 'pending';
/**
* Possible string representations of an account's type.
* @link https://stripe.com/docs/api/accounts/object#account_object-type
*/
const TYPE_CUSTOM = 'custom';
const TYPE_EXPRESS = 'express';
const TYPE_STANDARD = 'standard';
2018-03-06 13:52:56 +01:00
public static function getSavedNestedResources()
{
static $savedNestedResources = null;
if ($savedNestedResources === null) {
$savedNestedResources = new Util\Set([
'external_account',
'bank_account',
]);
}
return $savedNestedResources;
}
2019-05-16 21:41:14 +02:00
const PATH_CAPABILITIES = '/capabilities';
2018-03-06 13:52:56 +01:00
const PATH_EXTERNAL_ACCOUNTS = '/external_accounts';
const PATH_LOGIN_LINKS = '/login_links';
2019-05-03 02:22:27 +02:00
const PATH_PERSONS = '/persons';
2018-03-06 13:52:56 +01:00
2017-04-18 05:44:08 +02:00
public function instanceUrl()
{
if ($this['id'] === null) {
return '/v1/account';
} else {
return parent::instanceUrl();
}
}
/**
2018-03-06 13:52:56 +01:00
* @param array|string|null $id The ID of the account to retrieve, or an
* options array containing an `id` key.
2017-04-18 05:44:08 +02:00
* @param array|string|null $opts
*
* @return Account
*/
public static function retrieve($id = null, $opts = null)
{
if (!$opts && is_string($id) && substr($id, 0, 3) === 'sk_') {
$opts = $id;
$id = null;
}
return self::_retrieve($id, $opts);
}
/**
* @param array|null $params
* @param array|string|null $opts
*
2018-03-06 13:52:56 +01:00
* @return Account The rejected account.
*/
public function reject($params = null, $opts = null)
{
$url = $this->instanceUrl() . '/reject';
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
return $this;
}
/**
* @param array|null $clientId
* @param array|string|null $opts
*
* @return StripeObject Object containing the response from the API.
2017-04-18 05:44:08 +02:00
*/
2018-03-06 13:52:56 +01:00
public function deauthorize($clientId = null, $opts = null)
2017-04-18 05:44:08 +02:00
{
2018-03-06 13:52:56 +01:00
$params = [
'client_id' => $clientId,
'stripe_user_id' => $this->id,
];
2019-05-03 02:22:27 +02:00
return OAuth::deauthorize($params, $opts);
2017-04-18 05:44:08 +02:00
}
2019-05-16 21:41:14 +02:00
/*
* Capabilities methods
* We can not add the capabilities() method today as the Account object already has a
* capabilities property which is a hash and not the sub-list of capabilities.
*/
/**
* @param string $id The ID of the account to which the capability belongs.
* @param string $capabilityId The ID of the capability to retrieve.
2019-05-16 21:41:14 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Capability
*/
public static function retrieveCapability($id, $capabilityId, $params = null, $opts = null)
{
return self::_retrieveNestedResource($id, static::PATH_CAPABILITIES, $capabilityId, $params, $opts);
}
/**
* @param string $id The ID of the account to which the capability belongs.
* @param string $capabilityId The ID of the capability to update.
2019-05-16 21:41:14 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Capability
*/
public static function updateCapability($id, $capabilityId, $params = null, $opts = null)
{
return self::_updateNestedResource($id, static::PATH_CAPABILITIES, $capabilityId, $params, $opts);
}
/**
* @param string $id The ID of the account on which to retrieve the capabilities.
2019-05-16 21:41:14 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection The list of capabilities.
*/
public static function allCapabilities($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_CAPABILITIES, $params, $opts);
}
2017-04-18 05:44:08 +02:00
/**
* @param string $id The ID of the account on which to create the external account.
2017-04-18 05:44:08 +02:00
* @param array|null $params
2018-03-06 13:52:56 +01:00
* @param array|string|null $opts
2017-04-18 05:44:08 +02:00
*
2018-03-06 13:52:56 +01:00
* @return BankAccount|Card
2017-04-18 05:44:08 +02:00
*/
2018-03-06 13:52:56 +01:00
public static function createExternalAccount($id, $params = null, $opts = null)
2017-04-18 05:44:08 +02:00
{
2018-03-06 13:52:56 +01:00
return self::_createNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $params, $opts);
2017-04-18 05:44:08 +02:00
}
/**
* @param string $id The ID of the account to which the external account belongs.
* @param string $externalAccountId The ID of the external account to retrieve.
2018-03-06 13:52:56 +01:00
* @param array|null $params
2017-04-18 05:44:08 +02:00
* @param array|string|null $opts
*
2018-03-06 13:52:56 +01:00
* @return BankAccount|Card
2017-04-18 05:44:08 +02:00
*/
2018-03-06 13:52:56 +01:00
public static function retrieveExternalAccount($id, $externalAccountId, $params = null, $opts = null)
2017-04-18 05:44:08 +02:00
{
2018-03-06 13:52:56 +01:00
return self::_retrieveNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
2017-04-18 05:44:08 +02:00
}
/**
* @param string $id The ID of the account to which the external account belongs.
* @param string $externalAccountId The ID of the external account to update.
2017-04-18 05:44:08 +02:00
* @param array|null $params
* @param array|string|null $opts
*
2018-03-06 13:52:56 +01:00
* @return BankAccount|Card
2017-04-18 05:44:08 +02:00
*/
2018-03-06 13:52:56 +01:00
public static function updateExternalAccount($id, $externalAccountId, $params = null, $opts = null)
2017-04-18 05:44:08 +02:00
{
2018-03-06 13:52:56 +01:00
return self::_updateNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
2017-04-18 05:44:08 +02:00
}
/**
* @param string $id The ID of the account to which the external account belongs.
* @param string $externalAccountId The ID of the external account to delete.
2017-04-18 05:44:08 +02:00
* @param array|null $params
* @param array|string|null $opts
*
2018-03-06 13:52:56 +01:00
* @return BankAccount|Card
2017-04-18 05:44:08 +02:00
*/
2018-03-06 13:52:56 +01:00
public static function deleteExternalAccount($id, $externalAccountId, $params = null, $opts = null)
2017-04-18 05:44:08 +02:00
{
2018-03-06 13:52:56 +01:00
return self::_deleteNestedResource($id, static::PATH_EXTERNAL_ACCOUNTS, $externalAccountId, $params, $opts);
2017-04-18 05:44:08 +02:00
}
/**
* @param string $id The ID of the account on which to retrieve the external accounts.
2017-04-18 05:44:08 +02:00
* @param array|null $params
* @param array|string|null $opts
*
2019-05-03 02:22:27 +02:00
* @return Collection The list of external accounts (BankAccount or Card).
2017-04-18 05:44:08 +02:00
*/
2018-03-06 13:52:56 +01:00
public static function allExternalAccounts($id, $params = null, $opts = null)
2017-04-18 05:44:08 +02:00
{
2018-03-06 13:52:56 +01:00
return self::_allNestedResources($id, static::PATH_EXTERNAL_ACCOUNTS, $params, $opts);
}
/**
* @param string $id The ID of the account on which to create the login link.
2018-03-06 13:52:56 +01:00
* @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);
}
2019-05-16 21:41:14 +02:00
/**
* @param array|null $params
* @param array|string|null $options
*
* @return Collection The list of persons.
*/
public function persons($params = null, $options = null)
{
$url = $this->instanceUrl() . '/persons';
list($response, $opts) = $this->_request('get', $url, $params, $options);
$obj = Util\Util::convertToStripeObject($response, $opts);
$obj->setLastResponse($response);
return $obj;
}
2019-05-03 02:22:27 +02:00
/**
* @param string $id The ID of the account on which to create the person.
2019-05-03 02:22:27 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Person
*/
public static function createPerson($id, $params = null, $opts = null)
{
return self::_createNestedResource($id, static::PATH_PERSONS, $params, $opts);
}
/**
* @param string $id The ID of the account to which the person belongs.
* @param string $personId The ID of the person to retrieve.
2019-05-03 02:22:27 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Person
*/
public static function retrievePerson($id, $personId, $params = null, $opts = null)
{
return self::_retrieveNestedResource($id, static::PATH_PERSONS, $personId, $params, $opts);
}
/**
* @param string $id The ID of the account to which the person belongs.
* @param string $personId The ID of the person to update.
2019-05-03 02:22:27 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Person
*/
public static function updatePerson($id, $personId, $params = null, $opts = null)
{
return self::_updateNestedResource($id, static::PATH_PERSONS, $personId, $params, $opts);
}
/**
* @param string $id The ID of the account to which the person belongs.
* @param string $personId The ID of the person to delete.
2019-05-03 02:22:27 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Person
*/
public static function deletePerson($id, $personId, $params = null, $opts = null)
{
return self::_deleteNestedResource($id, static::PATH_PERSONS, $personId, $params, $opts);
}
/**
* @param string $id The ID of the account on which to retrieve the persons.
2019-05-03 02:22:27 +02:00
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection The list of persons.
*/
public static function allPersons($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_PERSONS, $params, $opts);
}
2018-03-06 13:52:56 +01:00
public function serializeParameters($force = false)
{
$update = parent::serializeParameters($force);
if (isset($this->_values['legal_entity'])) {
$entity = $this['legal_entity'];
if (isset($entity->_values['additional_owners'])) {
$owners = $entity['additional_owners'];
$entityUpdate = isset($update['legal_entity']) ? $update['legal_entity'] : [];
$entityUpdate['additional_owners'] = $this->serializeAdditionalOwners($entity, $owners);
$update['legal_entity'] = $entityUpdate;
}
}
2019-05-03 02:22:27 +02:00
if (isset($this->_values['individual'])) {
$individual = $this['individual'];
if (($individual instanceof Person) && !isset($update['individual'])) {
$update['individual'] = $individual->serializeParameters($force);
}
}
2018-03-06 13:52:56 +01:00
return $update;
}
private function serializeAdditionalOwners($legalEntity, $additionalOwners)
{
if (isset($legalEntity->_originalValues['additional_owners'])) {
$originalValue = $legalEntity->_originalValues['additional_owners'];
} else {
$originalValue = [];
}
if (($originalValue) && (count($originalValue) > count($additionalOwners))) {
throw new \InvalidArgumentException(
"You cannot delete an item from an array, you must instead set a new array"
);
}
$updateArr = [];
foreach ($additionalOwners as $i => $v) {
$update = ($v instanceof StripeObject) ? $v->serializeParameters() : $v;
if ($update !== []) {
2019-05-03 02:22:27 +02:00
if (!$originalValue ||
!array_key_exists($i, $originalValue) ||
($update != $legalEntity->serializeParamsValue($originalValue[$i], null, false, true))) {
2018-03-06 13:52:56 +01:00
$updateArr[$i] = $update;
}
}
}
return $updateArr;
2017-04-18 05:44:08 +02:00
}
}