Completed
Push — master ( c63f43...6c4da9 )
by Florian
9s
created

HostedIframe::getCvcMaxLengths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 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 Payone\Core\Model\Source\CreditcardTypes;
30
31
/**
32
 * Helper class for everything that has to do with hosted Iframe
33
 */
34
class HostedIframe extends \Payone\Core\Helper\Base
35
{
36
    /**
37
     * Configuration params for the hosted Iframe creditcard implementation
38
     *
39
     * @var array
40
     */
41
    protected $aHostedParams = null;
42
43
    /**
44
     * PAYONE payment helper
45
     *
46
     * @var \Payone\Core\Helper\Payment
47
     */
48
    protected $paymentHelper;
49
50
    /**
51
     * PAYONE toolkit helper
52
     *
53
     * @var \Payone\Core\Helper\Toolkit
54
     */
55
    protected $toolkitHelper;
56
57
    /**
58
     * Constructor
59
     *
60
     * @param \Magento\Framework\App\Helper\Context      $context
61
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
62
     * @param \Payone\Core\Helper\Shop                   $shopHelper
63
     * @param \Payone\Core\Helper\Payment                $paymentHelper
64
     */
65
    public function __construct(
66
        \Magento\Framework\App\Helper\Context $context,
67
        \Magento\Store\Model\StoreManagerInterface $storeManager,
68
        \Payone\Core\Helper\Shop $shopHelper,
69
        \Payone\Core\Helper\Payment $paymentHelper
70
    ) {
71
        parent::__construct($context, $storeManager, $shopHelper);
72
        $this->paymentHelper = $paymentHelper;
73
    }
74
75
    /**
76
     * Get hosted params configuration
77
     *
78
     * @return array
79
     */
80
    protected function getHostedParams()
81
    {
82
        if ($this->aHostedParams === null) {
83
            $this->aHostedParams = [];
84
            $sHostedParams = $this->getConfigParam('cc_template', 'creditcard'); // get params from config
85
            if ($sHostedParams) { // params set in config?
86
                $aHostedParams = $this->unserialize($sHostedParams); // array from serialized string
87
                if (is_array($aHostedParams) && !empty($aHostedParams)) {
88
                    $this->aHostedParams = $aHostedParams;
89
                }
90
            }
91
        }
92
        return $this->aHostedParams;
93
    }
94
95
    /**
96
     * Get the field config for one field type for hosted Iframe implementation
97
     *
98
     * @param  string $sName
99
     * @param  string $sParamPrefix
100
     * @return array
101
     */
102
    protected function getFieldConfigField($sName, $sParamPrefix)
103
    {
104
        $aHostedParams = $this->getHostedParams();
105
106
        $aField = [];
107
        if (!empty($aHostedParams)) {
108
            $aField['selector'] = $sName;
109
            $aField['type'] = $aHostedParams[$sParamPrefix.'_type'];
110
            $aField['size'] = $aHostedParams[$sParamPrefix.'_count'];
111
            $aField['maxlength'] = $aHostedParams[$sParamPrefix.'_max'];
112
            if ($aHostedParams[$sParamPrefix.'_style'] == "custom") {
113
                $aField['style'] = $aHostedParams[$sParamPrefix.'_css'];
114
            }
115
            if ($aHostedParams[$sParamPrefix.'_iframe'] == "custom") {
116
                $aField['iframe'] = [
117
                    'width' => $aHostedParams[$sParamPrefix.'_width'],
118
                    'height' => $aHostedParams[$sParamPrefix.'_height'],
119
                ];
120
            }
121
        }
122
        return $aField;
123
    }
124
125
    /**
126
     * Create field config array
127
     *
128
     * @return array
129
     */
130
    protected function getFieldConfig()
131
    {
132
        $aFields = [];
133
        $aFields['cardpan'] = $this->getFieldConfigField('cardpan', 'Number');
134
        if ($this->paymentHelper->isCheckCvcActive() === true) { // cvc field activated?
135
            $aFields['cardcvc2'] = $this->getFieldConfigField('cardcvc2', 'CVC');
136
            $aFields['cardcvc2']['length'] = $this->getCvcMaxLengths();
137
        }
138
        $aFields['cardexpiremonth'] = $this->getFieldConfigField('cardexpiremonth', 'Month');
139
        $aFields['cardexpireyear'] = $this->getFieldConfigField('cardexpireyear', 'Year');
140
        return $aFields;
141
    }
142
143
    /**
144
     * Return array with the cvc length of all creditcard types
145
     *
146
     * @return array
147
     */
148
    protected function getCvcMaxLengths()
149
    {
150
        $aLenghts = [];
151
        foreach (CreditcardTypes::getCreditcardTypes() as $sType => $aType) {
152
            $aLenghts[$sType] = $aType['cvc_length'];
153
        }
154
        return $aLenghts;
155
    }
156
157
    /**
158
     * Create default style array
159
     *
160
     * @param  array $aHostedParams
161
     * @return array
162
     */
163
    protected function getDefaultStyles($aHostedParams)
164
    {
165
        $aDefaultStyle = [];
166
        $aDefaultStyle['input'] = $aHostedParams['Standard_input'];
167
        $aDefaultStyle['select'] = $aHostedParams['Standard_selection'];
168
        $aDefaultStyle['iframe'] = [
169
            'width' => $aHostedParams['Iframe_width'],
170
            'height' => $aHostedParams['Iframe_height'],
171
        ];
172
        return $aDefaultStyle;
173
    }
174
175
    /**
176
     * Generate the complete hosted iframe configuration
177
     *
178
     * @return array
179
     */
180
    public function getHostedFieldConfig()
181
    {
182
        $aHostedParams = $this->getHostedParams(); // get hosted params from config
183
184
        $aFieldConfig = [];
185
        if (!empty($aHostedParams)) { // hosted iframe config existing?
186
            $aFieldConfig['fields'] = $this->getFieldConfig(); // generate config for all field types
187
            $aFieldConfig['defaultStyle'] = $this->getDefaultStyles($aHostedParams);
188
            if ($aHostedParams['Errors_active'] == "true") {
189
                $aFieldConfig['error'] = 'errorOutput'; // area to display error-messages (optional)
190
                $aFieldConfig['language'] = $aHostedParams['Errors_lang']; // has to be defined in javascript
191
            }
192
        }
193
        return $aFieldConfig;
194
    }
195
}
196