Issues (1092)

Model/Api/Request/Base.php (1 issue)

Labels
Severity
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\Model\Api\Request;
28
29
use Payone\Core\Model\PayoneConfig;
30
use Payone\Core\Model\Methods\PayoneMethod;
31
use Magento\Sales\Model\Order;
0 ignored issues
show
The type Magento\Sales\Model\Order 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
 * Base class for all PAYONE API requests
35
 *
36
 * @category  Payone
37
 * @package   Payone_Magento2_Plugin
38
 * @author    FATCHIP GmbH <[email protected]>
39
 * @copyright 2003 - 2016 Payone GmbH
40
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
41
 * @link      http://www.payone.de
42
 */
43
abstract class Base
44
{
45
    /**
46
     * Order id
47
     *
48
     * @var string
49
     */
50
    protected $sOrderId = null;
51
52
    /**
53
     * Array or request parameters
54
     *
55
     * @var array
56
     */
57
    protected $aParameters = [];
58
59
    /**
60
     * Response of the request
61
     *
62
     * @var array
63
     */
64
    protected $aResponse = false;
65
66
    /**
67
     * URL of PAYONE Server API
68
     *
69
     * @var string
70
     */
71
    protected $sApiUrl = 'https://api.pay1.de/post-gateway/';
72
73
    /**
74
     * Map for custom parameters to be added $sParamName => $sConfigName
75
     *
76
     * @var array
77
     */
78
    protected $aCustomParamMap = [
79
        'mid' => 'mid',
80
        'portalid' => 'portalid',
81
        'aid' => 'aid',
82
        'key' => 'key',
83
        'request' => 'request',
84
    ];
85
86
    /**
87
     * PAYONE shop helper
88
     *
89
     * @var \Payone\Core\Helper\Shop
90
     */
91
    protected $shopHelper;
92
93
    /**
94
     * PAYONE environment helper
95
     *
96
     * @var \Payone\Core\Helper\Environment
97
     */
98
    protected $environmentHelper;
99
100
    /**
101
     * PAYONE api helper
102
     *
103
     * @var \Payone\Core\Helper\Api
104
     */
105
    protected $apiHelper;
106
107
    /**
108
     * PAYONE toolkit helper
109
     *
110
     * @var \Payone\Core\Helper\Toolkit
111
     */
112
    protected $toolkitHelper;
113
114
    /**
115
     * API-log resource model
116
     *
117
     * @var \Payone\Core\Model\ResourceModel\ApiLog
118
     */
119
    protected $apiLog;
120
121
    /**
122
     * Store id for the current context
123
     *
124
     * @var string
125
     */
126
    protected $storeCode = null;
127
128
    /**
129
     * Constructor
130
     *
131
     * @param \Payone\Core\Helper\Shop                $shopHelper
132
     * @param \Payone\Core\Helper\Environment         $environmentHelper
133
     * @param \Payone\Core\Helper\Api                 $apiHelper
134
     * @param \Payone\Core\Helper\Toolkit             $toolkitHelper
135
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
136
     */
137
    public function __construct(
138
        \Payone\Core\Helper\Shop $shopHelper,
139
        \Payone\Core\Helper\Environment $environmentHelper,
140
        \Payone\Core\Helper\Api $apiHelper,
141
        \Payone\Core\Helper\Toolkit $toolkitHelper,
142
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog
143
    ) {
144
        $this->shopHelper = $shopHelper;
145
        $this->environmentHelper = $environmentHelper;
146
        $this->apiHelper = $apiHelper;
147
        $this->toolkitHelper = $toolkitHelper;
148
        $this->apiLog = $apiLog;
149
        $this->initRequest();
150
    }
151
152
    /**
153
     * Initialize request
154
     * Set all default parameters
155
     *
156
     * @return void
157
     */
158
    protected function initRequest()
159
    {
160
        $this->aParameters = [];
161
        $this->addParameter('mid', $this->shopHelper->getConfigParam('mid', 'global', 'payone_general', $this->storeCode)); // PayOne Merchant ID
162
        $this->addParameter('portalid', $this->shopHelper->getConfigParam('portalid', 'global', 'payone_general', $this->storeCode)); // PayOne Portal ID
163
        $this->addParameter('key', $this->toolkitHelper->hashString($this->shopHelper->getConfigParam('key', 'global', 'payone_general', $this->storeCode) ?? '')); // PayOne Portal Key
164
        $this->addParameter('encoding', $this->environmentHelper->getEncoding()); // Encoding
165
        $this->addParameter('integrator_name', 'Magento2'); // Shop-system
166
        $this->addParameter('integrator_version', $this->shopHelper->getMagentoVersion()); // Shop version
167
        $this->addParameter('solution_name', 'fatchip'); // Company developing the module
168
        $this->addParameter('solution_version', PayoneConfig::MODULE_VERSION); // Module version
169
    }
170
171
    /**
172
     * Set current store code and reinit base parameters
173
     *
174
     * @param  string $sStoreCode
175
     * @return void
176
     */
177
    public function setStoreCode($sStoreCode)
178
    {
179
        if ($this->storeCode != $sStoreCode) {
180
            $this->storeCode = $sStoreCode;
181
            $this->initRequest(); //reinit base parameters
182
        }
183
    }
184
185
    /**
186
     * Add parameter to request
187
     *
188
     * @param  string $sKey               parameter key
189
     * @param  string $sValue             parameter value
190
     * @param  bool   $blAddAsNullIfEmpty add parameter with value NULL if empty. Default is false
191
     * @return void
192
     */
193
    public function addParameter($sKey, $sValue, $blAddAsNullIfEmpty = false)
194
    {
195
        if ($blAddAsNullIfEmpty === true && empty($sValue)) {
196
            $sValue = 'NULL'; // add value as string NULL - needed in certain situations
197
        }
198
        $this->aParameters[$sKey] = $sValue;
199
    }
200
201
    /**
202
     * Remove parameter from request
203
     *
204
     * @param  string $sKey parameter key
205
     * @return void
206
     */
207
    public function removeParameter($sKey)
208
    {
209
        if (array_key_exists($sKey, $this->aParameters)) {// is parameter set?
210
            unset($this->aParameters[$sKey]);
211
        }
212
    }
213
214
    /**
215
     * Get parameter from request or return false if parameter was not set
216
     *
217
     * @param  string $sKey parameter key
218
     * @return string|bool
219
     */
220
    public function getParameter($sKey)
221
    {
222
        if (array_key_exists($sKey, $this->aParameters)) {// is parameter set?
223
            return $this->aParameters[$sKey];
224
        }
225
        return false;
226
    }
227
228
    /**
229
     * Return all parameters
230
     *
231
     * @return array
232
     */
233
    public function getParameters()
234
    {
235
        return $this->aParameters;
236
    }
237
238
    /**
239
     * Set response array
240
     *
241
     * @param  $aResponse
242
     * @return void
243
     */
244
    public function setResponse($aResponse)
245
    {
246
        $this->aResponse = $aResponse;
247
    }
248
249
    /**
250
     * Return the response array
251
     *
252
     * @return array
253
     */
254
    public function getResponse()
255
    {
256
        return $this->aResponse;
257
    }
258
259
    /**
260
     * Add non-global parameters specifically configured in the payment type
261
     *
262
     * @param  PayoneMethod $oPayment
263
     * @return void
264
     */
265
    protected function addCustomParameters(PayoneMethod $oPayment)
266
    {
267
        foreach ($this->aCustomParamMap as $sParamName => $sConfigName) {// add all custom parameters
268
            $sCustomConfig = $oPayment->getCustomConfigParam($sConfigName); // get custom config param
269
            if (!empty($sCustomConfig)) { // only add if the param is configured
270
                if ($sConfigName == 'key') {
271
                    $sCustomConfig = $this->toolkitHelper->hashString($sCustomConfig); // key isn't hashed in db
272
                }
273
                $this->addParameter($sParamName, $sCustomConfig); // add custom param to request
274
            }
275
        }
276
    }
277
278
    /**
279
     * Set the order id that is associated with this request
280
     *
281
     * @param  string $sOrderId
282
     * @return void
283
     */
284
    public function setOrderId($sOrderId)
285
    {
286
        $this->sOrderId = $sOrderId;
287
    }
288
289
    /**
290
     * Return order id if set
291
     *
292
     * @return string
293
     */
294
    public function getOrderId()
295
    {
296
        if ($this->sOrderId !== null) {// was order id set?
297
            return $this->sOrderId;
298
        }
299
        return '';
300
    }
301
302
    /**
303
     * Add the redirect urls to the request
304
     *
305
     * @param  PayoneMethod $oPayment
306
     * @param  Order        $oOrder
307
     * @return void
308
     */
309
    protected function addRedirectUrls(PayoneMethod $oPayment, ?Order $oOrder = null)
310
    {
311
        $this->addParameter('successurl', $oPayment->getSuccessUrl($oOrder));
312
        $this->addParameter('errorurl', $oPayment->getErrorUrl());
313
        $this->addParameter('backurl', $oPayment->getCancelUrl());
314
    }
315
316
    /**
317
     * Validate if all general required parameters are set
318
     *
319
     * @return bool
320
     */
321
    protected function validateParameters()
322
    {
323
        if ($this->getParameter('mid') === false || $this->getParameter('portalid') === false ||
324
            $this->getParameter('key') === false || $this->getParameter('mode') === false) {
325
            return false;
326
        }
327
        return true;
328
    }
329
330
    /**
331
     * Send the previously prepared request, log request and response into the database and return the response
332
333
     * @param  PayoneMethod $oPayment
334
     * @return array
335
     */
336
    protected function send(?PayoneMethod $oPayment = null)
337
    {
338
        if ($oPayment !== null && $oPayment->hasCustomConfig()) { // if payment type doesnt use the global settings
339
            $this->addCustomParameters($oPayment); // add custom connection settings
340
        }
341
342
        if (!$this->validateParameters()) {// all base parameters existing?
343
            return ["errormessage" => "Payone API Setup Data not complete (API-URL, MID, AID, PortalID, Key, Mode)"];
344
        }
345
        
346
        $sRequestUrl = $this->apiHelper->getRequestUrl($this->getParameters(), $this->sApiUrl);
347
        $aResponse = $this->apiHelper->sendApiRequest($sRequestUrl); // send request to PAYONE
348
349
        $this->setResponse($aResponse);
350
351
        $this->apiLog->addApiLogEntry($this->getParameters(), $aResponse, $aResponse['status'], $this->getOrderId()); // log request to db
352
353
        return $aResponse;
354
    }
355
}
356