Issues (1092)

Service/V1/Addresscheck.php (5 issues)

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;
0 ignored issues
show
The type Magento\Quote\Api\Data\AddressInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
The type Payone\Core\Service\V1\D...esscheckResponseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
     */
50
    protected $responseFactory;
51
52
    /**
53
     * Checkout session object
54
     *
55
     * @var \Magento\Checkout\Model\Session
0 ignored issues
show
The type Magento\Checkout\Model\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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";
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $sMessage  = /** @scrutinizer ignore-call */ __('Address corrected. Please confirm.')."\n\n";
Loading history...
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
0 ignored issues
show
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            $oResponse->setData('errormessage', /** @scrutinizer ignore-call */ __($this->addresscheck->getErrorMessage())); // stop checkout with errormsg
Loading history...
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) && array_key_exists('wrongCountry', $aResponse) === false) { // is a real response existing?
168
            $this->addScoreToSession($oAddress, $blIsBillingAddress);
169
            $oResponse = $this->handleResponse($oResponse, $oAddress, $aResponse);
170
        } elseif ($aResponse === true || isset($aResponse['wrongCountry'])) { // 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  string $cartId
182
     * @param  \Magento\Quote\Api\Data\AddressInterface $addressData
183
     * @param  bool $isBillingAddress
184
     * @param  bool $isVirtual
185
     * @param  double $dTotal
186
     * @return \Payone\Core\Service\V1\Data\AddresscheckResponse
187
     */
188
    public function checkAddress($cartId, \Magento\Quote\Api\Data\AddressInterface $addressData, $isBillingAddress, $isVirtual, $dTotal)
189
    {
190
        $oResponse = $this->responseFactory->create();
191
        $oResponse->setData('success', false); // set success to false as default, set to true later if true
192
        if ($this->addresscheck->isCheckNeededForQuote($isBillingAddress, $isVirtual, $dTotal)) {
193
            $oResponse = $this->handleAddresscheck($oResponse, $addressData, $isBillingAddress);
194
        }
195
        return $oResponse;
196
    }
197
}
198