Skip to content

Commit

Permalink
Add Braintree::Subscription.update
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Nov 20, 2011
1 parent 0a12182 commit fab3c92
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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`
Expand Down
7 changes: 7 additions & 0 deletions lib/fake_braintree/sinatra_app.rb
Expand Up @@ -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])
Expand Down
16 changes: 16 additions & 0 deletions lib/fake_braintree/subscription.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/fake_braintree/subscription_spec.rb
Expand Up @@ -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.