Passed
Pull Request — master (#74)
by oleksandr
05:10
created

assets/Yves/js/modules/payment-method.js   A

Complexity

Total Complexity 12
Complexity/F 1.5

Size

Lines of Code 83
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 50
mnd 4
bc 4
fnc 8
dl 0
loc 83
bpm 0.5
cpm 1.5
noi 2
c 0
b 0
f 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A payment-method.js ➔ check 0 16 2
A payment-method.js ➔ init 0 28 4
A payment-method.js ➔ checkCallback 0 6 2
A payment-method.js ➔ initHostedIframe 0 22 4
1
/**
2
 * MIT License
3
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
4
 */
5
6
'use strict';
7
8
var $ = require('jquery');
9
10
function init(config) {
11
    var $form = $(config.formSelector);
12
13
    initHostedIframe(config);
14
15
    var $bankAccountModeBban = $form.find(config.bankAccountModeBbanInput);
16
    $bankAccountModeBban.change(function() {
17
        $form.find(config.bankAccountInput).prop('disabled', false);
18
        $form.find(config.bankCodeInput).prop('disabled', false);
19
        $form.find(config.ibanInput).prop('disabled', true);
20
        $form.find(config.bicInput).prop('disabled', true);
21
    });
22
23
    var $bankAccountModeIbanBic = $form.find(config.bankAccountModeIbanBicInput);
24
    $bankAccountModeIbanBic.change(function() {
25
        $form.find(config.bankAccountInput).prop('disabled', true);
26
        $form.find(config.bankCodeInput).prop('disabled', true);
27
        $form.find(config.ibanInput).prop('disabled', false);
28
        $form.find(config.bicInput).prop('disabled', false);
29
    });
30
31
    if ($bankAccountModeBban.is(':checked')) {
32
        $bankAccountModeBban.change();
33
    } else {
34
        $bankAccountModeIbanBic.prop('checked', true);
35
        $bankAccountModeIbanBic.change();
36
    }
37
}
38
39
function initHostedIframe(config) {
40
    document.paymentform = document.getElementById(config.formId);
41
    var $form = $(config.formSelector);
42
    var clientApiConfig = JSON.parse($form.find(config.clientApiConfigInput).val());
43
    var language = $form.find(config.languageInput).val().substr(0, 2);
44
    config.hostedIframeConfig.language = Payone.ClientApi.Language[language];
0 ignored issues
show
Bug introduced by
The variable Payone seems to be never declared. If this is a global, consider adding a /** global: Payone */ 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...
45
46
    var iframes = new Payone.ClientApi.HostedIFrames(config.hostedIframeConfig, clientApiConfig);
47
48
    document.getElementById('cardtype').onchange = function () {
49
        iframes.setCardType(this.value);              // on change: set new type of credit card to process
50
    };
51
52
    document.getElementById('cardtype').onchange();
53
54
    $form.find('[type="submit"]').click(function() {
55
        if ($(config.currentPaymentMethodSelector).val() === 'payoneCreditCard') {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if $(config.currentPaymentM... === "payoneCreditCard" is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
56
            check(iframes, config);
57
            return false;
58
        }
59
    });
60
}
61
62
function check(iframes, config) {
63
    //Fix to make payone remote script work correctly. It tries to remove some node but can't find it.
64
    document.getElementsByTagName("body")[0].appendChild(document.createElement("p"));
65
    window.PayoneGlobals.options.payoneScript = document.getElementsByTagName("body")[0].lastChild;
66
67
    // Payone script works with such global array member.
68
    // Since our function checkCallback is inside the module, it is absent in "window".
69
    // Set it explicitly.
70
    window['checkCallback'] = checkCallback;
71
72
    if (iframes.isComplete() && $(config.cardholderInput).val()) {
73
        iframes.creditCardCheck('checkCallback');
74
    } else {
75
        $(config.errorDivSelector).text(config.hostedIframeConfig.language.transactionRejected);
76
    }
77
}
78
79
function checkCallback(response) {
80
    if (response.status === "VALID") {
81
        document.getElementById("pseudocardpan").value = response.pseudocardpan;
82
        document.paymentform.submit();
83
    }
84
}
85
86
module.exports = {
87
    init: init
88
};