Base::getParameter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 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
     * URL of PAYONE Server API
60
     *
61
     * @var string
62
     */
63
    protected $sApiUrl = 'https://api.pay1.de/post-gateway/';
64
65
    /**
66
     * Map for custom parameters to be added $sParamName => $sConfigName
67
     *
68
     * @var array
69
     */
70
    protected $aCustomParamMap = [
71
        'mid' => 'mid',
72
        'portalid' => 'portalid',
73
        'aid' => 'aid',
74
        'key' => 'key',
75
        'request' => 'request',
76
    ];
77
78
    /**
79
     * PAYONE shop helper
80
     *
81
     * @var \Payone\Core\Helper\Shop
82
     */
83
    protected $shopHelper;
84
85
    /**
86
     * PAYONE environment helper
87
     *
88
     * @var \Payone\Core\Helper\Environment
89
     */
90
    protected $environmentHelper;
91
92
    /**
93
     * PAYONE api helper
94
     *
95
     * @var \Payone\Core\Helper\Api
96
     */
97
    protected $apiHelper;
98
99
    /**
100
     * API-log resource model
101
     *
102
     * @var \Payone\Core\Model\ResourceModel\ApiLog
103
     */
104
    protected $apiLog;
105
106
    /**
107
     * Constructor
108
     *
109
     * @param \Payone\Core\Helper\Shop                $shopHelper
110
     * @param \Payone\Core\Helper\Environment         $environmentHelper
111
     * @param \Payone\Core\Helper\Api                 $apiHelper
112
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
113
     */
114
    public function __construct(
115
        \Payone\Core\Helper\Shop $shopHelper,
116
        \Payone\Core\Helper\Environment $environmentHelper,
117
        \Payone\Core\Helper\Api $apiHelper,
118
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog
119
    ) {
120
        $this->shopHelper = $shopHelper;
121
        $this->environmentHelper = $environmentHelper;
122
        $this->apiHelper = $apiHelper;
123
        $this->apiLog = $apiLog;
124
        $this->initRequest();
125
    }
126
127
    /**
128
     * Initialize request
129
     * Set all default parameters
130
     *
131
     * @return void
132
     */
133
    protected function initRequest()
134
    {
135
        $this->addParameter('mid', $this->shopHelper->getConfigParam('mid')); // PayOne Merchant ID
136
        $this->addParameter('portalid', $this->shopHelper->getConfigParam('portalid')); // PayOne Portal ID
137
        $this->addParameter('key', md5($this->shopHelper->getConfigParam('key'))); // PayOne Portal Key
138
        $this->addParameter('encoding', $this->environmentHelper->getEncoding()); // Encoding
139
        $this->addParameter('integrator_name', 'Magento2'); // Shop-system
140
        $this->addParameter('integrator_version', $this->shopHelper->getMagentoVersion()); // Shop version
141
        $this->addParameter('solution_name', 'fatchip'); // Company developing the module
142
        $this->addParameter('solution_version', PayoneConfig::MODULE_VERSION); // Module version
143
    }
144
145
    /**
146
     * Add parameter to request
147
     *
148
     * @param  string $sKey               parameter key
149
     * @param  string $sValue             parameter value
150
     * @param  bool   $blAddAsNullIfEmpty add parameter with value NULL if empty. Default is false
151
     * @return void
152
     */
153
    public function addParameter($sKey, $sValue, $blAddAsNullIfEmpty = false)
154
    {
155
        if ($blAddAsNullIfEmpty === true && empty($sValue)) {
156
            $sValue = 'NULL'; // add value as string NULL - needed in certain situations
157
        }
158
        $this->aParameters[$sKey] = $sValue;
159
    }
160
161
    /**
162
     * Remove parameter from request
163
     *
164
     * @param  string $sKey parameter key
165
     * @return void
166
     */
167
    public function removeParameter($sKey)
168
    {
169
        if (array_key_exists($sKey, $this->aParameters)) {// is parameter set?
170
            unset($this->aParameters[$sKey]);
171
        }
172
    }
173
174
    /**
175
     * Get parameter from request or return false if parameter was not set
176
     *
177
     * @param  string $sKey parameter key
178
     * @return string|bool
179
     */
180
    public function getParameter($sKey)
181
    {
182
        if (array_key_exists($sKey, $this->aParameters)) {// is parameter set?
183
            return $this->aParameters[$sKey];
184
        }
185
        return false;
186
    }
187
188
    /**
189
     * Return all parameters
190
     *
191
     * @return array
192
     */
193
    public function getParameters()
194
    {
195
        return $this->aParameters;
196
    }
197
198
    /**
199
     * Add non-global parameters specifically configured in the payment type
200
     *
201
     * @param  PayoneMethod $oPayment
202
     * @return void
203
     */
204
    protected function addCustomParameters(PayoneMethod $oPayment)
205
    {
206
        foreach ($this->aCustomParamMap as $sParamName => $sConfigName) {// add all custom parameters
207
            $sCustomConfig = $oPayment->getCustomConfigParam($sConfigName); // get custom config param
208
            if (!empty($sCustomConfig)) { // only add if the param is configured
209
                if ($sConfigName == 'key') {
210
                    $this->addParameter($sParamName, md5($sCustomConfig)); // key isn't hashed in db
211
                } else {
212
                    $this->addParameter($sParamName, $sCustomConfig); // add custom param to request
213
                }
214
            }
215
        }
216
    }
217
218
    /**
219
     * Set the order id that is associated with this request
220
     *
221
     * @param  string $sOrderId
222
     * @return void
223
     */
224
    public function setOrderId($sOrderId)
225
    {
226
        $this->sOrderId = $sOrderId;
227
    }
228
229
    /**
230
     * Return order id if set
231
     *
232
     * @return string
233
     */
234
    public function getOrderId()
235
    {
236
        if ($this->sOrderId !== null) {// was order id set?
237
            return $this->sOrderId;
238
        }
239
        return '';
240
    }
241
242
    /**
243
     * Add the redirect urls to the request
244
     *
245
     * @param  PayoneMethod $oPayment
246
     * @return void
247
     */
248
    protected function addRedirectUrls(PayoneMethod $oPayment)
249
    {
250
        $this->addParameter('successurl', $oPayment->getSuccessUrl());
251
        $this->addParameter('errorurl', $oPayment->getErrorUrl());
252
        $this->addParameter('backurl', $oPayment->getCancelUrl());
253
    }
254
255
    /**
256
     * Validate if all general required parameters are set
257
     *
258
     * @return bool
259
     */
260
    protected function validateParameters()
261
    {
262
        if ($this->getParameter('mid') === false || $this->getParameter('portalid') === false ||
263
            $this->getParameter('key') === false || $this->getParameter('mode') === false) {
264
            return false;
265
        }
266
        return true;
267
    }
268
269
    /**
270
     * Send the previously prepared request, log request and response into the database and return the response
271
272
     * @param  PayoneMethod $oPayment
273
     * @return array
274
     */
275
    protected function send(PayoneMethod $oPayment = null)
276
    {
277
        if ($oPayment !== null && $oPayment->hasCustomConfig()) { // if payment type doesnt use the global settings
278
            $this->addCustomParameters($oPayment); // add custom connection settings
279
        }
280
281
        if (!$this->validateParameters()) {// all base parameters existing?
282
            return ["errormessage" => "Payone API Setup Data not complete (API-URL, MID, AID, PortalID, Key, Mode)"];
283
        }
284
285
        $sRequestUrl = $this->apiHelper->getRequestUrl($this->getParameters(), $this->sApiUrl);
286
        $aResponse = $this->apiHelper->sendApiRequest($sRequestUrl); // send request to PAYONE
287
        $this->apiLog->addApiLogEntry($this, $aResponse, $aResponse['status']); // log request to db
288
289
        return $aResponse;
290
    }
291
}
292