|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2013-2014 eBay Enterprise, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* NOTICE OF LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
8
|
|
|
* that is bundled with this package in the file LICENSE.md. |
|
9
|
|
|
* It is also available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright Copyright (c) 2013-2015 eBay Enterprise, Inc. (http://www.ebayenterprise.com/) |
|
13
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace eBayEnterprise\RetailOrderManagement\Payload\Order; |
|
17
|
|
|
|
|
18
|
|
|
use DateInterval; |
|
19
|
|
|
use DateTime; |
|
20
|
|
|
use DOMXPath; |
|
21
|
|
|
|
|
22
|
|
|
trait TOrderContext |
|
23
|
|
|
{ |
|
24
|
|
|
use TBrowserData, TCustomerSessionInfo, TPayPalPayerInfo, TOrderContextCustomAttributeContainer; |
|
25
|
|
|
|
|
26
|
|
|
/** @var DateTime */ |
|
27
|
|
|
protected $tdlOrderTimestamp; |
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
protected $tdlMilliseconds = 0; |
|
30
|
|
|
|
|
31
|
|
|
public function getTdlOrderTimestamp() |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->tdlOrderTimestamp; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getTdlMilliseconds() |
|
37
|
|
|
{ |
|
38
|
|
|
return (int) $this->tdlMilliseconds; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The fraud system requires millisecond precision. |
|
43
|
|
|
* PHP DateTime does not support this, but we use that for backwards compatibility. |
|
44
|
|
|
* |
|
45
|
|
|
* @param DateTime |
|
46
|
|
|
* @return self |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setTdlOrderTimestamp(DateTime $tdlOrderTimestamp, $milliseconds = 0) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->tdlOrderTimestamp = $tdlOrderTimestamp; |
|
51
|
|
|
$this->tdlMilliseconds = (int) $milliseconds; |
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get property => XPath mapping for properties to be extracted from |
|
57
|
|
|
* serialized payloads for Order Context properties. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getOrderContextExtractionPaths() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
'hostname' => 'string(x:Context/x:BrowserData/x:HostName)', |
|
65
|
|
|
'ipAddress' => 'string(x:Context/x:BrowserData/x:IPAddress)', |
|
66
|
|
|
'sessionId' => 'string(x:Context/x:BrowserData/x:SessionId)', |
|
67
|
|
|
'userAgent' => 'string(x:Context/x:BrowserData/x:UserAgent)', |
|
68
|
|
|
'javascriptData' => 'string(x:Context/x:BrowserData/x:JavascriptData)', |
|
69
|
|
|
'referrer' => 'string(x:Context/x:BrowserData/x:Referrer)', |
|
70
|
|
|
'contentTypes' => 'string(x:Context/x:BrowserData/x:HTTPAcceptData/x:ContentTypes)', |
|
71
|
|
|
'encoding' => 'string(x:Context/x:BrowserData/x:HTTPAcceptData/x:Encoding)', |
|
72
|
|
|
'language' => 'string(x:Context/x:BrowserData/x:HTTPAcceptData/x:Language)', |
|
73
|
|
|
'charSet' => 'string(x:Context/x:BrowserData/x:HTTPAcceptData/x:CharSet)', |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get property => XPath mapping for optional properties to be extracted from |
|
79
|
|
|
* serialized payloads for Order Context properties. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getOrderContextOptionalExtractionPaths() |
|
84
|
|
|
{ |
|
85
|
|
|
return [ |
|
86
|
|
|
'connection' => 'x:Context/x:BrowserData/x:Connection', |
|
87
|
|
|
'cookies' => 'x:Context/x:BrowserData/x:Cookies', |
|
88
|
|
|
'userCookie' => 'x:Context/x:BrowserData/x:UserCookie', |
|
89
|
|
|
'userAgentOs' => 'x:Context/x:BrowserData/x:UserAgentOS', |
|
90
|
|
|
'userAgentCpu' => 'x:Context/x:BrowserData/x:UserAgentCPU', |
|
91
|
|
|
'headerFrom' => 'x:Context/x:BrowserData/x:HeaderFrom', |
|
92
|
|
|
'webBrowserName' => 'x:Context/x:BrowserData/x:EmbeddedWebBrowserFrom', |
|
93
|
|
|
'userPassword' => 'x:Context/x:SessionInfo/x:UserPassword', |
|
94
|
|
|
'timeOnFile' => 'x:Context/x:SessionInfo/x:TimeOnFile', |
|
95
|
|
|
'rtcTransactionResponseCode' => 'x:Context/x:SessionInfo/x:RTCTransactionResponseCode', |
|
96
|
|
|
'rtcReasonCode' => 'x:Context/x:SessionInfo/x:RTCReasonCodes', |
|
97
|
|
|
'authorizationAttempts' => 'x:Context/x:SessionInfo/x:AuthorizationAttempts', |
|
98
|
|
|
'payPalPayerId' => 'x:Context/x:PayPalPayerInfo/x:PayPalPayerID', |
|
99
|
|
|
'payPalPayerStatus' => 'x:Context/x:PayPalPayerInfo/x:PayPalPayerStatus', |
|
100
|
|
|
'payPalAddressStatus' => 'x:Context/x:PayPalPayerInfo/x:PayPalAddressStatus', |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get property => XPath mapping for subpayload properties to be extracted from |
|
106
|
|
|
* serialized payloads for Order Context properties. |
|
107
|
|
|
* |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getOrderContextSubpayloadExtractionPaths() |
|
111
|
|
|
{ |
|
112
|
|
|
return [ |
|
113
|
|
|
'orderContextCustomAttributes' => 'x:Context/x:CustomAttributes', |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Build an XML serialization of the order context - browser data, tdl order |
|
119
|
|
|
* timestamp, session info, PayPal payer info and custom attributes. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function serializeOrderContext() |
|
124
|
|
|
{ |
|
125
|
|
|
return '<Context>' |
|
126
|
|
|
. $this->serializeBrowserData() |
|
127
|
|
|
. $this->serializeTdlOrderTimestamp() |
|
128
|
|
|
. $this->serializeCustomerSessionInfo() |
|
129
|
|
|
. $this->serializePayPalPayerInfo() |
|
130
|
|
|
. $this->getOrderContextCustomAttributes()->serialize() |
|
131
|
|
|
. '</Context>'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Serialize the TDL order timestamp into an XML node with an ISO8601 time format with millisecond precision. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function serializeTdlOrderTimestamp() |
|
140
|
|
|
{ |
|
141
|
|
|
$timestamp = $this->getTdlOrderTimestamp(); |
|
142
|
|
|
if (!$timestamp) { |
|
143
|
|
|
return ''; |
|
144
|
|
|
} |
|
145
|
|
|
$date = $timestamp->format('Y-m-d'); |
|
146
|
|
|
$time = $timestamp->format('H:i:s'); |
|
147
|
|
|
$milliseconds = sprintf('%03d', $this->getTdlMilliseconds()); |
|
148
|
|
|
$zone = $timestamp->format('P'); |
|
149
|
|
|
return "{$date}T{$time}.{$milliseconds}{$zone}"; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Deserialize order context data that is not represented by primitive types, |
|
154
|
|
|
* e.g. DateTime objects, DateInterval objects. |
|
155
|
|
|
* |
|
156
|
|
|
* @param DOMXPath |
|
157
|
|
|
* @return self |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function deserializeExtraOrderContext(DOMXPath $xpath) |
|
160
|
|
|
{ |
|
161
|
|
|
$dateTimeProperties = [ |
|
162
|
|
|
'tdlOrderTimestamp' => 'string(x:Context/x:TdlOrderTimestamp)', |
|
163
|
|
|
'lastLogin' => 'string(x:Context/x:SessionInfo/x:LastLogin)', |
|
164
|
|
|
]; |
|
165
|
|
|
foreach ($dateTimeProperties as $property => $extractPath) { |
|
166
|
|
|
$value = $xpath->evaluate($extractPath); |
|
167
|
|
|
$this->$property = $value ? new DateTime($value) : null; |
|
168
|
|
|
} |
|
169
|
|
|
$timeOnSiteValue = $xpath->evaluate('string(x:Context/x:SessionInfo/x:TimeSpentOnSite)'); |
|
170
|
|
|
$this->timeSpentOnSite = $timeOnSiteValue |
|
171
|
|
|
? new DateInterval($this->convertHMSToDateInterval($timeOnSiteValue)) |
|
172
|
|
|
: null; |
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Convert a string of HH:MM:SS to a date interval spec string. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function convertHMSToDateInterval($hmsTime) |
|
183
|
|
|
{ |
|
184
|
|
|
$parts = explode(':', $hmsTime); |
|
185
|
|
|
return 'PT' . $parts[0] . 'H' . $parts[1] . 'M' . $parts[2] . 'S'; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|