Skip to content

Commit

Permalink
Add merchant_acount api
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed May 21, 2015
1 parent 10b50f6 commit 24f6c96
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 1 deletion.
95 changes: 95 additions & 0 deletions lib/fake_braintree/merchant_account.rb
@@ -0,0 +1,95 @@
module FakeBraintree
class MerchantAccount
include Helpers

def initialize(merchant_account_hash_from_params, options)
@merchant_account_hash = {
'id' => options[:id]
}
@merchant_account_hash.merge!(merchant_account_hash_from_params)
set_merchant_account_id
end

def create
if invalid?
response_for_invalid_merchant_account
else
create_merchant_account_with(merchant_account_hash)
response_for_created_merchant_account(merchant_account_hash)
end
end

def update
if merchant_account_exists_in_registry?
updates = merchant_account_hash
updated_merchant_account = update_existing_merchant_account(updates)
response_for_updated_merchant_account(updated_merchant_account)
else
response_for_merchant_account_not_found
end
end

def delete
delete_merchant_account_with_id(merchant_account_id)
deletion_response
end

private

def invalid?
false
end

def create_merchant_account_with(hash)
FakeBraintree.registry.merchant_accounts[hash['id'].to_s] = hash
end

def update_existing_merchant_account(updates_hash)
merchant_account_from_registry.merge!(updates_hash)
end

def merchant_account_hash
@merchant_account_hash
end

def merchant_account_from_registry
FakeBraintree.registry.merchant_accounts[merchant_account_id]
end

def merchant_account_exists_in_registry?
FakeBraintree.registry.merchant_accounts.key?(merchant_account_id)
end

def delete_merchant_account_with_id(id)
FakeBraintree.registry.merchant_accounts[id] = nil
end

def deletion_response
gzipped_response(200, '')
end

def response_for_created_merchant_account(hash)
gzipped_response(201, hash.to_xml(root: 'merchant_account'))
end

def response_for_updated_merchant_account(hash)
gzipped_response(200, hash.to_xml(root: 'merchant_account'))
end

def response_for_merchant_account_not_found
failure_response(404)
end

def failure_response(code)
gzipped_response(code, '')
end

def merchant_account_id
merchant_account_hash['id']
end

def set_merchant_account_id
@merchant_account_hash['id'] ||= create_id(@merchant_account_hash['merchant_id'])
end
end
end
3 changes: 2 additions & 1 deletion lib/fake_braintree/registry.rb
Expand Up @@ -4,7 +4,7 @@ def initialize
end

attr_accessor :customers,:subscriptions, :failures, :transactions, :redirects,
:credit_cards, :addresses, :payment_methods
:credit_cards, :addresses, :payment_methods, :merchant_accounts

def clear!
@addresses = {}
Expand All @@ -15,6 +15,7 @@ def clear!
@redirects = {}
@credit_cards = {}
@payment_methods = {}
@merchant_accounts = {}
end

def failure?(card_number)
Expand Down
25 changes: 25 additions & 0 deletions lib/fake_braintree/sinatra_app.rb
Expand Up @@ -9,6 +9,7 @@
require 'fake_braintree/transaction'
require 'fake_braintree/client_token'
require 'fake_braintree/credit_card_serializer'
require 'fake_braintree/merchant_account'

module FakeBraintree
class SinatraApp < Sinatra::Base
Expand Down Expand Up @@ -303,5 +304,29 @@ def hash_from_request_body_with_key(key)
response = "#{params['callback']}(#{{ status: 200 }.to_json})"
[200, headers, gzip(response)]
end

#Braintree::MerchantAccount.find
get '/merchants/:merchant_id/merchant_accounts/:merchant_account_id' do
merchant_account = FakeBraintree.registry.merchant_accounts[params[:merchant_account_id]]
if merchant_account
gzipped_response(200, merchant_account.to_xml(root: 'merchant_account'))
else
gzipped_response(404, {})
end
end

# Braintree::MerchantAccount.update
put '/merchants/:merchant_id/merchant_accounts/:merchant_account_id/update_via_api' do
merchant_account_hash = hash_from_request_body_with_key('merchant_account')
options = {id: params[:merchant_account_id], merchant_id: params[:merchant_id]}
MerchantAccount.new(merchant_account_hash, options).update
end

# Braintree::MerchantAccount.create
post '/merchants/:merchant_id/merchant_accounts/create_via_api' do
merchant_account_hash = hash_from_request_body_with_key('merchant_account')
options = {merchant_id: params[:merchant_id]}
MerchantAccount.new(merchant_account_hash, options).create
end
end
end
83 changes: 83 additions & 0 deletions spec/fake_braintree/merchant_account_spec.rb
@@ -0,0 +1,83 @@
require 'spec_helper'

describe 'Braintree::MerchantAccount.create' do
it 'successfully creates a merchant account' do
result = Braintree::MerchantAccount.create(
tos_accepted: true,
individual: {
first_name: 'first',
last_name: 'last',
email: 'test@test.com',
date_of_birth: '1971-01-01',
address: {
street_address: '1 E Main St',
locality: 'Chicago',
region: 'Illinois',
postal_code: '60622',
}
},
funding: {
destination: 'bank',
account_number: '9900000000',
routing_number: '021000021'
}
)
expect(result).to be_success
end

end

describe 'Braintree::MerchantAccount.find' do
it 'successfully finds a merchant account' do
result = Braintree::MerchantAccount.create(
individual: {
first_name: 'Bob',
last_name: 'Smith'
}
)
expect(Braintree::MerchantAccount.find(result.merchant_account.id).individual_details.first_name).to eq 'Bob'
end

it 'raises an error for a nonexistent customer' do
expect(lambda { Braintree::MerchantAccount.find('foo') }).to raise_error(Braintree::NotFoundError)
end

it 'finds merchant account created with custom id' do
Braintree::MerchantAccount.create(
id: 'bob-smith',
individual: {
first_name: 'Bob',
last_name: 'Smith'
}
)

expect(Braintree::MerchantAccount.find('bob-smith').individual_details.first_name).to eq 'Bob'
end

it 'finds merchant account created with custom integer id' do
Braintree::MerchantAccount.create(
id: 1,
individual: {
first_name: 'Bob',
last_name: 'Smith'
}
)

expect(Braintree::MerchantAccount.find(1).individual_details.first_name).to eq 'Bob'
end
end

describe 'Braintree::MerchantAccount.update' do
it 'successfully updates a merchant account' do
merchant_account_id = create_merchant_account.merchant_account.id
result = Braintree::MerchantAccount.update(merchant_account_id, individual: { first_name: 'Jerry'} )

expect(result).to be_success
expect(Braintree::MerchantAccount.find(merchant_account_id).individual_details.first_name).to eq 'Jerry'
end

it 'raises an error for a nonexistent merchant account' do
expect { Braintree::MerchantAccount.update('foo', { individual: { first_name: 'Bob' } }) }.to raise_error(Braintree::NotFoundError)
end

end
2 changes: 2 additions & 0 deletions spec/fake_braintree/registry_spec.rb
Expand Up @@ -7,6 +7,7 @@
it { should have_hash_accessor_for(:transactions) }
it { should have_hash_accessor_for(:redirects) }
it { should have_hash_accessor_for(:credit_cards) }
it { should have_hash_accessor_for(:merchant_accounts) }
end

describe FakeBraintree::Registry, '#clear!' do
Expand All @@ -16,6 +17,7 @@
it { should clear_hash_when_cleared(:transactions) }
it { should clear_hash_when_cleared(:redirects) }
it { should clear_hash_when_cleared(:credit_cards) }
it { should clear_hash_when_cleared(:merchant_accounts) }
end

describe FakeBraintree::Registry, '#failure?' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -29,6 +29,7 @@
config.include BraintreeHelpers
config.include CustomerHelpers
config.include SubscriptionHelpers
config.include MerchantAccountHelpers
config.include FakeBraintree::Helpers

config.before do
Expand Down
24 changes: 24 additions & 0 deletions spec/support/merchant_account_helpers.rb
@@ -0,0 +1,24 @@
module MerchantAccountHelpers
def create_merchant_account
Braintree::MerchantAccount.create(
tos_accepted: true,
individual: {
first_name: 'first',
last_name: 'last',
email: 'test@test.com',
date_of_birth: '1971-01-01',
address: {
street_address: '1 E Main St',
locality: 'Chicago',
region: 'Illinois',
postal_code: '60622',
}
},
funding: {
destination: 'bank',
account_number: '9900000000',
routing_number: '021000021'
}
)
end
end

0 comments on commit 24f6c96

Please sign in to comment.