javascript - 使用自定义数量设置 strip

标签 javascript jquery stripe-payments

我在设置自定义金额时遇到问题,我想将数据金额设置为用户在输入 id="custom-donation-amount"中选择的任何值,我应该怎么做。我的尝试没有用。

<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-image="images/button.png"
  data-key="pk_test_FTZOOu9FL0iYJXXXXXX"
  data-name="Fund"
  data-label= "DONATE"
  data-description="ysysys"
  data-amount = 
  >

    $('.donate-button').click(function(event){
    var self = $(this);
    var amount = 0;
     amount = $('#custom-donation-amount').val();
      if (amount === "") {
        amount = 50;
      }

    amount = self.attr('data-amount');
    amount = Math.abs(amount * 100);
  });

</script>
 <input type="number" id="custom-donation-amount" placeholder="50.00" min="0" step="10.00"/>

最佳答案

正在使用的特定方法(简单的嵌入式形式)不会达到所寻求的结果。 您必须改用更灵活的自定义集成,如 in the docs 所示. 提供的示例中唯一没有包括的是如何连接输入的金额,这一点也不难。

<input class="form-control"
       type="number"
       id="custom-donation-amount"
       placeholder="50.00"
       min="0"
       step="10.00"/>
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton ">Purchase</button>
<script>
  var handler = StripeCheckout.configure({
    key: 'pk_test_ppailxxxxxxxxxxxxxxxxxxx',
    image: '/square-image.png',
    token: function(token) {
      // Use the token to create the charge with a server-side script.
      // You can access the token ID with `token.id`
    }
  });

  document.getElementById('customButton').addEventListener('click', function(e) {
    // This line is the only real modification...
    var amount = $("#custom-donation-amount").val() * 100;
    handler.open({
      name: 'Demo Site',
      description: 'Some donation',
      // ... aside from this line where we use the value.
      amount: amount
    });
    e.preventDefault();
  });
</script>

请注意,您实际上必须填写传递给 StripeCheckout.configure 调用的 token: 函数。特别是,您需要提交表单或 ajax 请求并在您的服务器上进行相应处理。然后,您将在服务器上使用此信息以及 key ,向 Stripe 发送付款创建请求。 Stripe 文档详细说明了需要将哪些信息发送到他们的 API 调用,以及您需要将哪些信息传递给服务器上的 API 调用。特别要注意的是,您可能需要在 token 函数中额外获取价格等。

关于javascript - 使用自定义数量设置 strip ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21086362/

相关文章:

javascript - 在视口(viewport) Bootstrap 中更改类

ruby-on-rails - Stripe 结帐测试版 : Avoid creating new customer for every payment

php - 表单的不同 POST 选项,然后传递数据

javascript - Instagram 任务机器人正在快速触发事件

javascript - 无法加载资源 : the server responded with a status of 412 (Precondition Failed)

javascript - 在 Canvas Html5 的折线图中隐藏 xAxis 上的标签

javascript - 显示快速变化的数据库的最佳方式是什么?

ruby-on-rails - 带 3D secure 的 Stripe 订阅不完整

javascript - 无需单击的jquery表单验证 ->当确定显示div时

javascript - 检索onclick源后,无法使用element的方法