Passed
Push — master ( 040206...2f47c5 )
by
unknown
05:42 queued 02:29
created

Ratepay::getRatepayShopConfigById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
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 - 2020 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
/**
30
 * Helper class for ratepay payment
31
 */
32
class Ratepay extends \Payone\Core\Helper\Base
33
{
34
    /**
35
     * Object of profile request
36
     *
37
     * @var \Payone\Core\Model\Api\Request\Genericpayment\Profile
38
     */
39
    protected $profile;
40
41
    /**
42
     * Ratepay profile resource model
43
     *
44
     * @var \Payone\Core\Model\ResourceModel\RatepayProfileConfig
45
     */
46
    protected $profileResource;
47
48
    /**
49
     * Checkout session
50
     *
51
     * @var \Magento\Checkout\Model\Session
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Session 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...
52
     */
53
    protected $checkoutSession;
54
55
    /**
56
     * Payone API helper
57
     *
58
     * @var \Payone\Core\Helper\Api
59
     */
60
    protected $apiHelper;
61
62
    /**
63
     * Constructor
64
     *
65
     * @param \Magento\Framework\App\Helper\Context                 $context
66
     * @param \Magento\Store\Model\StoreManagerInterface            $storeManager
67
     * @param \Payone\Core\Helper\Shop                              $shopHelper
68
     * @param \Payone\Core\Model\Api\Request\Genericpayment\Profile $profile
69
     * @param \Payone\Core\Model\ResourceModel\RatepayProfileConfig $profileResource
70
     * @param \Magento\Checkout\Model\Session                       $checkoutSession
71
     * @param \Payone\Core\Helper\Api                               $apiHelper
72
     */
73
    public function __construct(
74
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Helper\Context 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...
75
        \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...
76
        \Payone\Core\Helper\Shop $shopHelper,
77
        \Payone\Core\Model\Api\Request\Genericpayment\Profile $profile,
78
        \Payone\Core\Model\ResourceModel\RatepayProfileConfig $profileResource,
79
        \Magento\Checkout\Model\Session $checkoutSession,
80
        \Payone\Core\Helper\Api $apiHelper
81
    ) {
82
        parent::__construct($context, $storeManager, $shopHelper);
83
        $this->profile = $profile;
84
        $this->profileResource = $profileResource;
85
        $this->checkoutSession = $checkoutSession;
86
        $this->apiHelper = $apiHelper;
87
    }
88
89
    /**
90
     * Returns json decoded array with ratepay shop config by given payment method
91
     *
92
     * @param  string $sPaymentMethod
93
     * @return array
94
     */
95
    public function getRatepayShopConfigByPaymentMethod($sPaymentMethod)
96
    {
97
        return $this->getRatepayShopConfigByPath("payone_payment/".$sPaymentMethod."/ratepay_shop_config");
98
    }
99
100
    /**
101
     * Extract shop_ids from configured shop ids for given payment method
102
     *
103
     * @param  string $sPaymentMethod
104
     * @return array
105
     */
106
    public function getRatepayShopConfigIdsByPaymentMethod($sPaymentMethod)
107
    {
108
        $aShopConfig = $this->getRatepayShopConfigByPaymentMethod($sPaymentMethod);
109
110
        $aShopIds = [];
111
        foreach ($aShopConfig as $aConfig) {
112
            if (!empty($aConfig['shop_id'])) {
113
                $aShopIds[] = $aConfig['shop_id'];
114
            }
115
        }
116
117
        return $aShopIds;
118
    }
119
120
    /**
121
     * Returns json decoded array with ratepay shop config id by full config path
122
     *
123
     * @param  string $sPath
124
     * @return array
125
     */
126
    public function getRatepayShopConfigByPath($sPath)
127
    {
128
        $aReturn = [];
129
130
        $sShopConfig = $this->getConfigParamByPath($sPath);
131
        if (!empty($sShopConfig)) {
132
            $aShopConfig = json_decode($sShopConfig, true);
133
            if (is_array($aShopConfig)) {
134
                foreach ($aShopConfig as $aConfig) {
135
                    if (!empty($aConfig['shop_id'])) {
136
                        $aReturn[] = $aConfig;
137
                    }
138
                }
139
            }
140
        }
141
        return $aReturn;
142
    }
143
144
    /**
145
     * Extract payment method from config path
146
     *
147
     * @param  string $sPath
148
     * @return bool|mixed
149
     */
150
    public function getPaymentMethodFromPath($sPath)
151
    {
152
        preg_match("/payone_payment\/(.*)\/ratepay_shop_config/", $sPath, $aMatch);
153
        if (is_array($aMatch) && isset($aMatch[1])) {
154
            return $aMatch[1];
155
        }
156
        return false;
157
    }
158
159
    /**
160
     * Imports new profile configuration
161
     *
162
     * @param  string $sShopId
163
     * @param  string $sCurrency
164
     * @param  string $sMethodCode
165
     * @return void
166
     */
167
    public function importProfileConfiguration($sShopId, $sCurrency, $sMethodCode)
168
    {
169
        if ($this->profileResource->profileExists($sShopId) === false) {
170
            $sMode = $this->getConfigParam('mode', $sMethodCode, 'payone_payment');
171
            $aResult = $this->profile->sendRequest($sShopId, $sCurrency, $sMode);
172
            if (isset($aResult['status'])) {
173
                if ($aResult['status'] == 'OK') {
174
                    $this->profileResource->insertProfileConfig($sShopId, $aResult);
175
                }
176
            }
177
        }
178
    }
179
180
    /**
181
     * Refreshes all of the profile configs of the given payment method
182
     *
183
     * @param  $sMethodCode
184
     * @return void
185
     */
186
    public function refreshProfiles($sMethodCode)
187
    {
188
        $sMode = $this->getConfigParam('mode', $sMethodCode, 'payone_payment');
189
190
        $aShopIds = $this->getRatepayShopConfigByPaymentMethod($sMethodCode);
191
        foreach ($aShopIds as $aConfig) {
192
            $aResult = $this->profile->sendRequest($aConfig['shop_id'], $aConfig['currency'], $sMode);
193
            $this->profileResource->updateProfileConfig($aConfig['shop_id'], $aResult);
194
        }
195
    }
196
197
    /**
198
     * Generates device fingerprint token vom customer id and time
199
     *
200
     * @return string
201
     */
202
    protected function generateDeviceFingerprintToken()
203
    {
204
        return md5($this->checkoutSession->getQuote()->getCustomer()->getId().'_'.microtime());
205
    }
206
207
    /**
208
     * Generates Ratepay device fingerprint token or takes it from the checkout session
209
     *
210
     * @return string
211
     */
212
    public function getRatepayDeviceFingerprintToken()
213
    {
214
        $sTokenFromSession = $this->checkoutSession->getPayoneRatepayDeviceFingerprintToken();
215
        if (empty($sTokenFromSession)) {
216
            $sTokenFromSession = $this->generateDeviceFingerprintToken();
217
            $this->checkoutSession->setPayoneRatepayDeviceFingerprintToken($sTokenFromSession);
218
        }
219
        return $sTokenFromSession;
220
    }
221
222
    /**
223
     * Get matching Ratepay shop id for current transaction
224
     *
225
     * @param  string $sMethodCode
226
     * @param  string $sCountryCode
227
     * @param  string $sCurrency
228
     * @param  double $dGrandTotal
229
     * @return string
230
     */
231
    public function getRatepayShopId($sMethodCode, $sCountryCode, $sCurrency, $dGrandTotal)
232
    {
233
        $aShopIds = $this->getRatepayShopConfigIdsByPaymentMethod($sMethodCode);
234
        $sShopId = $this->profileResource->getMatchingShopId($sMethodCode, $aShopIds, $sCountryCode, $sCurrency, $dGrandTotal);
235
236
        return $sShopId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sShopId also could return the type boolean which is incompatible with the documented return type string.
Loading history...
237
    }
238
239
    /**
240
     * Get matching Ratepay shop config for current transaction
241
     *
242
     * @param string $sShopId
243
     * @return array|false
244
     */
245
    public function getRatepayShopConfigById($sShopId)
246
    {
247
        $aProfileConfigs = $this->profileResource->getProfileConfigsByIds([$sShopId]);
248
        if (!empty($aProfileConfigs)) {
249
            return array_shift($aProfileConfigs);
250
        }
251
        return false;
252
    }
253
}
254