2016-08-24 59 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

你試過了嗎?如果所有其他的'救援'都沒有做任何事情,'救援=>'應該抓住一些東西。 –