RatepayProfileConfig::getProfileConfigsByIds()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
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\Model\ResourceModel;
28
29
use Payone\Core\Model\PayoneConfig;
30
31
/**
32
 * Resource model for payone_ratepay_profile_config table
33
 */
34
class RatepayProfileConfig extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Model\...urceModel\Db\AbstractDb 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...
35
{
36
    /**
37
     * Initialize connection and table
38
     *
39
     * @return void
40
     */
41
    protected function _construct()
42
    {
43
        $this->_init(\Payone\Core\Setup\Tables\RatepayProfileConfig::TABLE_RATEPAY_PROFILE_CONFIG, 'id');
44
    }
45
46
    /**
47
     * Returns all profile configs
48
     *
49
     * @return array
50
     */
51
    public function getAllProfileConfigs()
52
    {
53
        return $this->getProfileConfigsByIds([]);
54
    }
55
56
    /**
57
     * Get profile configs by given shop ids
58
     *
59
     * @param  array $aShopIds
60
     * @return array
61
     */
62
    public function getProfileConfigsByIds($aShopIds)
63
    {
64
        $oSelect = $this->getConnection()->select()
65
            ->from($this->getMainTable())
66
            ->order('shop_id ASC');
67
68
        if (!empty($aShopIds)) {
69
            $oSelect->where("shop_id IN ('".implode("','", $aShopIds)."')");
70
        }
71
72
        $aResult = $this->getConnection()->fetchAll($oSelect);
73
        return $aResult;
74
    }
75
76
    /**
77
     * Checks if given profile config exists in the database
78
     *
79
     * @param  string $sShopId
80
     * @return bool
81
     */
82
    public function profileExists($sShopId)
83
    {
84
        $aProfileConfigs = $this->getProfileConfigsByIds([$sShopId]);
85
        if (!empty($aProfileConfigs)) {
86
            return true;
87
        }
88
        return false;
89
    }
90
91
    /**
92
     * Convert strange Ratepay yes/no values to int-style bool values for the database
93
     *
94
     * @param  string $sValue
95
     * @return int
96
     */
97
    protected function convertYesNoToBool($sValue)
98
    {
99
        if (strtolower($sValue) == "yes") {
100
            return 1;
101
        }
102
103
        if (strtolower($sValue) == "no") {
104
            return 0;
105
        }
106
        return $sValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $sValue returns the type string which is incompatible with the documented return type integer.
Loading history...
107
    }
108
109
    /**
110
     * Fills data array for insert and update queries
111
     *
112
     * @param  array $aProfileResponse
113
     * @return array
114
     */
115
    protected function getDataArray($aProfileResponse)
116
    {
117
        $aData = [
118
            'profile_id'                            => isset($aProfileResponse['add_paydata[profile-id]']) ? $aProfileResponse['add_paydata[profile-id]'] : '',
119
            'merchant_name'                         => isset($aProfileResponse['add_paydata[merchant-name]']) ? $aProfileResponse['add_paydata[merchant-name]'] : '',
120
            'merchant_status'                       => isset($aProfileResponse['add_paydata[merchant-status]']) ? $aProfileResponse['add_paydata[merchant-status]'] : 0,
121
            'shop_name'                             => isset($aProfileResponse['add_paydata[shop-name]']) ? $aProfileResponse['add_paydata[shop-name]'] : '',
122
            'name'                                  => $aProfileResponse['add_paydata[name]'],
123
            'currency'                              => $aProfileResponse['add_paydata[currency]'],
124
            'type'                                  => $aProfileResponse['add_paydata[type]'],
125
            'activation_status_elv'                 => $aProfileResponse['add_paydata[activation-status-elv]'],
126
            'activation_status_installment'         => $aProfileResponse['add_paydata[activation-status-installment]'],
127
            'activation_status_invoice'             => $aProfileResponse['add_paydata[activation-status-invoice]'],
128
            'activation_status_prepayment'          => $aProfileResponse['add_paydata[activation-status-prepayment]'],
129
            'amount_min_longrun'                    => $aProfileResponse['add_paydata[amount-min-longrun]'],
130
            'b2b_pq_full'                           => $this->convertYesNoToBool($aProfileResponse['add_paydata[b2b-PQ-full]']),
131
            'b2b_pq_light'                          => isset($aProfileResponse['add_paydata[b2b-PQ-light]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[b2b-PQ-light]']) : 0,
132
            'b2b_elv'                               => $this->convertYesNoToBool($aProfileResponse['add_paydata[b2b-elv]']),
133
            'b2b_installment'                       => $this->convertYesNoToBool($aProfileResponse['add_paydata[b2b-installment]']),
134
            'b2b_invoice'                           => $this->convertYesNoToBool($aProfileResponse['add_paydata[b2b-invoice]']),
135
            'b2b_prepayment'                        => $this->convertYesNoToBool($aProfileResponse['add_paydata[b2b-prepayment]']),
136
            'country_code_billing'                  => $aProfileResponse['add_paydata[country-code-billing]'],
137
            'country_code_delivery'                 => $aProfileResponse['add_paydata[country-code-delivery]'],
138
            'delivery_address_pq_full'              => $this->convertYesNoToBool($aProfileResponse['add_paydata[delivery-address-PQ-full]']),
139
            'delivery_address_pq_light'             => isset($aProfileResponse['add_paydata[delivery-address-PQ-light]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[delivery-address-PQ-light]']) : 0,
140
            'delivery_address_elv'                  => $this->convertYesNoToBool($aProfileResponse['add_paydata[delivery-address-elv]']),
141
            'delivery_address_installment'          => $this->convertYesNoToBool($aProfileResponse['add_paydata[delivery-address-installment]']),
142
            'delivery_address_invoice'              => $this->convertYesNoToBool($aProfileResponse['add_paydata[delivery-address-invoice]']),
143
            'delivery_address_prepayment'           => $this->convertYesNoToBool($aProfileResponse['add_paydata[delivery-address-prepayment]']),
144
            'device_fingerprint_snippet_id'         => isset($aProfileResponse['add_paydata[device-fingerprint-snippet-id]']) ? $aProfileResponse['add_paydata[device-fingerprint-snippet-id]'] : NULL,
145
            'eligibility_device_fingerprint'        => isset($aProfileResponse['add_paydata[eligibility-device-fingerprint]']) ? $aProfileResponse['add_paydata[eligibility-device-fingerprint]'] : NULL,
146
            'eligibility_ratepay_elv'               => isset($aProfileResponse['add_paydata[eligibility-ratepay-elv]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[eligibility-ratepay-elv]']) : 0,
147
            'eligibility_ratepay_installment'       => isset($aProfileResponse['add_paydata[eligibility-ratepay-installment]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[eligibility-ratepay-installment]']) : 0,
148
            'eligibility_ratepay_invoice'           => isset($aProfileResponse['add_paydata[eligibility-ratepay-invoice]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[eligibility-ratepay-invoice]']) : 0,
149
            'eligibility_ratepay_pq_full'           => isset($aProfileResponse['add_paydata[eligibility-ratepay-pq-full]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[eligibility-ratepay-pq-full]']) : 0,
150
            'eligibility_ratepay_pq_light'          => isset($aProfileResponse['add_paydata[eligibility-ratepay-pq-light]']) ? $this->convertYesNoToBool($aProfileResponse['add_paydata[eligibility-ratepay-pq-light]']) : 0,
151
            'eligibility_ratepay_prepayment'        => $this->convertYesNoToBool($aProfileResponse['add_paydata[eligibility-ratepay-prepayment]']),
152
            'interest_rate_merchant_towards_bank'   => $aProfileResponse['add_paydata[interest-rate-merchant-towards-bank]'],
153
            'interestrate_default'                  => $aProfileResponse['add_paydata[interestrate-default]'],
154
            'interestrate_max'                      => $aProfileResponse['add_paydata[interestrate-max]'],
155
            'interestrate_min'                      => $aProfileResponse['add_paydata[interestrate-min]'],
156
            'min_difference_dueday'                 => $aProfileResponse['add_paydata[min-difference-dueday]'],
157
            'month_allowed'                         => $aProfileResponse['add_paydata[month-allowed]'],
158
            'month_longrun'                         => $aProfileResponse['add_paydata[month-longrun]'],
159
            'month_number_max'                      => $aProfileResponse['add_paydata[month-number-max]'],
160
            'month_number_min'                      => $aProfileResponse['add_paydata[month-number-min]'],
161
            'payment_amount'                        => $aProfileResponse['add_paydata[payment-amount]'],
162
            'payment_firstday'                      => $aProfileResponse['add_paydata[payment-firstday]'],
163
            'payment_lastrate'                      => $aProfileResponse['add_paydata[payment-lastrate]'],
164
            'rate_min_longrun'                      => $aProfileResponse['add_paydata[rate-min-longrun]'],
165
            'rate_min_normal'                       => $aProfileResponse['add_paydata[rate-min-normal]'],
166
            'service_charge'                        => $aProfileResponse['add_paydata[service-charge]'],
167
            'tx_limit_elv_max'                      => isset($aProfileResponse['add_paydata[tx-limit-elv-max]']) ? $aProfileResponse['add_paydata[tx-limit-elv-max]'] : 0,
168
            'tx_limit_elv_min'                      => isset($aProfileResponse['add_paydata[tx-limit-elv-min]']) ? $aProfileResponse['add_paydata[tx-limit-elv-min]'] : 0,
169
            'tx_limit_installment_max'              => isset($aProfileResponse['add_paydata[tx-limit-installment-max]']) ? $aProfileResponse['add_paydata[tx-limit-installment-max]'] : 0,
170
            'tx_limit_installment_min'              => isset($aProfileResponse['add_paydata[tx-limit-installment-min]']) ? $aProfileResponse['add_paydata[tx-limit-installment-min]'] : 0,
171
            'tx_limit_invoice_max'                  => isset($aProfileResponse['add_paydata[tx-limit-invoice-max]']) ? $aProfileResponse['add_paydata[tx-limit-invoice-max]'] : 0,
172
            'tx_limit_invoice_min'                  => isset($aProfileResponse['add_paydata[tx-limit-invoice-min]']) ? $aProfileResponse['add_paydata[tx-limit-invoice-min]'] : 0,
173
            'tx_limit_prepayment_max'               => isset($aProfileResponse['add_paydata[tx-limit-prepayment-max]']) ? $aProfileResponse['add_paydata[tx-limit-prepayment-max]'] : 0,
174
            'tx_limit_prepayment_min'               => isset($aProfileResponse['add_paydata[tx-limit-prepayment-min]']) ? $aProfileResponse['add_paydata[tx-limit-prepayment-min]'] : 0,
175
            'valid_payment_firstdays'               => $aProfileResponse['add_paydata[valid-payment-firstdays]'],
176
        ];
177
        return $aData;
178
    }
179
180
    /**
181
     * Updates existing profile config in the database
182
     *
183
     * @param  string $sShopId
184
     * @param  array $aProfileResponse
185
     * @return void
186
     */
187
    public function updateProfileConfig($sShopId, $aProfileResponse)
188
    {
189
        $data = $this->getDataArray($aProfileResponse);
190
        $where = ['shop_id = ?' => $sShopId];
191
        $this->getConnection()->update($this->getMainTable(), $data, $where);
192
    }
193
194
    /**
195
     * Insert new line into payone_ratepay_profile_config table
196
     *
197
     * @param  string $sShopId
198
     * @param  array  $aProfileResponse
199
     * @return void
200
     */
201
    public function insertProfileConfig($sShopId, $aProfileResponse)
202
    {
203
        $data = $this->getDataArray($aProfileResponse);
204
        $data['shop_id'] = $sShopId;
205
        $this->getConnection()->insert($this->getMainTable(), $data);
206
    }
207
208
    /**
209
     * Get matching shop id for current quote parameters
210
     *
211
     * @param  string $sMethodCode
212
     * @param  array $aShopIds
213
     * @param  string $sCountryCode
214
     * @param  string $sCurrency
215
     * @param  double $dGrandTotal
216
     * @param  bool $blGetConfigWithoutTotals
217
     * @return string|false
218
     */
219
    public function getMatchingShopId($sRatepayMethodIdentifier, $aShopIds, $sCountryCode, $sCurrency, $dGrandTotal, $blGetConfigWithoutTotals = false)
220
    {
221
        $oSelect = $this->getConnection()->select()
222
            ->from($this->getMainTable(), ['shop_id'])
223
            ->where("shop_id IN ('".implode("','", $aShopIds)."')")
224
            ->where("country_code_billing = :countryCode")
225
            ->where("currency = :currency")
226
            ->order('shop_id ASC')
227
            ->limit(1);
228
229
        $aParams = [
230
            'countryCode' => $sCountryCode,
231
            'currency' => $sCurrency,
232
        ];
233
234
        // $blGetConfigWithoutTotals = true mode is used to get a configuration without the totals being concidered.
235
        // This is needed in checkout when basket total is a little unter the min_limit but with shipping costs it's over the limit
236
        // CheckoutConfig javascript array is generated before shipping costs are added, but available payment methods are determined after shipping costs were added
237
        if ($blGetConfigWithoutTotals === false) {
238
            $oSelect = $oSelect->where("tx_limit_".$sRatepayMethodIdentifier."_min <= :grandTotal")
239
                ->where("tx_limit_".$sRatepayMethodIdentifier."_max >= :grandTotal");
240
            $aParams['grandTotal'] = $dGrandTotal;
241
        }
242
243
        $sShopId = $this->getConnection()->fetchOne($oSelect, $aParams);
244
        if (empty($sShopId)) {
245
            return false;
246
        }
247
        return $sShopId;
248
    }
249
}
250