条带形式不显示错误,而是显示正确的Ruby on Rails错误

时间:2014-10-21 17:11:45

标签: ruby-on-rails stripe-payments

目前我的代码没有在表单中显示错误。当我提交拒绝信用时,我得到一个实际正确的Ruby on Rails错误消息。如何将此错误消息显示在表单上?

有什么想法吗?谢谢!

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>

1 个答案:

答案 0 :(得分:0)

查看您的代码,我发现了一些潜在的问题:

您的save_with_payment消息实际上并未保存您的订阅模式。你需要在save_with_payment方法中实际调用'save'。这让我相信 Stripe :: Customer.create 会抛出一个异常,只会冒泡到你的控制器。

假设我是正确的,那么为了让您在表单上实际出现错误,您需要在订阅模型上触发验证错误。

有很多方法可以解决这个问题。快速而肮脏的方法可能是在自定义验证中实际创建条带客户。例如,

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

这会在您的模型/表单上提供所需的错误。在您的控制器中,您只需致电

@subscription.save

只是事后的想法: 如果代码执行到目前为止,我不清楚用户是否能够从错误中恢复,因为条带已经验证用户通过返回令牌提供了有效的信用卡。