Nicholas Haley
Nicholas Haley

Reputation: 4024

Shipping Address with Stripe in Rails

I am creating a payment system for a small online business using Stripe and Rails. I am using Stripe's Checkout feature which has made it very easy to set up basic payments.

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. I haven't used Stripe before so I don't have a great intuition on where to go from here. Here is what my code looks like:

charges/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

class ChargesController < ApplicationController
    before_filter :load_order

    def new
        if [email protected]_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? Thanks

Upvotes: 1

Views: 1291

Answers (1)

Luis Menjivar
Luis Menjivar

Reputation: 777

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.

One option

Let stripe handle the shipping address (check this posts) and click on pay with card to see what it looks like.

After submitting a payment Stripe generates a JSON response which you can handle and save to your database, check this post post.

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

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

Upvotes: 1

Related Questions