Passed
Pull Request — master (#189)
by
unknown
02:22
created

Base   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 299
rs 9.92
c 0
b 0
f 0
wmc 31

15 Methods

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