【问题标题】:Passing Real Time update to Stripe Checkout Amount将实时更新传递给 Stripe 结账金额
【发布时间】:2015-03-31 18:26:13
【问题描述】:

我正在尝试在 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>

如果我将 price 分配给 data-amount,我会遇到两个问题:

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 %>

怎么办?

【问题讨论】:

    标签: javascript ruby-on-rails stripe-payments pusher


    【解决方案1】:

    我建议在 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

    【讨论】:

    • 我可以把它分成两个
    • 我不明白为什么不这样做。试一试:)
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 2017-07-17
    • 2017-07-28
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    相关资源
    最近更新 更多