WooCommerce 2 step form with product add to cart and checkout without refresh

I am building a button that, when clicked, opens a pop-up with a 2-step form. In the first step, users fill in their details. After clicking “Next,” the user is registered and logged in (without refreshing the page). Then, we add a product to the cart and attempt to fetch the checkout without refreshing the page.

However, I am encountering an issue. When I try to fill in the payment details, I always get the error: “We were unable to process your order, please try again.”

When I refresh the page and try again, everything works fine.

It seems something is missing, but I don’t know what. Here is part of my code:

html code in wordpress

<div id="signup_checkout" class="mylightbox">
    <div class="mylightbox-content">
        <span class="myclose-button">×</span>
        <div id="signup">
            [gravityform id="1" title="false" description="false" ajax="true"]
        </div>
        <div id="checkout">
           
</div> </div> </div>

all this codes are in function.php
click on button open popup and add to cart

(".show_register").click(function() {
                var product = $(this).attr("rel");
                addToCart(product);

                document.getElementById("signup_checkout").style.display = "block";

                if (!document.body.classList.contains("logged-in")) {
                    // If the user is logged in, open lightbox1
                    document.getElementById("signup").style.display = "block";
                    document.getElementById("checkout").style.display = "none";
                } else {
                    // If the user is not logged in, open lightbox2
                    document.getElementById("checkout").style.display = "block";
                    document.getElementById("signup").style.display = "none";
                }
            });

add to cart section here i am also loading checkout js files and try to change the checkout:

function addToCart(product) {
                // Send an AJAX request to add the product to the cart
                jQuery('#checkout .woocommerce').addClass('woocommerce-checkout');

                $.ajax({
                    type: "POST",
                    url: "example.com/home/?add-to-cart=" + product,
                    data: {
                        action: "woocommerce_add_to_cart",
                        product_id: product,
                    },
                    success: function(response) {
                        // If the product is added successfully, fetch the checkout form
                        console.log("Product added to cart:", response);

                        function loadScript(id, src, attributes = {}) {
                            if (!document.getElementById(id)) {
                                var script = document.createElement('script');
                                script.id = id;
                                script.src = src;
                                script.type = 'text/javascript';
                                for (var key in attributes) {
                                    script.setAttribute(key, attributes[key]);
                                }
                                document.head.appendChild(script);
                            }
                        }

                        // Load WooCommerce checkout script
                        loadScript('wc-checkout-js', 'example.com/wp-content/plugins/woocommerce/assets/js/frontend/checkout.min.js?ver=8.7.0', { 'data-wp-strategy': 'defer' });

                        // Load WooCommerce Payments checkout script
                        loadScript('wc-payments-checkout-js', 'example.com/wp-content/plugins/woocommerce-payments/dist/checkout.js?ver=7.4.0', { 'data-wp-strategy': 'defer' });
      

                        setTimeout(function() {
                            // Fetch the checkout form after the product is added to the cart
                            var newForm = $(response).find('.woocommerce-checkout');
                            $('#checkout .woocommerce-checkout').replaceWith(newForm);

                            // Reinitialize WooCommerce checkout functions
                            jQuery(document.body).trigger('init_checkout');
                            jQuery(document.body).trigger('update_checkout');
                            jQuery(document.body).trigger('updated_checkout');
                        }, 2000);
                        

                    },
                    error: function(xhr, status, error) {
                        console.error("Error adding product to cart:", error);
                    }
                });
            }

when i add my code i can the payment like this
enter image description here

the only different when i am doing refresh is that i cant see the use a new payment method checkbox. i will just meantion again that with refresh the page everything working, and i dont want refresh the page.

Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật