Skip to content

Instantly share code, notes, and snippets.

@starflyer59
Created January 9, 2012 22:45
Show Gist options
  • Save starflyer59/1585397 to your computer and use it in GitHub Desktop.
Save starflyer59/1585397 to your computer and use it in GitHub Desktop.
Stripe attempt
Four code pages:
(1) The Error
(2) The Order/form
(3) the CoffeeScript
(4) The create action in the Orders_Controller
___________________________________________________________
The ERROR:
Stripe::InvalidRequestError in OrdersController#create
You must supply either a card or a customer id
Rails.root: /Users/Daniel/Sites/thegiggle
Application Trace | Framework Trace | Full Trace
app/controllers/orders_controller.rb:13:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"CX0XrLCw2OP7m/5A2T+Egs1VpjkfhEQwKy79rQhKUpM=",
"order"=>{"stripe_card_token"=>"tok_08NsQsIzUX6Msm",
"name"=>"Fred Jones",
"unit"=>"",
"city"=>"Minneapolis",
"state"=>"MN",
"zipcode"=>"55419",
"email"=>"steepr@gmail.com"},
"shopping_cart_id"=>"234"}
___________________________________________________________
The Form:
<%= form_for(@order) do |f| %>
<%= f.hidden_field :stripe_card_token %>
<% if @order.stripe_card_token.present? %>
Credit card has been provided.
<% else %>
<%= hidden_field_tag(:shopping_cart_id, @cart.id) %>
<div class="fieldCard2">
<%= label_tag :card_number, "Credit Card Number" %><br />
<%= text_field_tag :card_number, nil, name: nil %><br />
</div>
<div class="fieldCard2">
<%= label_tag :card_code, "Security Code on Card (CVV)" %><br />
<%= text_field_tag :card_code, nil, name: nil %><br />
</div>
<div class="fieldCard2">
<%= label_tag :card_month, "Card Expiration" %><br />
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
</div>
<% end %>
<h3 style="margin-left: 90px;">Whom shall we ship to?</h3>
<div class="fieldCard">
<%= f.label :name, "First and Last Name" %><br />
<%= f.text_field :name %>
</div>
<div class="fieldCard">
<%= f.label :address1, "Street Address" %>
<%= f.label :address2, "Unit or Apt" %><br />
<%= f.text_field :unit,:size => '20' %>
<%= f.text_field :unit, :size => '5' %>
</div>
<div class="fieldCard">
<%= f.label :city, "City" %><br />
<%= f.text_field :city %>
</div>
<div class="fieldCard">
<%= f.label :state, "State" %>
<%= f.label :zipcode, "Zipcode" %><br />
<%= render 'orders/state_select', {:f => f} %>
<%= f.text_field :zipcode, :size => '15' %>
</div>
<div class="fieldCard">
<%= f.label :email, "email receipt to:" %><br />
<%= f.text_field :email %>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<div class="actions" style="margin:10px 0 10px 140px">
<%= f.submit "checkout" %>
</div>
<% end %>
___________________________________________________________
COFFEESCRIPT
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
order.setupForm()
subscription =
setupForm: ->
$('#ordersForm').submit ->
$('input[type=submit]').attr('disabled', true)
order.processCard()
false
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, order.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$('#order_stripe_card_token').val(response.id)
$('#new_order')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
___________________________________________________________
Orders Controller (create action):
def create
@order = Order.new(params[:order])
cart = ShoppingCart.where("id = ?", params[:shopping_cart_id]).first
@order.line_items = cart.line_items
price = cart.total_price_cents
token = @order.stripe_card_token
Stripe::Charge.create(
:amount => price,
:currency => "usd",
:card => token,
:description => "Charge for giggletrigger@gmail.com" )
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: 'Order Successful! ' }
format.json { render json: @order, status: :created, location: @order }
else
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment