Rails Stripe Api 错误:无效的源对象:必须是字典或非空字符串 [英] Rails Stripe Api Error: Invalid source object: must be a dictionary or a non-empty string

查看:26
本文介绍了Rails Stripe Api 错误:无效的源对象:必须是字典或非空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照本 railscast 教程 http://railscasts.com/episodes/288-billing-with-stripe.对表单进行样式设置后,我开始收到以下错误:

您的信用卡有问题.无效的源对象:必须是字典或非空字符串.请参阅 https://stripe.com/docs'

上的 API 文档

在我的控制台中,我可以看到 stripe_card_token 作为任何空值提交

 参数:{"utf8"=>"✓", "authenticity_token"=>"zCP+nFHj3JC0ZyceWOf9D+Vx6wZRfKA0LmZqL7faGzaM1E/k7GgKHCygCktH6mqeq==>0">gktpg10mqe3R>sgktp=>s"", "email"=>"test@outlook.com"}, "commit"=>"创建

我以前遇到过这个错误,并通过从 application.js 中删除 require_tree 解决了它,但错误不断出现

我还尝试禁用其他一些线程中建议的 turbolinks.

这是我的提交表格:

<div ng-controller="PaymentFormCtrl" class="payment-form" name="paymentForm"><div class="通知"><span class="notification__text">我们使用安全的 256 位加密和符合 PCI 标准的信用卡处理,您的数据由我们保存.</span>

<center><h2>12$/月</h2></center><div class="card-type clearfix"><div class="card-type__label">支持的卡

<div class="card-type__icons"></div><br>

<%= form_for(@subscription) 做 |f|%><% if @subscription.errors.any?%><div id="error_explanation"><h2><%=pluralize(@subscription.errors.count, "error") %>禁止保存此订阅:</h2><ul><% @subscription.errors.full_messages.each do |message|%><li><%=消息%></li><%结束%>

<%结束%><%= f.hidden_​​field :stripe_card_token %><% 如果@subscription.stripe_card_token.present?%>已提供信用卡.<%其他%><div><%= f.text_field :email, class: "email-input card-input--full",placeholder: "Email" %>

<div><%= text_field_tag :card_number, nil, name: nil, class: "card-input card-input--full",placeholder: "credit card number", :ng =>{:模型=>"card.number" } %>

<div class="card-info clearfix"><div class="card-input--alldate" ng-model="card.date" card-date required ><%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month", class: "card-input--monthdatedrop"} %><%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", class: "card-input--yeardatedrop"} %>

<div ng-model="card.cvv" card-cvv 需要><%= text_field_tag :card_code, nil, name: nil, class: "card-input card-input--cvv", placeholder: "CVV" %>

<%结束%><div id="stripe_error"><noscript>JavaScript 未启用并且是此表单所必需的.首先在您的网络浏览器设置中启用它.</noscript>

<div ng-disabled="paymentForm.$invalid"><%= f.submit class: "payment-form__button" %>

<div class="payment-form__agreement">我们直接将您的数据传递给我们的支付处理器,并且从不存储您的信用卡详细信息.我们所有的连接都按照最高标准进行加密.您的信用卡信息对我们来说是安全的.在您同意我们的 TOS 后.

<%结束%>

这是我处理处理的 subscription.coffee 文件:

jQuery ->Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))订阅.setupForm()订阅 =设置表格:->$('#new_subscription').提交 ->$('input[type=submit]').attr('disabled', true)如果 $('#card_number').length订阅.processCard()错误的别的真的处理卡:->卡 ={number: $('#card_number').val(),cvc: $('#card_code').val(),expMonth: $('#card_month').val(),expYear: $('#card_year').val()}Stripe.createToken(card, subscription.handleStripeResponse)handleStripeResponse:(状态,响应)->如果状态 == 200$('#subscription_stripe_card_token').val(response.id)$('#new_subscription')[0].submit()别的$('#stripe_error').text(response.error.message)$('input[type=submit]').attr('disabled', false)

有人知道为什么 stripe_card_token 是空的吗?

更新

似乎subscriptions.coffee 文件似乎没有自动加载(因为删除了application.js 中的require_tree . 行).当我强制它加载时,我收到一个错误

SyntaxError: [stdin]:26:46: 意外换行

在 application.html.erb 中,但奇怪的是样式表标签:

 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =>真%>

将文件更改为 .js 文件并没有带来任何更改.我监督的 subscription.coffee 文件是否有错误?

解决方案

如果订阅从 Trial 更新到 Paid,您需要先创建客户付款来源

def update_subscription(params = {})如果订阅.试用?stripe_customer = Stripe::Customer.retrieve(stripe_customer_token)stripe_card_token = params['订阅']['stripe_card_token']stripe_customer.sources.create(来源:stripe_card_token)结尾结尾

然后更新 Stripe 订阅

开始条纹::订阅.更新(stripe_subscription_id,{数量:数量,计划:计划})救援/* 在这里做事!*/结尾

I have successfully implemented stripe credit card processing (Stripe api v2) into my rails app following this railscast tutorial http://railscasts.com/episodes/288-billing-with-stripe. After styling the form I started receiving the following error:

There was a problem with your credit card. Invalid source object: must be a dictionary or a non-empty string. See API docs at https://stripe.com/docs'

In my console I can see that the stripe_card_token is being submitted as any empty value

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"zCP+nFHj3JC0ZyceWOf9D+Vx6wZRfKA0LmZqL7faGzaM1E/k7GgKHCygCktH6mqe3oFNG1tqRpJpjKB728KE0g==", "subscription"=>{"stripe_card_token"=>"", "email"=>"test@outlook.com"}, "commit"=>"Create

I previously had this error and solved it by removing require_tree from application.js but the error keeps reappearing

I have also tried to disable turbolinks which is suggested in some other threads.

Here is my submission form:

<div ng-app="app" class="creditcontainer">
  <div ng-controller="PaymentFormCtrl" class="payment-form" name="paymentForm">
    <div class="notification">

      <span class="notification__text">
      We use secure 256 bit encryption and PCI compliant credit card processing, you data is save with us.
      </span>
    </div>
    <center><h2>12$/month</h2></center>
    <div class="card-type clearfix">
      <div class="card-type__label">
        Supported Cards
      </div>
      <div class="card-type__icons">

      </div><br>
    </div>
<%= form_for(@subscription) do |f| %>
  <% if @subscription.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@subscription.errors.count, "error") %> prohibited this subscription from being saved:</h2>

      <ul>
      <% @subscription.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <%= f.hidden_field :stripe_card_token %>



<% if @subscription.stripe_card_token.present? %>
  Credit card has been provided.
<% else %>
  <div>
    <%= f.text_field :email, class: "email-input card-input--full",placeholder: "Email" %>
  </div>
  <div>
  <%= text_field_tag :card_number, nil, name: nil, class: "card-input card-input--full",placeholder: "Credit card number", :ng => { :model => "card.number" } %>
  </div>


  <div class="card-info clearfix">


    <div class = "card-input--alldate" ng-model="card.date" card-date required >
    <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month", class: "card-input--monthdatedrop"} %>
    <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", class: "card-input--yeardatedrop"} %>
  </div>
  <div ng-model="card.cvv" card-cvv required>
    <%= text_field_tag :card_code, nil, name: nil, class: "card-input card-input--cvv", placeholder: "CVV" %>
  </div>
<% 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>
  <div ng-disabled="paymentForm.$invalid">
    <%= f.submit class: "payment-form__button" %>
  </div>
  <div class="payment-form__agreement">
       We directly pass your data on to our payment processor and never store your credit card details. All our connections are encrypted to the highest standards. Your credit card information is safe with us. Upon paying your agree to our TOS.
    </div>
  </div>
</div>


<% end %>

This is my subscription.coffee file which handles the processing:

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

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

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#subscription_stripe_card_token').val(response.id)
      $('#new_subscription')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

Anyone has any ideas why the stripe_card_token is empty?

Update

it seems that somehow the subscriptions.coffee file does not seem to get auto loaded (because of the removal of the require_tree . line in application.js). When I force it to load I get an error

SyntaxError: [stdin]:26:46: unexpected newline

in the application.html.erb, but weirdly for the stylesheet tag:

  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>

Changing the file to a .js file did not bring any change. Is there an error in my subscription.coffee file that I have overseen?

解决方案

In case of subscription update from Trial to Paid you need to first create the customer payment source

def update_subscription(params = {})
  if subscription.trial?
      stripe_customer = Stripe::Customer.retrieve(stripe_customer_token)
      stripe_card_token = params['subscription']['stripe_card_token']
      stripe_customer.sources.create(source: stripe_card_token)
  end
end

and then update the Stripe Subscription

begin
  Stripe::Subscription.update(
    stripe_subscription_id,
    { quantity: quantity, plan: plan }
  )
rescue
  /* Do Stuff Here! */
end

这篇关于Rails Stripe Api 错误:无效的源对象:必须是字典或非空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆