Skip to content

Commit aa74e89

Browse files
authored
Merge pull request #109 from oalders/report-transaction
Add support for report transaction API
2 parents a85f921 + 34167a0 commit aa74e89

8 files changed

Lines changed: 441 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
CHANGELOG
22
=========
33

4+
* Added support for Report Transaction API. Report Transaction requests can be
5+
created via `TransactionReport.Builder()`.
6+
47
1.13.0 (2020-04-06)
58
-------------------
69

README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
## Description ##
55

6-
This package provides an API for the [MaxMind minFraud Score, Insights, and
7-
Factors web services](https://dev.maxmind.com/minfraud/).
6+
This package provides an API for the [MaxMind minFraud Score, Insights, Factors
7+
and Report Transaction web services](https://dev.maxmind.com/minfraud/).
88

99
## Installation ##
1010

@@ -87,6 +87,18 @@ FactorsResponse factors = client.factors(transaction);
8787
If the request succeeds, a model object will be returned for the endpoint.
8888
If the request fails, an exception will be thrown.
8989

90+
To report a transaction:
91+
92+
```java
93+
TransactionReport transaction = new TransactionReport.Builder(
94+
InetAddress.getByName("1.1.1.1"), Tag.NOT_FRAUD
95+
).build();
96+
client.reportTransaction(transaction);
97+
```
98+
99+
`reportTransaction()` has a `void` return type. If the request fails, an
100+
exception will be thrown.
101+
90102
See the API documentation for more details.
91103

92104
### Exceptions ###
@@ -113,7 +125,9 @@ Checked exceptions:
113125
* `HttpException` -This will be thrown when an unexpected HTTP error
114126
occurs such as an internal server error or other unexpected status code.
115127

116-
## Example
128+
## Examples
129+
130+
### Insights
117131

118132
```java
119133

@@ -222,6 +236,34 @@ WebServiceClient client = new WebServiceClient.Builder(6, "ABCD567890").build();
222236
System.out.println(client.insights(request));
223237
```
224238

239+
### Report Transactions API
240+
241+
MaxMind encourages the use of this API, as data received through this channel
242+
is continually used to improve the accuracy of our fraud detection algorithms.
243+
244+
To use the Report Transactions API, create a new `TransactionReport` object. An
245+
IP address and a valid tag are required arguments. Additional params may also
246+
be set, as documented below.
247+
248+
If the report is successful, nothing is returned. If the report fails, an
249+
exception with be thrown.
250+
251+
See the API documentation for more details.
252+
253+
```java
254+
TransactionReport transaction = new TransactionReport.Builder(InetAddress.getByName("1.1.1.1"), Tag.NOT_FRAUD)
255+
.chargebackCode("mycode")
256+
.maxmindId("12345678")
257+
.minfraudId(UUID.fromString("58fa38d8-4b87-458b-a22b-f00eda1aa20d"))
258+
.notes("notes go here")
259+
.transactionId("foo")
260+
.build();
261+
262+
WebServiceClient client = new WebServiceClient.Builder(6, "ABCD567890").build();
263+
264+
client.reportTransaction(transaction);
265+
```
266+
225267
## Support ##
226268

227269
Please report all issues with this code using the
@@ -246,6 +288,6 @@ This API uses [Semantic Versioning](https://semver.org/).
246288

247289
## Copyright and License ##
248290

249-
This software is Copyright (c) 2015-2018 by MaxMind, Inc.
291+
This software is Copyright (c) 2015-2020 by MaxMind, Inc.
250292

251293
This is free software, licensed under the Apache License, Version 2.0.

dev-bin/release.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ popd
5252

5353
mvn versions:display-dependency-updates
5454

55-
read -e -p "Continue given above dependencies? (y/n) " should_continue
55+
read -e -p -r "Continue given above dependencies? (y/n) " should_continue
5656

5757
if [ "$should_continue" != "y" ]; then
5858
echo "Aborting"
@@ -78,7 +78,7 @@ cat README.md >> $page
7878
if [ -n "$(git status --porcelain)" ]; then
7979
git diff
8080

81-
read -e -p "Commit README.md changes? " should_commit
81+
read -e -p -r "Commit README.md changes? " should_commit
8282
if [ "$should_commit" != "y" ]; then
8383
echo "Aborting"
8484
exit 1
@@ -105,7 +105,7 @@ $notes
105105
106106
"
107107

108-
read -e -p "Push to origin? " should_push
108+
read -e -p -r "Push to origin? " should_push
109109

110110
if [ "$should_push" != "y" ]; then
111111
echo "Aborting"

src/main/java/com/maxmind/minfraud/WebServiceClient.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
1111
import com.maxmind.minfraud.exception.*;
1212
import com.maxmind.minfraud.request.Transaction;
13+
import com.maxmind.minfraud.request.TransactionReport;
1314
import com.maxmind.minfraud.response.FactorsResponse;
1415
import com.maxmind.minfraud.response.InsightsResponse;
1516
import com.maxmind.minfraud.response.ScoreResponse;
@@ -282,7 +283,39 @@ public ScoreResponse score(Transaction transaction) throws IOException,
282283
return responseFor("score", transaction, ScoreResponse.class);
283284
}
284285

285-
private <T> T responseFor(String service, Transaction transaction, Class<T> cls)
286+
/**
287+
* Make a Report Transaction request to the web service using the TransactionReport
288+
* request object passed to the method.
289+
*
290+
* @param transaction A TransactionReport request object.
291+
* @throws InsufficientFundsException when there are insufficient funds on
292+
* the account.
293+
* @throws AuthenticationException when there is a problem authenticating.
294+
* @throws InvalidRequestException when the request is invalid for some
295+
* other reason.
296+
* @throws PermissionRequiredException when permission is required to use the
297+
* service.
298+
* @throws MinFraudException when the web service returns unexpected
299+
* content.
300+
* @throws HttpException when the web service returns an unexpected
301+
* response.
302+
* @throws IOException when some other IO error occurs.
303+
*/
304+
public void reportTransaction(TransactionReport transaction) throws IOException,
305+
MinFraudException, InsufficientFundsException, InvalidRequestException,
306+
AuthenticationException, PermissionRequiredException, HttpException {
307+
if (transaction == null) {
308+
throw new IllegalArgumentException("transaction report must not be null");
309+
}
310+
URL url = createUrl(WebServiceClient.pathBase + "transactions/report");
311+
HttpPost request = requestFor(transaction, url);
312+
313+
try (CloseableHttpResponse response = httpClient.execute(request)) {
314+
maybeThrowException(response, url);
315+
}
316+
}
317+
318+
private <T> T responseFor(String service, AbstractModel transaction, Class<T> cls)
286319
throws IOException, MinFraudException {
287320
if (transaction == null) {
288321
throw new IllegalArgumentException("transaction must not be null");
@@ -295,7 +328,7 @@ private <T> T responseFor(String service, Transaction transaction, Class<T> cls)
295328
}
296329
}
297330

298-
private HttpPost requestFor(Transaction transaction, URL url)
331+
private HttpPost requestFor(AbstractModel transaction, URL url)
299332
throws MinFraudException, IOException {
300333
Credentials credentials = new UsernamePasswordCredentials(Integer.toString(accountId), licenseKey);
301334

@@ -321,18 +354,23 @@ private HttpPost requestFor(Transaction transaction, URL url)
321354
return request;
322355
}
323356

324-
private <T> T handleResponse(CloseableHttpResponse response, URL url, Class<T> cls)
325-
throws MinFraudException, IOException {
357+
private void maybeThrowException(CloseableHttpResponse response, URL url) throws IOException, MinFraudException {
326358
int status = response.getStatusLine().getStatusCode();
327359
if (status >= 400 && status < 500) {
328360
this.handle4xxStatus(response, url);
329361
} else if (status >= 500 && status < 600) {
330362
throw new HttpException("Received a server error (" + status
331363
+ ") for " + url, status, url);
332-
} else if (status != 200) {
364+
} else if (status != 200 && status != 204) {
333365
throw new HttpException("Received an unexpected HTTP status ("
334366
+ status + ") for " + url, status, url);
335367
}
368+
}
369+
370+
private <T> T handleResponse(CloseableHttpResponse response, URL url, Class<T> cls)
371+
throws MinFraudException, IOException {
372+
maybeThrowException(response, url);
373+
336374
HttpEntity entity = response.getEntity();
337375

338376
InjectableValues inject = new Std().addValue(

0 commit comments

Comments
 (0)