From bbec79ccba10f46c9b3e5370f32c6f46da13361f Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Sun, 14 Oct 2018 09:00:32 -0700 Subject: [PATCH] Add new methods to the Invoice resource --- lib/stripe/invoice.rb | 37 +++++++++++++++++++++-------- test/stripe/coupon_test.rb | 7 ++++++ test/stripe/invoice_test.rb | 47 +++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 10 deletions(-) diff --git a/lib/stripe/invoice.rb b/lib/stripe/invoice.rb index d3126f373..24d09fd6c 100644 --- a/lib/stripe/invoice.rb +++ b/lib/stripe/invoice.rb @@ -4,28 +4,45 @@ module Stripe class Invoice < APIResource extend Stripe::APIOperations::List include Stripe::APIOperations::Save + include Stripe::APIOperations::Delete extend Stripe::APIOperations::Create OBJECT_NAME = "invoice".freeze - def self.upcoming(params, opts = {}) - resp, opts = request(:get, upcoming_url, params, opts) - Util.convert_to_stripe_object(resp.data, opts) + def finalize_invoice(params = {}, opts = {}) + url = resource_url + "/finalize" + resp, opts = request(:post, url, params, opts) + initialize_from(resp.data, opts) + end + + def mark_uncollectible(params = {}, opts = {}) + url = resource_url + "/mark_uncollectible" + resp, opts = request(:post, url, params, opts) + initialize_from(resp.data, opts) end def pay(params = {}, opts = {}) - resp, opts = request(:post, pay_url, params, opts) + url = resource_url + "/pay" + resp, opts = request(:post, url, params, opts) initialize_from(resp.data, opts) end - def self.upcoming_url - resource_url + "/upcoming" + def send_invoice(params = {}, opts = {}) + url = resource_url + "/send" + resp, opts = request(:post, url, params, opts) + initialize_from(resp.data, opts) + end + + def self.upcoming(params, opts = {}) + url = resource_url + "/upcoming" + resp, opts = request(:get, url, params, opts) + Util.convert_to_stripe_object(resp.data, opts) end - private_class_method :upcoming_url - def pay_url - resource_url + "/pay" + def void_invoice(params = {}, opts = {}) + url = resource_url + "/void" + resp, opts = request(:post, url, params, opts) + initialize_from(resp.data, opts) end - private :pay_url end end diff --git a/test/stripe/coupon_test.rb b/test/stripe/coupon_test.rb index ab6a21c08..14980d013 100644 --- a/test/stripe/coupon_test.rb +++ b/test/stripe/coupon_test.rb @@ -40,5 +40,12 @@ class CouponTest < Test::Unit::TestCase assert_requested :post, "#{Stripe.api_base}/v1/coupons/25OFF" assert coupon.is_a?(Stripe::Coupon) end + + should "be deletable" do + coupon = Stripe::Coupon.retrieve("25OFF") + coupon = coupon.delete + assert_requested :delete, "#{Stripe.api_base}/v1/coupons/#{coupon.id}" + assert coupon.is_a?(Stripe::Coupon) + end end end diff --git a/test/stripe/invoice_test.rb b/test/stripe/invoice_test.rb index 773916f8f..a0e89110e 100644 --- a/test/stripe/invoice_test.rb +++ b/test/stripe/invoice_test.rb @@ -38,6 +38,33 @@ class InvoiceTest < Test::Unit::TestCase assert invoice.is_a?(Stripe::Invoice) end + should "be deletable" do + invoice = Stripe::Invoice.retrieve("in_123") + invoice = invoice.delete + assert_requested :delete, "#{Stripe.api_base}/v1/invoices/#{invoice.id}" + assert invoice.is_a?(Stripe::Invoice) + end + + context "#finalize" do + should "finalize invoice" do + invoice = Stripe::Invoice.retrieve("in_123") + invoice = invoice.finalize_invoice + assert_requested :post, + "#{Stripe.api_base}/v1/invoices/#{invoice.id}/finalize" + assert invoice.is_a?(Stripe::Invoice) + end + end + + context "#mark_uncollectible" do + should "mark invoice as uncollectible" do + invoice = Stripe::Invoice.retrieve("in_123") + invoice = invoice.mark_uncollectible + assert_requested :post, + "#{Stripe.api_base}/v1/invoices/#{invoice.id}/mark_uncollectible" + assert invoice.is_a?(Stripe::Invoice) + end + end + context "#pay" do should "pay invoice" do invoice = Stripe::Invoice.retrieve("in_123") @@ -61,6 +88,16 @@ class InvoiceTest < Test::Unit::TestCase end end + context "#send" do + should "send invoice" do + invoice = Stripe::Invoice.retrieve("in_123") + invoice = invoice.send_invoice + assert_requested :post, + "#{Stripe.api_base}/v1/invoices/#{invoice.id}/send" + assert invoice.is_a?(Stripe::Invoice) + end + end + context "#upcoming" do should "retrieve upcoming invoices" do invoice = Stripe::Invoice.upcoming( @@ -109,5 +146,15 @@ class InvoiceTest < Test::Unit::TestCase assert invoice.is_a?(Stripe::Invoice) end end + + context "#void" do + should "void invoice" do + invoice = Stripe::Invoice.retrieve("in_123") + invoice = invoice.void_invoice + assert_requested :post, + "#{Stripe.api_base}/v1/invoices/#{invoice.id}/void" + assert invoice.is_a?(Stripe::Invoice) + end + end end end