2016-08-24 5 views
0

たとえば、カードを意図的に使用しないと、エラーページが表示されます。そうですね: enter image description here エラーページの代わりに、フラッシュで通知する必要があります。私は以下のコードを持っていますが、なぜフラッシュを使用して警告していませんか?ストライプ検証が失敗すると、なぜフラッシュが表示されないのですか?

class ChargesController < ApplicationController 
    def new 

    end 


    def create 
    # Amount in cents 
    @amount = 100 

    # Get the credit card details submitted by the form 
    customer = Stripe::Customer.create(
     :email => params[:email], 
     :source => params[:stripeToken] 
    ) 

    # Create the charge on Stripe's servers - this will charge the user's card 
    begin 
     Stripe::Charge.create(
      :amount => @amount, 
      :currency => 'usd', 
      :customer => customer.id, 
      :description => 'Example charge custom form' 
    ) 

     current_user.subscribed = true 
     current_user.stripe_id = customer.id 
     current_user.expiry_date = Date.today + 30.days 
     current_user.save 


     flash[:success] = "Thank you for subscribing. Your account has been unlocked." 
     redirect_to root_path 

     rescue Stripe::CardError => e 
     flash[:error] = e.message 
     redirect_to root_path 
    end 


end 

end 

答えて

0

結果を確認してください。

フラッシュまたはリダイレクトを入れないでください。 raiseなどを使用して、コードがどこで検出されるかを確認してください。

begin 
    # Use Stripe's library to make requests... 
rescue Stripe::CardError => e 
    # Since it's a decline, Stripe::CardError will be caught 

rescue Stripe::RateLimitError => e 
    # Too many requests made to the API too quickly 
rescue Stripe::InvalidRequestError => e 
    # Invalid parameters were supplied to Stripe's API 
rescue Stripe::AuthenticationError => e 
    # Authentication with Stripe's API failed 
    # (maybe you changed API keys recently) 
rescue Stripe::APIConnectionError => e 
    # Network communication with Stripe failed 
rescue Stripe::StripeError => e 
    # Display a very generic error to the user, and maybe send 
    # yourself an email 
rescue => e 
    # Something else happened, completely unrelated to Stripe 
end 
+0

私は何かが欠けていない限り、これは何も変わっていないと思います! –

+0

あなたはそれを試しましたか?他のすべての 'レスキュー 'が何もしなかったなら、' rescue => e'は何かをキャッチすべきです。 –

関連する問題