Skip to content

Commit

Permalink
Merge pull request #536 from stripe/remi-add-person
Browse files Browse the repository at this point in the history
Add support for the Person resource
  • Loading branch information
remi-stripe authored Oct 30, 2018
2 parents 1ab5c54 + 3dd0007 commit e1d2fb8
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ php:

env:
global:
- STRIPE_MOCK_VERSION=0.33.0
- STRIPE_MOCK_VERSION=0.35.0
matrix:
- AUTOLOAD=1
- AUTOLOAD=0
Expand Down
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
require(dirname(__FILE__) . '/lib/OrderReturn.php');
require(dirname(__FILE__) . '/lib/PaymentIntent.php');
require(dirname(__FILE__) . '/lib/Payout.php');
require(dirname(__FILE__) . '/lib/Person.php');
require(dirname(__FILE__) . '/lib/Plan.php');
require(dirname(__FILE__) . '/lib/Product.php');
require(dirname(__FILE__) . '/lib/Recipient.php');
Expand Down
79 changes: 79 additions & 0 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static function getSavedNestedResources()

const PATH_EXTERNAL_ACCOUNTS = '/external_accounts';
const PATH_LOGIN_LINKS = '/login_links';
const PATH_PERSONS = '/persons';

public function instanceUrl()
{
Expand Down Expand Up @@ -107,6 +108,21 @@ public function reject($params = null, $opts = null)
return $this;
}

/**
* @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;
}

/**
* @param array|null $clientId
* @param array|string|null $opts
Expand Down Expand Up @@ -197,6 +213,69 @@ public static function createLoginLink($id, $params = null, $opts = null)
return self::_createNestedResource($id, static::PATH_LOGIN_LINKS, $params, $opts);
}

/**
* @param array|null $id The ID of the account on which to create the person.
* @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 array|null $id The ID of the account to which the person belongs.
* @param array|null $personId The ID of the person to retrieve.
* @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 array|null $id The ID of the account to which the person belongs.
* @param array|null $personId The ID of the person to update.
* @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 array|null $id The ID of the account to which the person belongs.
* @param array|null $personId The ID of the person to delete.
* @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 array|null $id The ID of the account on which to retrieve the persons.
* @param array|null $params
* @param array|string|null $opts
*
* @return Person
*/
public static function allPersons($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_PERSONS, $params, $opts);
}

public function serializeParameters($force = false)
{
$update = parent::serializeParameters($force);
Expand Down
92 changes: 92 additions & 0 deletions lib/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Stripe;

/**
* Class Person
*
* @package Stripe
*
* @property string $id
* @property string $object
* @property string $account
* @property mixed $address
* @property mixed $address_kana
* @property mixed $address_kanji
* @property int $created
* @property bool $deleted
* @property mixed $dob
* @property string $email
* @property string $first_name
* @property string $first_name_kana
* @property string $first_name_kanji
* @property string $gender
* @property bool $id_number_provided
* @property string $last_name
* @property string $last_name_kana
* @property string $last_name_kanji
* @property string $maiden_name
* @property StripeObject $metadata
* @property string $phone
* @property mixed $relationship
* @property mixed $requirements
* @property bool $ssn_last_4_provided
* @property mixed $verification
*/
class Person extends ApiResource
{
const OBJECT_NAME = "person";

use ApiOperations\Delete;
use ApiOperations\Update;

/**
* @return string The API URL for this Stripe account reversal.
*/
public function instanceUrl()
{
$id = $this['id'];
$account = $this['account'];
if (!$id) {
throw new Error\InvalidRequest(
"Could not determine which URL to request: " .
"class instance has invalid ID: $id",
null
);
}
$id = Util\Util::utf8($id);
$account = Util\Util::utf8($account);

$base = Account::classUrl();
$accountExtn = urlencode($account);
$extn = urlencode($id);
return "$base/$accountExtn/persons/$extn";
}

/**
* @param array|string $_id
* @param array|string|null $_opts
*
* @throws \Stripe\Error\InvalidRequest
*/
public static function retrieve($_id, $_opts = null)
{
$msg = "Persons cannot be accessed without an account ID. " .
"Retrieve a Person using \$account->retrievePerson('person_id') instead.";
throw new Error\InvalidRequest($msg, null);
}

/**
* @param string $_id
* @param array|null $_params
* @param array|string|null $_options
*
* @throws \Stripe\Error\InvalidRequest
*/
public static function update($_id, $_params = null, $_options = null)
{
$msg = "Persons cannot be accessed without an account ID. " .
"Retrieve a Person using \$account->retrievePerson('person_id') instead.";
throw new Error\InvalidRequest($msg, null);
}
}
1 change: 1 addition & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static function convertToStripeObject($resp, $opts)
\Stripe\OrderReturn::OBJECT_NAME => 'Stripe\\OrderReturn',
\Stripe\PaymentIntent::OBJECT_NAME => 'Stripe\\PaymentIntent',
\Stripe\Payout::OBJECT_NAME => 'Stripe\\Payout',
\Stripe\Person::OBJECT_NAME => 'Stripe\\Person',
\Stripe\Plan::OBJECT_NAME => 'Stripe\\Plan',
\Stripe\Product::OBJECT_NAME => 'Stripe\\Product',
\Stripe\Recipient::OBJECT_NAME => 'Stripe\\Recipient',
Expand Down
71 changes: 71 additions & 0 deletions tests/Stripe/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class AccountTest extends TestCase
{
const TEST_RESOURCE_ID = 'acct_123';
const TEST_EXTERNALACCOUNT_ID = 'ba_123';
const TEST_PERSON_ID = 'person_123';

public function testIsListable()
{
Expand Down Expand Up @@ -116,6 +117,18 @@ public function testIsDeauthorizable()
$resource->deauthorize();
}

public function testPersons()
{
$account = Account::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'get',
'/v1/accounts/' . $account->id . '/persons'
);
$persons = $account->persons();
$this->assertTrue(is_array($persons->data));
$this->assertInstanceOf("Stripe\\Person", $persons->data[0]);
}

public function testCanCreateExternalAccount()
{
$this->expectsRequest(
Expand Down Expand Up @@ -180,6 +193,64 @@ public function testCanCreateLoginLink()
$this->assertInstanceOf("Stripe\\LoginLink", $resource);
}

public function testCanCreatePerson()
{
$this->expectsRequest(
'post',
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/persons'
);
$resource = Account::createPerson(self::TEST_RESOURCE_ID, [
"dob" => [
"day" => 1,
"month" => 1,
"year" => 1980
]
]);
$this->assertInstanceOf("Stripe\\Person", $resource);
}

public function testCanRetrievePerson()
{
$this->expectsRequest(
'get',
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/persons/' . self::TEST_PERSON_ID
);
$resource = Account::retrievePerson(self::TEST_RESOURCE_ID, self::TEST_PERSON_ID);
$this->assertInstanceOf("Stripe\\Person", $resource);
}

public function testCanUpdatePerson()
{
$this->expectsRequest(
'post',
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/persons/' . self::TEST_PERSON_ID
);
$resource = Account::updatePerson(self::TEST_RESOURCE_ID, self::TEST_PERSON_ID, [
"first_name" => "First name",
]);
$this->assertInstanceOf("Stripe\\Person", $resource);
}

public function testCanDeletePerson()
{
$this->expectsRequest(
'delete',
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/persons/' . self::TEST_PERSON_ID
);
$resource = Account::deletePerson(self::TEST_RESOURCE_ID, self::TEST_PERSON_ID);
$this->assertTrue($resource->deleted);
}

public function testCanListPersons()
{
$this->expectsRequest(
'get',
'/v1/accounts/' . self::TEST_RESOURCE_ID . '/persons'
);
$resources = Account::allPersons(self::TEST_RESOURCE_ID);
$this->assertTrue(is_array($resources->data));
}

public function testSerializeNewAdditionalOwners()
{
$obj = Util\Util::convertToStripeObject([
Expand Down
4 changes: 1 addition & 3 deletions tests/Stripe/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ public function testCanCreateSource()
'/v1/customers/' . self::TEST_RESOURCE_ID . '/sources'
);
$resource = Customer::createSource(self::TEST_RESOURCE_ID, ["source" => "btok_123"]);
$this->assertInstanceOf("Stripe\\BankAccount", $resource);
}

public function testCanRetrieveSource()
Expand All @@ -197,7 +196,6 @@ public function testCanRetrieveSource()
'/v1/customers/' . self::TEST_RESOURCE_ID . '/sources/' . self::TEST_SOURCE_ID
);
$resource = Customer::retrieveSource(self::TEST_RESOURCE_ID, self::TEST_SOURCE_ID);
$this->assertInstanceOf("Stripe\\BankAccount", $resource);
}

public function testCanUpdateSource()
Expand All @@ -218,7 +216,7 @@ public function testCanDeleteSource()
'/v1/customers/' . self::TEST_RESOURCE_ID . '/sources/' . self::TEST_SOURCE_ID
);
$resource = Customer::deleteSource(self::TEST_RESOURCE_ID, self::TEST_SOURCE_ID);
$this->assertInstanceOf("Stripe\\BankAccount", $resource);
$this->assertInstanceOf("Stripe\\AlipayAccount", $resource);
}

public function testCanListSources()
Expand Down
59 changes: 59 additions & 0 deletions tests/Stripe/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Stripe;

class PersonTest extends TestCase
{
const TEST_ACCOUNT_ID = 'acct_123';
const TEST_RESOURCE_ID = 'person_123';

public function testHasCorrectUrl()
{
$resource = \Stripe\Account::retrievePerson(self::TEST_ACCOUNT_ID, self::TEST_RESOURCE_ID);
$this->assertSame(
"/v1/accounts/" . self::TEST_ACCOUNT_ID . "/persons/" . self::TEST_RESOURCE_ID,
$resource->instanceUrl()
);
}

/**
* @expectedException \Stripe\Error\InvalidRequest
*/
public function testIsNotDirectlyRetrievable()
{
Person::retrieve(self::TEST_RESOURCE_ID);
}

public function testIsSaveable()
{
$resource = \Stripe\Account::retrievePerson(self::TEST_ACCOUNT_ID, self::TEST_RESOURCE_ID);
$resource->first_name = "value";
$this->expectsRequest(
'post',
'/v1/accounts/' . self::TEST_ACCOUNT_ID . '/persons/' . self::TEST_RESOURCE_ID
);
$resource->save();
$this->assertSame("Stripe\\Person", get_class($resource));
}

/**
* @expectedException \Stripe\Error\InvalidRequest
*/
public function testIsNotDirectlyUpdatable()
{
Person::update(self::TEST_RESOURCE_ID, [
"first_name" => ["John"],
]);
}

public function testIsDeletable()
{
$resource = \Stripe\Account::retrievePerson(self::TEST_ACCOUNT_ID, self::TEST_RESOURCE_ID);
$this->expectsRequest(
'delete',
'/v1/accounts/' . self::TEST_ACCOUNT_ID . '/persons/' . self::TEST_RESOURCE_ID
);
$resource->delete();
$this->assertSame("Stripe\\Person", get_class($resource));
}
}
Loading

0 comments on commit e1d2fb8

Please sign in to comment.