Passed
Push — master ( 81a28c...968877 )
by Florian
28s
created

Base::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * Constructor
115
     *
116
     * @param \Payone\Core\Helper\Shop                $shopHelper
117
     * @param \Payone\Core\Helper\Environment         $environmentHelper
118
     * @param \Payone\Core\Helper\Api                 $apiHelper
119
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
120
     */
121
    public function __construct(
122
        \Payone\Core\Helper\Shop $shopHelper,
123
        \Payone\Core\Helper\Environment $environmentHelper,
124
        \Payone\Core\Helper\Api $apiHelper,
125
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog
126
    ) {
127
        $this->shopHelper = $shopHelper;
128
        $this->environmentHelper = $environmentHelper;
129
        $this->apiHelper = $apiHelper;
130
        $this->apiLog = $apiLog;
131
        $this->initRequest();
132
    }
133
134
    /**
135
     * Initialize request
136
     * Set all default parameters
137
     *
138
     * @return void
139
     */
140
    protected function initRequest()
141
    {
142
        $this->addParameter('mid', $this->shopHelper->getConfigParam('mid')); // PayOne Merchant ID
143
        $this->addParameter('portalid', $this->shopHelper->getConfigParam('portalid')); // PayOne Portal ID
144
        $this->addParameter('key', md5($this->shopHelper->getConfigParam('key'))); // PayOne Portal Key
145
        $this->addParameter('encoding', $this->environmentHelper->getEncoding()); // Encoding
146
        $this->addParameter('integrator_name', 'Magento2'); // Shop-system
147
        $this->addParameter('integrator_version', $this->shopHelper->getMagentoVersion()); // Shop version
148
        $this->addParameter('solution_name', 'fatchip'); // Company developing the module
149
        $this->addParameter('solution_version', PayoneConfig::MODULE_VERSION); // Module version
150
    }
151
152
    /**
153
     * Add parameter to request
154
     *
155
     * @param  string $sKey               parameter key
156
     * @param  string $sValue             parameter value
157
     * @param  bool   $blAddAsNullIfEmpty add parameter with value NULL if empty. Default is false
158
     * @return void
159
     */
160
    public function addParameter($sKey, $sValue, $blAddAsNullIfEmpty = false)
161
    {
162
        if ($blAddAsNullIfEmpty === true && empty($sValue)) {
163
            $sValue = 'NULL'; // add value as string NULL - needed in certain situations
164
        }
165
        $this->aParameters[$sKey] = $sValue;
166
    }
167
168
    /**
169
     * Remove parameter from request
170
     *
171
     * @param  string $sKey parameter key
172
     * @return void
173
     */
174
    public function removeParameter($sKey)
175
    {
176
        if (array_key_exists($sKey, $this->aParameters)) {// is parameter set?
177
            unset($this->aParameters[$sKey]);
178
        }
179
    }
180
181
    /**
182
     * Get parameter from request or return false if parameter was not set
183
     *
184
     * @param  string $sKey parameter key
185
     * @return string|bool
186
     */
187
    public function getParameter($sKey)
188
    {
189
        if (array_key_exists($sKey, $this->aParameters)) {// is parameter set?
190
            return $this->aParameters[$sKey];
191
        }
192
        return false;
193
    }
194
195
    /**
196
     * Return all parameters
197
     *
198
     * @return array
199
     */
200
    public function getParameters()
201
    {
202
        return $this->aParameters;
203
    }
204
205
    /**
206
     * Set response array
207
     *
208
     * @param  $aResponse
209
     * @return void
210
     */
211
    public function setResponse($aResponse)
212
    {
213
        $this->aResponse = $aResponse;
214
    }
215
216
    /**
217
     * Return the response array
218
     *
219
     * @return array
220
     */
221
    public function getResponse()
222
    {
223
        return $this->aResponse;
224
    }
225
226
    /**
227
     * Add non-global parameters specifically configured in the payment type
228
     *
229
     * @param  PayoneMethod $oPayment
230
     * @return void
231
     */
232
    protected function addCustomParameters(PayoneMethod $oPayment)
233
    {
234
        foreach ($this->aCustomParamMap as $sParamName => $sConfigName) {// add all custom parameters
235
            $sCustomConfig = $oPayment->getCustomConfigParam($sConfigName); // get custom config param
236
            if (!empty($sCustomConfig)) { // only add if the param is configured
237
                if ($sConfigName == 'key') {
238
                    $this->addParameter($sParamName, md5($sCustomConfig)); // key isn't hashed in db
239
                } else {
240
                    $this->addParameter($sParamName, $sCustomConfig); // add custom param to request
241
                }
242
            }
243
        }
244
    }
245
246
    /**
247
     * Set the order id that is associated with this request
248
     *
249
     * @param  string $sOrderId
250
     * @return void
251
     */
252
    public function setOrderId($sOrderId)
253
    {
254
        $this->sOrderId = $sOrderId;
255
    }
256
257
    /**
258
     * Return order id if set
259
     *
260
     * @return string
261
     */
262
    public function getOrderId()
263
    {
264
        if ($this->sOrderId !== null) {// was order id set?
265
            return $this->sOrderId;
266
        }
267
        return '';
268
    }
269
270
    /**
271
     * Add the redirect urls to the request
272
     *
273
     * @param  PayoneMethod $oPayment
274
     * @return void
275
     */
276
    protected function addRedirectUrls(PayoneMethod $oPayment)
277
    {
278
        $this->addParameter('successurl', $oPayment->getSuccessUrl());
279
        $this->addParameter('errorurl', $oPayment->getErrorUrl());
280
        $this->addParameter('backurl', $oPayment->getCancelUrl());
281
    }
282
283
    /**
284
     * Validate if all general required parameters are set
285
     *
286
     * @return bool
287
     */
288
    protected function validateParameters()
289
    {
290
        if ($this->getParameter('mid') === false || $this->getParameter('portalid') === false ||
291
            $this->getParameter('key') === false || $this->getParameter('mode') === false) {
292
            return false;
293
        }
294
        return true;
295
    }
296
297
    /**
298
     * Send the previously prepared request, log request and response into the database and return the response
299
300
     * @param  PayoneMethod $oPayment
301
     * @return array
302
     */
303
    protected function send(PayoneMethod $oPayment = null)
304
    {
305
        if ($oPayment !== null && $oPayment->hasCustomConfig()) { // if payment type doesnt use the global settings
306
            $this->addCustomParameters($oPayment); // add custom connection settings
307
        }
308
309
        if (!$this->validateParameters()) {// all base parameters existing?
310
            return ["errormessage" => "Payone API Setup Data not complete (API-URL, MID, AID, PortalID, Key, Mode)"];
311
        }
312
        
313
        $sRequestUrl = $this->apiHelper->getRequestUrl($this->getParameters(), $this->sApiUrl);
314
        $aResponse = $this->apiHelper->sendApiRequest($sRequestUrl); // send request to PAYONE
315
        $this->setResponse($aResponse);
316
317
        $this->apiLog->addApiLogEntry($this->getParameters(), $aResponse, $aResponse['status'], $this->getOrderId()); // log request to db
318
319
        return $aResponse;
320
    }
321
}
322