[英]Cannot charge a customer that has no active card stripe
我正在使用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
這是我的嵌入式javascript的haml:
.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"
我認為你應該把:card => params[:stripeToken]
放入Stripe::Customer.create
而不是Stripe::Charge.create
我聯系了條紋,他們想出來了。 這是haml推理此代碼的方式。 所以不是這樣的:
= form_tag charges_path :id => 'payment-form' do
你應該做這個:
= form_tag charges_path do #payment-form
它會正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.