Skip to content

Commit

Permalink
Merge pull request #758 from stripe/remi-add-creditnote
Browse files Browse the repository at this point in the history
Add support for CreditNote
  • Loading branch information
remi-stripe authored Apr 18, 2019
2 parents d915cd6 + 0233deb commit 06498db
Show file tree
Hide file tree
Showing 4 changed files with 82 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 @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions lib/stripe/credit_note.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions test/stripe/credit_note_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 06498db

Please sign in to comment.