Skip to content

Commit 678eea7

Browse files
committed
Make IP address optional when reporting transactions
1 parent 7499837 commit 678eea7

File tree

5 files changed

+103
-11
lines changed

5 files changed

+103
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## v2.6.0
4+
5+
* Updated the validation for the Report Transactions API to make the
6+
`ip_address` parameter optional. Now the `tag` and at least one of the
7+
following parameters must be supplied: `ip_address`, `maxmind_id`,
8+
`minfraud_id`, `transaction_id`.
9+
310
## v2.5.0 (2024-04-16)
411

512
* Equivalent domain names are now normalized when `hash_address` is used.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ channel is used to improve the accuracy of their fraud detection
208208
algorithms.
209209

210210
To use the Report Transaction API, create a
211-
`Minfraud::Components::Report::Transaction` object. An IP address and a
212-
valid tag are required arguments for this API. Additional parameters may be
213-
set, as shown below.
211+
`Minfraud::Components::Report::Transaction` object. A valid tag and at least
212+
one of the following are required parameters: ip_address, maxmind_id,
213+
minfraud_id, transaction_id. Additional parameters may be set, as shown below.
214214

215215
If the report is successful, nothing is returned. If the report fails, an
216216
exception will be thrown.

lib/minfraud/components/report/transaction.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class Transaction < Base
1010
include ::Minfraud::Enum
1111

1212
# The IP address of the customer placing the order. This should be
13-
# passed as a string like "152.216.7.110".
13+
# passed as a string like "152.216.7.110". This field is not required
14+
# if you provide at least one of the transaction's minfraud_id,
15+
# maxmind_id, or transaction_id. You are encouraged to provide it, if
16+
# possible.
1417
#
1518
# @return [String, nil]
1619
attr_accessor :ip_address
@@ -34,16 +37,19 @@ class Transaction < Base
3437

3538
# A unique eight character string identifying a minFraud Standard or
3639
# Premium request. These IDs are returned in the maxmindID field of a
37-
# response for a successful minFraud request. This field is not
38-
# required, but you are encouraged to provide it, if possible.
40+
# response for a successful minFraud request. This field is not required
41+
# if you provide at least one of the transaction's ip_address,
42+
# maxmind_id, or transaction_id. You are encouraged to provide it, if
43+
# possible.
3944
#
4045
# @return [String, nil]
4146
attr_accessor :maxmind_id
4247

4348
# A UUID that identifies a minFraud Score, minFraud Insights, or
4449
# minFraud Factors request. This ID is returned at /id in the response.
45-
# This field is not required, but you are encouraged to provide it if
46-
# the request was made to one of these services.
50+
# This field is not required if you provide at least one of the
51+
# transaction's ip_address, maxmind_id, or transaction_id. You are
52+
# encouraged to provide it, if possible.
4753
#
4854
# @return [String, nil]
4955
attr_accessor :minfraud_id
@@ -56,9 +62,10 @@ class Transaction < Base
5662
# @return [String, nil]
5763
attr_accessor :notes
5864

59-
# The transaction ID you originally passed to minFraud. This field is
60-
# not required, but you are encouraged to provide it or the
61-
# transaction's maxmind_id or minfraud_id.
65+
# The transaction ID you originally passed to minFraud. This field
66+
# is not required if you provide at least one of the transaction's
67+
# ip_address, maxmind_id, or minfraud_id. You are encouraged to
68+
# provide it, if possible.
6269
#
6370
# @return [String, nil]
6471
attr_accessor :transaction_id
@@ -73,6 +80,21 @@ def initialize(params = {})
7380
@notes = params[:notes]
7481
@transaction_id = params[:transaction_id]
7582
self.tag = params[:tag]
83+
84+
validate
85+
end
86+
87+
private
88+
89+
def validate
90+
return if !Minfraud.enable_validation
91+
92+
if ip_address.nil? &&
93+
minfraud_id.nil? &&
94+
(maxmind_id.nil? || maxmind_id.empty?) &&
95+
(transaction_id.nil? || transaction_id.empty?)
96+
raise ArgumentError, 'At least one of the following is required: ip_address, minfraud_id, maxmind_id, transaction_id.'
97+
end
7698
end
7799
end
78100
end

spec/components/general_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
end
1515

1616
describe Minfraud::Components::Report::Transaction do
17+
before do
18+
Minfraud.configure { |c| c.enable_validation = false }
19+
end
1720
describe '#initialize' do
1821
it { is_expected.to be_an_instance_of described_class }
1922
it { is_expected.to respond_to :to_json }

spec/components/report/transaction_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
describe Minfraud::Components::Report::Transaction do
66
describe '#initialize' do
7+
before do
8+
Minfraud.configure { |c| c.enable_validation = false }
9+
end
710
context 'with an invalid type' do
811
it 'raises an exception' do
912
expect do
@@ -42,4 +45,61 @@
4245
end
4346
end
4447
end
48+
49+
describe 'validation' do
50+
before do
51+
Minfraud.configure { |c| c.enable_validation = true }
52+
end
53+
context 'missing required identifier field' do
54+
it 'raises an exception' do
55+
expect do
56+
described_class.new(tag: :suspected_fraud)
57+
end.to raise_exception(ArgumentError)
58+
end
59+
end
60+
context 'with tag + ip_address' do
61+
it 'does not raise an exception' do
62+
report = described_class.new(
63+
ip_address: '1.2.3.4',
64+
tag: :suspected_fraud
65+
)
66+
67+
expect(report.ip_address).to eq '1.2.3.4'
68+
expect(report.tag).to eq :suspected_fraud
69+
end
70+
end
71+
context 'with tag + maxmind_id' do
72+
it 'does not raise an exception' do
73+
report = described_class.new(
74+
tag: :suspected_fraud,
75+
maxmind_id: '12345678'
76+
)
77+
78+
expect(report.tag).to eq :suspected_fraud
79+
expect(report.maxmind_id).to eq '12345678'
80+
end
81+
end
82+
context 'with tag + minfraud_id' do
83+
it 'does not raise an exception' do
84+
report = described_class.new(
85+
tag: :suspected_fraud,
86+
minfraud_id: '58fa38d8-4b87-458b-a22b-f00eda1aa20d'
87+
)
88+
89+
expect(report.tag).to eq :suspected_fraud
90+
expect(report.minfraud_id).to eq '58fa38d8-4b87-458b-a22b-f00eda1aa20d'
91+
end
92+
end
93+
context 'with tag + transaction_id' do
94+
it 'does not raise an exception' do
95+
report = described_class.new(
96+
tag: :suspected_fraud,
97+
transaction_id: '1FA254yZ'
98+
)
99+
100+
expect(report.tag).to eq :suspected_fraud
101+
expect(report.transaction_id).to eq '1FA254yZ'
102+
end
103+
end
104+
end
45105
end

0 commit comments

Comments
 (0)