Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Braintree - Multiple Merchant Account IDs for a multiple currency #235

Open
wants to merge 3 commits into
base: 2-4-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/spree/gateway/braintree_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def capture(amount, authorization_code, ignored_options = {})
end

def create_profile(payment)
if Spree::Config.get(:currency) != payment.currency
if preferred_merchant_account_id.blank?
fail 'Preference merchant_account_id has to be defined when using multiple currencies'
end
self.preferred_merchant_account_id += "-#{payment.currency.downcase}"
end
if payment.source.gateway_customer_profile_id.nil?
response = provider.store(payment.source, options_for_payment(payment))

Expand Down
46 changes: 45 additions & 1 deletion spec/models/gateway/braintree_gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,56 @@
name: 'John Doe',
cc_type: 'mastercard')

@payment = create(:payment, source: @credit_card, order: order, payment_method: @gateway, amount: 10.00)
@payment = build(:payment, source: @credit_card, order: order, payment_method: @gateway, amount: 10.00)
@payment.payment_method.environment = 'test'
end

context 'with a multi currency setup' do
before do
allow(@payment).to receive(:currency).and_return('EUR')
expect(Spree::Config.get(:currency)).not_to eq(@payment.currency)
end

it 'throws an exception if merchant_account_id is not defined' do
expect {
@payment.save
}.to raise_exception('Preference merchant_account_id has to be defined when using multiple currencies')
end

it 'appends a currency code to the merchant_account_id' do
@gateway.preferences[:merchant_account_id] = 'my_account_id'

expect {
@payment.save
}.to raise_exception
expect(@gateway.preferences[:merchant_account_id]).to eq('my_account_id-eur')
end
end

context 'with a single currency setup' do
before do
expect(Spree::Config.get(:currency)).to eq(@payment.currency)
end

it 'does not throw an exception if merchant_account_id is not defined' do
expect {
@payment.save
}.not_to raise_exception
end

it 'does not append a currency code to the merchant_account_id' do
@gateway.preferences[:merchant_account_id] = 'my_account_id'

expect {
@payment.save
}.to raise_exception
expect(@gateway.preferences[:merchant_account_id]).to eq('my_account_id')
end
end

context 'when a credit card is created' do
it 'it has the address associated on the remote payment profile' do
@payment.save
remote_customer = @gateway.provider.instance_variable_get(:@braintree_gateway).customer.find(@credit_card.gateway_customer_profile_id)
remote_address = remote_customer.addresses.first rescue nil
expect(remote_address).not_to be_nil
Expand Down