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