Completed
Push — master ( a489c5...b511b4 )
by Florian
06:23
created

shipping-mixin.js ➔ ... ➔ mixin.payoneUpdateField   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
nc 4
nop 3
dl 0
loc 14
rs 9.2
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 - 2018 Payone GmbH
21
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
22
 * @link      http://www.payone.de
23
 */
24
/*jshint browser:true jquery:true*/
25
/*global alert*/
26
define([
27
    'jquery',
28
    'Payone_Core/js/action/addresscheck',
29
    'Payone_Core/js/action/edit-address',
30
    'Magento_Checkout/js/model/quote',
31
    'Magento_Checkout/js/action/select-shipping-address',
32
    'Magento_Checkout/js/checkout-data'
33
], function ($, addresscheck, editAddress, quote, selectShippingAddressAction, checkoutData) {
34
    'use strict';
35
36
    var mixin = {
37
        payoneCheckAddress: function () {
38
            if (window.checkoutConfig.payment.payone.addresscheckEnabled && window.checkoutConfig.payment.payone.addresscheckShippingEnabled) {
39
                return true;
40
            }
41
            return false;
42
        },
43
        saveNewAddress: function () {
44
            if (!this.payoneCheckAddress()) {
45
                return this._super();
46
            }
47
48
            if (!this.source.get('payone_address_checked')) {
49
                addresscheck(this.source.get('shippingAddress'), false, this, 'saveNewAddress');
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...
50
            } else {
51
                this.source.set('payone_address_checked', false);
52
                return this._super();
53
            }
54
        },
55
        payoneReplaceInSelectedShippingAddress: function (sReplace, sReplaceWith) {
56
            var oElem = $('.shipping-address-item.selected-item');
57
            if (oElem.length > 0) {
58
                oElem.html(oElem.html().replace(sReplace, sReplaceWith));
59
            }
60
        },
61
        payoneUpdateField: function(oSourceAddress, oResponseAddress, sField) {
62
            if (sField === "street") {
63
                if (oSourceAddress[sField][0] !== oResponseAddress[sField][0]) {
64
                    this.payoneReplaceInSelectedShippingAddress(oSourceAddress[sField][0], oResponseAddress[sField][0]);
65
                    oSourceAddress[sField][0] = oResponseAddress[sField][0];
66
                }
67
            } else {
68
                if (oSourceAddress[sField] !== oResponseAddress[sField]) {
69
                    this.payoneReplaceInSelectedShippingAddress(oSourceAddress[sField], oResponseAddress[sField]);
70
                    oSourceAddress[sField] = oResponseAddress[sField];
71
                }
72
            }
73
            return oSourceAddress;
74
        },
75
        payoneUpdateAddress: function (addressData) {
76
            if (this.isFormInline) {
77
                this.payoneUpdateAddressSource(addressData);
78
            } else {
79
                this.payoneUpdateAddressRegistered(addressData);
80
            }
81
        },
82
        payoneUpdateAddressSource: function (addressData) {
83
            this.source.set('shippingAddress.postcode', addressData.postcode);
84
            this.source.set('shippingAddress.firstname', addressData.firstname);
85
            this.source.set('shippingAddress.lastname', addressData.lastname);
86
            this.source.set('shippingAddress.street.0', addressData.street[0]);
87
            this.source.set('shippingAddress.city', addressData.city);
88
            this.source.set('shippingAddress.country_id', addressData.country_id);
89
        },
90
        payoneUpdateAddressRegistered: function (addressData) {
91
            var newShippingAddress = quote.shippingAddress();
92
            var aUpdateFields = ["postcode", "firstname", "lastname", "street", "city"];
93
            for (var i = 0; i < aUpdateFields.length; i++) {
94
                newShippingAddress = this.payoneUpdateField(newShippingAddress, addressData, aUpdateFields[i]);
95
            }
96
97
            this.payoneUpdateAddressSource(addressData);
98
99
            editAddress(addressData);
100
101
            selectShippingAddressAction(newShippingAddress);
102
            checkoutData.setSelectedShippingAddress(newShippingAddress.getKey());
103
        },
104
        payoneContinue: function (sType) {
105
            if (sType == 'saveNewAddress') {
106
                this.source.set('payone_address_checked', true);
107
                this.saveNewAddress();
108
            } else if (sType == 'setShippingInformation') {
109
                this.source.set('payone_guest_address_checked', true);
110
                this.setShippingInformation();
111
            }
112
        },
113
        setShippingInformation: function () {
114
            if (!this.payoneCheckAddress()) {
115
                return this._super();
116
            }
117
118
            if (!this.source.get('payone_guest_address_checked')) {
119
                if (this.validateShippingInformation()) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.validateShippingInformation() 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...
120
                    if (!this.isFormInline) {
121
                        addresscheck(quote.shippingAddress(), false, this, 'setShippingInformation');
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...
122
                    } else {
123
                        addresscheck(this.source.get('shippingAddress'), false, this, 'setShippingInformation');
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...
124
                    }
125
                }
126
            } else {
127
                this.source.set('payone_guest_address_checked', false);
128
                return this._super();
129
            }
130
        }
131
    }
132
133
    return function (shipping) {
134
        return shipping.extend(mixin);
135
    };
136
});
137