代码之家  ›  专栏  ›  技术社区  ›  shahnawaz raza

加载Stripe元素支付表单时检测?

  •  0
  • shahnawaz raza  · 技术社区  · 2 年前

    我使用Stripe元素加载支付表单,但有时需要5-10秒才能完全加载表单。

    有没有办法检测表单何时完全加载完成,这样我就可以在加载之前显示加载动画/图像?

    console.log(“加载的API脚本”); this.stripe=条纹(‘…………’);

      const create_customer_response = JSON.parse(localStorage.getItem("create_customer_response"));
      const client_secret = create_customer_response["intent"]["client_secret"]
      const options = {
        clientSecret: client_secret,
      };
    
      elements = this.stripe.elements(options);
    
      const payment = elements.create('payment');
      const paymentElement = document.getElementById('payment-element');
    
      if (paymentElement) {
        payment.mount(paymentElement);
        
        this.isPaymentElementMounted = true; // Set the flag to true when the payment element is mounted
        console.log('Payment element mounted successfully.');
        console.log("this.stripe ", this.stripe);
        console.log(elements);
        this.spinner.hide();
      } else {
        console.error('#payment-element not found in the DOM. Make sure your template is correct.');
      }
    }
    scriptElementStripe.onerror = () => {
      // console.log('Could not load the Google API Script!');
    }
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   shahnawaz raza    2 年前

    这是有效的解决方案。:)

    this.stripe=条纹('..');

      const create_customer_response = JSON.parse(localStorage.getItem("create_customer_response"));
      const client_secret = create_customer_response["intent"]["client_secret"]
      const options = {
        clientSecret: client_secret,
      };
    
      elements = this.stripe.elements(options);
    
      const payment = elements.create('payment');
      const paymentElement = document.getElementById('payment-element');
    
      if (paymentElement) {
        payment.mount(paymentElement);
    
        // Listen for the 'ready' event
        payment.on('ready', () => {
          this.isPaymentElementMounted = true; // Set the flag to true when the payment element is ready
          console.log('Payment element is ready.');
          console.log("this.stripe ", this.stripe);
          console.log(elements);
          this.spinner.hide();
        });
    
    
      } else {
        console.error('#payment-element not found in the DOM. Make sure your template is correct.');
      }
    }
    scriptElementStripe.onerror = () => {
      // console.log('Could not load the Google API Script!');
    }