Completed
Push — master ( 341936...178071 )
by Florian
03:31
created

Component.extend.validate   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 6.7272
cc 7
nc 6
nop 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 - 2017 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
        'Payone_Core/js/view/payment/method-renderer/base',
27
        'jquery',
28
        'mage/translate',
29
        'Magento_Checkout/js/model/quote'
30
    ],
31
    function (Component, $, $t, quote) {
32
        'use strict';
33
        return Component.extend({
34
            defaults: {
35
                template: 'Payone_Core/payment/payolution_debit',
36
                birthday: '',
37
                birthmonth: '',
38
                birthyear: '',
39
                iban: '',
40
                bic: '',
41
                tradeRegistryNumber: '',
42
                agreement: false,
43
                mandate: false
44
            },
45
            initObservable: function () {
46
                this._super()
47
                    .observe([
48
                        'birthday',
49
                        'birthmonth',
50
                        'birthyear',
51
                        'iban',
52
                        'bic',
53
                        'tradeRegistryNumber',
54
                        'agreement',
55
                        'mandate'
56
                    ]);
57
                return this;
58
            },
59
            getData: function () {
60
                document.getElementById(this.getCode() + '_iban').value = this.getCleanedNumber(this.iban());
61
                document.getElementById(this.getCode() + '_bic').value = this.getCleanedNumber(this.bic());
62
63
                var parentReturn = this._super();
64
                if (parentReturn.additional_data === null) {
65
                    parentReturn.additional_data = {};
66
                }
67
                if (this.requestBirthday()) {
68
                    parentReturn.additional_data.dateofbirth = this.birthyear() + this.birthmonth() + this.birthday();
69
                }
70
                if (this.isB2bMode()) {
71
                    parentReturn.additional_data.trade_registry_number = this.tradeRegistryNumber();
72
                    parentReturn.additional_data.b2bmode = true;
73
                }
74
                parentReturn.additional_data.iban = this.getCleanedNumber(this.iban());
75
                parentReturn.additional_data.bic = this.getCleanedNumber(this.bic());
76
                return parentReturn;
77
            },
78
            getCleanedNumber: function (sDirtyNumber) {
79
                var sCleanedNumber = '';
80
                var sTmpChar;
81
                for (var i = 0; i < sDirtyNumber.length; i++) {
82
                    sTmpChar = sDirtyNumber.charAt(i);
83
                    if (sTmpChar != ' ' && (!isNaN(sTmpChar) || /^[A-Za-z]/.test(sTmpChar))) {
84
                        if (/^[a-z]/.test(sTmpChar)) {
85
                            sTmpChar = sTmpChar.toUpperCase();
86
                        }
87
                        sCleanedNumber = sCleanedNumber + sTmpChar;
88
                    }
89
                }
90
                return sCleanedNumber;
91
            },
92
            /** Returns payment method instructions */
93
            getInstructions: function () {
94
                return window.checkoutConfig.payment.instructions[this.item.method];
95
            },
96
            displayPayolutionOverlay: function () {
97
                $('#' + this.getCode() + '_overlay').show();
98
            },
99
            removePayolutionOverlay: function () {
100
                $('#' + this.getCode() + '_overlay').hide();
101
            },
102
            getPrivacyDeclaration: function () {
103
                return window.checkoutConfig.payment.payone.payolution.privacyDeclaration.invoice;
104
            },
105
            isB2bMode: function () {
106
                if (window.checkoutConfig.payment.payone.payolution.b2bMode.invoice == true &&
107
                    quote.billingAddress() != null &&
108
                    typeof quote.billingAddress().company != 'undefined' &&
109
                    quote.billingAddress().company != ''
110
                ) {
111
                    return true;
112
                }
113
                return false;
114
            },
115
            requestBirthday: function () {
116
                return !this.isB2bMode();
117
            },
118
            validate: function () {
119
                if (this.agreement() == false) {
120
                    this.messageContainer.addErrorMessage({'message': $t('Please confirm the transmission of the necessary data to payolution!')});
121
                    return false;
122
                }
123
                if (this.mandate() == false) {
124
                    this.messageContainer.addErrorMessage({'message': $t('Please confirm the SEPA mandate!')});
125
                    return false;
126
                }
127
                if (this.requestBirthday() == true && !this.isBirthdayValid(this.birthyear(), this.birthmonth(), this.birthday())) {
128
                    this.messageContainer.addErrorMessage({'message': $t('You have to be at least 18 years old to use this payment type!')});
129
                    return false;
130
                }
131
                if (this.iban() == '') {
132
                    this.messageContainer.addErrorMessage({'message': $t('Please enter a valid IBAN.')});
133
                    return false;
134
                }
135
                if (this.bic() == '') {
136
                    this.messageContainer.addErrorMessage({'message': $t('Please enter a valid BIC.')});
137
                    return false;
138
                }
139
                return true;
140
            }
141
        });
142
    }
143
);
144