Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |