Passed
Push — master ( 23c688...69f989 )
by Florian
12:07 queued 10s
created

Component.extend.getPlaceOrderDeferredObject   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
1
/**
2
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
3
 * it under the terms of the GNU Lesser General Public License as published by
4
 * the Free Software Foundation, either version 3 of the License, or
5
 * (at your option) any later version.
6
 *
7
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public License
13
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
14
 *
15
 * PHP version 5
16
 *
17
 * @category  Payone
18
 * @package   Payone_Magento2_Plugin
19
 * @author    FATCHIP GmbH <[email protected]>
20
 * @copyright 2003 - 2016 Payone GmbH
21
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
22
 * @link      http://www.payone.de
23
 */
24
define(
25
    [
26
        'Magento_Checkout/js/view/payment/default',
27
        'jquery',
28
        'Magento_Checkout/js/model/payment/additional-validators',
29
        'Magento_Checkout/js/action/set-payment-information',
30
        'mage/url',
31
        'mage/translate',
32
        'Magento_Checkout/js/checkout-data',
33
        'Magento_Checkout/js/action/select-payment-method',
34
        'Magento_Checkout/js/action/place-order'
35
    ],
36
    function (Component, $, additionalValidators, setPaymentInformationAction, url, $t, checkoutData, selectPaymentMethodAction, placeOrderAction) {
37
        'use strict';
38
        return Component.extend({
39
            redirectToPayoneController: function(sUrl) {
40
                window.location.replace(url.build(sUrl));
41
            },
42
43
            handleRedirectAction: function(sUrl) {
44
                var self = this;
45
46
                // update payment method information if additional data was changed
47
                this.selectPaymentMethod();
48
                this.isPlaceOrderActionAllowed(false);
49
50
                this.getPlaceOrderDeferredObject()
51
                .fail(
52
                    function () {
53
                        self.isPlaceOrderActionAllowed(true);
54
                    }
55
                ).done(
56
                    function () {
57
                        self.afterPlaceOrder();
58
                        self.redirectToPayoneController(sUrl);
59
                    }
60
                );
61
            },
62
63
            getPlaceOrderDeferredObject: function () {
64
                if (window.checkoutConfig.payment.payone.orderDeferredExists === true) {
65
                    return this._super();
66
                }
67
                // fallback for pre 2.1.0 Magentos
68
                return $.when(
69
                    placeOrderAction(this.getData(), this.redirectAfterPlaceOrder, this.messageContainer)
70
                );
71
            },
72
73
            handleSetPaymentInformation: function(sUrl) {
74
                var self = this;
75
76
                // update payment method information if additional data was changed
77
                this.selectPaymentMethod();
78
                this.isPlaceOrderActionAllowed(false);
79
80
                $.when(
81
                    setPaymentInformationAction(this.messageContainer, self.getData())
82
                ).fail(
83
                    function () {
84
                        self.isPlaceOrderActionAllowed(true);
85
                    }
86
                ).done(
87
                    function () {
88
                        self.redirectToPayoneController(sUrl);
89
                    }
90
                );
91
            },
92
93
            continueToPayone: function () {
94
                if (this.validate() && additionalValidators.validate()) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.validate() && addit...alValidators.validate() 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...
95
                    this.handleRedirectAction('payone/onepage/redirect/');
96
                    return false;
97
                }
98
            },
99
            
100
            handleCreditcardPayment: function () {
101
                var firstValidation = additionalValidators.validate();
102
                if (!(firstValidation)) {
103
                    return false;
104
                }
105
106
                if (this.validate() && firstValidation) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.validate() && firstValidation 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...
107
                    if (document.getElementById(this.getCode() + '_pseudocardpan').value != '') {
108
                        this.handleRedirectAction('payone/onepage/redirect/');
109
                        return false;
110
                    } else {
111
                        this.handleCreditcardCheck();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
112
                    }
113
                }
114
            },
115
            
116
            handleDebitPayment: function () {
117
                if (this.validate() && additionalValidators.validate()) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.validate() && addit...alValidators.validate() 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...
118
                    if (window.checkoutConfig.payment.payone.validateBankCode == true && window.checkoutConfig.payment.payone.bankCodeValidatedAndValid == false) {
119
                        this.handleBankaccountCheck();
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
120
                    } else {
121
                        this.handleSetPaymentInformation('payone/onepage/debit/');
122
                        return false;
123
                    }
124
                }
125
            },
126
127
            isBirthdayValid: function (iYear, iMonth, iDay) {
128
                if (!$.isNumeric(iYear) || !$.isNumeric(iMonth) || !$.isNumeric(iDay)) {
129
                    return false;
130
                }
131
132
                var sBirthDate = iYear + "-" + iMonth + "-" + iDay;
133
                var oBirthDate = new Date(sBirthDate);
134
                var oMinDate = new Date(new Date().setYear(new Date().getFullYear() - 18));
135
                if (oBirthDate > oMinDate) {
136
                    return false;
137
                }
138
139
                return true;
140
            },
141
            initialize: function () {
142
                this._super();
143
                if(this.getCode() === window.checkoutConfig.payment.payone.canceledPaymentMethod) {
144
                    selectPaymentMethodAction({method: this.getCode()});
145
                    checkoutData.setSelectedPaymentMethod(this.item.method);
146
                    if (window.checkoutConfig.payment.payone.isError === true) {
147
                        this.messageContainer.addErrorMessage({'message': $t('There has been an error processing your payment')});
148
                    } else {
149
                        this.messageContainer.addSuccessMessage({'message': $t('Payment has been canceled.')});
150
                    }
151
                }
152
                return this;
153
            }
154
        });
155
    }
156
);
157