简体   繁体   English

Rails中带有条纹的收货地址

[英]Shipping Address with Stripe in Rails

I am creating a payment system for a small online business using Stripe and Rails. 我正在使用Stripe和Rails为小型在线企业创建支付系统。 I am using Stripe's Checkout feature which has made it very easy to set up basic payments. 我正在使用Stripe的Checkout功能,该功能使设置基本付款变得非常容易。

Because the business sells physical objects I need to collect shipping address information the user and include it within the payment submission. 由于公司出售实物,因此我需要收集用户的送货地址信息,并将其包括在付款提交中。 I've noticed that Stripe has pretty bad documentation on how to implement this however. 我注意到,Stripe关于如何实现此功能的文档非常糟糕。 I haven't used Stripe before so I don't have a great intuition on where to go from here. 我以前从未使用过Stripe,所以我对从这里去哪里没有很好的直觉。 Here is what my code looks like: 这是我的代码:

charges/new.html.erb 费用/ 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 %> 

controllers/chargers_controller.rb 控制器/ 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

Should I be collecting the saving the shipping info into an Address model and then passing the information into customer or charge object? 我是否应该将运输信息保存到地址模型中,然后将其传递给客户或收费对象? Or can I create fields within the Stripe Checkout modal? 还是可以在Stripe Checkout模式中创建字段? Thanks 谢谢

Just in case you are not aware you can create test charges and check them in your dashboard . 万一您不知道可以创建测试费用并在仪表板上检查它们,以防万一。 Use a test card number like 4242424242424242. The above code looks good to me. 使用测试卡号,例如4242424242424242。上面的代码对我来说不错。

One option 一种选择

Let stripe handle the shipping address (check this posts) and click on pay with card to see what it looks like. 让Stripe处理收货地址(检查此帖子) ,然后单击“用卡付款”以查看其外观。

After submitting a payment Stripe generates a JSON response which you can handle and save to your database, check this post post . 提交付款后,Stripe会生成一个JSON响应,您可以处理它并将其保存到数据库中,请查看此帖子

Another option: 另外一个选项:

You code your application so that the user can not place an order with out typing shipping address and once user types shipping address you allow them to pay with Stripe 您对应用程序进行编码,以使用户无法在未键入送货地址的情况下下订单,并且一旦用户键入送货地址,您就可以让他们使用Stripe付款

Let me know if this helps or if you are stuck. 让我知道这是否有帮助或您是否陷入困境。 I am sure that you already look at stripes docs but just in case here is a link 我确定您已经看过Stripes文档,但以防万一,这里是一个链接

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

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