From bcd3785b4506b06e373a686542b31c45566ff4d9 Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Thu, 19 Feb 2015 16:40:19 -0600 Subject: [PATCH] VRFS-2785 : Update recurly client with refund functionality. Unit test to verify. --- ruby/lib/jam_ruby/recurly_client.rb | 40 +++++++++++++++++++++++ ruby/spec/jam_ruby/recurly_client_spec.rb | 18 ++++++++++ 2 files changed, 58 insertions(+) diff --git a/ruby/lib/jam_ruby/recurly_client.rb b/ruby/lib/jam_ruby/recurly_client.rb index 419e3a377..eb70824c9 100644 --- a/ruby/lib/jam_ruby/recurly_client.rb +++ b/ruby/lib/jam_ruby/recurly_client.rb @@ -70,6 +70,46 @@ module JamRuby account end + def refund_user_subscription(current_user, jam_track) + jam_track_right=JamRuby::JamTrackRight.where("user_id=? AND jam_track_id=?", current_user.id, jam_track.id).first + if jam_track_right + refund_subscription(jam_track_right) + else + raise RecurlyClientError, "The user #{current_user} does not have a subscription to #{jam_track}" + end + end + + def refund_subscription(jam_track_right) + account = get_account(jam_track_right.user) + if (account.present?) + terminated = false + begin + jam_track = jam_track_right.jam_track + account.subscriptions.find_each do |subscription| + puts "subscription.plan.plan_code: #{subscription.plan.plan_code} / #{jam_track.plan_code} / #{subscription.plan.plan_code == jam_track.plan_code}" + if(subscription.plan.plan_code == jam_track.plan_code) + subscription.terminate(:full) + raise RecurlyClientError.new(subscription.errors) if subscription.errors.any? + terminated = true + end + end + + if terminated + jam_track_right.destroy() + else + raise RecurlyClientError, "Subscription '#{jam_track.plan_code}' not found for this user; could not issue refund." + end + + rescue Recurly::Error, NoMethodError => x + raise RecurlyClientError, x.to_s + end + + else + raise RecurlyClientError, "Could not find account to refund order." + end + account + end + def place_order(current_user, jam_track) account = get_account(current_user) if (account.present?) diff --git a/ruby/spec/jam_ruby/recurly_client_spec.rb b/ruby/spec/jam_ruby/recurly_client_spec.rb index 1cf58722f..82217851d 100644 --- a/ruby/spec/jam_ruby/recurly_client_spec.rb +++ b/ruby/spec/jam_ruby/recurly_client_spec.rb @@ -98,6 +98,24 @@ describe RecurlyClient do @user.jam_track_rights.last.jam_track.id.should eq(@jamtrack.id) end + it "can refund subscription" do + @client.find_or_create_account(@user, @billing_info) + + # Place order: + expect{@client.place_order(@user, @jamtrack)}.not_to raise_error() + active_subs=@client.get_account(@user).subscriptions.find_all{|t|t.state=='active'} + @jamtrack.reload + @jamtrack.jam_track_rights.should have(1).items + + # Refund: + expect{@client.refund_user_subscription(@user, @jamtrack)}.not_to raise_error() + active_subs=@client.get_account(@user).subscriptions.find_all{|t|t.state=='active'} + active_subs.should have(0).items + + @jamtrack.reload + @jamtrack.jam_track_rights.should have(0).items + end + it "detects error on double order" do @client.find_or_create_account(@user, @billing_info) expect{@client.place_order(@user, @jamtrack)}.not_to raise_error()