简体   繁体   English

在Rails中使用自定义集成进行条带检查

[英]Stripe Checkout with Custom Integration in Rails

I am trying to implement Stripe Checkout using the custom integration in a rails app - my checkout form shows a green checkmark saying it submitted but the payment is not being processed. 我正在尝试使用rails应用程序中的自定义集成实现Stripe Checkout - 我的结帐表单显示绿色复选标记,表示已提交,但付款未处理。 The simple integration works well, as do subscription payments on other parts of my site. 简单的集成效果很好,我网站其他部分的订阅付款也是如此。

Like the simple integration, I am trying to place the custom integration script inside of a form_tag - I followed the Rails Checkout guide , which unfortunately is only written for the simple integration. 就像简单的集成一样,我试图将自定义集成脚本放在form_tag中 - 我遵循Rails Checkout指南 ,遗憾的是,它只是为简单的集成而编写的。 Like the guide, I have a charges controller, with new and create actions to show the form and create the charges. 像指南一样,我有一个收费控制器,有新的和创建动作来显示表格并创建收费。

Charges Controller: 收费控制器:

class ChargesController < ApplicationController

def new
end

def create
  # Amount in cents
  @amount = 500

  customer = Stripe::Customer.create(
    :email => params[:stripeEmail],
    :card  => params[:stripeToken]
  )

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => @amount,
    :description => 'Rails Stripe customer',
    :currency    => 'usd'
  )

rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to charges_path
end

end

And in my new view, the form is setup as follows: 在我的新视图中,表单设置如下:

<%= form_tag charges_path do %>
  <script src="https://checkout.stripe.com/checkout.js"></script>

    <button id="customButton" class="btn btn-large btn-primary">Buy Now</button>

    <script>
      var handler = StripeCheckout.configure({
        key: '<%= ENV["STRIPE_PUBLIC_KEY"] %>',
        image: '/assets/my_logo.png',
        token: function(token, args) {
          // Use the token to create the charge with a server-side script.
        }
      });

      document.getElementById('customButton').addEventListener('click', function(e) {
        // Open Checkout with further options
        handler.open({
          name: 'My Company',
          description: 'Product ($60.00)',
          amount: 60*100,
          shippingAddress: true
        });
        e.preventDefault();
      });
    </script>
<% end %>

I have tried just about everything I can think of, but the form will not be submitted to trigger the create action. 我已经尝试了我能想到的所有内容,但不会提交表单来触发创建操作。 I see the note to use a server side script, but can anyone point me in the right direction on what I may be missing? 我看到使用服务器端脚本的注释,但是有人能指出我可能缺少的正确方向吗?

Any help is much appreciated!! 任何帮助深表感谢!! Thanks! 谢谢!

You need to finish the token callback function. 您需要完成令牌回调函数。

First pass in the response from the Stripe handler as an argument and then append the token id and email as inputs to the form before submitting it: (untested) 首先将Stripe处理程序的响应作为参数传递,然后在提交之前将令牌ID和电子邮件作为输入附加到表单:(未经测试)

token: function(response) {
  var tokenInput = $("<input type=hidden name=stripeToken />").val(response.id);
  var emailInput = $("<input type=hidden name=stripeEmail />").val(response.email);
  $("form").append(tokenInput).append(emailInput).submit();
}

Here is a working solution. 这是一个有效的解决方案。 Add an id to your form tag. 在表单标记中添加ID。 Add a stripeToken and stripeEmail hidden field to your form tag. 将stripeToken和stripeEmail隐藏字段添加到表单标记。 Then when we receive the token from Stripe we will use JavaScript to set the values of the hidden fields and submit the form by referencing their id's: 然后,当我们从Stripe收到令牌时,我们将使用JavaScript设置隐藏字段的值,并通过引用其ID来提交表单:

<%= form_tag charges_path, id: 'chargeForm' do %>
  <script src="https://checkout.stripe.com/checkout.js"></script>
  <%= hidden_field_tag 'stripeToken' %>
  <%= hidden_field_tag 'stripeEmail' %>
  <button id="customButton" class="btn btn-large btn-primary">Buy Now</button>

  <script>
    var handler = StripeCheckout.configure({
      key: '<%= ENV["STRIPE_PUBLIC_KEY"] %>',
      image: '/assets/my_logo.png',
      token: function(token, args) { 
        document.getElementById("stripeToken").value = token.id;
        document.getElementById("stripeEmail").value = token.email;
        document.getElementById("chargeForm").submit();
      }
    });

    document.getElementById('customButton').addEventListener('click', function(e) { 
      // Open Checkout with further options
      handler.open({
        name: 'My Company',
        description: 'Product ($60.00)',
        amount: 60*100,
        shippingAddress: true
      });
      e.preventDefault();
    });
  </script>
<% end %>

This can be solved many ways but bare in mind this is a JavaScript problem nothing to do with Rails or Stripe really. 这可以通过多种方式解决,但请记住这是一个与Rails或Stripe无关的JavaScript问题。 Stripe are simply giving us the token we can do whatever we want with it with JavaScript. Stripe只是给我们一个令牌,我们可以用JavaScript做任何我们想做的事情。

I think you don't want to preventDefault here, because that prevents your form from being submitted to the server. 我认为你不想在这里阻止默认,因为这会阻止你的表单被提交到服务器。 Does it submit the form to the create action when you take out e.preventDefault(); 当您取出e.preventDefault();时,它是否将表单提交给create动作e.preventDefault(); ?

With the "simple integration" you can still change the text in the default blue button with the data-label attribute(eg data-label='Buy now') using the configuration options . 通过“简单集成”,您仍然可以使用配置选项使用data-label属性(例如data-label ='立即购买')更改默认蓝色按钮中的文本。 But yeh you have to use "custom integration" to fully style the button yourself 但是你必须使用“自定义集成”来自己完成按钮的样式

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

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