Skip to content
Permalink
Browse files

Refactor Subscription.

  • Loading branch information
Gabe Berke-Williams
Gabe Berke-Williams committed Nov 20, 2011
1 parent a23a61c commit 3ed090e5d16286cb2d7cd83e8451739e41dada5d
@@ -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
@@ -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
@@ -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
@@ -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
@@ -18,6 +18,7 @@

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

config.before do
FakeBraintree.clear!
@@ -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.
You can’t perform that action at this time.