diff --git a/lib/stripe.rb b/lib/stripe.rb index f9d86619a..543f194f0 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -96,6 +96,7 @@ require "stripe/subscription_item" require "stripe/subscription_schedule" require "stripe/subscription_schedule_revision" +require "stripe/tax_id" require "stripe/terminal/connection_token" require "stripe/terminal/location" require "stripe/terminal/reader" diff --git a/lib/stripe/customer.rb b/lib/stripe/customer.rb index 687c37557..cf32c804a 100644 --- a/lib/stripe/customer.rb +++ b/lib/stripe/customer.rb @@ -16,6 +16,9 @@ class Customer < APIResource nested_resource_class_methods :source, operations: %i[create retrieve update delete list] + nested_resource_class_methods :tax_id, + operations: %i[create retrieve delete list] + # The API request for deleting a card or bank account and for detaching a # source object are the same. class << self diff --git a/lib/stripe/tax_id.rb b/lib/stripe/tax_id.rb new file mode 100644 index 000000000..2488b933d --- /dev/null +++ b/lib/stripe/tax_id.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Stripe + class TaxId < APIResource + extend Stripe::APIOperations::List + include Stripe::APIOperations::Delete + + OBJECT_NAME = "tax_id".freeze + + def resource_url + if !respond_to?(:customer) || customer.nil? + raise NotImplementedError, + "Tax Ids cannot be accessed without a customer ID." + end + "#{Customer.resource_url}/#{CGI.escape(customer)}/tax_ids/#{CGI.escape(id)}" + end + + def self.retrieve(_id, _opts = {}) + raise NotImplementedError, "Tax Ids cannot be retrieved without a customer ID. Retrieve a tax id using customer.retrieve_tax_id('tax_id')" + end + end +end diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index e641be324..72c2e5ea6 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -106,6 +106,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength SubscriptionItem::OBJECT_NAME => SubscriptionItem, SubscriptionSchedule::OBJECT_NAME => SubscriptionSchedule, SubscriptionScheduleRevision::OBJECT_NAME => SubscriptionScheduleRevision, + TaxId::OBJECT_NAME => TaxId, Terminal::ConnectionToken::OBJECT_NAME => Terminal::ConnectionToken, Terminal::Location::OBJECT_NAME => Terminal::Location, Terminal::Reader::OBJECT_NAME => Terminal::Reader, diff --git a/test/stripe/customer_test.rb b/test/stripe/customer_test.rb index 33e4a0754..9bce3c1ab 100644 --- a/test/stripe/customer_test.rb +++ b/test/stripe/customer_test.rb @@ -129,5 +129,47 @@ class CustomerTest < Test::Unit::TestCase assert_equal true, c.source.save_with_parent end end + + context "#create_tax_id" do + should "create a tax id" do + Stripe::Customer.create_tax_id( + "cus_123", + type: "eu_vat", + value: "11111", + ) + assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids" + end + end + + context "#retrieve_tax_id" do + should "retrieve a tax id" do + Stripe::Customer.retrieve_tax_id( + "cus_123", + "txi_123" + ) + assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids/txi_123" + end + end + + context "#delete_tax_id" do + should "delete a tax id" do + Stripe::Customer.delete_tax_id( + "cus_123", + "txi_123" + ) + assert_requested :delete, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids/txi_123" + end + end + + context "#list_tax_ids" do + should "list the customer's tax ids" do + sources = Stripe::Customer.list_tax_ids( + "cus_123" + ) + assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids" + assert sources.is_a?(Stripe::ListObject) + assert sources.data.is_a?(Array) + end + end end end diff --git a/test/stripe/tax_id_test.rb b/test/stripe/tax_id_test.rb new file mode 100644 index 000000000..9f81ccb2d --- /dev/null +++ b/test/stripe/tax_id_test.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class TaxIdTest < Test::Unit::TestCase + context "#resource_url" do + should "return a resource URL" do + tax_id = Stripe::TaxId.construct_from( + id: "txi_123", + customer: "cus_123" + ) + assert_equal "/v1/customers/cus_123/tax_ids/txi_123", + tax_id.resource_url + end + + should "raise without a customer" do + tax_id = Stripe::TaxId.construct_from(id: "txi_123") + assert_raises NotImplementedError do + tax_id.resource_url + end + end + end + + should "raise on #retrieve" do + assert_raises NotImplementedError do + Stripe::TaxId.retrieve("txi_123") + end + end + end +end