1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* PHP version 5 |
18
|
|
|
* |
19
|
|
|
* @category Payone |
20
|
|
|
* @package Payone_Magento2_Plugin |
21
|
|
|
* @author FATCHIP GmbH <[email protected]> |
22
|
|
|
* @copyright 2003 - 2016 Payone GmbH |
23
|
|
|
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
24
|
|
|
* @link http://www.payone.de |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Payone\Core\Service\V1; |
28
|
|
|
|
29
|
|
|
use Payone\Core\Api\AddresscheckInterface; |
30
|
|
|
use Payone\Core\Service\V1\Data\AddresscheckResponse; |
31
|
|
|
use Magento\Quote\Api\Data\AddressInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Web API model for the PAYONE addresscheck |
35
|
|
|
*/ |
36
|
|
|
class Addresscheck implements AddresscheckInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* PAYONE addresscheck request model |
40
|
|
|
* |
41
|
|
|
* @var \Payone\Core\Model\Risk\Addresscheck |
42
|
|
|
*/ |
43
|
|
|
protected $addresscheck; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Factory for the response object |
47
|
|
|
* |
48
|
|
|
* @var \Payone\Core\Service\V1\Data\AddresscheckResponseFactory |
49
|
|
|
*/ |
50
|
|
|
protected $responseFactory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Checkout session object |
54
|
|
|
* |
55
|
|
|
* @var \Magento\Checkout\Model\Session |
56
|
|
|
*/ |
57
|
|
|
protected $checkoutSession; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor |
61
|
|
|
* |
62
|
|
|
* @param \Payone\Core\Model\Risk\Addresscheck $addresscheck |
63
|
|
|
* @param \Payone\Core\Service\V1\Data\AddresscheckResponseFactory $responseFactory |
64
|
|
|
* @param \Magento\Checkout\Model\Session $checkoutSession |
65
|
|
|
*/ |
66
|
|
|
public function __construct( |
67
|
|
|
\Payone\Core\Model\Risk\Addresscheck $addresscheck, |
68
|
|
|
\Payone\Core\Service\V1\Data\AddresscheckResponseFactory $responseFactory, |
69
|
|
|
\Magento\Checkout\Model\Session $checkoutSession |
70
|
|
|
) { |
71
|
|
|
$this->addresscheck = $addresscheck; |
72
|
|
|
$this->responseFactory = $responseFactory; |
73
|
|
|
$this->checkoutSession = $checkoutSession; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generate the confirm message from the given address |
78
|
|
|
* |
79
|
|
|
* @param AddressInterface $addressData |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
protected function getConfirmMessage(AddressInterface $addressData) |
83
|
|
|
{ |
84
|
|
|
$sMessage = __('Address corrected. Please confirm.')."\n\n"; |
85
|
|
|
$sMessage .= $addressData->getFirstname().' '.$addressData->getLastname()."\n"; |
86
|
|
|
|
87
|
|
|
$mStreet = $addressData->getStreet(); |
88
|
|
|
if (is_array($mStreet)) { // address can be string |
89
|
|
|
$sMessage .= $mStreet[0]."\n"; // add first line of address array |
90
|
|
|
} else { |
91
|
|
|
$sMessage .= $mStreet."\n"; // add string directly |
92
|
|
|
} |
93
|
|
|
$sMessage .= $addressData->getPostcode().' '.$addressData->getCity(); |
94
|
|
|
|
95
|
|
|
return $sMessage; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add the score to the correct session variable |
100
|
|
|
* |
101
|
|
|
* @param AddressInterface $oAddress |
102
|
|
|
* @param bool $blIsBillingAddress |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
protected function addScoreToSession(AddressInterface $oAddress, $blIsBillingAddress) |
106
|
|
|
{ |
107
|
|
|
$sScore = $this->addresscheck->getScore($oAddress); |
108
|
|
|
if ($blIsBillingAddress === true) { // is billing address? |
109
|
|
|
$this->checkoutSession->setPayoneBillingAddresscheckScore($sScore); |
110
|
|
|
} else { |
111
|
|
|
$this->checkoutSession->setPayoneShippingAddresscheckScore($sScore); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set error message if checkout is configured to stop on error or set success = true instead |
117
|
|
|
* |
118
|
|
|
* @param AddresscheckResponse $oResponse |
119
|
|
|
* @return AddresscheckResponse |
120
|
|
|
*/ |
121
|
|
|
protected function handleErrorCase(AddresscheckResponse $oResponse) |
122
|
|
|
{ |
123
|
|
|
$sHandleError = $this->addresscheck->getConfigParam('handle_response_error'); |
124
|
|
|
if ($sHandleError == 'stop_checkout') { |
125
|
|
|
$oResponse->setData('errormessage', __($this->addresscheck->getErrorMessage())); // stop checkout with errormsg |
126
|
|
|
} elseif ($sHandleError == 'continue_checkout') { |
127
|
|
|
$oResponse->setData('success', true); // continue anyways |
128
|
|
|
} |
129
|
|
|
return $oResponse; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Handle the response according to its return status |
134
|
|
|
* |
135
|
|
|
* @param AddresscheckResponse $oResponse |
136
|
|
|
* @param AddressInterface $oAddress |
137
|
|
|
* @param array $aResponse |
138
|
|
|
* @return AddresscheckResponse |
139
|
|
|
*/ |
140
|
|
|
protected function handleResponse(AddresscheckResponse $oResponse, AddressInterface $oAddress, $aResponse) |
141
|
|
|
{ |
142
|
|
|
if ($aResponse['status'] == 'VALID') { // data was checked successfully |
143
|
|
|
$oAddress = $this->addresscheck->correctAddress($oAddress); |
144
|
|
|
if ($this->addresscheck->isAddressCorrected() === true) { // was address changed? |
145
|
|
|
$oResponse->setData('correctedAddress', $oAddress); |
146
|
|
|
$oResponse->setData('confirmMessage', $this->getConfirmMessage($oAddress)); |
147
|
|
|
} |
148
|
|
|
$oResponse->setData('success', true); |
149
|
|
|
} elseif ($aResponse['status'] == 'INVALID') { // given data invalid |
150
|
|
|
$oResponse->setData('errormessage', $this->addresscheck->getInvalidMessage($aResponse['customermessage'])); |
151
|
|
|
} elseif ($aResponse['status'] == 'ERROR') { // an error occured in the API |
152
|
|
|
$oResponse = $this->handleErrorCase($oResponse); |
153
|
|
|
} |
154
|
|
|
return $oResponse; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Send addresscheck request and handle the response object |
159
|
|
|
* |
160
|
|
|
* @param AddresscheckResponse $oResponse |
161
|
|
|
* @param AddressInterface $oAddress |
162
|
|
|
* @param bool $blIsBillingAddress |
163
|
|
|
* @return AddresscheckResponse |
164
|
|
|
*/ |
165
|
|
|
protected function handleAddresscheck(AddresscheckResponse $oResponse, AddressInterface $oAddress, $blIsBillingAddress) { |
166
|
|
|
$aResponse = $this->addresscheck->getResponse($oAddress, $blIsBillingAddress); |
167
|
|
|
if (is_array($aResponse)) { // is a real response existing? |
168
|
|
|
$this->addScoreToSession($oAddress, $blIsBillingAddress); |
169
|
|
|
$oResponse = $this->handleResponse($oResponse, $oAddress, $aResponse); |
170
|
|
|
} elseif ($aResponse === true) { // check lifetime still valid, set success to true |
171
|
|
|
$oResponse->setData('success', true); |
172
|
|
|
} |
173
|
|
|
return $oResponse; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* PAYONE addresscheck |
178
|
|
|
* The full class-paths must be given here otherwise the Magento 2 WebApi |
179
|
|
|
* cant handle this with its fake type system! |
180
|
|
|
* |
181
|
|
|
* @param \Magento\Quote\Api\Data\AddressInterface $addressData |
182
|
|
|
* @param bool $isBillingAddress |
183
|
|
|
* @param bool $isVirtual |
184
|
|
|
* @param double $dTotal |
185
|
|
|
* @return \Payone\Core\Service\V1\Data\AddresscheckResponse |
186
|
|
|
*/ |
187
|
|
|
public function checkAddress(\Magento\Quote\Api\Data\AddressInterface $addressData, $isBillingAddress, $isVirtual, $dTotal) |
188
|
|
|
{ |
189
|
|
|
$oResponse = $this->responseFactory->create(); |
190
|
|
|
$oResponse->setData('success', false); // set success to false as default, set to true later if true |
191
|
|
|
if ($this->addresscheck->isCheckNeededForQuote($isBillingAddress, $isVirtual, $dTotal)) { |
192
|
|
|
$oResponse = $this->handleAddresscheck($oResponse, $addressData, $isBillingAddress); |
193
|
|
|
} |
194
|
|
|
return $oResponse; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|