Skip to content

Commit

Permalink
Refactor Subscription.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Nov 20, 2011
1 parent a23a61c commit 3ed090e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
7 changes: 3 additions & 4 deletions lib/fake_braintree/sinatra_app.rb
Expand Up @@ -35,10 +35,9 @@ class SinatraApp < Sinatra::Base

# Braintree::Subscription.create
post "/merchants/:merchant_id/subscriptions" do
response_hash = Subscription.new(request).response_hash

FakeBraintree.subscriptions[response_hash["id"]] = response_hash
gzipped_response(201, response_hash.to_xml(:root => 'subscription'))
subscription_hash = Hash.from_xml(request.body).delete("subscription")
options = {:merchant_id => params[:merchant_id]}
Subscription.new(subscription_hash, options).create
end

# Braintree::Subscription.find
Expand Down
37 changes: 24 additions & 13 deletions lib/fake_braintree/subscription.rb
Expand Up @@ -2,28 +2,39 @@ module FakeBraintree
class Subscription
include Helpers

def initialize(request)
@subscription_hash = Hash.from_xml(request.body).delete("subscription")
def initialize(subscription_hash, options)
@subscription_hash = subscription_hash.merge("merchant_id" => options[:merchant_id],
"id" => options[:id])
end

def response_hash
response_hash = {}
response_hash["id"] = md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6]
response_hash["transactions"] = []
response_hash["add_ons"] = []
response_hash["discounts"] = []
response_hash["plan_id"] = @subscription_hash["plan_id"]
response_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now)
response_hash["payment_method_token"] = @subscription_hash["payment_method_token"]
response_hash["status"] = Braintree::Subscription::Status::Active
def create
hash = subscription_hash
FakeBraintree.subscriptions[hash["id"]] = hash
gzipped_response(201, hash.to_xml(:root => 'subscription'))
end

def subscription_hash
subscription_hash = @subscription_hash.dup
subscription_hash["id"] ||= subscription_id
subscription_hash["transactions"] = []
subscription_hash["add_ons"] = []
subscription_hash["discounts"] = []
subscription_hash["plan_id"] = @subscription_hash["plan_id"]
subscription_hash["next_billing_date"] = braintree_formatted_date(1.month.from_now)
subscription_hash["payment_method_token"] = @subscription_hash["payment_method_token"]
subscription_hash["status"] = Braintree::Subscription::Status::Active

response_hash
subscription_hash
end

private

def braintree_formatted_date(date)
date.strftime('%Y-%m-%d')
end

def subscription_id
md5("#{@subscription_hash["payment_method_token"]}#{Time.now.to_f}")[0,6]
end
end
end
18 changes: 7 additions & 11 deletions spec/fake_braintree/subscription_spec.rb
Expand Up @@ -24,19 +24,15 @@
first_result.subscription.id.should_not == second_result.subscription.id
end

it "stores created subscriptions in FakeBraintree.subscriptions" do
FakeBraintree.subscriptions[create_subscription.subscription.id].should_not be_nil
end

it "sets the next billing date to a string of 1.month.from_now in UTC" do
Timecop.freeze do
result = Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => plan_id)

result.subscription.next_billing_date.should == 1.month.from_now.utc.strftime('%Y-%m-%d')
create_subscription.subscription.next_billing_date.should == 1.month.from_now.utc.strftime('%Y-%m-%d')
end
end

def create_subscription
Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => plan_id)
end
end

describe "Braintree::Subscription.find" do
Expand All @@ -53,6 +49,6 @@ def create_subscription

let(:payment_method_token) { cc_token }
let(:plan_id) { 'my-plan-id' }
let(:subscription_id) { Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id).subscription.id }
let(:subscription_id) { Braintree::Subscription.create(:payment_method_token => payment_method_token,
:plan_id => plan_id).subscription.id }
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -18,6 +18,7 @@

config.include BraintreeHelpers
config.include CustomerHelpers
config.include SubscriptionHelpers

config.before do
FakeBraintree.clear!
Expand Down
6 changes: 6 additions & 0 deletions spec/support/subscription_helpers.rb
@@ -0,0 +1,6 @@
module SubscriptionHelpers
def create_subscription
Braintree::Subscription.create(:payment_method_token => cc_token,
:plan_id => 'my_plan_id')
end
end

0 comments on commit 3ed090e

Please sign in to comment.