Completed
Push — master ( 15a67d...6eccc2 )
by Florian
14s
created

js/payone/core/masterpass.js   A

Complexity

Total Complexity 28
Complexity/F 1.4

Size

Lines of Code 155
Function Count 20

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 155
rs 10
c 2
b 0
f 0
wmc 28
mnd 2
bc 29
fnc 20
bpm 1.45
cpm 1.4
noi 5

10 Functions

Rating   Name   Duplication   Size   Complexity  
A PayoneCheckout.handlePlaceOrderButton 0 14 3
A window.unlockActivity 0 4 1
B window.placeOrder 0 37 1
A PayoneCheckout.getChooseMethodeUrl 0 3 1
A window.lockActivity 0 4 1
A PayoneCheckout.getPlaceOrderUrl 0 3 1
A PayoneCheckout.reloadReview 0 19 1
A PayoneCheckout.getReloadReviewUrl 0 3 1
A PayoneCheckout.init 0 33 2
A window.chooseMethod 0 25 1
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) 2018 <[email protected]> - www.fatchip.de
18
 * @author          FATCHIP GmbH <[email protected]>
19
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
20
 * @link            http://www.fatchip.de
21
 */
22
23
var PayoneCheckout = {
24
    shippingMethodCode: false,
25
    baseUrl: "",
26
    chooseMethodUrl: "payone_core/mastercardMasterpass/chooseShippingMethod",
27
    placeOrderUrl: "payone_core/mastercardMasterpass/placeOrder",
28
    reloadReviewUrl: "checkout/onepage/review/",
29
    init: function (baseUrl) {
30
31
        this.baseUrl = baseUrl;
32
33
        var button = jQuery('#placeOrder');
34
        button.on('click', function () {
35
            window.placeOrder(PayoneCheckout.getPlaceOrderUrl());
36
        });
37
38
        var availableMethods = jQuery('input[type="radio"][name="shipping_method"]');
39
        if (availableMethods.length > 1) {
40
            availableMethods.on('click', function (event) {
41
                if (event.currentTarget.checked === true) {
42
                    PayoneCheckout.shippingMethodCode = event.currentTarget.value;
43
                    window.chooseMethod(
44
                        PayoneCheckout.getChooseMethodeUrl(),
45
                        PayoneCheckout.shippingMethodCode
46
                    );
47
                }
48
            });
49
        }
50
        else {
51
            var method = availableMethods.get(0).value;
52
            window.chooseMethod(
53
                PayoneCheckout.getChooseMethodeUrl(),
54
                method
55
            );
56
        }
57
58
        this.handlePlaceOrderButton();
59
60
        this.reloadReview();
61
    },
62
    handlePlaceOrderButton: function() {
63
        var availableMethods = jQuery('input[type="radio"][name="shipping_method"]');
64
        var checkedMethod = availableMethods.filter(':checked');
65
        if (checkedMethod.length === 1) {
66
            PayoneCheckout.shippingMethodCode = checkedMethod[0].value;
67
            jQuery('#placeOrder').attr('disabled', false);
68
        } else if (availableMethods.length === 1) {
69
            // In case there's only one method that's not already checked
70
            var singleMethod = availableMethods.filter(':first');
71
            singleMethod.attr('checked', true);
72
            jQuery('#placeOrder').attr('disabled', false);
73
            PayoneCheckout.shippingMethodCode = singleMethod[0].value;
74
        }
75
    },
76
    reloadReview: function () {
77
        jQuery.ajax(
78
            {
79
                url: this.getReloadReviewUrl(),
80
                error: function (result) {
81
                    console.log(result);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
82
                },
83
                success: function (result) {
84
                    var review = jQuery(result).filter('#checkout-review-table-wrapper');
85
                    var container = jQuery('#checkout-review-load');
86
                    container.html(review);
87
                    PayoneCheckout.handlePlaceOrderButton();
88
                },
89
                complete: function(request, status) {
0 ignored issues
show
Unused Code introduced by
The parameter request 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...
Unused Code introduced by
The parameter status 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...
90
                    window.unlockActivity();
91
                }
92
            }
93
        );
94
    },
95
    getChooseMethodeUrl: function () {
96
        return this.baseUrl + this.chooseMethodUrl;
97
    },
98
    getPlaceOrderUrl: function () {
99
        return this.baseUrl + this.placeOrderUrl;
100
    },
101
    getReloadReviewUrl: function () {
102
        return this.baseUrl + this.reloadReviewUrl;
103
    }
104
};
105
106
window.chooseMethod = function(url, methodCode) {
107
    window.lockActivity();
108
    jQuery.ajax(
109
        {
110
            url: url,
111
            data: {
112
                code: methodCode
113
            },
114
            error: function(result) {
0 ignored issues
show
Unused Code introduced by
The parameter result 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...
115
                window.unlockActivity();
116
                alert('An error occurred during the Masterpass Checkout.');
117
            },
118
            success: function (result) {
119
                var response = JSON.parse(result);
120
                if (response.code !== 200) {
121
                    window.unlockActivity();
122
                    alert(response.data.message);
123
                    return;
124
                }
125
126
                PayoneCheckout.reloadReview();
127
            }
128
        }
129
    );
130
};
131
132
window.placeOrder = function (url) {
133
    var agreementCollection = jQuery("[id^=agreement-]");
134
    var agreement = [];
135
    agreementCollection.each(function() {
136
        if (this.checked) {
137
            var index = $(this).attr("name").match(/.*\[(.*)\]/);
138
            if (index.length > 1) {
139
                agreement.push(index[1]);
140
            }
141
        }
142
    });
143
144
    window.lockActivity();
145
    jQuery.ajax(
146
        {
147
            url: url,
148
            data: {
149
                agreement: agreement
150
            },
151
            error: function(result) {
0 ignored issues
show
Unused Code introduced by
The parameter result 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...
152
                window.unlockActivity();
153
                alert('An error occurred during the Masterpass Checkout.');
154
            },
155
            success: function (result) {
156
                window.unlockActivity();
157
158
                var response = JSON.parse(result);
159
                if (response.code !== 200) {
160
                    alert(response.data.message);
161
                    return;
162
                }
163
164
                window.location = response.data.redirectUrl;
165
            }
166
        }
167
    );
168
};
169
170
window.lockActivity = function() {
171
    var fadeOut = jQuery('.fadeOut');
172
    fadeOut.show();
173
};
174
window.unlockActivity = function() {
175
    var fadeOut = jQuery('.fadeOut');
176
    fadeOut.hide();
177
};