Skip to content

Commit

Permalink
Add email to Braintree customer
Browse files Browse the repository at this point in the history
Issue: solidusio#309

Braintree comes with free basic fraud protection. One of the tools
is Risk Threshold Rules (velocity checks) [1]. One of the checks is
checking amount of transactions per customer email. However, the email
was not being attached to the customer when creating it before the
transaction.

This fixes that so developers can utilize this check.

[1] https://developer.paypal.com/braintree/articles/guides/fraud-tools/basic/risk-threshold-rules
  • Loading branch information
RyanofWoods committed Dec 10, 2021
1 parent e2b87a4 commit 384490e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/solidus_paypal_braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ def paypal_payee_email_for(source, options)
def customer_profile_params(payment)
params = {}

params[:email] = payment&.order&.email

if store_in_vault && payment.source.try(:nonce)
params[:payment_method_nonce] = payment.source.nonce
end
Expand Down
79 changes: 79 additions & 0 deletions spec/fixtures/cassettes/gateway/customer.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions spec/models/solidus_paypal_braintree/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,56 @@
end
end

describe '#customer_profile_params' do
subject(:params) { gateway.send(:customer_profile_params, payment) }

let(:payment) do
build(:payment, {
payment_method: gateway,
source: source
})
end

context 'when payment does not belong to an order' do
before { allow(payment).to receive(:order).and_return(nil) }

it 'has the email param as nil' do
expect(subject[:email]).to be_nil
end
end

context 'when payment belongs to an order' do
it 'has no email param' do
expect(subject[:email]).to eq(payment.order.email)
end
end
end

describe "Braintree Customer" do
subject(:customer) { braintree.customer.create(params).customer }

let(:params) { gateway.send(:customer_profile_params, payment) }

let(:payment) do
build(:payment, {
payment_method: gateway,
source: source
})
end

cassette_options = {
cassette_name: 'gateway/customer',
match_requests_on: [:braintree_uri]
}

context "with customer", vcr: cassette_options do
it 'saves the customer email correctly' do
allow(payment.order).to receive(:email).and_return('[email protected]')
expect(subject.email).to eq(payment.order.email)
end
end
end

shared_examples "sources_by_order" do
let(:order) { FactoryBot.create :order, user: user, state: "complete", completed_at: Time.current }
let(:gateway) { new_gateway.tap(&:save!) }
Expand Down

0 comments on commit 384490e

Please sign in to comment.