加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Stripe:创建客户和/或收费时的错误消息 –

发布时间:2020-12-17 03:03:46 所属栏目:百科 来源:网络整理
导读:我正在我的Rails应用程序中实现Stripe加载条目表单时出现无效令牌错误 – 我还没有提交客户数据.我大多遵循 http://railscasts.com/episodes/288-billing-with-stripe教程.我做了一些修改,因为它有点不完整. books / show.html.erb是我链接到表单的页面: bT
我正在我的Rails应用程序中实现Stripe&加载条目表单时出现无效令牌错误 – 我还没有提交客户数据.我大多遵循 http://railscasts.com/episodes/288-billing-with-stripe教程.我做了一些修改,因为它有点不完整.

books / show.html.erb是我链接到表单的页面:

<b>Title:</b>  <%= @book.title %> </p>
<b>Author:</b>  <% authorid = @book.author %></p>

<%= @book.id %>
<%= link_to "Buy Now",new_purchase_path(:book_id => @book.id) %>

purchase / new.html.erb是用户填写信息的地方.加载时,我收到无效令牌错误:

<%= form_for @purchase do |f| %>
  <% if @purchase.errors.any? %>
    <%= pluralize(@purchase.errors.count,"error") %> prohibited this purchase from being saved.
    <% @purchase.errors.full_messages.each do |msg| %>
      <%= msg %>
    <% end %>
  <% end %>

  <%= f.hidden_field :stripe_card_token %>

  <% if @purchase.stripe_card_token.present? %>
    Credit card has been provided.
  <% else %>
    <%= label_tag :card_number,"Credit Card Number" %>
    <%= text_field_tag :card_number,nil,name: nil %><p>

    <%= label_tag :card_code,"Security Code on Card (CVV)" %>
    <%= text_field_tag :card_code,name: nil %><p>

    <%= label_tag :card_month,"Card Expiration" %>
    <%= select_month nil,{add_month_numbers: true},{name: nil,id: "card_month"} %>
    <%= select_year nil,{start_year: Date.today.year,end_year: Date.today.year+15},id: "card_year"} %>
  <% end %>

<div id="stripe_error">
  <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>

  <%= f.submit "Purchase" %>
<% end %>

purchases.js.coffee与教程中的几乎相同.我添加了一些提醒.根据我的Stripe仪表板的状态是402.这是POST / v1 /令牌错误,响应正文是:

error:
  type: "card_error"
  message: "This card number looks invalid"
  param: "number"

purchases.js.coffee:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  purchase.setupForm()

purchase =
   setupForm: ->
    $('#new_purchase').submit ->
      $('input[type=submit]').attr('disabled',true)
     if $('#card_number').length
       purchase.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,purchase.handleStripeResponse)

  handleStripeResponse: (status,response) ->
    if status == 200
      alert('This token can still be charged.')
      alert(response.id)
      $('#purchase_stripe_card_token').val(response.id)
      $('#new_purchase')[0].submit()
    else
      alert(response.error.message) 
      alert('The token was invalid,or has been used.')
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled',false)

我已经尝试了几个版本的purchase.rb模型,比如注释掉Stripe :: Charge函数,但仍然得到402令牌错误.但是,创建客户是成功的(代码200).

class Purchase < ActiveRecord::Base

  attr_accessible :stripe_customer_token,:author_id,:book_id
  attr_accessor :stripe_card_token

  belongs_to :book

def save_with_payment
  if valid?

    customer = Stripe::Customer.create(
      :description => "customer email",:card => stripe_card_token
    )
    self.stripe_customer_token = customer.id

#    charge = Stripe::Charge.create(  - this code doesn't work either
#      :amount => 1000,#      :currency => "usd",#      :card => stripe_card_token,#      :description => "book title"
#    )
    save!
  end

  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base,"There was a problem with your credit card."
    false
  end
end

如果我取消注释Stripe :: Charge代码,我得到的错误是:
PurchasesController #crera中的Stripe :: CardError
无法向没有有效卡的客户收费

而且,我的purchases_controller.rb中的create方法

def create
  @purchase = Purchase.new(params[:purchase])
  if @purchase.save_with_payment
    redirect_to @purchase,:notice => "Thank you for purchasing this book!"
  else
    render :new
  end
end

这是在purchases_controller.rb中我的新方法:

def new

06006

end

但是,如果我在提交购买后点击“返回”按钮(返回购买/ new.html.erb页面),则会在我的数据库中输入第二次“购买”,并且我的条带日志中的该POST令牌的代码为200 (通过)!!!

这是从coffeescript编译的javascript:

(function() {

var purchase;

jQuery(function() {

Stripe.setPublishableKey($(‘meta[name=”stripe-key”]’).attr(‘content’));

return purchase.setupForm();

});

purchase = {

setupForm: function() {

06007

},

processCard: function() {

06008

},

handleStripeResponse: function(status,response) {

06009

}

};

}).call(this);

解决方法

正如我在上面的评论中所提到的,CoffeeScript的缩进稍微偏离(CS中的空格很重要).这不是语法错误,所以它仍然编译,但生成的JS不是你想要的.这是正确缩进的代码.

purchase =
  setupForm: ->
    $('#new_purchase').submit ->
      $('input[type=submit]').attr('disabled',true)
      if $('#card_number').length
        purchase.processCard()
        false
      else
        true
  # ...

在您发布的JS中,条件if($(‘#card_number’.长度)存在于绑定到submit事件的匿名函数之外.这会导致条件在页面加载后立即运行,而不是在提交表单时运行.由于在初始加载页面时“#card_number”输入中没有任何内容,因此以静默方式执行条件的替代(else分支).以下是JS应该是这样的:

$('#new_purchase').submit(function() {
  $('input[type=submit]').attr('disabled',true);
  if ($('#card_number').length) {
    purchase.processCard();
    return false;
  } else {
    return true;
  }
});

导航回“新页面”时所看到的行为是在“#card_number”字段中输入页面加载的结果(执行processCard函数),这是您想要的,而不是您想要的时间.

如果我的推测是正确的,那么在CoffeeScript中修复缩进就是解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读