1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class used to connecto the the MaxMind ninFraud API |
5
|
|
|
*@author nicolaas [at] sunnysideup.co.nz |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use MaxMind\MinFraud; |
9
|
|
|
use Sokil\IsoCodes\Database\Subdivisions; |
10
|
|
|
|
11
|
|
|
class MinFraudAPIConnector extends Object |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* REQUIRED! |
16
|
|
|
* @var String |
17
|
|
|
*/ |
18
|
|
|
private static $account_id = ""; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* REQUIRED! |
23
|
|
|
* @var String |
24
|
|
|
*/ |
25
|
|
|
private static $license_key = ""; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
public function getConnection() |
29
|
|
|
{ |
30
|
|
|
$mf = new MinFraud( |
31
|
|
|
Config::inst()->get('MinFraudAPIConnector', 'account_id'), |
32
|
|
|
Config::inst()->get('MinFraudAPIConnector', 'license_key') |
33
|
|
|
); |
34
|
|
|
return $mf; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Creates the `MinFraud` object and builds the request with all the data available in the order |
39
|
|
|
* |
40
|
|
|
* @param Order $order - the order to be assessed |
41
|
|
|
* |
42
|
|
|
* @return MinFraud |
43
|
|
|
*/ |
44
|
|
|
public function buildRequest($order) |
45
|
|
|
{ |
46
|
|
|
$shippingAddress = null; |
|
|
|
|
47
|
|
|
if ($order->ShippingAddress()->exists()) { |
|
|
|
|
48
|
|
|
$shippingAddress = $order->ShippingAddress(); |
|
|
|
|
49
|
|
|
} else { |
50
|
|
|
$shippingAddress = $order->BillingAddress(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
$mf = $this->getConnection(); |
55
|
|
|
|
56
|
|
|
$request = $mf->withAccount( |
57
|
|
|
[ |
58
|
|
|
'user_id' => $order->MemberID, |
59
|
|
|
'username_md5' => md5($order->Member()->Email), |
|
|
|
|
60
|
|
|
] |
61
|
|
|
)->withEvent( |
62
|
|
|
[ |
63
|
|
|
'transaction_id' => (string)$order->ID, |
64
|
|
|
'time' => date("c", strtotime($order->Created)), //see: https://stackoverflow.com/questions/22296712/convert-datetime-to-rfc-3339 should we use Created or LastEdited? |
65
|
|
|
'type' => 'purchase', |
66
|
|
|
] |
67
|
|
|
)->withEmail( |
68
|
|
|
[ |
69
|
|
|
'address' => $order->Member()->Email, |
|
|
|
|
70
|
|
|
'domain' => substr(strrchr($order->Member()->Email, "@"), 1), |
|
|
|
|
71
|
|
|
] |
72
|
|
|
)->withBilling( |
73
|
|
|
[ |
74
|
|
|
'first_name' => $order->BillingAddress()->FirstName, |
|
|
|
|
75
|
|
|
'last_name' => $order->BillingAddress()->Surname, |
|
|
|
|
76
|
|
|
'address' => $order->BillingAddress()->Address, |
|
|
|
|
77
|
|
|
'address_2' => $order->BillingAddress()->Address2, |
|
|
|
|
78
|
|
|
'city' => $order->BillingAddress()->City, |
|
|
|
|
79
|
|
|
//'region' => '', // see: https://en.wikipedia.org/wiki/ISO_3166-2 |
80
|
|
|
'country' => $order->BillingAddress()->Country, |
|
|
|
|
81
|
|
|
'postal' => $order->BillingAddress()->PostalCode, |
|
|
|
|
82
|
|
|
'phone_number' => $order->BillingAddress()->Phone |
|
|
|
|
83
|
|
|
] |
84
|
|
|
)->withShipping( |
85
|
|
|
[ |
86
|
|
|
'first_name' => $shippingAddress->FirstName, |
87
|
|
|
'last_name' => $shippingAddress->Surname, |
88
|
|
|
'address' => $shippingAddress->Address, |
89
|
|
|
'address_2' => $shippingAddress->Address2, |
90
|
|
|
'city' => $shippingAddress->City, |
91
|
|
|
//'region' => '', // see: https://en.wikipedia.org/wiki/ISO_3166-2 |
92
|
|
|
'country' => $shippingAddress->Country, |
93
|
|
|
'postal' => $shippingAddress->PostalCode, |
94
|
|
|
'phone_number' => $shippingAddress->Phone |
95
|
|
|
] |
96
|
|
|
)->withOrder( |
97
|
|
|
[ |
98
|
|
|
'amount' => $order->getTotal(), |
99
|
|
|
'currency' => $order->getTotalAsMoney()->currency, |
100
|
|
|
//'discount_code' => '', //do we want to use this? |
101
|
|
|
'is_gift' => false, |
102
|
|
|
'has_gift_message' => false, |
103
|
|
|
'referrer_uri' => Director::absoluteURL('/'), |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
$deviceDetails = OrderStatusLog_DeviceDetails::get()->filter(['OrderID' => $order->ID])->first(); |
109
|
|
|
if ($deviceDetails && $deviceDetails->exists()) { |
110
|
|
|
$request = $request->withDevice( |
111
|
|
|
[ |
112
|
|
|
'ip_address' => $deviceDetails->IPAddress, |
113
|
|
|
'user_agent' => $deviceDetails->UserAgent, |
114
|
|
|
'accept_language' => $deviceDetails->AcceptLanguage, |
115
|
|
|
'session_id' => $deviceDetails->SessionID, |
116
|
|
|
] |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
foreach ($order->Items() as $orderItem) { |
122
|
|
|
$itemID = $orderItem->BuyableID; |
123
|
|
|
$product = DataObject::get_by_id($orderItem->BuyableClassName, $orderItem->BuyableID); |
124
|
|
|
if ($product && $product->exists()) { |
125
|
|
|
$itemID = $product->InternalItemID; |
126
|
|
|
} |
127
|
|
|
$request = $request->withShoppingCartItem( |
128
|
|
|
[ |
129
|
|
|
'item_id' => (string)$itemID, |
130
|
|
|
'quantity' => (int)$orderItem->Quantity, |
131
|
|
|
'price' => $orderItem->CalculatedTotal, |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $request; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* minFraud Score provides the risk assessment of the transaction with the riskScore and the IP address risk as expressed in the IP Risk Score. |
142
|
|
|
* Use minFraud Score to assess risk with these data points or use it as part of your own risk modeling. |
143
|
|
|
* |
144
|
|
|
* @param Order $order - the order to be assessed |
145
|
|
|
* |
146
|
|
|
* @return MinFraud\Model\Score minFraud Score model object |
147
|
|
|
*/ |
148
|
|
|
public function getScore($order) |
149
|
|
|
{ |
150
|
|
|
$request = $this->buildRequest($order); |
151
|
|
|
return $request->score(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* minFraud Insights provides a wide range of data points in addition to the riskScore and the IP Risk Score. |
156
|
|
|
* |
157
|
|
|
* Use minFraud Insights to score transactions and to get the data points you need for manual review, advanced rule creation, and internal risk modeling. |
158
|
|
|
* |
159
|
|
|
* @param Order $order - the order to be assessed |
160
|
|
|
* |
161
|
|
|
* @return MinFraud\Model\Insights minFraud Insights model object |
162
|
|
|
*/ |
163
|
|
|
public function getInsights($order) |
164
|
|
|
{ |
165
|
|
|
$request = $this->buildRequest($order); |
166
|
|
|
return $request->insights(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* minFraud Factors provides detail on the specific components used to determine the riskScore. These subscores provide insight into how we arrived at a riskScore for a given transaction. |
171
|
|
|
* |
172
|
|
|
* Such detail on the factors contributing to the riskScore help you better assess the risk of a transaction as part of manual review. Use subscores as parameters in rules to disposition transactions, or as part of internal risk modeling. |
173
|
|
|
* |
174
|
|
|
* In addition to the subscores, minFraud Factors includes all the data of minFraud Insights. |
175
|
|
|
* |
176
|
|
|
* @param Order $order - the order to be assessed |
177
|
|
|
* |
178
|
|
|
* @return MinFraud\Model\Factors minFraud Factors model object |
179
|
|
|
*/ |
180
|
|
|
public function getFactors($order) |
181
|
|
|
{ |
182
|
|
|
$request = $this->buildRequest($order); |
183
|
|
|
return $request->factors(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.