Completed
Push — master ( a010d0...acbc61 )
by Florian
04:56
created

ratepay.js ➔ ratepayRateCalculatorAction   C

Complexity

Conditions 8
Paths 28

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 6.2894
c 0
b 0
f 0
cc 8
nc 28
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 *
3
 * NOTICE OF LICENSE
4
 *
5
 * This source file is subject to the GNU General Public License (GPL 3)
6
 * that is bundled with this package in the file LICENSE.txt
7
 *
8
 * DISCLAIMER
9
 *
10
 * Do not edit or add to this file if you wish to upgrade Payone_Core to newer
11
 * versions in the future. If you wish to customize Payone_Core for your
12
 * needs please refer to http://www.payone.de for more information.
13
 *
14
 * @category        Payone
15
 * @package         js
16
 * @subpackage      payone
17
 * @copyright       Copyright (c) 2016 <[email protected]> - www.fatchip.com
18
 * @author          Robert Müller <[email protected]>
19
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
20
 * @link            http://www.fatchip.com
21
 *
22
 *
23
 * @category        Payone
24
 * @package         js
25
 * @subpackage      payone
26
 * @copyright       Copyright (c) 2016 <[email protected]> - www.e3n.de
27
 * @author          Tim Rein <[email protected]>
28
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
29
 * @link            http://www.e3n.de
30
 */
31
32
/**
33
 *
34
 * @param mode
35
 * @param paymentMethod
36
 * @param url
37
 */
38
function switchRateOrRuntime(mode, paymentMethod, url)
0 ignored issues
show
Unused Code introduced by
The parameter url is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
39
{
40
    if (mode == 'rate') {
41
        document.getElementById(paymentMethod + '_SwitchToTerm').className = 'ratepay-Active';
42
        document.getElementById(paymentMethod + '_SwitchToRuntime').className = '';
43
        document.getElementById(paymentMethod + '_ContentTerm').style.display = 'block';
44
        document.getElementById(paymentMethod + '_ContentRuntime').style.display = 'none';
45
    } else if (mode == 'runtime') {
46
        document.getElementById(paymentMethod + '_SwitchToRuntime').className = 'ratepay-Active';
47
        document.getElementById(paymentMethod + '_SwitchToTerm').className = '';
48
        document.getElementById(paymentMethod + '_ContentRuntime').style.display = 'block';
49
        document.getElementById(paymentMethod + '_ContentTerm').style.display = 'none';
50
    }
51
}
52
53
/**
54
 *
55
 * @param mode
56
 * @param paymentMethod
57
 * @param url
58
 */
59
function ratepayRateCalculatorAction(mode, paymentMethod, url)
60
{
61
    var calcValue,
62
        calcMethod,
63
        notification,
64
        html,
65
        ratePayshopId,
66
        amount,
67
        ratePayCurrency,
68
        ajaxLoader = $("ajaxLoaderId"),
69
        cover = $("cover");
70
71
    ajaxLoader.setStyle({
72
        display: 'block'
73
    });
74
    cover.setStyle({
75
        display: 'block'
76
    });
77
78
79
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
80
        xmlhttp = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable xmlhttp seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.xmlhttp.
Loading history...
81
    } else {// code for IE6, IE5
82
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
0 ignored issues
show
Bug introduced by
The variable ActiveXObject seems to be never declared. If this is a global, consider adding a /** global: ActiveXObject */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
83
    }
84
    amount = document.getElementById('amount').value;
85
    ratePayshopId = document.getElementById('ratePayShopId').value;
86
    ratePayCurrency = document.getElementById('ratePayCurrency').value;
87
    if (mode == 'rate') {
88
        calcValue = document.getElementById(paymentMethod + '-rate').value;
89
90
91
        calcMethod = 'calculation-by-rate';
92
        if (document.getElementById('debitSelect')) {
93
            dueDate = document.getElementById('debitSelect').value;
0 ignored issues
show
Bug introduced by
The variable dueDate seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.dueDate.
Loading history...
94
        } else {
95
            dueDate= '';
96
        }
97
    } else if (mode == 'runtime') {
98
        calcValue = document.getElementById(paymentMethod + '-runtime').value;
99
        calcMethod = 'calculation-by-time';
100
        notification = (document.getElementById(paymentMethod + '_Notification') == null) ? 0 : 1;
101
        if(document.getElementById('debitSelectRuntime')){
102
            dueDate = document.getElementById('debitSelectRuntime').value;
103
        } else {
104
            dueDate= '';
105
        }
106
    }
107
    xmlhttp.open("POST", url, false);
108
109
    xmlhttp.setRequestHeader("Content-Type",
110
        "application/x-www-form-urlencoded");
111
112
    xmlhttp.send("paymentMethod=" + paymentMethod + "&calcValue=" + calcValue + "&calcMethod=" + calcMethod + "&dueDate=" + dueDate
0 ignored issues
show
Bug introduced by
The variable calcMethod does not seem to be initialized in case mode == "runtime" on line 97 is false. Are you sure this can never be the case?
Loading history...
Bug introduced by
The variable calcValue does not seem to be initialized in case mode == "runtime" on line 97 is false. Are you sure this can never be the case?
Loading history...
Bug introduced by
The variable dueDate does not seem to be initialized in case mode == "runtime" on line 97 is false. Are you sure this can never be the case?
Loading history...
113
                 + "&notification=" + notification
0 ignored issues
show
Bug introduced by
The variable notification seems to not be initialized for all possible execution paths.
Loading history...
114
                 + "&ratePayshopId=" + ratePayshopId + "&ratePayCurrency=" + ratePayCurrency + "&amount=" + amount
115
                );
116
117
    if (xmlhttp.responseText != null) {
118
        html = xmlhttp.responseText;
119
        document.getElementById(paymentMethod + '_ResultContainer').innerHTML = html;
120
        document.getElementById(paymentMethod + '_ResultContainer').style.display = 'block';
121
        document.getElementById(paymentMethod + '_ResultContainer').style.padding = '3px 0 0 0';
122
        document.getElementById(paymentMethod + '_SwitchToTerm').style.display = 'none';
123
124
        ajaxLoader.setStyle({
125
            display: 'none'
126
        });
127
        cover.setStyle({
128
            display: 'none'
129
        });
130
    }
131
132
}
133
/**
134
 * @param element
135
 */
136
function payoneSwitchPayRate(element)
137
{
138
    var ElementValue = element.value;
139
    var ElementValueSplit = ElementValue.split('_');
140
    var typeId = ElementValueSplit[0];
141
    var typeCode = ElementValueSplit[1];
142
    $("payone_ratepay_type").setValue(typeCode);
143
    $("payone_ratepay_config_id").setValue(typeId);
144
145
    if(typeCode === 'RPS'){
146
        $("ratepay-main-cont").setStyle({
147
            display: 'block'
148
        });
149
    } else {
150
        $("ratepay-main-cont").setStyle({
151
            display: 'none'
152
        });
153
    }
154
}
155
156
/**
157
 *
158
 * @param payment_code
159
 */
160
function payoneRatepayCustomerDobInput(payment_code)
161
{
162
    var daySelect = $(payment_code + '_additional_fields_customer_dob_day');
163
    var monthSelect = $(payment_code + '_additional_fields_customer_dob_month');
164
    var yearSelect = $(payment_code + '_additional_fields_customer_dob_year');
165
    var hiddenDobFull = $(payment_code + '_additional_fields_customer_dob_full');
166
167
    if (daySelect == undefined || monthSelect == undefined || yearSelect == undefined
168
        || hiddenDobFull == undefined)  {
169
        return;
170
    }
171
172
    hiddenDobFull.value = yearSelect.value + "-" + monthSelect.value + "-" + daySelect.value;
173
}
174
175
var Translator = new Translate([]);
0 ignored issues
show
Bug introduced by
The variable Translate seems to be never declared. If this is a global, consider adding a /** global: Translate */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
176
177
Validation.add('validate-18-years', Translator.translate('You have to be at least 18 years old to use this payment type!'), function (value) {
0 ignored issues
show
Bug introduced by
The variable Validation seems to be never declared. If this is a global, consider adding a /** global: Validation */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
178
    var oBirthDate = new Date(value);
179
    var oMinDate = new Date(new Date().setYear(new Date().getFullYear() - 18));
180
    if(oBirthDate > oMinDate) {
181
        return false;
182
    }
183
    return true;
184
});