Eric
Eric

Reputation: 793

Undefined method 'suspend_paypal' for User

I'm using paypal-recurring gem for my app and i can't figure out how to cancel a user's subscription. i tried gabriel's answer from a similar question on here, but i get a NoMethodError in SubscriptionsController#suspend undefined method 'suspend_paypal' for User` when trying to cancel the subscription.

In my subscriptions.rb model

class Subscription < ApplicationRecord

  belongs_to :plan
  belongs_to :user
  validates_presence_of :plan_id
  validates_presence_of :user_id
  attr_accessor :paypal_payment_token

def save_with_payment
  if valid?
    if paypal_payment_token.present?
      save_with_paypal_payment
    end
  end

end

def paypal
  PaypalPayment.new(self)
end

def save_with_paypal_payment
  response = paypal.make_recurring
  self.paypal_recurring_profile_token = response.profile_id
  save!
end




def suspend_paypal
  paypal.suspend
  self.status = "canceled"
  save
end



def payment_provided?
paypal_payment_token.present?
end
end

subscriptions_controller

    class SubscriptionsController < ApplicationController
      before_action :authenticate_user! , only: [:new,  :edit]

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

    def index
    end



    def create

      @user = current_user

        @subscription = Subscription.new(subscription_params)
      if @subscription.save_with_payment
        redirect_to root_path, :notice => "Thank you for subscribing"
        @user.premium!
      else
        render :new
      end
    end


    def suspend
     @user = current_user
     @user.suspend_paypal
     flash[:success] = 'Subscription Canceled.'
     @user.free!
     redirect_to root_path
    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


    private

    def subscription_params
      params.require(:subscription).permit!
    end

end

paypal_payment.rb

  class PaypalPayment
def initialize(subscription)
    @subscription = subscription
  end

def checkout_details
process :checkout_details
end


def payment_active?
  self.status = "active"
  end

  def suspend
       process :suspend, :profile_id => @subscription.paypal_recurring_profile_token
   end
def checkout_url(options)
  process(:checkout, options).checkout_url
end

def save_with_paypal_payment
  response = paypal.make_recurring
  self.paypal_recurring_profile_token = response.profile_id
  save!

end



def make_recurring
  process :request_payment
  process :create_recurring_profile, period: :monthly, frequency: 1, start_at: Time.zone.now
end

private

def process(action, options = {})
  options = options.reverse_merge(
  token: @subscription.paypal_payment_token,
  payer_id: @subscription.paypal_cutomer_token,
  description: @subscription.plan.name,
  amount: @subscription.plan.price,
  currency: "USD"



  )
  response = PayPal::Recurring.new(options).send(action)
  raise response.errors.inspect if response.errors.present?
  response


end

end

View

<%= link_to "Suspend paypal", subscriptions_suspend_path, :data => { :confirm => "Are you sure?" } %>

Upvotes: 1

Views: 95

Answers (1)

Andrew Schwartz
Andrew Schwartz

Reputation: 4657

You have defined suspend_paypal on the Subscription model, but you're trying to call it on user. Get the subscription instead and call the method:

# class SubscriptionsController

def suspend
   @subscription = Subscription.find(params[:id])
   @subscription.suspend_paypal
   flash[:success] = 'Subscription Canceled.'
   current_user.free!
   redirect_to root_path
end

Note -- you should add authorization logic, otherwise any user who can authenticate can suspend an arbitrary subscription by sending its ID. If a user has_many subscriptions, subscription = current_user.subscriptions.find(params[:id]). If has_one, just current_user.subscription and validate that that matches params[:id] for good measure.

You probably also don't need subscription to be an ivar since you're simply redirecting to root. Just replace @subscription with subscription. Use ivars when you need to access those variables in the views.

Upvotes: 3

Related Questions