2015-07-19 101 views
3

我正在尝试创建客户并使用Rails和Stripe为该客户收费。客户正在Stripe中创建,但在尝试执行收费时,我一直收到错误Cannot charge a customer that has no active card条纹:无法为没有活动卡的客户收费

我跟随作为Rails初学者的条纹教程,所以我不知道该从哪里出发。任何帮助将不胜感激。

Rails代码:

def process_payment 
    customer = Stripe::Customer.create email: email, 
             card: card_token 

    Stripe::Charge.create customer: customer.id, 
          amount: level.price*100, 
          description: level.name, 
          card: card_token, 
          currency: 'usd' 

    end 

    def create 
    @registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"], 
                   card_token: stripe_params["stripeToken"]) 
    raise "Please, check registration errors" unless @registration.valid? 
    @registration.process_payment 
    @registration.save 
    redirect_to @registration, notice: 'Registration was successfully created.' 
    end 

enter image description here

回答

2

尝试创建无card_token充电。您不需要再次指定卡,因为卡已连接到客户,您通过customer参数指定了该客户。

customer = Stripe::Customer.create email: email, 
            source: card_token 

Stripe::Charge.create customer: customer.id, 
         amount: level.price*100, 
         description: level.name, 
         currency: 'usd' 

另外,通过card_token通过source PARAM,而非弃用card PARAM。这里是谈论这个变化的博客文章:https://stripe.com/blog/unifying-payment-types-in-the-api

更多信息:https://stripe.com/docs/api/ruby#create_customer 和:https://stripe.com/docs/api#create_charge

+0

感谢您的帮助!尽管从充电电话中移除卡片令牌并没有帮助。我附上了测试数据错误的截图。往上看。 –

+0

我认为Stripe可能已经改变了客户API,因此'card'现在是'source'(请参阅https://stripe.com/docs/api#create_customer)。试试看看是否有帮助。 – mysmallidea

+0

仍然有同样的错误... –

0

你试图访问参数,所以你必须使用params关键字。

相反的card_token,用params["card_token"]