1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\FirstAtlanticCommerce\Message; |
4
|
|
|
|
5
|
|
|
use Alcohol\ISO3166; |
6
|
|
|
use Omnipay\Common\Exception\InvalidRequestException; |
7
|
|
|
use Omnipay\FirstAtlanticCommerce\Message\AbstractRequest; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* FACPG2 Authorize Request |
11
|
|
|
*/ |
12
|
|
|
class AuthorizeRequest extends AbstractRequest |
13
|
|
|
{ |
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* @var string; |
|
|
|
|
16
|
|
|
*/ |
|
|
|
|
17
|
|
|
protected $requestName = 'AuthorizeRequest'; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* Transaction code (flag as a authorization) |
|
|
|
|
21
|
|
|
* |
|
|
|
|
22
|
|
|
* @var int; |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
protected $transactionCode = 0; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
|
|
|
|
27
|
|
|
* Returns the signature for the request. |
|
|
|
|
28
|
|
|
* |
|
|
|
|
29
|
|
|
* @return string base64 encoded sha1 hash of the merchantPassword, merchantId, |
|
|
|
|
30
|
|
|
* acquirerId, transactionId, amount and currency code. |
|
|
|
|
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
protected function generateSignature() |
|
|
|
|
33
|
|
|
{ |
|
|
|
|
34
|
|
|
$signature = $this->getMerchantPassword(); |
|
|
|
|
35
|
|
|
$signature .= $this->getMerchantId(); |
|
|
|
|
36
|
|
|
$signature .= $this->getAcquirerId(); |
|
|
|
|
37
|
|
|
$signature .= $this->getTransactionId(); |
|
|
|
|
38
|
|
|
$signature .= $this->formatAmount(); |
|
|
|
|
39
|
|
|
$signature .= $this->getCurrencyNumeric(); |
|
|
|
|
40
|
|
|
|
41
|
|
|
return base64_encode( sha1($signature, true) ); |
|
|
|
|
42
|
|
|
} |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* Validate and construct the data for the request |
|
|
|
|
46
|
|
|
* |
|
|
|
|
47
|
|
|
* @return array |
|
|
|
|
48
|
|
|
*/ |
|
|
|
|
49
|
|
|
public function getData() |
|
|
|
|
50
|
|
|
{ |
|
|
|
|
51
|
|
|
$this->validate('merchantId', 'merchantPassword', 'acquirerId', 'transactionId', 'amount', 'currency', 'card'); |
|
|
|
|
52
|
|
|
|
53
|
|
|
// Check for AVS and require billingAddress1 and billingPostcode |
|
|
|
|
54
|
|
|
if ( $this->getRequireAvsCheck() ) |
|
|
|
|
55
|
|
|
{ |
|
|
|
|
56
|
|
|
$this->getCard()->validate('billingAddress1', 'billingPostcode'); |
|
|
|
|
57
|
|
|
} |
|
|
|
|
58
|
|
|
|
59
|
|
|
// Tokenized cards require the CVV and nothing else, token replaces the card number |
|
|
|
|
60
|
|
|
if ( $this->getCardReference() ) |
|
|
|
|
61
|
|
|
{ |
|
|
|
|
62
|
|
|
$this->validate('cardReference'); |
|
|
|
|
63
|
|
|
$this->getCard()->validate('cvv', 'expiryMonth', 'expiryYear'); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$cardDetails = [ |
|
|
|
|
66
|
|
|
'CardCVV2' => $this->getCard()->getCvv(), |
|
|
|
|
67
|
|
|
'CardExpiryDate' => $this->getCard()->getExpiryDate('my'), |
|
|
|
|
68
|
|
|
'CardNumber' => $this->getCardReference() |
|
|
|
|
69
|
|
|
]; |
|
|
|
|
70
|
|
|
} |
|
|
|
|
71
|
|
|
else |
|
|
|
|
72
|
|
|
{ |
|
|
|
|
73
|
|
|
$this->getCard()->validate(); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$cardDetails = [ |
|
|
|
|
76
|
|
|
'CardCVV2' => $this->getCard()->getCvv(), |
|
|
|
|
77
|
|
|
'CardExpiryDate' => $this->getCard()->getExpiryDate('my'), |
|
|
|
|
78
|
|
|
'CardNumber' => $this->getCard()->getNumber(), |
|
|
|
|
79
|
|
|
'IssueNumber' => $this->getCard()->getIssueNumber() |
|
|
|
|
80
|
|
|
]; |
|
|
|
|
81
|
|
|
} |
|
|
|
|
82
|
|
|
|
83
|
|
|
// Only pass the StartDate if year/month are set otherwise it returns 1299 |
|
|
|
|
84
|
|
|
if ( $this->getCard()->getStartYear() && $this->getCard()->getStartMonth() ) |
|
|
|
|
85
|
|
|
{ |
|
|
|
|
86
|
|
|
$cardDetails['StartDate'] = $this->getCard()->getStartDate('my'); |
|
|
|
|
87
|
|
|
} |
|
|
|
|
88
|
|
|
|
89
|
|
|
$transactionDetails = [ |
|
|
|
|
90
|
|
|
'AcquirerId' => $this->getAcquirerId(), |
|
|
|
|
91
|
|
|
'Amount' => $this->formatAmount(), |
|
|
|
|
92
|
|
|
'Currency' => $this->getCurrencyNumeric(), |
|
|
|
|
93
|
|
|
'CurrencyExponent' => $this->getCurrencyDecimalPlaces(), |
|
|
|
|
94
|
|
|
'IPAddress' => $this->getClientIp(), |
|
|
|
|
95
|
|
|
'MerchantId' => $this->getMerchantId(), |
|
|
|
|
96
|
|
|
'OrderNumber' => $this->getTransactionId(), |
|
|
|
|
97
|
|
|
'Signature' => $this->generateSignature(), |
|
|
|
|
98
|
|
|
'SignatureMethod' => 'SHA1', |
|
|
|
|
99
|
|
|
'TransactionCode' => $this->getTransactionCode() |
|
|
|
|
100
|
|
|
]; |
|
|
|
|
101
|
|
|
|
102
|
|
|
$billingDetails = [ |
|
|
|
|
103
|
|
|
'BillToAddress' => $this->getCard()->getAddress1(), |
|
|
|
|
104
|
|
|
'BillToZipPostCode' => $this->formatPostcode(), |
|
|
|
|
105
|
|
|
'BillToFirstName' => $this->getCard()->getFirstName(), |
|
|
|
|
106
|
|
|
'BillToLastName' => $this->getCard()->getLastName(), |
|
|
|
|
107
|
|
|
'BillToCity' => $this->getCard()->getCity(), |
|
|
|
|
108
|
|
|
'BillToCountry' => $this->formatCountry(), |
|
|
|
|
109
|
|
|
'BillToEmail' => $this->getCard()->getEmail(), |
|
|
|
|
110
|
|
|
'BillToTelephone' => $this->getCard()->getPhone(), |
|
|
|
|
111
|
|
|
'BillToFax' => $this->getCard()->getFax() |
|
|
|
|
112
|
|
|
]; |
|
|
|
|
113
|
|
|
|
114
|
|
|
// FAC only accepts two digit state abbreviations from the USA |
|
|
|
|
115
|
|
|
if ( $billingDetails['BillToCountry'] == 840 ) |
|
|
|
|
116
|
|
|
{ |
|
|
|
|
117
|
|
|
$billingDetails['BillToState'] = $this->formatState(); |
|
|
|
|
118
|
|
|
} |
|
|
|
|
119
|
|
|
|
120
|
|
|
$data = [ |
|
|
|
|
121
|
|
|
'TransactionDetails' => $transactionDetails, |
|
|
|
|
122
|
|
|
'CardDetails' => $cardDetails, |
|
|
|
|
123
|
|
|
'BillingDetails' => $billingDetails |
|
|
|
|
124
|
|
|
]; |
|
|
|
|
125
|
|
|
|
126
|
|
|
return $data; |
|
|
|
|
127
|
|
|
} |
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
|
|
|
|
130
|
|
|
* Returns the country as the numeric ISO 3166-1 code |
|
|
|
|
131
|
|
|
* |
|
|
|
|
132
|
|
|
* @throws Omnipay\Common\Exception\InvalidRequestException |
|
|
|
|
133
|
|
|
* |
|
|
|
|
134
|
|
|
* @return int ISO 3166-1 numeric country |
|
|
|
|
135
|
|
|
*/ |
|
|
|
|
136
|
|
|
protected function formatCountry() |
|
|
|
|
137
|
|
|
{ |
|
|
|
|
138
|
|
|
$country = $this->getCard()->getCountry(); |
|
|
|
|
139
|
|
|
|
140
|
|
|
if ( !is_null($country) && !is_numeric($country) ) |
|
|
|
|
141
|
|
|
{ |
|
|
|
|
142
|
|
|
$iso3166 = new ISO3166; |
|
|
|
|
143
|
|
|
|
144
|
|
|
if ( strlen($country) == 2 ) |
|
|
|
|
145
|
|
|
{ |
|
|
|
|
146
|
|
|
$country = $iso3166->getByAlpha2($country)['numeric']; |
|
|
|
|
147
|
|
|
} |
|
|
|
|
148
|
|
|
elseif ( strlen($country) == 3 ) |
|
|
|
|
149
|
|
|
{ |
|
|
|
|
150
|
|
|
$country = $iso3166->getByAlpha3($country)['numeric']; |
|
|
|
|
151
|
|
|
} |
|
|
|
|
152
|
|
|
else |
|
|
|
|
153
|
|
|
{ |
|
|
|
|
154
|
|
|
throw new InvalidRequestException("The country parameter must be ISO 3166-1 numeric, aplha2 or alpha3."); |
|
|
|
|
155
|
|
|
} |
|
|
|
|
156
|
|
|
} |
|
|
|
|
157
|
|
|
|
158
|
|
|
return $country; |
|
|
|
|
159
|
|
|
} |
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
|
|
|
|
162
|
|
|
* Returns the billing state if its a US abbreviation or throws an exception |
|
|
|
|
163
|
|
|
* |
|
|
|
|
164
|
|
|
* @throws Omnipay\Common\Exception\InvalidRequestException |
|
|
|
|
165
|
|
|
* |
|
|
|
|
166
|
|
|
* @return string State abbreviation |
|
|
|
|
167
|
|
|
*/ |
|
|
|
|
168
|
|
|
public function formatState() |
|
|
|
|
169
|
|
|
{ |
|
|
|
|
170
|
|
|
$state = $this->getCard()->getState(); |
|
|
|
|
171
|
|
|
|
172
|
|
|
if ( strlen($state) != 2 ) |
|
|
|
|
173
|
|
|
{ |
|
|
|
|
174
|
|
|
throw new InvalidRequestException("The state must be a two character abbreviation."); |
|
|
|
|
175
|
|
|
} |
|
|
|
|
176
|
|
|
|
177
|
|
|
return $state; |
|
|
|
|
178
|
|
|
} |
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* Returns the postal code sanitizing dashes and spaces and throws exceptions with other |
|
|
|
|
182
|
|
|
* non-alphanumeric characters |
|
|
|
|
183
|
|
|
* |
|
|
|
|
184
|
|
|
* @throws Omnipay\Common\Exception\InvalidRequestException |
|
|
|
|
185
|
|
|
* |
|
|
|
|
186
|
|
|
* @return string Postal code |
|
|
|
|
187
|
|
|
*/ |
|
|
|
|
188
|
|
|
public function formatPostcode() |
|
|
|
|
189
|
|
|
{ |
|
|
|
|
190
|
|
|
$postal = preg_replace( '/[\s\-]/', '', $this->getCard()->getPostcode() ); |
|
|
|
|
191
|
|
|
|
192
|
|
|
if ( preg_match('/[^a-z0-9]/i', $postal) ) |
|
|
|
|
193
|
|
|
{ |
|
|
|
|
194
|
|
|
throw new InvalidRequestException("The postal code must be alpha-numeric."); |
|
|
|
|
195
|
|
|
} |
|
|
|
|
196
|
|
|
|
197
|
|
|
return $postal; |
|
|
|
|
198
|
|
|
} |
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
|
|
|
|
201
|
|
|
* Returns endpoint for authorize requests |
|
|
|
|
202
|
|
|
* |
|
|
|
|
203
|
|
|
* @return string Endpoint URL |
|
|
|
|
204
|
|
|
*/ |
|
|
|
|
205
|
|
|
protected function getEndpoint() |
|
|
|
|
206
|
|
|
{ |
|
|
|
|
207
|
|
|
return parent::getEndpoint() . 'Authorize'; |
|
|
|
|
208
|
|
|
} |
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
|
|
|
|
211
|
|
|
* Returns the transaction code based on the AVS check requirement |
|
|
|
|
212
|
|
|
* |
|
|
|
|
213
|
|
|
* @return int Transaction Code |
|
|
|
|
214
|
|
|
*/ |
|
|
|
|
215
|
|
|
protected function getTransactionCode() |
|
|
|
|
216
|
|
|
{ |
|
|
|
|
217
|
|
|
return $this->getRequireAvsCheck() ? $this->transactionCode + 1 : $this->transactionCode; |
|
|
|
|
218
|
|
|
} |
|
|
|
|
219
|
|
|
|
220
|
|
|
/** |
|
|
|
|
221
|
|
|
* Return the authorize response object |
|
|
|
|
222
|
|
|
* |
|
|
|
|
223
|
|
|
* @param \SimpleXMLElement $xml Response xml object |
|
|
|
|
224
|
|
|
* |
|
|
|
|
225
|
|
|
* @return AuthorizeResponse |
|
|
|
|
226
|
|
|
*/ |
|
|
|
|
227
|
|
|
protected function newResponse($xml) |
|
|
|
|
228
|
|
|
{ |
|
|
|
|
229
|
|
|
return new AuthorizeResponse($this, $xml); |
|
|
|
|
230
|
|
|
} |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|