使用自定义金额设置条带

时间:2014-01-13 07:59:11

标签: 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"/>

4 个答案:

答案 0 :(得分:14)

正在使用的特定方法(简单的嵌入式表格)不适用于所寻求的目的。 您必须使用更灵活的自定义集成,如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>

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

答案 1 :(得分:4)

你需要另一个脚本标签,你不能在有源的脚本标签中使用javascript。

<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="0"></script>
<script type="text/javascript">
    $(function() {
        $('.donate-button').click(function(event) {
            var amount = $('#custom-donation-amount').val();        
            $('.stripe-button').attr('data-amount', amount)
        });
    });
</script>


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

答案 2 :(得分:0)

这可以在简单的结帐表单中获得可变金额。我不确定您使用的是什么框架,但我能够使用gon在我的Ruby(Padrino - not Rails)应用程序中实现此目的,如下所示。 &#39; charge_amount_pence&#39;是一个辅助方法,返回表示要进行的充电的字符串。根据您的特殊需要,可以将其设置为用户通过Ajax请求选择的变量,例如。

<script type="text/javascript">
    window.gon = {};
    gon.charge = charge_amount_pence;
</script>

在结帐脚本中:

"data-amount" = gon.charge

我实际上在我的控制器中使用相同的辅助方法来确保实际收取正确的金额。

答案 3 :(得分:0)

如何创建新的条纹元素?这对我有用,

<script type="text/javascript">
    $(function() {
        $('.donate-button').click(function(event) {
            var holder = document.getElementById('aNewDiv');
            var script = document.createElement('script');
            script.src = 'https://checkout.stripe.com/checkout.js';
            script.setAttribute('class', 'stripe-button');
            script.setAttribute('data-amount', $('#Amount').val()*100);
            script.setAttribute('data-key',"pk_test_W0ML1BmfFMa6z3MgD90s7WeN");
            script.setAttribute('data-name', "Fund");
            script.setAttribute('data-description', "ysysys");
            script.setAttribute('data-locale', "auto");
        }
    }
</script>