Currently my code is not displaying errors in the form. When I submit the credit for declines, I get an actual proper Ruby on Rails error message. How do I get this error message to appear on the form?
Any ideas? Thanks!
subscriptions_controller.rb
class SubscriptionsController < ApplicationController
before_filter :authenticate_user!
def new
@subscription = Subscription.new
end
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
def show
@subscription = Subscription.find(params[:id])
end
def subscription_params
params.require(:subscription).permit(:stripe_card_token)
end
end
subscription.rb
class Subscription < ActiveRecord::Base
belongs_to :user
attr_accessor :stripe_card_token
def save_with_payment
customer = Stripe::Customer.create(
:card => stripe_card_token,
:description => "name",
:plan => 121,
:email => "email")
self.stripe_customer_id = customer.id
self.plan = 121
end
end
subscriptions.js.coffee
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
subscription =
setupForm: ->
$('#new_subscription').submit (e) ->
$('input[type=submit]').attr('disabled', true)
subscription.processCard()
return false
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, subscription.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$('#subscription_stripe_card_token').val(response.id)
$('#new_subscription')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
false
new.html.erb
<div class='panel panel-default'>
<div class='panel-heading'>
<h2>Subscribe</h2>
</div>
<div class='panel-body'>
<%= form_for @subscription, :html => {:class => 'main-form'} do |f| %>
<%= f.hidden_field :stripe_card_token %>
<div id='stripe_error' class="alert alert-info" style='display:none'>
</div>
<span class="help-block">Nothing is billed to your card for 7 days. <b>Guaranteed. </b>
<br>If you choose to continue after 7 days, only then will you be billed.</span>
<div class='form-group'>
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil, class: 'form-control input-box', :placeholder => 'Credit Card Number' %>
</div>
<div class='row'>
<div class="col-xs-6">
<%= label_tag :card_code, "Security Code on Card (CVC)" %>
<%= text_field_tag :card_code, nil, name: nil, class: 'form-control input-box', :placeholder => 'Security Code on Card (CVC)' %>
</div>
<div class="col-xs-6">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year+1, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
</div>
</div>
<div>
<%= f.submit "Subscribe", class: 'btn standard-button' %>
</div>
<% end %>
</div>
</div>
Looking at your code I see a couple of potential issues:
Your save_with_payment message does not actually save your subscription model. You would need to actually call 'save' inside the save_with_payment method. This leads me to believe that Stripe::Customer.create is throwing an exception that is just getting bubbled up to your controller.
Assuming I am correct, then in order for you to actually get an error on the form you will need to trigger a validation error on your subscription model.
There are a number of ways you could approach this. A quick and dirty way might be to actually create the stripe customer in your custom validation itself. eg,
validate :can_create_stripe_customer, :if => :customer_id_is_nil_and_stripe_card_token_present?
def can_create_stripe_customer
customer = Stripe::Customer.create(
:card => stripe_card_token,
:description => "name",
:plan => 121,
:email => "email")
self.stripe_customer_id = customer.id
self.plan = 121
rescue Exception > e
errors.add(:base, e.message)
false
end
This would give you the desired error on your model/form. In your controller you would simply call
@subscription.save
Just an afterthought: if the code execution has gotten this far it's not clear to me that the user will be able to recover from the error because stripe has already validated that the user has provided a valid credit card by returning a token.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.