From abdf4a46ca2004c6ae8342f47eaecb08f4aa3945 Mon Sep 17 00:00:00 2001 From: Annie Li Date: Thu, 15 Feb 2024 15:31:37 -0800 Subject: [PATCH 1/2] Fix TaxId tests to support top-level TaxId methods --- lib/TaxId.php | 40 +++----------------------------------- tests/Stripe/TaxIdTest.php | 37 +++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/lib/TaxId.php b/lib/TaxId.php index 71ce1d710..2827d9c2a 100644 --- a/lib/TaxId.php +++ b/lib/TaxId.php @@ -25,7 +25,10 @@ class TaxId extends ApiResource { const OBJECT_NAME = 'tax_id'; + use ApiOperations\All; + use ApiOperations\Create; use ApiOperations\Delete; + use ApiOperations\Retrieve; const TYPE_AD_NRT = 'ad_nrt'; const TYPE_AE_TRN = 'ae_trn'; @@ -100,41 +103,4 @@ class TaxId extends ApiResource const VERIFICATION_STATUS_UNAVAILABLE = 'unavailable'; const VERIFICATION_STATUS_UNVERIFIED = 'unverified'; const VERIFICATION_STATUS_VERIFIED = 'verified'; - - /** - * @return string the API URL for this tax id - */ - public function instanceUrl() - { - $id = $this['id']; - $customer = $this['customer']; - if (!$id) { - throw new Exception\UnexpectedValueException( - "Could not determine which URL to request: class instance has invalid ID: {$id}" - ); - } - $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 null|array|string $_opts - * - * @throws \Stripe\Exception\BadMethodCallException - */ - public static function retrieve($_id, $_opts = null) - { - $msg = 'Tax IDs cannot be retrieved without a customer ID. Retrieve ' . - "a tax ID using `Customer::retrieveTaxId('customer_id', " . - "'tax_id_id')`."; - - throw new Exception\BadMethodCallException($msg); - } } diff --git a/tests/Stripe/TaxIdTest.php b/tests/Stripe/TaxIdTest.php index c08586305..8b9c87161 100644 --- a/tests/Stripe/TaxIdTest.php +++ b/tests/Stripe/TaxIdTest.php @@ -10,24 +10,49 @@ final class TaxIdTest extends \Stripe\TestCase { use TestHelper; - const TEST_CUSTOMER_ID = 'cus_123'; const TEST_RESOURCE_ID = 'txi_123'; - public function testHasCorrectUrl() + public function testIsListable() { - $resource = \Stripe\Customer::retrieveTaxId(self::TEST_CUSTOMER_ID, self::TEST_RESOURCE_ID); + $this->expectsRequest( + 'get', + '/v1/tax_ids' + ); + $resources = TaxId::all(); + static::compatAssertIsArray($resources->data); + static::assertInstanceOf(\Stripe\TaxId::class, $resources->data[0]); + } + + public function testIsRetrievable() + { + $this->expectsRequest( + 'get', + '/v1/tax_ids/' . self::TEST_RESOURCE_ID + ); + $resource = \Stripe\TaxId::retrieve(self::TEST_RESOURCE_ID); + static::assertInstanceOf(\Stripe\TaxId::class, $resource); static::assertSame( - '/v1/customers/' . self::TEST_CUSTOMER_ID . '/tax_ids/' . self::TEST_RESOURCE_ID, + '/v1/tax_ids/' . self::TEST_RESOURCE_ID, $resource->instanceUrl() ); } + public function testIsCreatable() + { + $this->expectsRequest( + 'post', + '/v1/tax_ids' + ); + $resource = TaxId::create(['type' => 'eu_vat', 'value' => 'DE123456789']); + static::assertInstanceOf(\Stripe\TaxId::class, $resource); + } + public function testIsDeletable() { - $resource = \Stripe\Customer::retrieveTaxId(self::TEST_CUSTOMER_ID, self::TEST_RESOURCE_ID); + $resource = \Stripe\TaxId::retrieve(self::TEST_RESOURCE_ID); $this->expectsRequest( 'delete', - '/v1/customers/' . self::TEST_CUSTOMER_ID . '/tax_ids/' . self::TEST_RESOURCE_ID + '/v1/tax_ids/' . self::TEST_RESOURCE_ID ); $resource->delete(); static::assertInstanceOf(\Stripe\TaxId::class, $resource); From bd444599712d99e1ffe6683db8d481f1fa2103cc Mon Sep 17 00:00:00 2001 From: Annie Li Date: Thu, 15 Feb 2024 15:36:09 -0800 Subject: [PATCH 2/2] Add TaxIdService --- init.php | 1 + lib/Service/CoreServiceFactory.php | 2 + lib/Service/TaxIdService.php | 76 ++++++++++++++++++++++++++++++ lib/StripeClient.php | 1 + 4 files changed, 80 insertions(+) create mode 100644 lib/Service/TaxIdService.php diff --git a/init.php b/init.php index 28aab4049..2500ca394 100644 --- a/init.php +++ b/init.php @@ -238,6 +238,7 @@ require __DIR__ . '/lib/Service/Tax/TaxServiceFactory.php'; require __DIR__ . '/lib/Service/Tax/TransactionService.php'; require __DIR__ . '/lib/Service/TaxCodeService.php'; +require __DIR__ . '/lib/Service/TaxIdService.php'; require __DIR__ . '/lib/Service/TaxRateService.php'; require __DIR__ . '/lib/Service/Terminal/ConfigurationService.php'; require __DIR__ . '/lib/Service/Terminal/ConnectionTokenService.php'; diff --git a/lib/Service/CoreServiceFactory.php b/lib/Service/CoreServiceFactory.php index fd372479a..39891109a 100644 --- a/lib/Service/CoreServiceFactory.php +++ b/lib/Service/CoreServiceFactory.php @@ -61,6 +61,7 @@ * @property SubscriptionScheduleService $subscriptionSchedules * @property Tax\TaxServiceFactory $tax * @property TaxCodeService $taxCodes + * @property TaxIdService $taxIds * @property TaxRateService $taxRates * @property Terminal\TerminalServiceFactory $terminal * @property TestHelpers\TestHelpersServiceFactory $testHelpers @@ -133,6 +134,7 @@ class CoreServiceFactory extends \Stripe\Service\AbstractServiceFactory 'subscriptionSchedules' => SubscriptionScheduleService::class, 'tax' => Tax\TaxServiceFactory::class, 'taxCodes' => TaxCodeService::class, + 'taxIds' => TaxIdService::class, 'taxRates' => TaxRateService::class, 'terminal' => Terminal\TerminalServiceFactory::class, 'testHelpers' => TestHelpers\TestHelpersServiceFactory::class, diff --git a/lib/Service/TaxIdService.php b/lib/Service/TaxIdService.php new file mode 100644 index 000000000..a12644fe1 --- /dev/null +++ b/lib/Service/TaxIdService.php @@ -0,0 +1,76 @@ + + */ + public function all($params = null, $opts = null) + { + return $this->requestCollection('get', '/v1/tax_ids', $params, $opts); + } + + /** + * Creates a new account or customer tax_id object. + * + * @param null|array $params + * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts + * + * @throws \Stripe\Exception\ApiErrorException if the request fails + * + * @return \Stripe\TaxId + */ + public function create($params = null, $opts = null) + { + return $this->request('post', '/v1/tax_ids', $params, $opts); + } + + /** + * Deletes an existing account or customer tax_id object. + * + * @param string $id + * @param null|array $params + * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts + * + * @throws \Stripe\Exception\ApiErrorException if the request fails + * + * @return \Stripe\TaxId + */ + public function delete($id, $params = null, $opts = null) + { + return $this->request('delete', $this->buildPath('/v1/tax_ids/%s', $id), $params, $opts); + } + + /** + * Retrieves an account or customer tax_id object. + * + * @param string $id + * @param null|array $params + * @param null|RequestOptionsArray|\Stripe\Util\RequestOptions $opts + * + * @throws \Stripe\Exception\ApiErrorException if the request fails + * + * @return \Stripe\TaxId + */ + public function retrieve($id, $params = null, $opts = null) + { + return $this->request('get', $this->buildPath('/v1/tax_ids/%s', $id), $params, $opts); + } +} diff --git a/lib/StripeClient.php b/lib/StripeClient.php index 5d309e222..013693431 100644 --- a/lib/StripeClient.php +++ b/lib/StripeClient.php @@ -61,6 +61,7 @@ * @property \Stripe\Service\SubscriptionScheduleService $subscriptionSchedules * @property \Stripe\Service\Tax\TaxServiceFactory $tax * @property \Stripe\Service\TaxCodeService $taxCodes + * @property \Stripe\Service\TaxIdService $taxIds * @property \Stripe\Service\TaxRateService $taxRates * @property \Stripe\Service\Terminal\TerminalServiceFactory $terminal * @property \Stripe\Service\TestHelpers\TestHelpersServiceFactory $testHelpers