0

如何將該腳本以紅寶石形式合併,以便條紋收費如果不創建貼子,則不會通過。紅寶石,條紋和javascript

現在,如果我刪除驗證帖子發佈如果收費經過,如果收費不通過帖子不張貼。

只要我添加驗證和缺少的東西,通過點擊付費時,收費是正確的,驗證停止在數據庫中保存後,但收費仍然通過。

娶妻

<%= form_for @post do |f| %> 


    <div> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
    </div> 

    <div> 
    <%= f.label :email %> 
    <%= f.text_field :email %> 
    </div> 

    <div> 
    <%= f.label :phone_number %> 
    <%= f.text_field :phone_number %> 
    </div> 

    <div> 
    <%= f.label :post %> 
    <%= f.text_area :post %> 
    </div> 

    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
      data-description="Good Luck" 
      data-amount="500" 
      data-locale="auto"></script> 


<% end %> 

柱控制器

class PostsController < ApplicationController 

    before_action :authenticate_admin!, only: [:index] 

    def index 
    @posts = Post.all 
    end 

    def new 
    @post = Post.new 
    end 

    def show 

    end 

    def create 
    @post = Post.new(post_params) 

    if @post.save 
     redirect_to @post, notice: 'Your post was create' 
    else 
     render :new 
    end 

     # Amount in cents 
     @amount = 150 

     customer = Stripe::Customer.create(
     :email => params[:stripeEmail], 
     :source => params[:stripeToken] 
    ) 

     charge = Stripe::Charge.create(
     :customer => customer.id, 
     :amount  => @amount, 
     :description => 'Rails Stripe customer', 
     :currency => 'usd' 
    ) 

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


    end 

    private 

    def post_params 
    params.require(:post).permit(:name, :email, :phone_number, :post) 
    end 

end 

樁模型

validates :name, :phone_number, :post, presence: true 

    validates :email, 
      presence: true, 
      format: { with: /\b[A-Z0-9._%a-z\-][email protected](?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/} 

回答

0

你需要包括beginend在你的try/catch塊的rescue或我不相信它會正常工作:

begin 
    charge = Stripe::Charge.create(
    :customer => customer.id, 
    :amount  => @amount, 
    :description => 'Rails Stripe customer', 
    :currency => 'usd' 
) 

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

您可能還需要確認params[:stripeEmail]params[:stripeToken]實際上包含(預期的)東西,你嘗試創建客戶之前。

+1

的'高清create'是,或多或少,一個隱含的'begin',所以你並不需要一個明確的'begin'如果你想'救援'適用於整個方法。 –

+0

是的,這件事情沒有奏效。你有其他建議嗎? –

+0

如果驗證未通過,我不希望打開條帶電荷彈出窗口。 –

0

您需要將if塊中的條紋交互放入@post.save。這樣的事情:

def create 
    @post = Post.new(post_params) 

    if @post.save 
    # Amount in cents 
    @amount = 150 

    begin 
     customer = Stripe::Customer.create(
     :email => params[:stripeEmail], 
     :source => params[:stripeToken] 
    ) 

     charge = Stripe::Charge.create(
     :customer => customer.id, 
     :amount  => @amount, 
     :description => 'Rails Stripe customer', 
     :currency => 'usd' 
    ) 

     redirect_to @post, notice: 'Your post was created.' 
    rescue Stripe::CardError => e 
     flash[:error] = e.message 
     redirect_to new_charge_path 
    end 
    else 
    render :new 
    end 

此外,我需要評論的事實,這並不真正屬於控制器。我會建立一個類來處理Stripe交互。

這樣的話,在你的控制器,你會碰到這樣的:

if @post.save 
    begin 
    MyStripeStuff.create_and_charge_customer({ 
     email: params[:stripeEmail], 
     token: params[:stripeToken], 
     amount: 150, 
     currency: 'usd', 
     description: 'Rails Stripe customer' 
    }) 
    redirect_to @post, notice: 'Your post was created.' 
    rescue Stripe::CardError => e 
    flash[:error] = e.message 
    redirect_to new_charge_path 
    end 
else 
    render :new 
end