Completed
Push — master ( 44dc6b...3ab156 )
by Florian
10s
created

Component.extend.isB2bMode   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.2
cc 4
nc 2
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 - 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
        'mage/translate',
28
        'Magento_Checkout/js/model/quote'
29
    ],
30
    function (Component, $t, quote) {
31
        'use strict';
32
        return Component.extend({
33
            defaults: {
34
                template: 'Payone_Core/payment/safe_invoice',
35
                birthday: '',
36
                birthmonth: '',
37
                birthyear: ''
38
            },
39
            initObservable: function () {
40
                this._super()
41
                    .observe([
42
                        'birthday',
43
                        'birthmonth',
44
                        'birthyear'
45
                    ]);
46
                return this;
47
            },
48
            isB2bMode: function () {
49
                if (quote.billingAddress() !== null &&
50
                    typeof quote.billingAddress().company !== 'undefined' &&
51
                    quote.billingAddress().company !== ''
52
                ) {
53
                    return true;
54
                }
55
                return false;
56
            },
57
            requestBirthday: function () {
58
                if (window.checkoutConfig.payment.payone.customerHasGivenBirthday === false && !this.isB2bMode()) {
59
                    return true;
60
                }
61
                return false;
62
            },
63
            isCustomerTooYoung: function () {
64
                var sBirthDate = this.birthyear() + "-" + this.birthmonth() + "-" + this.birthday();
65
                var oBirthDate = new Date(sBirthDate);
66
                var oMinDate = new Date(new Date().setYear(new Date().getFullYear() - 18));
67
                if(oBirthDate < oMinDate) {
68
                    return false;
69
                }
70
                return true;
71
            },
72
            validate: function () {
73
                if (this.requestBirthday() && this.isCustomerTooYoung()) {
74
                    this.messageContainer.addErrorMessage({'message': $t('You have to be at least 18 years old to use this payment type!')});
75
                    return false;
76
                }
77
                return true;
78
            },
79
            getData: function () {
80
                var parentReturn = this._super();
81
                if (parentReturn.additional_data === null) {
82
                    parentReturn.additional_data = {};
83
                }
84
                parentReturn.additional_data.birthday = this.birthday();
85
                parentReturn.additional_data.birthmonth = this.birthmonth();
86
                parentReturn.additional_data.birthyear = this.birthyear();
87
                return parentReturn;
88
            },
89
            /** Returns payment method instructions */
90
            getInstructions: function () {
91
                return window.checkoutConfig.payment.instructions[this.item.method];
92
            }
93
        });
94
    }
95
);
96