Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Berke-Williams committed Oct 24, 2011
1 parent 7bca081 commit 0e6f4f5
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/fake_braintree/sinatra_app.rb
Expand Up @@ -18,20 +18,25 @@ def gzip(content)
def gzipped_response(status_code, uncompressed_content)
[status_code, { "Content-Encoding" => "gzip" }, gzip(uncompressed_content)]
end

def md5(content)
Digest::MD5.hexdigest(content)
end
end

# Braintree::Customer.create
post "/merchants/:merchant_id/customers" do
customer = Hash.from_xml(request.body).delete("customer")
if FakeBraintree.failure?(customer["credit_card"]["number"])
gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
else
customer["id"] ||= Digest::MD5.hexdigest("#{params[:merchant_id]}#{Time.now.to_f}")
customer["id"] ||= md5("#{params[:merchant_id]}#{Time.now.to_f}")
customer["merchant-id"] = params[:merchant_id]
if customer["credit_card"] && customer["credit_card"].is_a?(Hash)
customer["credit_card"].delete("__content__")
if !customer["credit_card"].empty?
customer["credit_card"]["last_4"] = customer["credit_card"].delete("number")[-4..-1]
customer["credit_card"]["token"] = Digest::MD5.hexdigest("#{customer['merchant_id']}#{customer['id']}#{Time.now.to_f}")
customer["credit_card"]["token"] = md5("#{customer['merchant_id']}#{customer['id']}#{Time.now.to_f}")
expiration_date = customer["credit_card"].delete("expiration_date")
customer["credit_card"]["expiration_month"] = expiration_date.split('/')[0]
customer["credit_card"]["expiration_year"] = expiration_date.split('/')[1]
Expand All @@ -52,22 +57,22 @@ def gzipped_response(status_code, uncompressed_content)

put "/merchants/:merchant_id/customers/:id" do
customer = Hash.from_xml(request.body).delete("customer")
if !FakeBraintree.failure?(customer["credit_card"]["number"])
customer["id"] = params[:id]
if FakeBraintree.failure?(customer["credit_card"]["number"])
gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
else
customer["id"] = params[:id]
customer["merchant-id"] = params[:merchant_id]
if customer["credit_card"] && customer["credit_card"].is_a?(Hash)
customer["credit_card"].delete("__content__")
if !customer["credit_card"].empty?
customer["credit_card"]["last_4"] = customer["credit_card"].delete("number")[-4..-1]
customer["credit_card"]["token"] = Digest::MD5.hexdigest("#{customer['merchant_id']}#{customer['id']}#{Time.now.to_f}")
customer["credit_card"]["token"] = md5("#{customer['merchant_id']}#{customer['id']}#{Time.now.to_f}")
credit_card = customer.delete("credit_card")
customer["credit_cards"] = [credit_card]
end
end
FakeBraintree.customers[params["id"]] = customer
gzipped_response(200, customer.to_xml(:root => 'customer'))
else
gzipped_response(422, FakeBraintree.failure_response(customer["credit_card"]["number"]).to_xml(:root => 'api_error_response'))
end
end

Expand All @@ -76,10 +81,11 @@ def gzipped_response(status_code, uncompressed_content)
gzipped_response(200, "")
end

# Braintree::Subscription.create
post "/merchants/:merchant_id/subscriptions" do
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<subscription>\n <plan-id type=\"integer\">2</plan-id>\n <payment-method-token>b22x</payment-method-token>\n</subscription>\n"
subscription = Hash.from_xml(request.body).delete("subscription")
subscription["id"] ||= Digest::MD5.hexdigest("#{subscription["payment_method_token"]}#{Time.now.to_f}")
subscription["id"] ||= md5("#{subscription["payment_method_token"]}#{Time.now.to_f}")
subscription["transactions"] = []
subscription["add_ons"] = []
subscription["discounts"] = []
Expand All @@ -97,16 +103,22 @@ def gzipped_response(status_code, uncompressed_content)
put "/merchants/:merchant_id/subscriptions/:id" do
subscription = Hash.from_xml(request.body).delete("subscription")
subscription["transactions"] = []
subscription["add_ons"] = []
subscription["discounts"] = []
subscription["add_ons"] = []
subscription["discounts"] = []
FakeBraintree.subscriptions[params["id"]] = subscription
gzipped_response(200, subscription.to_xml(:root => 'subscription'))
end

# Braintree::Transaction.search
post "/merchants/:merchant_id/transactions/advanced_search_ids" do
# "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<search>\n <created-at>\n <min type=\"datetime\">2011-01-10T14:14:26Z</min>\n </created-at>\n</search>\n"
gzipped_response(200, "<search-results>\n <page-size type=\"integer\">50</page-size>\n <ids type=\"array\">\n <item>49sbx6</item>\n </ids>\n</search-results>\n")
gzipped_response(200,
['<search-results>',
' <page-size type="integer">50</page-size>',
' <ids type="array">',
' <item>49sbx6</item>',
' </ids>',
"</search-results>\n"].join("\n"))
end

# Braintree::Transaction.search
Expand All @@ -123,13 +135,12 @@ def gzipped_response(status_code, uncompressed_content)
# Braintree::Transaction.sale
# Braintree::CreditCard.sale
post "/merchants/:merchant_id/transactions" do
transaction = Hash.from_xml(request.body)["transaction"]
transaction_id = Digest::MD5.hexdigest("#{params[:merchant_id]}#{Time.now.to_f}")
transaction_response = {"id" => transaction_id, "amount" => transaction["amount"]}

if FakeBraintree.decline_all_cards?
gzipped_response(422, FakeBraintree.create_failure.to_xml(:root => 'api_error_response'))
else
transaction = Hash.from_xml(request.body)["transaction"]
transaction_id = md5("#{params[:merchant_id]}#{Time.now.to_f}")
transaction_response = {"id" => transaction_id, "amount" => transaction["amount"]}
FakeBraintree.transaction.replace(transaction_response)
gzipped_response(200, transaction_response.to_xml(:root => "transaction"))
end
Expand Down

0 comments on commit 0e6f4f5

Please sign in to comment.