Skip to content

Commit

Permalink
Add support for TaxId resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Apr 19, 2019
1 parent f3991b5 commit 7385e22
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 2 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.52.0
- STRIPE_MOCK_VERSION=0.54.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 @@ -119,6 +119,7 @@
require(dirname(__FILE__) . '/lib/SubscriptionItem.php');
require(dirname(__FILE__) . '/lib/SubscriptionSchedule.php');
require(dirname(__FILE__) . '/lib/SubscriptionScheduleRevision.php');
require(dirname(__FILE__) . '/lib/TaxId.php');
require(dirname(__FILE__) . '/lib/Terminal/ConnectionToken.php');
require(dirname(__FILE__) . '/lib/Terminal/Location.php');
require(dirname(__FILE__) . '/lib/Terminal/Reader.php');
Expand Down
51 changes: 51 additions & 0 deletions lib/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static function getSavedNestedResources()
}

const PATH_SOURCES = '/sources';
const PATH_TAX_IDS = '/tax_ids';

/**
* @param array|null $params
Expand Down Expand Up @@ -202,4 +203,54 @@ public static function allSources($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_SOURCES, $params, $opts);
}

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

/**
* @param string|null $id The ID of the customer to which the tax id belongs.
* @param string|null $taxIdId The ID of the tax id to retrieve.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApiResource
*/
public static function retrieveTaxId($id, $taxIdId, $params = null, $opts = null)
{
return self::_retrieveNestedResource($id, static::PATH_TAX_IDS, $taxIdId, $params, $opts);
}

/**
* @param string|null $id The ID of the customer to which the tax id belongs.
* @param string|null $taxIdId The ID of the tax id to delete.
* @param array|null $params
* @param array|string|null $opts
*
* @return ApiResource
*/
public static function deleteTaxId($id, $taxIdId, $params = null, $opts = null)
{
return self::_deleteNestedResource($id, static::PATH_TAX_IDS, $taxIdId, $params, $opts);
}

/**
* @param string|null $id The ID of the customer on which to retrieve the tax ids.
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection The list of tax ids.
*/
public static function allTaxIds($id, $params = null, $opts = null)
{
return self::_allNestedResources($id, static::PATH_TAX_IDS, $params, $opts);
}
}
71 changes: 71 additions & 0 deletions lib/TaxId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Stripe;

/**
* Class TaxId
*
* @package Stripe
*
* @property string $id
* @property string $object
* @property string $country
* @property int $created
* @property string $customer
* @property bool $deleted
* @property bool $livemode
* @property type $type
* @property string $value
* @property mixed $verification
*/
class TaxId extends ApiResource
{

const OBJECT_NAME = "tax_id";

use ApiOperations\Delete;

/**
* Possible string representations of a tax id's type.
* @link https://stripe.com/docs/api/customers/tax_id_object#tax_id_object-type
*/
const TYPE_AU_ABN = 'au_abn';
const TYPE_EU_VAT = 'eu_vat';
const TYPE_NZ_GST = 'nz_gst';
const TYPE_UNKNOWN = 'unknown';

/**
* @return string The API URL for this tax id.
*/
public function instanceUrl()
{
$id = $this['id'];
$customer = $this['customer'];
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);
$customer = Util\Util::utf8($customer);

$base = Customer::classUrl();
$customerExtn = urlencode($customer);
$extn = urlencode($id);
return "$base/$customerExtn/tax_ids/$extn";
}

/**
* @param array|string $_id
* @param array|string|null $_opts
*
* @throws \Stripe\Error\InvalidRequest
*/
public static function retrieve($_id, $_opts = null)
{
$msg = "Tax Ids cannot be accessed without a customer ID. " .
"Retrieve a Tax Id using Customer::retrieveTaxId('tax_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 @@ -132,6 +132,7 @@ public static function convertToStripeObject($resp, $opts)
\Stripe\SubscriptionItem::OBJECT_NAME => 'Stripe\\SubscriptionItem',
\Stripe\SubscriptionSchedule::OBJECT_NAME => 'Stripe\\SubscriptionSchedule',
\Stripe\SubscriptionScheduleRevision::OBJECT_NAME => 'Stripe\\SubscriptionScheduleRevision',
\Stripe\TaxId::OBJECT_NAME => 'Stripe\\TaxId',
\Stripe\ThreeDSecure::OBJECT_NAME => 'Stripe\\ThreeDSecure',
\Stripe\Terminal\ConnectionToken::OBJECT_NAME => 'Stripe\\Terminal\\ConnectionToken',
\Stripe\Terminal\Location::OBJECT_NAME => 'Stripe\\Terminal\\Location',
Expand Down
41 changes: 41 additions & 0 deletions tests/Stripe/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CustomerTest extends TestCase
{
const TEST_RESOURCE_ID = 'cus_123';
const TEST_SOURCE_ID = 'ba_123';
const TEST_TAX_ID_ID = 'txi_123';

public function testIsListable()
{
Expand Down Expand Up @@ -263,4 +264,44 @@ public function testSerializeSourceMap()
];
$this->assertSame($expected, $obj->serializeParameters());
}

public function testCanCreateTaxId()
{
$this->expectsRequest(
'post',
'/v1/customers/' . self::TEST_RESOURCE_ID . '/tax_ids'
);
$resource = Customer::createTaxId(self::TEST_RESOURCE_ID, [
"type" => TaxId::TYPE_EU_VAT,
"value" => "11111",
]);
}

public function testCanRetrieveTaxId()
{
$this->expectsRequest(
'get',
'/v1/customers/' . self::TEST_RESOURCE_ID . '/tax_ids/' . self::TEST_TAX_ID_ID
);
$resource = Customer::retrieveTaxId(self::TEST_RESOURCE_ID, self::TEST_TAX_ID_ID);
}

public function testCanDeleteTaxId()
{
$this->expectsRequest(
'delete',
'/v1/customers/' . self::TEST_RESOURCE_ID . '/tax_ids/' . self::TEST_TAX_ID_ID
);
$resource = Customer::deleteTaxId(self::TEST_RESOURCE_ID, self::TEST_TAX_ID_ID);
}

public function testCanListTaxIds()
{
$this->expectsRequest(
'get',
'/v1/customers/' . self::TEST_RESOURCE_ID . '/tax_ids'
);
$resources = Customer::allTaxIds(self::TEST_RESOURCE_ID);
$this->assertTrue(is_array($resources->data));
}
}
29 changes: 29 additions & 0 deletions tests/Stripe/TaxIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Stripe;

class TaxIdTest extends TestCase
{
const TEST_CUSTOMER_ID = 'cus_123';
const TEST_RESOURCE_ID = 'txi_123';

public function testHasCorrectUrl()
{
$resource = \Stripe\Customer::retrieveTaxId(self::TEST_CUSTOMER_ID, self::TEST_RESOURCE_ID);
$this->assertSame(
"/v1/customers/" . self::TEST_CUSTOMER_ID . "/tax_ids/" . self::TEST_RESOURCE_ID,
$resource->instanceUrl()
);
}

public function testIsDeletable()
{
$resource = \Stripe\Customer::retrieveTaxId(self::TEST_CUSTOMER_ID, self::TEST_RESOURCE_ID);
$this->expectsRequest(
'delete',
'/v1/customers/' . self::TEST_CUSTOMER_ID . '/tax_ids/' . self::TEST_RESOURCE_ID
);
$resource->delete();
$this->assertSame("Stripe\\TaxId", get_class($resource));
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once(__DIR__ . '/StripeMock.php');

define("MOCK_MINIMUM_VERSION", "0.52.0");
define("MOCK_MINIMUM_VERSION", "0.54.0");

if (\Stripe\StripeMock::start()) {
register_shutdown_function('\Stripe\StripeMock::stop');
Expand Down

0 comments on commit 7385e22

Please sign in to comment.