簡體   English   中英

無法使用條帶向客戶收費

[英]Unable to charge customer using stripe

我正嘗試用信用卡詳細信息保存客戶,並在以后收費。

到目前為止,我能夠執行以下操作:

  1. 將卡令牌獲取到Stripe(我在Stripe的日志和Rails控制台中看到它)

  2. 創建一個客戶,將其發送到Stripe並將客戶ID保存在我的數據庫中。

當我嘗試向客戶收費時,出現以下錯誤:

Stripe :: CardError(無法向沒有活動卡的客戶收費)

也許卡令牌沒有正確分配給客戶? 我該如何解決? 也許我很想念這很簡單,但是一段時間以來我一直在努力尋求解決方案。

application.rb中:

class Application < ActiveRecord::Base

  attr_accessor :stripe_card_token

  def save_with_payment
    if valid?
      customer = Stripe::Customer.create(
        description: id, 
        email: self.user.email,
        source: self.stripe_card_token
      )
      self.stripe_customer_token = customer.id
      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 

applications.js.coffee:

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

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

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      exp_month: $('#card_month').val()
      exp_year: $('#card_year').val()
    Stripe.createToken(card, application.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#application_stripe_card_token').val(response.id)
      $('#new_application')[0].submit() ->
       return true if($form.find('.application_stripe_card_token').val())

    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

applications_controller.rb

class ApplicationsController < ApplicationController

  before_action :set_application, only: [:show, :edit, :update, :confirmation, :charge]
  after_action :charge, only: [:create]

  def new 
    @listing = Listing.find(params[:listing_id])
    @application = Application.new

    if Guarantor.where(application_id: @application.id).first.blank?
      @guarantor = Guarantor.new(params[:guarantor])
    end
  end 

  def create
    @listing = Listing.find(params[:listing_id])
    @application = current_user.applications.create(application_params)       

    if params[:btnSubmit]
      redirect_to confirmation_listing_application_path(@application.listing_id, @application.id)
    elsif @application.save_with_payment
      if params[:application][:roommates_attributes].present?
        params[:application][:roommates_attributes].values.each do |a|
          @email = a[:email]
        end
        @user = User.where(email: @email).first
        if @user.blank?
          flash[:error] = "The email address doesn't exist in our records"
          redirect_to new_listing_application_path(@application.listing_id)
          @application.destroy
        else          
          redirect_to confirmation_listing_application_path(@application.listing_id, @application.id), :notice => "Thank you for applying!"
        end
      else
        redirect_to confirmation_listing_application_path(@application.listing_id, @application.id), :notice => "Thank you for applying!"
      end
    end   
  end

  def charge
    Stripe::Charge.create(
      :amount   => 1500, 
      :currency => "usd",
      :customer => @application.stripe_customer_token 
    )
  end

  private

  def set_application
    @application = Application.find(params[:id])
  end

  def application_params
    params.require(:application).permit(
      :_destroy,
      :user_id,
      :listing_id, 
      :stripe_customer_token,
      :st_address,
      :unit,
      :city,
      :state
    )
  end
end

form.html.erb

<%= form_for [@listing, @application], html: {id: "new_application", multipart: true} do |f| %>
  <%= f.hidden_field :stripe_card_token %>

  <% if @application.stripe_customer_token.present? %>
    Credit Card has been provided.
  <% else %>
    <div class="row">
      <div class="col-md-6">
        <%= label_tag :card_number, "Credit Card Number" %>
        <%= text_field_tag :card_number, nil, name: nil, "data-stripe" => "number" %>
      </div>
    </div>

    <div class="row">
      <div class="col-md-6">
        <%= label_tag :card_code, "Security Code (CVC)" %>
        <%= text_field_tag :card_code, nil, name: nil, "data-stripe" => "cvc" %>
      </div>
    </div>

    <div class="row">
      <div class="col-md-6">
        <%= label_tag :card_month, "Card Expiration" %>
        <%= select_month nil, {add_month_numbers: true},    {name: nil, id: "card_month", "data-stripe" => "exp-month" } %>
        <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", "data-stripe" => "exp-year"} %>
      </div>
    </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 class="nextBackBtns">
    <a herf="#" class="btn btn-primary-custom btnBack" data-content="details">Back</a>
    <%= f.submit "Save", class: "btn btn-primary" %>
  </div> 
<% end %>

您的application_params()篩選器方法似乎不允許從您的表單提交的stripe_card_token 我相信,如果將其添加到permit()過濾器列表中,則應該能夠將值傳遞到控制器,以便可以在需要時使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM