Magento – Magento 2: How to refresh payment method on some condition

ajaxmagento2payment-methods

Like I have applied a checkbox on payment page for using it as credit score inspite of money. There is problem when my credit is 100 % used i.e. by using credit score grand total becomes 0. In that case Zero checkout payment should come but its not coming and when i reload the page that zero checkout payment method comes is there any way to refresh payment method list on my checkbox click. You can check in below image what i am talking about.

enter image description here

Best Answer

you can use some code like this:

define([
    'uiComponent',
    'Magento_Checkout/js/model/quote',
    'ko',
    'jquery',
    'Magento_Checkout/js/action/get-totals',
    'Magento_Checkout/js/model/full-screen-loader',
     'Magento_Checkout/js/action/get-payment-information'
], function (Component,quote,ko , jQuery, getTotalsAction, fullScreenLoader,getPaymentInformationAction) {
    'use strict';

    // some code

     return Component.extend({
        //some code
        reloadPayment: function() {
            var self = this;

            fullScreenLoader.startLoader();
            jQuery.ajax({
                    url: URL,
                    type: "POST",
                    data: {
                        checked: self.creditValue(),
                        quote_id: quoteId
                     },
                    success: function(response) {
                        if (response) {
                            var deferred = jQuery.Deferred();
                            getTotalsAction([], deferred);
                            // var deferred = $.Deferred();
                             fullScreenLoader.stopLoader();
                            getPaymentInformationAction(deferred);
                            jQuery.when(deferred).done(function () {
                                isApplied(false);
                                totals.isLoading(false);
                            });
                            messageContainer.addSuccessMessage({
                                'message': message
                            });
                           totals.isLoading(true);


                        }
                    }
            });
           return true;
        } 

    });
});

in this code, I'm reloading the payment method when ajax request is succeeded.

Main part of the code for reloading payment info:

getPaymentInformationAction(deferred);
jQuery.when(deferred).done(function () {
    isApplied(false);
    totals.isLoading(false);
});
Related Topic