繁体   English   中英

将实时更新传递给条纹签出金额

[英]Passing Real Time update to Stripe Checkout Amount

我正在尝试在Stripe Checkout UI中实施可变价格,该价格将由Pusher更新。

首先,将Pusher消息分配给名为price的全局变量。

  <script type="text/javascript">
      var pusher = new Pusher('7a5433c6fc39502a4a02');
      var channel = pusher.subscribe('the_channel');
      var price
      channel.bind('the_event', function(data) {
      price = data.message
      });
  </script>

如果我将价格分配给数据量,那么我将面临两个问题:

1-从流程角度来看,该字段默认情况下为空,仅在Pusher发送消息时填充。

2-从语法上讲,当按下时,该变量当前甚至不呈现消息

<%= form_tag charges_path, class: 'stripeform' do %>
    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
            data-amount= <%= @price %>
    </script>
<% end %>

该怎么办?

我建议您在Rails控制器中为@price设置默认值。

然后,在Stripe中查看文档之后,您可以使用类似的方法来创建价格可变的按钮。 请记住,如Stripe文档所述,您仍然必须以正确的金额创建费用

<script src="https://checkout.stripe.com/checkout.js"></script>

<div id="price-display">$<%= @price %></div>
<button id="customButton">Purchase</button>

<script>
  var price = parseInt("<%= @price %>");

  var pusher = new Pusher('7a5433c6fc39502a4a02');
  var channel = pusher.subscribe('the_channel');
  channel.bind('the_event', function(data) {
    price = parseInt(data.message);
    document.getElementById('price-display').innerHTML = "$" + data.message;
  });

  var handler = StripeCheckout.configure({
    key: "<%= Rails.configuration.stripe[:publishable_key] %>",
    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`
    }
  });

  $('#customButton').on('click', function(e) {
    // Open Checkout with further options
    handler.open({
      name: 'Demo Site',
      description: '2 widgets',
      amount: price
    });
    e.preventDefault();
  });

  // Close Checkout on page navigation
  $(window).on('popstate', function() {
    handler.close();
  });
</script>

请参阅此处以了解更多与Stripe相关的信息: https : //stripe.com/docs/checkout#integration-custom

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM