1

我使用的是「條紋」寶石設置付款。一切都很順利,但每當我提交付款時,它都會給我提供錯誤'您必須提供有效的卡'。 以下是我的代碼。Rails的條紋 - 你必須提供一個有效的卡在我的應用程序

廣告控制器

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) 

任何幫助將不勝感激。 :)

+0

是他們只是複製粘貼ň錯誤,但我已經他們整理出來:) – coreypizzle

+0

這是直播或測試?在test..I認爲唯一可行的就是4242424242424242的卡號... –

回答

3

在你Ad模型,您有:

attr_accessor :stripe_card_token 

,然後使用令牌save_with_payment

customer = Stripe::Customer.create(..., card: stripe_card_token) 
self.stripe_customer_token = customer.id 

到目前爲止好。但是你用ad_params當您創建Ad過濾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和得到的東西去。

+0

對不起,我沒有注意到這個答案我結束了反正搞清楚了這一點,但我還是會投你的答案。 – coreypizzle

相關問題