Permalink
Browse files

Flush the remote subscription cache after retrying a charge

  • Loading branch information...
1 parent 401d0c8 commit e3cd30d0e821fed05acb6cad6386958a5c31a74d @jferris jferris committed Dec 7, 2011
Showing with 61 additions and 4 deletions.
  1. +1 −0 lib/ashmont.rb
  2. +12 −2 lib/ashmont/subscription.rb
  3. +48 −2 spec/ashmont/subscription_spec.rb
View
@@ -5,6 +5,7 @@
module Ashmont
class << self
attr_accessor :merchant_account_time_zone
+ attr_accessor :merchant_account_id
end
self.merchant_account_time_zone = 'Eastern Time (US & Canada)'
@@ -14,16 +14,18 @@ def initialize(token = nil)
delegate :transactions, :status, :to => :remote_subscription
def save(attributes)
+ attributes_for_merchant = add_merchant_to_attributes(attributes)
if token
- update(attributes)
+ update(attributes_for_merchant)
else
- create(attributes)
+ create(attributes_for_merchant)
end
end
def retry_charge
transaction = Braintree::Subscription.retry_charge(token).transaction
result = Braintree::Transaction.submit_for_settlement(transaction.id)
+ reload
if result.success?
true
else
@@ -47,6 +49,14 @@ def reload
private
+ def add_merchant_to_attributes(attributes)
+ if Ashmont.merchant_account_id
+ attributes.merge(:merchant_account_id => Ashmont.merchant_account_id)
+ else
+ attributes
+ end
+ end
+
def create(attributes)
result = Braintree::Subscription.create(attributes)
if result.success?
@@ -66,14 +66,28 @@
Ashmont::Errors.should have_received(:new).with(transaction, error_messages)
end
+ it "reloads the subscription after retrying a charge" do
+ transaction = stub("transaction", :id => 'abc')
+ retry_result = stub("retry-result", :transaction => transaction)
+ settlement_result = stub("settlement-result", :success? => true)
+ past_due_subscription = stub("subscription", :status=> "past_due")
+ active_subscription = stub("subscription", :status=> "active")
+ Braintree::Subscription.stubs(:retry_charge => retry_result)
+ Braintree::Transaction.stubs(:submit_for_settlement => settlement_result)
+ Braintree::Subscription.stubs(:find).returns(past_due_subscription).then.returns(active_subscription)
+
+ subscription = Ashmont::Subscription.new('xyz')
+ expect { subscription.retry_charge }.to change { subscription.status }.from("past_due").to("active")
+ end
+
it "updates a subscription" do
token = 'xyz'
Braintree::Subscription.stubs(:update => 'expected')
subscription = Ashmont::Subscription.new(token)
result = subscription.save(:name => "Billy")
- Braintree::Subscription.should have_received(:update).with(token, :name => "Billy")
+ Braintree::Subscription.should have_received(:update).with(token, has_entries(:name => "Billy"))
result.should == "expected"
end
@@ -87,11 +101,35 @@
subscription = Ashmont::Subscription.new
subscription.save(attributes).should be_true
- Braintree::Subscription.should have_received(:create).with(attributes)
+ Braintree::Subscription.should have_received(:create).with(has_entries(attributes))
subscription.token.should == token
subscription.status.should == "fine"
end
+ it "passes a configured merchant account id" do
+ remote_subscription = stub('remote-subscription', :id => "xyz", :status => "fine")
+ result = stub("result", :subscription => remote_subscription, :success? => true)
+ Braintree::Subscription.stubs(:create => result)
+
+ with_configured_merchant_acount_id do |merchant_account_id|
+ subscription = Ashmont::Subscription.new
+ subscription.save({})
+
+ Braintree::Subscription.should have_received(:create).with(has_entries(:merchant_account_id => merchant_account_id))
+ end
+ end
+
+ it "doesn't pass a merchant account id when not is configured" do
+ remote_subscription = stub('remote-subscription', :id => "xyz", :status => "fine")
+ result = stub("result", :subscription => remote_subscription, :success? => true)
+ Braintree::Subscription.stubs(:create => result)
+
+ subscription = Ashmont::Subscription.new
+ subscription.save({})
+
+ Braintree::Subscription.should have_received(:create).with(has_entries(:merchant_account_id => nil)).never
+ end
+
it "returns the most recent transaction" do
Timecop.freeze(Time.now) do
dates = [2.days.ago, 3.days.ago, 1.day.ago]
@@ -113,4 +151,12 @@
subscription.status.should == "old"
subscription.reload.status.should == "new"
end
+
+ def with_configured_merchant_acount_id
+ merchant_account_id = "jkl"
+ Ashmont.merchant_account_id = merchant_account_id
+ yield merchant_account_id
+ ensure
+ Ashmont.merchant_account_id = nil
+ end
end

0 comments on commit e3cd30d

Please sign in to comment.