From 0233deb5c6a2ab776efdbb2047f3abecd558f0aa Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Fri, 5 Apr 2019 18:18:59 -0700 Subject: [PATCH] Add support for CreditNote --- lib/stripe.rb | 1 + lib/stripe/credit_note.rb | 19 ++++++++++ lib/stripe/util.rb | 1 + test/stripe/credit_note_test.rb | 61 +++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 lib/stripe/credit_note.rb create mode 100644 test/stripe/credit_note_test.rb diff --git a/lib/stripe.rb b/lib/stripe.rb index f9d86619a..915c367d7 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -52,6 +52,7 @@ require "stripe/checkout/session" require "stripe/country_spec" require "stripe/coupon" +require "stripe/credit_note" require "stripe/customer" require "stripe/discount" require "stripe/dispute" diff --git a/lib/stripe/credit_note.rb b/lib/stripe/credit_note.rb new file mode 100644 index 000000000..c1120b92e --- /dev/null +++ b/lib/stripe/credit_note.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Stripe + class CreditNote < APIResource + extend Stripe::APIOperations::List + include Stripe::APIOperations::Save + extend Stripe::APIOperations::Create + + OBJECT_NAME = "credit_note".freeze + + custom_method :void_credit_note, http_verb: :post, http_path: "void" + + def void_credit_note(params = {}, opts = {}) + url = resource_url + "/void" + resp, opts = request(:post, url, params, opts) + initialize_from(resp.data, opts) + end + end +end diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index e641be324..99cead606 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -61,6 +61,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength Checkout::Session::OBJECT_NAME => Checkout::Session, CountrySpec::OBJECT_NAME => CountrySpec, Coupon::OBJECT_NAME => Coupon, + CreditNote::OBJECT_NAME => CreditNote, Customer::OBJECT_NAME => Customer, Discount::OBJECT_NAME => Discount, Dispute::OBJECT_NAME => Dispute, diff --git a/test/stripe/credit_note_test.rb b/test/stripe/credit_note_test.rb new file mode 100644 index 000000000..58e50b450 --- /dev/null +++ b/test/stripe/credit_note_test.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class CreditNoteTest < Test::Unit::TestCase + should "be listable" do + credit_notes = Stripe::CreditNote.list + assert_requested :get, "#{Stripe.api_base}/v1/credit_notes" + assert credit_notes.data.is_a?(Array) + assert credit_notes.first.is_a?(Stripe::CreditNote) + end + + should "be retrievable" do + credit_note = Stripe::CreditNote.retrieve("cn_123") + assert_requested :get, "#{Stripe.api_base}/v1/credit_notes/cn_123" + assert credit_note.is_a?(Stripe::CreditNote) + end + + should "be creatable" do + credit_note = Stripe::CreditNote.create( + amount: 100, + invoice: "in_123", + reason: "duplicate" + ) + assert_requested :post, "#{Stripe.api_base}/v1/credit_notes" + assert credit_note.is_a?(Stripe::CreditNote) + end + + should "be saveable" do + credit_note = Stripe::CreditNote.retrieve("cn_123") + credit_note.metadata["key"] = "value" + credit_note.save + assert_requested :post, "#{Stripe.api_base}/v1/credit_notes/#{credit_note.id}" + end + + should "be updateable" do + credit_note = Stripe::CreditNote.update("cn_123", metadata: { key: "value" }) + assert_requested :post, "#{Stripe.api_base}/v1/credit_notes/cn_123" + assert credit_note.is_a?(Stripe::CreditNote) + end + + context "#void_credit_note" do + should "void credit_note" do + credit_note = Stripe::CreditNote.retrieve("cn_123") + credit_note = credit_note.void_credit_note + assert_requested :post, + "#{Stripe.api_base}/v1/credit_notes/#{credit_note.id}/void" + assert credit_note.is_a?(Stripe::CreditNote) + end + end + + context ".void_credit_note" do + should "void credit_note" do + credit_note = Stripe::CreditNote.void_credit_note("cn_123") + assert_requested :post, "#{Stripe.api_base}/v1/credit_notes/cn_123/void" + assert credit_note.is_a?(Stripe::CreditNote) + end + end + end +end