From 47fa20af822cd4a794ec1f007ee861f1ac5713d6 Mon Sep 17 00:00:00 2001 From: Tomas Witek Date: Sat, 5 Sep 2015 13:02:28 +0200 Subject: [PATCH 1/3] Multiple merchant account ids for a multiple currency --- app/models/spree/gateway/braintree_gateway.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/spree/gateway/braintree_gateway.rb b/app/models/spree/gateway/braintree_gateway.rb index 56efed73..98573a94 100644 --- a/app/models/spree/gateway/braintree_gateway.rb +++ b/app/models/spree/gateway/braintree_gateway.rb @@ -53,6 +53,7 @@ def capture(amount, authorization_code, ignored_options = {}) end def create_profile(payment) + self.preferred_merchant_account_id += "-#{payment.currency.downcase}" if Spree::Config.get(:currency) != payment.currency if payment.source.gateway_customer_profile_id.nil? response = provider.store(payment.source, options_for_payment(payment)) From dd9c9f6a3fc36506be9990d400753b6cdd073181 Mon Sep 17 00:00:00 2001 From: Tomas Witek Date: Sat, 5 Sep 2015 22:34:32 +0200 Subject: [PATCH 2/3] Throw an exception if merchant_account_id is not defined in Braintree setting --- app/models/spree/gateway/braintree_gateway.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/spree/gateway/braintree_gateway.rb b/app/models/spree/gateway/braintree_gateway.rb index 98573a94..3e13ea50 100644 --- a/app/models/spree/gateway/braintree_gateway.rb +++ b/app/models/spree/gateway/braintree_gateway.rb @@ -53,7 +53,12 @@ def capture(amount, authorization_code, ignored_options = {}) end def create_profile(payment) - self.preferred_merchant_account_id += "-#{payment.currency.downcase}" if Spree::Config.get(:currency) != payment.currency + 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)) From b4c7213c7adf06f8d7ed75a1254110ea359c6b8b Mon Sep 17 00:00:00 2001 From: Tomas Witek Date: Sat, 5 Sep 2015 22:35:00 +0200 Subject: [PATCH 3/3] Specs for a Braintree mutlicurrency setup --- spec/models/gateway/braintree_gateway_spec.rb | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/spec/models/gateway/braintree_gateway_spec.rb b/spec/models/gateway/braintree_gateway_spec.rb index 437affba..84baec30 100644 --- a/spec/models/gateway/braintree_gateway_spec.rb +++ b/spec/models/gateway/braintree_gateway_spec.rb @@ -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