Rails Stripe - 您必须提供有效卡
Posted
技术标签:
【中文标题】Rails Stripe - 您必须提供有效卡【英文标题】:Rails Stripe - You must supply a valid card 【发布时间】:2014-02-03 00:05:09 【问题描述】:在我的应用中,我使用“Stripe” gem 来设置付款。一切都很好,但是每当我提交付款时,它都会给我一个错误“您必须提供有效的卡”。 下面是我的代码。
广告控制器
class AdsController < ApplicationController
before_action :set_ad, only: [:show, :edit, :update, :destroy]
# GET /ads
# GET /ads.json
def index
@ads = Ad.order('created_at DESC').search(params[:search])
@ads_small = Ad.where(:size => "small").order('created_at DESC')
@ads_medium = Ad.where(:size => "medium").order('created_at DESC')
@ads_featured = Ad.where(:size => "featured").order('created_at DESC')
end
def myads
@ads_mine = Ad.where(:user_id => current_user.id )
end
# GET /ads/1
# GET /ads/1.json
def show
end
# GET /ads/new
def new
@ad = Ad.new
end
# GET /ads/1/edit
def edit
end
# POST /ads
# POST /ads.json
def create
@ad = Ad.new(ad_params)
@ad.user_id = current_user.id
respond_to do |format|
if @ad.save_with_payment
format.html redirect_to @ad, notice: 'Ad was successfully created.'
format.json render action: 'show', status: :created, location: @ad
else
format.html render action: 'new'
format.json render json: @ad.errors, status: :unprocessable_entity
end
end
end
# PATCH/PUT /ads/1
# PATCH/PUT /ads/1.json
def update
respond_to do |format|
if @ad.update(ad_params)
format.html redirect_to @ad, notice: 'Ad was successfully updated.'
format.json head :no_content
else
format.html render action: 'edit'
format.json render json: @ad.errors, status: :unprocessable_entity
end
end
end
# DELETE /ads/1
# DELETE /ads/1.json
def destroy
@ad.destroy
respond_to do |format|
format.html redirect_to ads_url
format.json head :no_content
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_ad
@ad = Ad.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def ad_params
params.require(:ad).permit(:title, :url, :preview, :location, :size, :info, :search)
end
end
广告模型
class Ad < ActiveRecord::Base
belongs_to :user
has_attached_file :preview, :styles => :medium => "125x125^", :featured => "250x250^", :showpg => "400x400^" , :convert_options => :medium => "-gravity center -extent 125x125", :featured => "-gravity center -extent 250x250", :showpg => "-gravity center -extent 400x400"
validates :title, length: maximum: 35
validates :url, length: maximum: 40
attr_accessor :stripe_card_token
def self.search(search)
if search
find(:all, :conditions => ['LOWER(title) ILIKE ? or LOWER(info) ILIKE ? or LOWER(location) ILIKE ?', ("%#search.downcase%"), ("%#search.downcase%"), ("%#search.downcase%")])
else
find(:all)
end
end
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: "ad stripe customer", plan: "ad_f", card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
end
end
ads.js.coffee
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
ad.setupForm()
ad =
setupForm: ->
$('#new_ad').submit ->
$('input[type=submit]').attr('disabled', true)
ad.processCard()
false
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, ad.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 200
$('#ad_stripe_card_token').val(response.id)
$('#new_ad')[0].submit()
else
alert(response.error.message)
$('input[type=submit]').attr('disabled', false)
任何帮助将不胜感激。 :)
【问题讨论】:
是的,它们只是复制 n 粘贴错误,但我已经将它们整理出来了 :) 这是直播还是测试?在测试中..我认为唯一有效的是 4242424242424242 作为卡号... 【参考方案1】:在您的 Ad
模型中,您有:
attr_accessor :stripe_card_token
然后你使用save_with_payment
中的令牌:
customer = Stripe::Customer.create(..., card: stripe_card_token)
self.stripe_customer_token = customer.id
到目前为止一切顺利。但是当您创建Ad
时,您使用ad_params
在控制器中过滤params
:
def ad_params
params.require(:ad).permit(:title, :url, :preview, :location, :size, :info, :search)
end
我在任何地方的允许列表中都看不到:stripe_card_token
。大概您的 HTML 中的 #ad_stripe_card_token
如下所示:
<input type="hidden" name="ad[stripe_card_token]">
所以您应该能够将:stripe_card_token
添加到ad_params
中的允许列表中并让事情顺利进行。
【讨论】:
对不起,我没有注意到这个答案我最终还是想通了,但我仍然会投票赞成你的答案。以上是关于Rails Stripe - 您必须提供有效卡的主要内容,如果未能解决你的问题,请参考以下文章
使用 Stripe on Rails 免费订阅 - 无需卡即可获得令牌
iOS 中的 Stripe 集成 - 您没有提供 API 密钥?
Visual Studio 代码:提供的 javaHome 必须是有效目录。您提供:D:\jdk