Ruby on rails 条带令牌未结转至控制器rails 4 问题

Ruby on rails 条带令牌未结转至控制器rails 4 问题,ruby-on-rails,ruby-on-rails-4,coffeescript,stripe-payments,Ruby On Rails,Ruby On Rails 4,Coffeescript,Stripe Payments,我已经测试了CoffeeScript,表单调用Stripe,使用适当的响应令牌设置隐藏字段,然后提交表单。我的问题是,一旦提交了令牌,控制器似乎无法正确获取令牌并抛出此错误:条带::InvalidRequestError-您必须提供卡或客户id 接下来,我尝试将生成的令牌硬编码到控制器中,看看这是否有效。我提交了表格,工作顺利,最后收到了付款。我几乎不知道下一步该做什么。我想知道我是忘记了什么还是遗漏了什么,因为付款嵌套在作业下 宝石版 Ruby:2.1.0 Rails:4.0.1 条纹:1.

我已经测试了CoffeeScript,表单调用Stripe,使用适当的响应令牌设置隐藏字段,然后提交表单。我的问题是,一旦提交了令牌,控制器似乎无法正确获取令牌并抛出此错误:条带::InvalidRequestError-您必须提供卡或客户id

接下来,我尝试将生成的令牌硬编码到控制器中,看看这是否有效。我提交了表格,工作顺利,最后收到了付款。我几乎不知道下一步该做什么。我想知道我是忘记了什么还是遗漏了什么,因为付款嵌套在作业下

宝石版
  • Ruby:2.1.0
  • Rails:4.0.1
  • 条纹:1.9.9
文件夹 /payment/new.html.erb payment.rb
classpayment
我看到至少有两个问题。我想在进步之后可能会有更多

  • 您无权访问
    中的
    参数
    #使用付款保存"

    问题发生在这一行:

    # Get the credit card details submitted by the form
    token = params[:stripe_customer_token]
    
    参数受强参数保护,您无权访问它

    修复方法是允许在
    payment_params
    中使用所有需要的参数,并在此方法中重用它

  • 实际上,您在
    #create
    中没有
    @price

    这个问题与这个问题没有直接关系,但它是存在的

    此实例变量
    @price
    位于
    #new
    中。而
    #create
    是另一个实例,因此您不能再次使用它

    修复方法是从
    支付参数


  • 谢谢你的回复!我已经用你的解决方案更新了价格,这似乎解决了另一个问题。我确实通过将付款添加到params token=params[:payment][:stripe\u customer\u token]中解决了第一个问题
    class PaymentsController < ApplicationController
      def new
        set_assignment
        @payment = @assignment.build_payment
        @price = @assignment.price
      end
    
      def create
        set_assignment
        @payment = @assignment.build_payment(payment_params)
    
        if save_with_payment
          redirect_to assignments_path, :notice => "Payment received, Thank you!"
    
          # since payment was successful, set assignment paid to true
          Assignment.update(@assignment, assignment_paid: true, project_status: "In Progress")
        else
          render :new
        end
      end
    
      private
    
        def save_with_payment
    
          # Set your secret key: remember to change this to your live secret key in production
          # See your keys here https://manage.stripe.com/account
          Stripe.api_key = Rails.configuration.stripe[:secret_key]
    
          # Get the credit card details submitted by the form
          token = params[:stripe_customer_token]
    
          # How much the assignment costs, which must be converted to cents
          @amount = (@price * 100)
    
          # Create the charge on Stripe's servers - this will charge the user's card
          begin
            charge = Stripe::Charge.create(
              :amount => @amount,
              :currency => "cad",
              :card => token,
              :description => "some description of the product"
            )
          rescue Stripe::CardError => e
            redirect_to @assignment, :notice => "The card has been declined"
          end
        end
    
        def set_assignment
          @assignment = Assignment.friendly.find(params[:assignment_id])
        end
    
        def payment_params
          params.require(:payment).permit(
            :stripe_customer_token
          )
        end
    end
    
    $ ->
      Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
      payment.setupForm()
    
    payment =
      setupForm: ->
        $('#new_payment').submit ->
          $('input[type=submit]').attr('disabled', true)
          if $('#card_number').length
            payment.processCard()
            false
          else
            true
    
      processCard: ->
        card =
          number: $('#card_number').val()
          cvc: $('#card_code').val()
          expMonth: $('#card_month').val()
          expYear: $('#card_year').val()
        Stripe.createToken(card, payment.handleStripeResponse)
    
      handleStripeResponse: (status, response) ->
        if status == 200
          console.log response
          $('#payment_stripe_customer_token').val(response.id)
          $('#new_payment')[0].submit()
        else
          $('#stripe_error').text(response.error.message)
          $('input[type=submit]').attr('disabled', false)
    
    class Payment < ActiveRecord::Base
      belongs_to :assignment
    end
    
    # Get the credit card details submitted by the form
    token = params[:stripe_customer_token]