From b5cd9e3682a25867b8a044cafd0ead453a2f4728 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Fri, 12 Apr 2019 19:10:17 -0700 Subject: [PATCH] Add support for TaxId resource and APIs --- .travis.yml | 2 +- lib/stripe.rb | 1 + lib/stripe/customer.rb | 3 +++ lib/stripe/tax_id.rb | 22 +++++++++++++++++++ lib/stripe/util.rb | 1 + test/stripe/customer_test.rb | 42 ++++++++++++++++++++++++++++++++++++ test/stripe/tax_id_test.rb | 31 ++++++++++++++++++++++++++ test/test_helper.rb | 2 +- 8 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 lib/stripe/tax_id.rb create mode 100644 test/stripe/tax_id_test.rb diff --git a/.travis.yml b/.travis.yml index 5105e3e88..d481a476f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ sudo: false env: global: # If changing this number, please also change it in `test/test_helper.rb`. - - STRIPE_MOCK_VERSION=0.52.0 + - STRIPE_MOCK_VERSION=0.54.0 cache: directories: diff --git a/lib/stripe.rb b/lib/stripe.rb index 915c367d7..8bda2f85f 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -97,6 +97,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..97cad24f4 --- /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 99cead606..1a784d36a 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -107,6 +107,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 7f008d8ce..4a1346dd4 100644 --- a/test/stripe/customer_test.rb +++ b/test/stripe/customer_test.rb @@ -180,5 +180,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 diff --git a/test/test_helper.rb b/test/test_helper.rb index fa7b786bf..0b43f3202 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,7 +17,7 @@ require ::File.expand_path("../stripe_mock", __FILE__) # If changing this number, please also change it in `.travis.yml`. -MOCK_MINIMUM_VERSION = "0.52.0".freeze +MOCK_MINIMUM_VERSION = "0.54.0".freeze MOCK_PORT = Stripe::StripeMock.start # Disable all real network connections except those that are outgoing to