无法向没有活动卡条带的客户收费

时间:2016-01-10 02:21:48

标签: javascript ruby-on-rails stripe-payments

我正在使用stripe.js切换条带检出。当我输入卡片时,一切都有效,除非它从未通过。每当我点击提交时,我都会收到错误消息:

Cannot charge a customer that has no active card 

我尝试过使用测试卡和真实的信用卡号码,他们两个都给了我同样的错误。这是我的stripe.rb:

class ChargesController < ApplicationController
    before_action :authenticate_user!
    def new
    end

    def create
      # Amount in cents
      @amount = 500

      customer = Stripe::Customer.create(
        :email => params[:stripeEmail],
        :source  => params[:stripeToken]
      )

      Stripe.api_key = 'sk_test_string'

charge = Stripe::Charge.create(
:amount => 1000,
:customer => customer.id,
:currency => "usd",
:card => params[:stripeToken] # replace full card details with the string token from our params
)

    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to new_charge_path
    end
end

这是我的haml with embeded javascript:

.well{:style => "margin-left: 0px; position: relative; min-width: 650px; min-height: 180px; max-height: 180px"}
    = form_tag charges_path :id => 'payment-form' do
      .row
      .row
        %div
          %label.control-label{:for => "number"} Card Number
          %input#number{"data-stripe" => "number", :pattern => "[\\d ]*", :placeholder => "**** **** **** ****", :size => "20", :style => "width: 18em", :type => "text", :maxlength => "16"}/
      .row
        %div
          %label.control-label{:for => "cvc"} CVC
          %input#cvc{"data-stripe" => "cvc", :pattern => "\\d*", :placeholder => "***", :size => "3", :style => "width: 3em", :type => "text"}/
          = image_tag "credit.png"
        %div
          %label.control-label Exp (MM/YYYY)
          %input#exp-month{"data-stripe" => "exp-month", :pattern => "\\d*", :placeholder => "MM", :size => "2", :type => "text"}/
          %span /
          %input#exp-year{"data-stripe" => "exp-year", :pattern => "\\d*", :placeholder => "YYYY", :size => "4", :type => "text"}/
      .row
        .price
          5.00
        %div
          %button.btn.btn-primary.btn-large{:type => "submit"} Buy Now
:javascript
 Stripe.setPublishableKey('pk_test_string');
 $('#payment-form').submit(function(event) {
   var form = $(this);
   form.find('button').prop('disabled', true);
   Stripe.createToken(form, stripeResponseHandler);
   return false;
 });
 function stripeResponseHandler(status, response) {
   var form = $('#payment-form');
   if (response.error) {
     form.find('.payment-errors').text(response.error.message);
     form.find('button').prop('disabled', false);
   } else {
     var token = response.id;
     form.append($('<input type="hidden" name="stripeToken">').val(token));
     form.get(0).submit();
   }

编辑:  我进入了条带上的错误日志,它给了我这个:

error:
    message: "Cannot charge a customer that has no active card"
    type: "card_error"
    param: "card"
    code: "missing"

但我填写了卡错误,它应该正常工作。如果它有助于我使用测试键。

编辑2:这是以条带

发送的内容
id: cus_7gzOPGpAB2DMYY
object: "customer"
account_balance: 0
created: 1452402410
currency: null
default_source: null
delinquent: false
description: "Thank you"
discount: null
email: null
livemode: false
metadata:
shipping: null
sources:
    object: "list"
    data:
    has_more: false
    total_count: 0
    url: "/v1/customers/cus_7gzOPGpAB2DMYY/sources"
subscriptions:
    object: "list"
    data:
    has_more: false
    total_count: 0
    url: "/v1/customers/cus_7gzOPGpAB2DMYY/subscriptions"

2 个答案:

答案 0 :(得分:6)

我认为你应该将:card => params[:stripeToken]放入Stripe::Customer.create而不是Stripe::Charge.create

答案 1 :(得分:1)

我联系了条纹,他们想出来了。这是haml推理此代码的方式。所以不要这样:

correct_test_() -> {setup, % Setup fun() -> ?assertMatch({ok, Table}, dets:open_file("hello", [])), Table end, % Cleanup fun(Table) -> ?assertMatch(ok, dets:close(Table)). end, % Tests [ % This will fail, but the cleanup WILL be called ?_assert(false) ] }.

你应该这样做:

= form_tag charges_path :id => 'payment-form' do

它会正常工作。