Skip to content

Magento2 how to get payment method using jquery or knockout

Magento 2 how to get payment method using jquery or knockout when customer click on payment method,need to get selected payment method code.

requirejs-config.js

var config = {
    map: {
        '*': {
            checkoutjs: 'Magento_Checkout/js/myfile'
        }
    }

};

myfile.js

define(
    [
        'jquery',
        'ko',
        'Magento_Checkout/js/model/quote',
        'jquery/jquery-storageapi',
    ],
    function($, ko ,quote) {
        'use strict';

        return function (paymentMethod) {
            //Our custom code should be here.
            console.log("paymentMethod.method",paymentMethod.method);
            if(paymentMethod.method == 'paymentfee')
            {
                alert('Payment Fee');
                quote.paymentMethod(paymentMethod);

            } else {
                quote.paymentMethod(paymentMethod);
            }
                $(document).on('change', 'input[name="payment[method]"]', function() {
                    var selectedPaymentMethod = checkoutData.getSelectedPaymentMethod();
                    console.log("selectedPaymentMethod",selectedPaymentMethod);
                    //if(!your_condition)
                    //{
                        //$('button.checkout').attr('disabled', 'disabled');
                        $("button.checkout").css("display", "block");
                    //}
                });

        }
    }
);

I am trying the above code but it is not working.
Need to get the current payment method code when clicking on the radio button.
OR
Is any other way to get current payment method code when click on radio button ?