2014-03-25 59 views
0

我有條紋設置,我想刪除文件上的當前用戶卡並添加一個新的。InvalidRequest客戶沒有ID卡

在這方面,我得到錯誤信息Stripe::InvalidRequestErrorCustomer cus_3hWPcPgUi04tCO does not have card with ID {@user.subscription.stripe_card_id}

它指向上線用戶控制器customer.cards.retrieve("{@user.subscription.stripe_card_id}").delete()

我不知道如何得到它來調用卡ID,因爲它應該是使用card_103hWP26pFykwX9Rv8dExvYh的測試卡ID。

class SubscriptionsController < ApplicationController 

    def new 
    plan = Plan.find(params[:plan_id]) 
    @subscription = plan.subscriptions.build 
    if params[:PayerID] 
     @subscription.paypal_customer_token = params[:PayerID] 
     @subscription.paypal_payment_token = params[:token] 
     @subscription.email = @subscription.paypal.checkout_details.email 
    end 
    end 

    def create 
    @subscription = Subscription.new(params[:subscription]) 
    if @subscription.save_with_payment 
     redirect_to @subscription, :notice => "Thank you for subscribing!" 
    else 
     render :new 
    end 
    end 

    def show 
    @subscription = Subscription.find(params[:id]) 
    end 

    def paypal_checkout 
    plan = Plan.find(params[:plan_id]) 
    subscription = plan.subscriptions.build 
    redirect_to subscription.paypal.checkout_url(
     return_url: new_subscription_url(:plan_id => plan.id), 
     cancel_url: root_url 
    ) 
    end 

    def updatesubscription 
     @user = current_user 
     @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token) 
     @customer.update_subscription(:plan => "1", :prorate => true) 
    current_user.save! 
     flash.alert = 'Your subscription has been updated!' 
     redirect_to root_url 
    end 

    def cancelsubscription 
     @user = current_user 
     @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token) 
     @customer.cancel_subscription() 
     current_user.save! 
     flash.alert = 'Your subscription has been cancelled successfully!' 
     redirect_to root_url 
     end 

     def showcard 
     @user = current_user 
     Stripe::Customer.retrieve(@user.subscription.stripe_customer_token).cards.all() 
     end 

     def changecard 
      @user = current_user  
      @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token) 

      card = @customer.cards.create({ 
       :card => @user.subscription.stripe_customer_token 
      }) 

      @customer.default_card = card 
      @customer.save 
      end 

      def suspend 
      @user = current_user 
      @user.subscription.suspend_paypal 
       flash.alert = 'Billing has been suspended!' 
       redirect_to root_url 
      end 

      def reactivate 
      @user = current_user 
      @user.subscription.reactivate_paypal 
       flash.alert = 'Billing has been activated!' 
       redirect_to root_url 
      end 

     def updatebilling 
      @user = current_user 
      customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token) 
      customer.cards.retrieve("{@user.subscription.stripe_card_id}").delete() 
      customer.cards.create({ 
        card: { 
        number: params[:user][:scardnumber], 
        exp_month: params[:user][:sexp_month], 
        exp_year: params[:user][:sexp_year], 
        cvc: params[:user][:scvc], 
        name: params[:user][:sname], 
        address_line1: params[:user][:sbilling_address1], 
        address_line2: params[:user][:sbilling_address2], 
        address_city: params[:user][:saddress_city], 
        address_zip: params[:user][:saddress_zip], 
        address_state: params[:user][:saddress_state], 
        address_country: params[:user][:saddress_country] 
        } 
       }) 
       if customer.save! 
        @user.stripe_card_id = customer.active_card.id 
        @user.save! 
        flash.alert = 'Billing information updated successfully!' 
        redirect_to root_url 
       else 
        flash.alert = 'Stripe error' 
        redirect_to root_url 
       end 
       end 
end 

回答

1

領域應該是一個字符串已經,只是做

customer.cards.retrieve(@user.subscription.stripe_card_id).delete() 
+0

這是我在這裏發佈之前的原始代碼。我刪除了hashtag,因爲它給了我一個未定義的方法。 – xps15z

+0

再次考慮它之後,你應該能夠使用'@ user.subscription.stripe_card_id'而不是插入它。該字段應該已經是一個字符串。 – CWitty

+0

答案是正確的。我剛纔問了一個錯誤的問題。但它已被接受! – xps15z

2

只需使用的

customer.cards.retrieve(@user.subscription.stripe_card_id).delete()

代替

customer.cards.retrieve("{@user.subscription.stripe_card_id}").delete()