航运地址与条纹在Rails



我正在创建一个使用Stripe和Rails的小型在线业务支付系统。我正在使用Stripe的Checkout功能,它使设置基本支付变得非常容易。

因为业务销售物理对象,所以我需要收集用户的送货地址信息,并将其包含在付款提交中。我注意到Stripe在如何实现这一点上有非常糟糕的文档。我以前没有使用过Stripe,所以我没有一个很好的直觉。下面是我的代码:

/new.html.erb指控

<%= form_tag charges_path do %>
  <article>
    <label class="amount">
      <span>Amount: <%= @order.subtotal %></span>
    </label>
  </article>
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-image="http://www.fairmountbagel.com/images/bagels/pavot2.png"
          data-description="Your Bagel Clock"
          data-name="Bagel-O-Clock"
          data-amount="<%= @amount %>"></script>
<% end %>

控制器/chargers_controller.rb

class ChargesController < ApplicationController
    before_filter :load_order
    def new
        if !@order.order_items.empty?
            @amount = (@order.subtotal.to_f * 100).to_i
        else
            redirect_to root_path
        end
    end
    def create
        @amount = (@order.subtotal.to_f * 100).to_i
        customer = Stripe::Customer.create(
            :email => @order.customer_info.email,
            :card  => params[:stripeToken]
            )
        charge = Stripe::Charge.create(
            :customer    => customer.id,
            :amount      => @amount,
            :description => 'Rails Stripe customer',
            :currency    => 'cad'
            )
        @order.order_items.destroy_all
    rescue Stripe::CardError => e
        flash[:error] = e.message
        redirect_to charges_path
    end
    private
    def load_order
        @order = current_order
    end
end

我是否应该收集并保存送货信息到Address模型中,然后将该信息传递给客户或收费对象?或者我可以在Stripe Checkout模式中创建字段吗?由于

以防万一,您可以创建测试收费并在仪表板中检查它们。使用像4242424242424242这样的测试卡号。上面的代码看起来不错。

一个选择

让条纹处理送货地址(检查这篇文章),并点击支付卡,看看它看起来像什么。

提交付款后,Stripe生成一个JSON响应,您可以处理并保存到数据库中,请检查此post post。

另一种选择:

你编写你的应用程序,这样用户就不能在没有输入送货地址的情况下下单,一旦用户输入送货地址,你就允许他们使用Stripe支付

让我知道这是否有帮助,或者如果你卡住了。我相信你们已经看过条纹文档了但以防万一这里有一个链接

最新更新