0

I am stuck. Can someone please correct my code?

define([
'underscore',
'Magento_Checkout/js/model/payment/method-list',
'Magento_Checkout/js/action/select-payment-method',
'mage/utils/wrapper',
'Magento_Checkout/js/model/quote',
], function (_, methodList, selectPaymentMethod, wrapper, quote) {
'use strict';

var isCashOnDeliveryMethod = function (paymentMethod) {
    return paymentMethod.method === 'cashondelivery';
}
return function (paymentService) {




    var setPaymentMethods = paymentService.setPaymentMethods;
    setPaymentMethods = wrapper.wrap(setPaymentMethods, function(original,methods){
        console.log('before');
        var result = original();
        var state = quote.shippingAddress().regionCode;
        console.log(state);
        if(state != "AZ"){
            var cod = _.find(methods, isCashOnDeliveryMethod);
            methods = _.without(methods, cod);
            console.log(methods)
            console.log(methodList(methods));

        }
        selectPaymentMethod(methods);
        console.log('after');
        return setPaymentMethods.apply(this, arguments);
        // return setPaymentMethods;
        // return result;
    });

    paymentService.setPaymentMethods = setPaymentMethods;

    var getAvailablePaymentMethods = paymentService.getAvailablePaymentMethods;
    getAvailablePaymentMethods = wrapper.wrap(getAvailablePaymentMethods, function(original,allMethods){
        // var state = quote.shippingAddress().regionCode;
        // console.log(state)
        console.log('before avail');
        // console.log(quote.shippingAddress().regionCode);
        var result = original();
        console.log('after avail');
        console.log(allMethods);
        // return result;
        return getAvailablePaymentMethods.apply(this, arguments)
    });

    paymentService.getAvailablePaymentMethods = getAvailablePaymentMethods;
    return paymentService;
};


});

1 Answer 1

1
define([
    'underscore',
    'Magento_Checkout/js/action/select-payment-method',
    'mage/utils/wrapper',
    'Magento_Checkout/js/model/quote'
], function (_, selectPaymentMethod, wrapper, quote) {
    'use strict';

    return function (paymentService) {
        let cashOnDeliveryCode = 'cashondelivery',
            isCashOnDeliveryMethod = function (paymentMethod) {
                return paymentMethod.method === cashOnDeliveryCode;
            },
            isCashOnDeliveryRegion = function () {
                return quote.shippingAddress() && quote.shippingAddress().regionCode === 'CA';
            };

        paymentService.getAvailablePaymentMethods = wrapper.wrap(paymentService.getAvailablePaymentMethods, function (_super) {
            let methods = _super();
            if (!isCashOnDeliveryRegion()) {
                return _.reject(methods, isCashOnDeliveryMethod);
            }

            return methods;
        });

        paymentService.setPaymentMethods = wrapper.wrap(paymentService.setPaymentMethods, function (_super, methods) {
            if (isCashOnDeliveryRegion()) {
                // select method by default
                if (quote.paymentMethod()) {
                    quote.paymentMethod().method = cashOnDeliveryCode;
                }
                let cashOnDeliveryMethod = _.find(methods, isCashOnDeliveryMethod);
                selectPaymentMethod(cashOnDeliveryMethod);
            } else {
                methods = _.reject(methods, isCashOnDeliveryMethod);
            }

            return _super(methods);
        });

        return paymentService;
    }
});
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.