条带令牌未携带到控制器导轨4
问题
我已经测试了CoffeeScript,并且表单调用Stripe,使用正确的响应令牌设置隐藏字段并提交表单。 我的问题是,一旦提交控制器似乎没有正确获取令牌并抛出此错误: Stripe :: InvalidRequestError – 您必须提供卡或客户ID 。
接下来,我累了拿出生成的令牌并将其硬编码到控制器中,看看是否可行。 我提交了表格,在Stripes结束时收到了付款。 关于接下来要尝试什么,我几乎没有想法。 我想知道我是否遗忘了某些东西或遗漏了某些东西,因为付款是在作业下嵌套的。
gem版本
- Ruby:2.1.0
- Rails:4.0.1
- 条纹:1.9.9
档
/payment/new.html.erb
This assignment has already been paid for.
JavaScript is not enabled and is required for this form. First enable it in your web browser settings.
payment_controller.rb
class PaymentsController "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
payment.js.coffee
$ -> 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)
payment.rb
class Payment < ActiveRecord::Base belongs_to :assignment end
我看到至少有两个问题。 我想在进展之后可能会有更多。
-
您无权访问
#save_with_payment
params
问题发生在这一行:
# Get the credit card details submitted by the form token = params[:stripe_customer_token]
params受strong_params保护,您无法访问它。
修复是允许
payment_params
所有需要的参数,并在此方法中重复使用它。 -
实际上你在
#create
没有@price
此问题与问题无直接关系,但存在。
这个实例变量
@price
在#new
。 而#create
是另一个实例,所以你不能再拥有它。解决方法是从
payment_params
获取它
- Rails,CoffeeScript和ERB
- Backbone和Rails关联:避免JSON HashWithIndifferentAccess错误
- 将Rails应用程序部署到Heroku时出现语法错误:ExecJS :: RuntimeError:SyntaxError:意外字符
- 使用will_paginate gem在rails 3.2.3中调用Ajax
- Rails Turbolinks中的Facebook插件页面无法加载
- 在Rails 4中显示所选类别的子类别
- 在HAML模板中使用:coffescriptfilter的性能影响?
- 我如何在.js.haml文件中构建html?
- 使用把手迭代javascript对象