Permalink
Browse files

Add Braintree::Subscription.update

  • Loading branch information...
1 parent 0a12182 commit fab3c92c41a4a9c7af49513c9640b415069127d3 Gabe Berke-Williams committed Nov 20, 2011
Showing with 34 additions and 0 deletions.
  1. +1 −0 README.md
  2. +7 −0 lib/fake_braintree/sinatra_app.rb
  3. +16 −0 lib/fake_braintree/subscription.rb
  4. +10 −0 spec/fake_braintree/subscription_spec.rb
View
@@ -17,6 +17,7 @@ Currently in alpha (i.e. it does not support every Braintree call).
### Subscription
* `Braintree::Subscription.find`
* `Braintree::Subscription.create`
+* `Braintree::Subscription.update`
### CreditCard
* `Braintree::CreditCard.find`
@@ -50,6 +50,13 @@ class SinatraApp < Sinatra::Base
end
end
+ # Braintree::Subscription.update
+ put "/merchants/:merchant_id/subscriptions/:id" do
+ subscription_hash = Hash.from_xml(request.body).delete("subscription")
+ options = {:id => params[:id], :merchant_id => params[:merchant_id]}
+ Subscription.new(subscription_hash, options).update
+ end
+
# Braintree::CreditCard.find
get "/merchants/:merchant_id/payment_methods/:credit_card_token" do
credit_card = FakeBraintree.credit_card_from_token(params[:credit_card_token])
@@ -13,6 +13,13 @@ def create
gzipped_response(201, hash.to_xml(:root => 'subscription'))
end
+ def update
+ if existing_subscription_hash
+ hash = update_existing_subscription!
+ gzipped_response(200, hash.to_xml(:root => 'subscription'))
+ end
+ end
+
def subscription_hash
subscription_hash = @subscription_hash.dup
subscription_hash["id"] ||= subscription_id
@@ -29,6 +36,15 @@ def subscription_hash
private
+ def existing_subscription_hash
+ @subscription_hash['id'] && FakeBraintree.subscriptions[@subscription_hash["id"]]
+ end
+
+ def update_existing_subscription!
+ new_hash = existing_subscription_hash.merge(subscription_hash)
+ FakeBraintree.subscriptions[@subscription_hash['id']] = new_hash
+ end
+
def braintree_formatted_date(date)
date.strftime('%Y-%m-%d')
end
@@ -52,3 +52,13 @@
let(:subscription_id) { Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id).subscription.id }
end
+
+describe "Braintree::Subscription.update" do
+ it "can update a subscription" do
+ Braintree::Subscription.update(subscription_id, :plan_id => 'a_new_plan')
+ Braintree::Subscription.find(subscription_id).plan_id.should == 'a_new_plan'
+ end
+
+ let(:subscription_id) { subscription.subscription.id }
+ let(:subscription) { create_subscription }
+end

0 comments on commit fab3c92

Please sign in to comment.