使用create方法集成Stripe付款

时间:2016-06-21 09:05:15

标签: ruby-on-rails ruby stripe-payments

我想将条带费用与我在控制器中的创建方法正确整合。

typescriptServices.js

问题在于正确验证整个表格。除了条纹字段,我还有为@individual_training设计的字段。当Stripe没有任何错误,但@individual_training有,条纹注册付款。当@ individual_training.save为真时,我希望记录条带费用。反而。

如果数据在第一部分(条带字段)和表格的第二部分(@individual_training字段)中是正确的,那么应该注册付款并且@ individual_training.save。

我希望能够清楚地解释这个问题。

1 个答案:

答案 0 :(得分:1)

根据您在评论中所写的内容,以下内容应该为您做点什么

 def create
    @individual_training = IndividualTraining.new(individual_training_params)
    ..
    if @individual_training.valid? # run validation, ensure it's ok, but not yet save
      begin
        charge = Stripe::Charge.create(..) # if there is exception rescue block is called
        ...
        # unless you modified individual_training following save will be positive
        if !charge.errors? && @individual_training.save # save should be true as you checked valid? before
          redirect_to :back, notice: 'error'
        else
          render :new # this is if charge has errors
        end          
      rescue Stripe::CardError => e
        flash[:danger] = e.message
        render :new
      end     
    else # this happens if individual_training  is not valid
      render :new
    end
  end

charge.errors?的代码应更改为Stripe操作结果的实际检查。