Skip to content

Commit 697c148

Browse files
committed
Make IP address optional when reporting transactions
1 parent 1bc1280 commit 697c148

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

HISTORY.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
History
44
-------
55

6+
2.11.0
7+
+++++++++++++++++++
8+
9+
* Updated the validation for the Report Transactions API to make the
10+
``ip_address`` parameter optional. Now the ``tag`` and at least one of the
11+
following parameters must be supplied: ``ip_address``, ``maxmind_id``,
12+
``minfraud_id``, ``transaction_id``.
13+
614
2.10.0 (2024-04-16)
715
+++++++++++++++++++
816

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ The method takes a dictionary representing the report to be sent to the web
9898
service. The structure of this dictionary should be in `the format specified
9999
in the REST API documentation
100100
<https://dev.maxmind.com/minfraud/report-a-transaction?lang=en>`__. The
101-
``ip_address`` and ``tag`` fields are required. All other fields are optional.
101+
required fields are ``tag`` and one or more of the following: ``ip_address``,
102+
``maxmind_id``, ``minfraud_id``, ``transaction_id``.
102103

103104
Request Validation (for all request methods)
104105
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

minfraud/validation.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,33 @@ def _uuid(s: str) -> str:
379379
raise ValueError
380380

381381

382-
validate_report = Schema(
382+
def _transaction_id(s: Optional[str]) -> str:
383+
if isinstance(s, str) and len(s) > 0:
384+
return s
385+
raise ValueError
386+
387+
388+
validate_report_schema = Schema(
383389
{
384390
"chargeback_code": str,
385-
Required("ip_address"): _ip_address,
391+
"ip_address": _ip_address,
386392
"maxmind_id": _maxmind_id,
387393
"minfraud_id": _uuid,
388394
"notes": str,
389395
Required("tag"): _tag,
390-
"transaction_id": str,
396+
"transaction_id": _transaction_id,
391397
},
392398
)
399+
400+
401+
def validate_at_least_one_identifier_field(report):
402+
optional_fields = ["ip_address", "maxmind_id", "minfraud_id", "transaction_id"]
403+
if not any(field in report for field in optional_fields):
404+
raise ValueError("The report must contain at least one of the following fields: 'ip_address', 'maxmind_id', 'minfraud_id', 'transaction_id'.")
405+
return True
406+
407+
408+
def validate_report(report):
409+
validate_report_schema(report)
410+
validate_at_least_one_identifier_field(report)
411+
return True

tests/test_validation.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@ def setup_report(self, report):
5151

5252
def check_invalid_report(self, report):
5353
self.setup_report(report)
54+
self.check_invalid_report_no_setup(report)
55+
56+
def check_invalid_report_no_setup(self, report):
5457
with self.assertRaises(MultipleInvalid, msg=f"{report} is invalid"):
5558
validate_report(report)
5659

5760
def check_report(self, report):
5861
self.setup_report(report)
62+
self.check_report_no_setup(report)
63+
64+
def check_report_no_setup(self, report):
5965
try:
6066
validate_report(report)
6167
except MultipleInvalid as e:
@@ -431,3 +437,9 @@ def test_tag(self):
431437
self.check_report({"tag": good})
432438
for bad in ("risky_business", "", None):
433439
self.check_invalid_report({"tag": bad})
440+
441+
def test_report_valid_identifier(self):
442+
self.check_report_no_setup({"tag": "chargeback", "ip_address": "1.1.1.1"})
443+
self.check_report_no_setup({"tag": "chargeback", "minfraud_id": "58fa38d8-4b87-458b-a22b-f00eda1aa20d"})
444+
self.check_report_no_setup({"tag": "chargeback", "maxmind_id": "12345678"})
445+
self.check_report_no_setup({"tag": "chargeback", "transaction_id": "abc123"})

0 commit comments

Comments
 (0)