VRFS-2785 : Update recurly client with refund functionality. Unit test to verify.

This commit is contained in:
Steven Miers 2015-02-19 16:40:19 -06:00
parent 0907c1acd1
commit bcd3785b45
2 changed files with 58 additions and 0 deletions

View File

@ -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?)

View File

@ -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()