【问题标题】:prevent stripe from checkout if other inputs are empty如果其他输入为空,则防止条带结帐
【发布时间】:2020-01-04 07:16:45
【问题描述】:

所以我用条纹做简单的“预订”功能,我遇到了以下问题:这是我的表单代码

<form id="formid" action="/checkout" method="POST">
    <input type="text" name="kurtuma" id="zaza">
    <script 
    src="//checkout.stripe.com/v2/checkout.js",
    class="stripe-button",
    data-key="{{{keyPublishable}}}",
    data-locale="auto",
    data-description="Sample Charge",
    data-amount="500"
    data-label="გადახდა">
    </script>
  <button type="submit">book</button>
</form>

这就是我向服务器发送数据的方式

  $(document).ready(()=>{
  form.submit((e)=>{
    e.preventDefault()
    const data = $('#formid').serialize();
    $.ajax({
      method: 'POST',
      url: "/checkout",
      data: data,
      success: (r)=>{
          alert(r.success)
      },
      error: (xhr,textStatus,error)=>{
         //alerting error
      }
  })
 })
})

如果用户提交表单时未填写“kurtuma”字段和条带表单,它会在提交时打开条带表单并提醒用户必须填写 kurtuma 字段

但主要问题是,如果用户填写了条带表单,它会成功进行交易,并且只有在重定向后才会显示错误,提醒用户填写 kurtomo 字段。如果其他字段为空,我想要的是防止条带进行交易。 这是我处理服务器端验证的方式

app.post('/checkout', async (req,res)=>{
    try{
    const {kurtuma} = req.body
    if(!kurtuma){
        return res.status(409).send({
            message: 'geliko sheavse kurtuma fieldi'
        })
    }
    stripe.customers.create({
        email: req.body.stripeEmail,
        source: req.body.stripeToken
    })
    .then(customer =>{
        stripe.charges.create({
            amount: 500,
            description: '5$ charge',
            currency: 'usd',
            customer: customer.id
        })
     })
    return res.status(200).send({success: true}).

谢谢!

【问题讨论】:

    标签: javascript node.js forms express stripe-payments


    【解决方案1】:

    如果您正在使用 Stripe 的 Checkout 并希望在完成另一个 &lt;input&gt; 后提交表单,您应该使用 Custom (Javascript) Checkout integration,而不是此处的表单块。

    这允许您设置点击处理程序,您可以运行自己的验证,如果验证成功,您可以告诉 Checkout 打开并完成。

    <script src="https://checkout.stripe.com/checkout.js"></script>
    <button id="customButton">Purchase</button>
    <script>
    var handler = StripeCheckout.configure({
      key: 'pk_test_xxxyyyy',
      locale: 'auto',
      token: function(token) {
        // You can access the token ID with `token.id`.
        // Get the token ID to your server-side code for use.
      }
    });
    
    document.getElementById('customButton').addEventListener('click', function(e) {
      e.preventDefault();
    
      // if form validation succeeds
      if(myFormValidates()) {
        // Open Checkout with further options:
        handler.open({
          name: 'Business Cats',
          description: '2 widgets',
          zipCode: true,
          amount: 2000
        });
      }
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多