Completed
Push — master ( 0ce82e...a0cd9c )
by Florian
12s
created

Api::addPayoneOrderData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
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\Helper;
28
29
use Magento\Quote\Model\Quote;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote 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...
30
use Payone\Core\Model\Methods\PayoneMethod;
31
use Magento\Sales\Model\Order as SalesOrder;
0 ignored issues
show
Bug introduced by
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
 * Helper class for everything that has to do with APIs
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
class Api extends Base
44
{
45
    /**
46
     * PAYONE connection curl php
47
     *
48
     * @var \Payone\Core\Helper\Connection\CurlPhp
49
     */
50
    protected $connCurlPhp;
51
52
    /**
53
     * PAYONE connection curl cli
54
     *
55
     * @var \Payone\Core\Helper\Connection\CurlCli
56
     */
57
    protected $connCurlCli;
58
59
    /**
60
     * PAYONE connection fsockopen
61
     *
62
     * @var \Payone\Core\Helper\Connection\Fsockopen
63
     */
64
    protected $connFsockopen;
65
66
    /**
67
     * Fields to copy from the request array to the order
68
     *
69
     * @var array
70
     */
71
    protected $requestToOrder = [
72
        'reference' => 'payone_refnr',
73
        'request' => 'payone_authmode',
74
        'mode' => 'payone_mode',
75
        'mandate_identification' => 'payone_mandate_id',
76
        'workorderid' => 'payone_workorder_id',
77
        'add_paydata[installment_duration]' => 'payone_installment_duration',
78
    ];
79
80
    /**
81
     * Fields to copy from the response to the order
82
     *
83
     * @var array
84
     */
85
    protected $responseToOrder = [
86
        'txid' => 'payone_txid',
87
        'mandate_identification' => 'payone_mandate_id',
88
        'clearing_reference' => 'payone_clearing_reference',
89
        'add_paydata[clearing_reference]' => 'payone_clearing_reference',
90
        'add_paydata[workorderid]' => 'payone_workorder_id',
91
        'clearing_bankaccount' => 'payone_clearing_bankaccount',
92
        'clearing_bankcode' => 'payone_clearing_bankcode',
93
        'clearing_bankcountry' => 'payone_clearing_bankcountry',
94
        'clearing_bankname' => 'payone_clearing_bankname',
95
        'clearing_bankaccountholder' => 'payone_clearing_bankaccountholder',
96
        'clearing_bankcity' => 'payone_clearing_bankcity',
97
        'clearing_bankiban' => 'payone_clearing_bankiban',
98
        'clearing_bankbic' => 'payone_clearing_bankbic'
99
    ];
100
101
    /**
102
     * Constructor
103
     *
104
     * @param \Magento\Framework\App\Helper\Context      $context
105
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
106
     * @param \Payone\Core\Helper\Shop                   $shopHelper
107
     * @param \Payone\Core\Helper\Connection\CurlPhp     $connCurlPhp
108
     * @param \Payone\Core\Helper\Connection\CurlCli     $connCurlCli
109
     * @param \Payone\Core\Helper\Connection\Fsockopen   $connFsockopen
110
     */
111
    public function __construct(
112
        \Magento\Framework\App\Helper\Context $context,
113
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface 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...
114
        \Payone\Core\Helper\Shop $shopHelper,
115
        \Payone\Core\Helper\Connection\CurlPhp $connCurlPhp,
116
        \Payone\Core\Helper\Connection\CurlCli $connCurlCli,
117
        \Payone\Core\Helper\Connection\Fsockopen $connFsockopen
118
    ) {
119
        parent::__construct($context, $storeManager, $shopHelper);
120
        $this->connCurlPhp = $connCurlPhp;
121
        $this->connCurlCli = $connCurlCli;
122
        $this->connFsockopen = $connFsockopen;
123
    }
124
125
    /**
126
     * Check which communication possibilities are existing and send the request
127
     *
128
     * @param  string $sRequestUrl
129
     * @return array
130
     */
131
    public function sendApiRequest($sRequestUrl)
132
    {
133
        $aParsedRequestUrl = parse_url($sRequestUrl);
134
        if ($aParsedRequestUrl === false) {
135
            return ["errormessage" => "Payone API request URL could not be parsed."];
136
        }
137
138
        if ($this->connCurlPhp->isApplicable()) {
139
            // php native curl exists so we gonna use it for requesting
140
            $aResponse = $this->connCurlPhp->sendCurlPhpRequest($aParsedRequestUrl);
141
        } elseif ($this->connCurlCli->isApplicable()) {
142
            // cli version of curl exists on server
143
            $aResponse = $this->connCurlCli->sendCurlCliRequest($aParsedRequestUrl);
144
        } else {
145
            // last resort => via sockets
146
            $aResponse = $this->connFsockopen->sendSocketRequest($aParsedRequestUrl);
147
        }
148
149
        $aResponse = $this->formatOutputByResponse($aResponse);
150
151
        return $aResponse;
152
    }
153
154
    /**
155
     * Format response to a clean output array
156
     *
157
     * @param  array $aResponse
158
     * @return array
159
     */
160
    protected function formatOutputByResponse($aResponse)
161
    {
162
        $aOutput = [];
163
164
        if (is_array($aResponse)) { // correct response existing?
0 ignored issues
show
introduced by
The condition is_array($aResponse) is always true.
Loading history...
165
            foreach ($aResponse as $iLinenum => $sLine) { // go through line by line
166
                $iPos = strpos($sLine, "=");
167
                if ($iPos > 0) { // is a "=" as delimiter existing?
168
                    $aOutput[substr($sLine, 0, $iPos)] = trim(substr($sLine, $iPos + 1));
169
                } elseif (!empty($sLine)) { // is line not empty?
170
                    $aOutput[$iLinenum] = $sLine; // add the line unedited
171
                }
172
            }
173
        }
174
175
        return $aOutput;
176
    }
177
178
    /**
179
     * Generate the request url out of the params and die api url
180
     *
181
     * @param  array  $aParameters
182
     * @param  string $sApiUrl
183
     * @return string
184
     */
185
    public function getRequestUrl($aParameters, $sApiUrl)
186
    {
187
        $sRequestUrl = '';
188
        foreach ($aParameters as $sKey => $mValue) {
189
            if (is_array($mValue)) { // might be array
190
                foreach ($mValue as $i => $sSubValue) {
191
                    $sRequestUrl .= "&".$sKey."[".$i."]=".urlencode($sSubValue);
192
                }
193
            } else {
194
                $sRequestUrl .= "&".$sKey."=".urlencode($mValue);
195
            }
196
        }
197
        $sRequestUrl = $sApiUrl."?".substr($sRequestUrl, 1);
198
        return $sRequestUrl;
199
    }
200
201
    /**
202
     * Copy Data to order by given map
203
     *
204
     * @param SalesOrder $oOrder
205
     * @param array $aData
206
     * @param array $aMap
207
     * @return SalesOrder
208
     */
209
    protected function addDataToOrder(SalesOrder $oOrder, $aData, $aMap)
210
    {
211
        foreach ($aMap as $sFrom => $sTo) {
212
            if (isset($aData[$sFrom])) {
213
                $oOrder->setData($sTo, $aData[$sFrom]);
214
            }
215
        }
216
        return $oOrder;
217
    }
218
219
    /**
220
     * Add PAYONE information to the order object to be saved in the DB
221
     *
222
     * @param  SalesOrder $oOrder
223
     * @param  array      $aRequest
224
     * @param  array      $aResponse
225
     * @return void
226
     */
227
    public function addPayoneOrderData(SalesOrder $oOrder, $aRequest, $aResponse)
228
    {
229
        $this->addDataToOrder($oOrder, $aRequest, $this->requestToOrder);
230
        $this->addDataToOrder($oOrder, $aResponse, $this->responseToOrder);
231
    }
232
233
    /**
234
     * Check if invoice-data has to be added to the authorization request
235
     *
236
     * @param  PayoneMethod $oPayment
237
     * @return bool
238
     */
239
    public function isInvoiceDataNeeded(PayoneMethod $oPayment)
240
    {
241
        $blInvoiceEnabled = (bool)$this->getConfigParam('transmit_enabled', 'invoicing'); // invoicing enabled?
242
        if ($blInvoiceEnabled || $oPayment->needsProductInfo()) {
243
            return true; // invoice data needed
244
        }
245
        return false; // invoice data not needed
246
    }
247
248
    /**
249
     * Return base or display currency of the order depending on the config
250
     *
251
     * @param  SalesOrder $oOrder
252
     * @return null|string
253
     */
254
    public function getCurrencyFromOrder(SalesOrder $oOrder)
255
    {
256
        $sCurrency = $oOrder->getBaseCurrencyCode();
257
        if ($this->getConfigParam('currency') == 'display') {
258
            $sCurrency = $oOrder->getOrderCurrencyCode();
259
        }
260
        return $sCurrency;
261
    }
262
263
    /**
264
     * Return base or display currency of the quote depending on the config
265
     *
266
     * @param  Quote $oQuote
267
     * @return string
268
     */
269
    public function getCurrencyFromQuote(Quote $oQuote)
270
    {
271
        $sCurrency = $oQuote->getBaseCurrencyCode();
272
        if ($this->getConfigParam('currency') == 'display') {
273
            $sCurrency = $oQuote->getQuoteCurrencyCode();
274
        }
275
        return $sCurrency;
276
    }
277
278
    /**
279
     * Return base or display amount of the quote depending on the config
280
     *
281
     * @param  Quote $oQuote
282
     * @return float
283
     */
284
    public function getQuoteAmount(Quote $oQuote)
285
    {
286
        $dAmount = $oQuote->getBaseGrandTotal();
287
        if ($this->getConfigParam('currency') == 'display') {
288
            $dAmount = $oQuote->getGrandTotal(); // send display amount instead of base amount
289
        }
290
        return $dAmount;
291
    }
292
}
293