Skip to content

Commit

Permalink
Add support for Customer Balance Transaction resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed May 7, 2019
1 parent cd05d36 commit b3cc38e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
require "stripe/coupon"
require "stripe/credit_note"
require "stripe/customer"
require "stripe/customer_balance_transaction"
require "stripe/discount"
require "stripe/dispute"
require "stripe/ephemeral_key"
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Customer < APIResource
nested_resource_class_methods :tax_id,
operations: %i[create retrieve delete list]

nested_resource_class_methods :customer_balance_transaction,
operations: %i[create retrieve list]

# The API request for deleting a card or bank account and for detaching a
# source object are the same.
class << self
Expand Down
21 changes: 21 additions & 0 deletions lib/stripe/customer_balance_transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Stripe
class CustomerBalanceTransaction < APIResource
OBJECT_NAME = "customer_balance_transaction".freeze

def resource_url
if !respond_to?(:customer) || customer.nil?
raise NotImplementedError,
"Customer Balance Transactions cannot be accessed without a customer ID."
end
"#{Customer.resource_url}/#{CGI.escape(customer)}/customer_balance_transactions/#{CGI.escape(id)}"
end

def self.retrieve(_id, _opts = {})
raise NotImplementedError,
"Customer Balance Transactions cannot be retrieved without a customer ID. " \
"Retrieve a Customer Balance Transaction using Customer.retrieve_customer_balance_transaction('cus_123', 'cbtxn_123')"
end
end
end
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength
Coupon::OBJECT_NAME => Coupon,
CreditNote::OBJECT_NAME => CreditNote,
Customer::OBJECT_NAME => Customer,
CustomerBalanceTransaction::OBJECT_NAME => CustomerBalanceTransaction,
Discount::OBJECT_NAME => Discount,
Dispute::OBJECT_NAME => Dispute,
EphemeralKey::OBJECT_NAME => EphemeralKey,
Expand Down
31 changes: 31 additions & 0 deletions test/stripe/customer_balance_transaction_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require ::File.expand_path("../../test_helper", __FILE__)

module Stripe
class CustomerBalanceTransactionTest < Test::Unit::TestCase
context "#resource_url" do
should "return a resource URL" do
transaction = Stripe::CustomerBalanceTransaction.construct_from(
id: "cbtxn_123",
customer: "cus_123"
)
assert_equal "/v1/customers/cus_123/customer_balance_transactions/cbtxn_123",
transaction.resource_url
end

should "raise without a customer" do
transaction = Stripe::CustomerBalanceTransaction.construct_from(id: "cbtxn_123")
assert_raises NotImplementedError do
transaction.resource_url
end
end
end

should "raise on #retrieve" do
assert_raises NotImplementedError do
Stripe::CustomerBalanceTransaction.retrieve("cbtxn_123")
end
end
end
end
32 changes: 32 additions & 0 deletions test/stripe/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,37 @@ class CustomerTest < Test::Unit::TestCase
assert sources.data.is_a?(Array)
end
end

context "#create_customer_balance_transaction" do
should "create a customer balance transaction" do
Stripe::Customer.create_customer_balance_transaction(
"cus_123",
amount: 1234,
currency: "usd"
)
assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/customer_balance_transactions"
end
end

context "#retrieve_customer_balance_transaction" do
should "retrieve a customer balance transaction" do
Stripe::Customer.retrieve_customer_balance_transaction(
"cus_123",
"cbtxn_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/customer_balance_transactions/cbtxn_123"
end
end

context "#list_customer_balance_transactions" do
should "list the customer balance transactions" do
sources = Stripe::Customer.list_customer_balance_transactions(
"cus_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/customer_balance_transactions"
assert sources.is_a?(Stripe::ListObject)
assert sources.data.is_a?(Array)
end
end
end
end

0 comments on commit b3cc38e

Please sign in to comment.