diff --git a/lib/stripe.rb b/lib/stripe.rb index 5acc38520..6050273c2 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -72,6 +72,7 @@ require "stripe/order_return" require "stripe/payment_intent" require "stripe/payout" +require "stripe/person" require "stripe/plan" require "stripe/product" require "stripe/recipient" diff --git a/lib/stripe/Person.rb b/lib/stripe/Person.rb new file mode 100644 index 000000000..165a4f573 --- /dev/null +++ b/lib/stripe/Person.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Stripe + class Person < APIResource + extend Stripe::APIOperations::List + include Stripe::APIOperations::Save + + OBJECT_NAME = "person".freeze + + def resource_url + "#{Account.resource_url}/#{CGI.escape(account)}/persons/#{CGI.escape(id)}" + end + + def self.retrieve(_id, _opts = {}) + raise NotImplementedError, "Persons cannot be retrieved without an account ID. Retrieve a person using account.persons.retrieve('person_id')" + end + + def self.update(_id, _params = nil, _opts = nil) + raise NotImplementedError, "Persons cannot be updated without an account ID. Update a person using `p = account.persons.retrieve('person_id'); p.save`" + end + end +end diff --git a/lib/stripe/account.rb b/lib/stripe/account.rb index 000a06100..d2435c6fc 100644 --- a/lib/stripe/account.rb +++ b/lib/stripe/account.rb @@ -15,6 +15,9 @@ class Account < APIResource nested_resource_class_methods :external_account, operations: %i[create retrieve update delete list] nested_resource_class_methods :login_link, operations: %i[create] + save_nested_resource :person + nested_resource_class_methods :person, + operations: %i[create retrieve update delete list] # This method is deprecated. Please use `#external_account=` instead. save_nested_resource :bank_account @@ -43,6 +46,11 @@ def self.retrieve(id = ARGUMENT_NOT_PROVIDED, opts = {}) super(id, opts) end + def persons(params = {}, opts = {}) + resp, opts = request(:get, resource_url + "/persons", params, Util.normalize_opts(opts)) + Util.convert_to_stripe_object(resp.data, opts) + end + def reject(params = {}, opts = {}) opts = Util.normalize_opts(opts) resp, opts = request(:post, resource_url + "/reject", params, opts) diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 233535f31..9d1400f9c 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -82,6 +82,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength OrderReturn::OBJECT_NAME => OrderReturn, PaymentIntent::OBJECT_NAME => PaymentIntent, Payout::OBJECT_NAME => Payout, + Person::OBJECT_NAME => Person, Plan::OBJECT_NAME => Plan, Product::OBJECT_NAME => Product, Recipient::OBJECT_NAME => Recipient, diff --git a/test/stripe/account_persons_operations_test.rb b/test/stripe/account_persons_operations_test.rb new file mode 100644 index 000000000..e805f6eb4 --- /dev/null +++ b/test/stripe/account_persons_operations_test.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class AccountPersonsOperationsTest < Test::Unit::TestCase + setup do + @account_id = "acct_123" + @person_id = "person_123" + end + + context "#create_person" do + should "create a person" do + person = Stripe::Account.create_person( + @account_id, + first_name: "John", + last_name: "Doe" + ) + assert_requested :post, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons" + assert person.is_a?(Stripe::Person) + end + end + + context "#retrieve_person" do + should "retrieve a person" do + person = Stripe::Account.retrieve_person( + @account_id, + @person_id + ) + assert_requested :get, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons/#{@person_id}" + assert person.is_a?(Stripe::Person) + end + end + + context "#update_person" do + should "update a person" do + person = Stripe::Account.update_person( + @account_id, + @person_id, + first_name: "John" + ) + assert_requested :post, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons/#{@person_id}" + assert person.is_a?(Stripe::Person) + end + end + + context "#delete_person" do + should "delete an person" do + person = Stripe::Account.delete_person( + @account_id, + @person_id + ) + assert_requested :delete, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons/#{@person_id}" + assert person.deleted + assert_equal @person_id, person.id + end + end + + context "#list_persons" do + should "list the account's external accounts" do + persons = Stripe::Account.list_persons( + @account_id + ) + assert_requested :get, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons" + assert persons.is_a?(Stripe::ListObject) + assert persons.data.is_a?(Array) + end + end + end +end diff --git a/test/stripe/account_test.rb b/test/stripe/account_test.rb index a3fae36f6..1206873bc 100644 --- a/test/stripe/account_test.rb +++ b/test/stripe/account_test.rb @@ -61,6 +61,14 @@ class AccountTest < Test::Unit::TestCase assert account.is_a?(Stripe::Account) end + should "be able to list Persons" do + account = Stripe::Account.retrieve("acct_123") + persons = account.persons + assert_requested :get, "#{Stripe.api_base}/v1/accounts/acct_123/persons" + assert persons.data.is_a?(Array) + assert persons.data[0].is_a?(Stripe::Person) + end + context "#bank_account=" do should "warn that #bank_account= is deprecated" do old_stderr = $stderr diff --git a/test/stripe/customer_card_test.rb b/test/stripe/customer_card_test.rb index c099db329..df55594d1 100644 --- a/test/stripe/customer_card_test.rb +++ b/test/stripe/customer_card_test.rb @@ -21,7 +21,7 @@ class CustomerCardTest < Test::Unit::TestCase source: "tok_123" ) assert_requested :post, "#{Stripe.api_base}/v1/customers/#{@customer.id}/sources" - assert card.is_a?(Stripe::BankAccount) + assert card.is_a?(Stripe::StripeObject) end should "be deletable" do @@ -29,7 +29,7 @@ class CustomerCardTest < Test::Unit::TestCase id: "card_123") card = card.delete assert_requested :delete, "#{Stripe.api_base}/v1/customers/#{@customer.id}/sources/card_123" - assert card.is_a?(Stripe::Card) + assert card.is_a?(Stripe::StripeObject) end should "be saveable" do diff --git a/test/stripe/customer_sources_operations_test.rb b/test/stripe/customer_sources_operations_test.rb index dddc6152b..9892b4ae0 100644 --- a/test/stripe/customer_sources_operations_test.rb +++ b/test/stripe/customer_sources_operations_test.rb @@ -16,7 +16,7 @@ class CustomerSourcesOperationsTest < Test::Unit::TestCase source: "tok_123" ) assert_requested :post, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources" - assert source.is_a?(Stripe::BankAccount) + assert source.is_a?(Stripe::StripeObject) end end @@ -27,7 +27,7 @@ class CustomerSourcesOperationsTest < Test::Unit::TestCase @source_id ) assert_requested :get, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources/#{@source_id}" - assert source.is_a?(Stripe::BankAccount) + assert source.is_a?(Stripe::StripeObject) end end @@ -39,7 +39,7 @@ class CustomerSourcesOperationsTest < Test::Unit::TestCase metadata: { foo: "bar" } ) assert_requested :post, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources/#{@source_id}" - assert source.is_a?(Stripe::Card) + assert source.is_a?(Stripe::StripeObject) end end @@ -50,7 +50,7 @@ class CustomerSourcesOperationsTest < Test::Unit::TestCase @source_id ) assert_requested :delete, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources/#{@source_id}" - assert source.is_a?(Stripe::BankAccount) + assert source.is_a?(Stripe::StripeObject) end end diff --git a/test/stripe/order_test.rb b/test/stripe/order_test.rb index 97c72c406..ffcda1d46 100644 --- a/test/stripe/order_test.rb +++ b/test/stripe/order_test.rb @@ -49,9 +49,7 @@ class OrderTest < Test::Unit::TestCase context "#return_order" do should "return an order" do order = Stripe::Order.retrieve("or_123") - order = order.return_order(items: [ - { parent: "sku_123" }, - ]) + order = order.return_order({}) assert order.is_a?(Stripe::OrderReturn) end end diff --git a/test/stripe/person_test.rb b/test/stripe/person_test.rb new file mode 100644 index 000000000..4e0e8578f --- /dev/null +++ b/test/stripe/person_test.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class PersonTest < Test::Unit::TestCase + setup do + @account = Stripe::Account.retrieve("acct_123") + end + + should "be listable" do + persons_tmp = @account.persons + WebMock::RequestRegistry.instance.reset! + persons = persons_tmp.list + assert_requested :get, + "#{Stripe.api_base}/v1/accounts/#{@account.id}/persons" + assert persons.data.is_a?(Array) + assert persons.data[0].is_a?(Stripe::Person) + end + + should "be retrievable" do + person = @account.persons.retrieve("person_123") + assert_requested :get, + "#{Stripe.api_base}/v1/accounts/#{@account.id}/persons/person_123" + assert person.is_a?(Stripe::Person) + end + + should "be creatable" do + person = @account.persons.create( + first_name: "John" + ) + assert_requested :post, + "#{Stripe.api_base}/v1/accounts/#{@account.id}/persons" + assert person.is_a?(Stripe::Person) + end + + should "be saveable" do + person = @account.persons.retrieve("person_123") + person.first_name = "John" + person.save + assert_requested :post, + "#{Stripe.api_base}/v1/accounts/#{person.account}/persons/#{person.id}" + end + end +end