Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CreditNote #758

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay static method!

assert credit_note.is_a?(Stripe::CreditNote)
end
end
end
end