samedi 25 avril 2015

How to fix this error "undefined method `encoding' for nil:NilClassa " and get canceling subscription plan worked?


This is my first time working with Stripe and Rails and now I am trying to allow premium users to cancel their subscriptions.

I can upgrade a user from standard level to premium level with my code, but I am having issues when I attempt to downgrade a premium user to the standard level.

I have followed Stripe Ruby API References of "Cancel a subscription": http://ift.tt/1brVgNv, but I got this error when I clicked the "cancel subscription" button:

NoMethodError - undefined method encoding' for nil:NilClass: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/cgi/util.rb:7:inescape' stripe (1.21.0) lib/stripe/list_object.rb:19:in retrieve' app/controllers/subscriptions_controller.rb:55:indowngrade'

My rails version is 4.2.1.

My code:

class SubscriptionsController < ApplicationController

def create
    subscription = Subscription.new
      stripe_sub = nil
    if current_user.stripe_customer_id.blank?
      # Creates a Stripe Customer object, for associating with the charge
      customer = Stripe::Customer.create(
        email: current_user.email,
        card: params[:stripeToken],
        plan: 'premium_plan'
        )
      current_user.stripe_customer_id = customer.id
      current_user.save!
      stripe_sub = customer.subscriptions.first
    else
      customer = Stripe::Customer.retrieve(current_user.stripe_customer_id)
      stripe_sub = customer.subscriptions.create(
        plan: 'premium_plan'
        )
    end

    current_user.subid = stripe_sub.id

    current_user.subscription.save!

    update_user_to_premium
    flash[:success] = "Thank you for your subscription!"

    redirect_to root_path 

    # Handle exceptions
    rescue Stripe::CardError => e
     flash[:error] = e.message
     redirect_to new_subscriptions_path
  end


  def downgrade

    customer = Stripe::Customer.retrieve(current_user.stripe_customer_id)
    customer.subscriptions.retrieve(current_user.subid).delete

    downgrade_user_to_standard
    flash[:success] = "Sorry to see you go."
    redirect_to user_path(current_user)

  end
end

ApplitionController:

class ApplicationController < ActionController::Base
def update_user_to_premium
    current_user.update_attributes(role: "premium")
   end

   def downgrade_user_to_standard
    current_user.update_attributes(role: "standard")
   end
end

config/initializers/stripe.rb:

Rails.configuration.stripe = {
   publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'],
   secret_key: ENV['STRIPE_SECRET_KEY']
 }

 # Set our app-stored secret key with Stripe
 Stripe.api_key = Rails.configuration.stripe[:secret_key]

Any help will be appreciated!

Update: Thanks for help from stacksonstacks, all I need is asserting 'subscription.user = current_user' under 'current_user.subid = stripe_sub.id', and then call subscription id with "subscription = current_user.subscription" in downgrade method. Now subscription cancelling works!


Aucun commentaire:

Enregistrer un commentaire