Skip to content

Commit

Permalink
Add support for the Person resource
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Oct 30, 2018
1 parent 9bc6ae8 commit f256e0d
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions lib/stripe/Person.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/stripe/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
70 changes: 70 additions & 0 deletions test/stripe/account_persons_operations_test.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions test/stripe/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/stripe/customer_card_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ 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
card = Stripe::Card.construct_from(customer: @customer.id,
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
Expand Down
8 changes: 4 additions & 4 deletions test/stripe/customer_sources_operations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down
4 changes: 1 addition & 3 deletions test/stripe/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions test/stripe/person_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f256e0d

Please sign in to comment.