From 8df3b743812800c67a5652eeea8a0d34c683f60a Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Fri, 3 May 2019 21:07:13 -0700 Subject: [PATCH] Add support for Customer Balance Transaction resource and APIs --- lib/stripe/customer_balance_transaction.rb | 27 ++++++++++++ lib/stripe/object_types.rb | 1 + lib/stripe/resources.rb | 1 + lib/stripe/resources/customer.rb | 3 ++ .../customer_balance_transaction_test.rb | 37 ++++++++++++++++ test/stripe/customer_test.rb | 43 +++++++++++++++++++ 6 files changed, 112 insertions(+) create mode 100644 lib/stripe/customer_balance_transaction.rb create mode 100644 test/stripe/customer_balance_transaction_test.rb diff --git a/lib/stripe/customer_balance_transaction.rb b/lib/stripe/customer_balance_transaction.rb new file mode 100644 index 000000000..79567017c --- /dev/null +++ b/lib/stripe/customer_balance_transaction.rb @@ -0,0 +1,27 @@ +# 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)}/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 + + def self.update(_id, _params = nil, _opts = nil) + raise NotImplementedError, + "Customer Balance Transactions cannot be retrieved without a customer ID. " \ + "Update a Customer Balance Transaction using Customer.update_balance_transaction('cus_123', 'cbtxn_123', params)" + end + end +end diff --git a/lib/stripe/object_types.rb b/lib/stripe/object_types.rb index 906e59cf0..acc8052fb 100644 --- a/lib/stripe/object_types.rb +++ b/lib/stripe/object_types.rb @@ -29,6 +29,7 @@ def self.object_names_to_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, diff --git a/lib/stripe/resources.rb b/lib/stripe/resources.rb index 30020621d..25a296448 100644 --- a/lib/stripe/resources.rb +++ b/lib/stripe/resources.rb @@ -19,6 +19,7 @@ require "stripe/resources/coupon" require "stripe/resources/credit_note" require "stripe/resources/customer" +require "stripe/customer_balance_transaction" require "stripe/resources/discount" require "stripe/resources/dispute" require "stripe/resources/ephemeral_key" diff --git a/lib/stripe/resources/customer.rb b/lib/stripe/resources/customer.rb index 3b703829e..94d20b4ce 100644 --- a/lib/stripe/resources/customer.rb +++ b/lib/stripe/resources/customer.rb @@ -19,6 +19,9 @@ class Customer < APIResource nested_resource_class_methods :source, operations: %i[create retrieve update delete list] + nested_resource_class_methods :balance_transaction, + operations: %i[create retrieve update 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/test/stripe/customer_balance_transaction_test.rb b/test/stripe/customer_balance_transaction_test.rb new file mode 100644 index 000000000..b577c5259 --- /dev/null +++ b/test/stripe/customer_balance_transaction_test.rb @@ -0,0 +1,37 @@ +# 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/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 + + should "raise on #update" do + assert_raises NotImplementedError do + Stripe::CustomerBalanceTransaction.update("cbtxn_123") + end + end + end +end diff --git a/test/stripe/customer_test.rb b/test/stripe/customer_test.rb index ad61bd90c..1eefcfa74 100644 --- a/test/stripe/customer_test.rb +++ b/test/stripe/customer_test.rb @@ -222,5 +222,48 @@ class CustomerTest < Test::Unit::TestCase assert sources.data.is_a?(Array) end end + + context "#create_balance_transaction" do + should "create a customer balance transaction" do + Stripe::Customer.create_balance_transaction( + "cus_123", + amount: 1234, + currency: "usd" + ) + assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions" + end + end + + context "#retrieve_balance_transaction" do + should "retrieve a customer balance transaction" do + Stripe::Customer.retrieve_balance_transaction( + "cus_123", + "cbtxn_123" + ) + assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions/cbtxn_123" + end + end + + context "#update_balance_transaction" do + should "update a customer balance transaction" do + Stripe::Customer.update_balance_transaction( + "cus_123", + "cbtxn_123", + description: "new" + ) + assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions/cbtxn_123" + end + end + + context "#list_balance_transactions" do + should "list the customer balance transactions" do + sources = Stripe::Customer.list_balance_transactions( + "cus_123" + ) + assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions" + assert sources.is_a?(Stripe::ListObject) + assert sources.data.is_a?(Array) + end + end end end