0

I have this service class in RoR:

class PaypalService
  attr_reader :api  

  def initialize()        
    @api = PayPal::SDK::Merchant::API.new
    puts "PaypalService initilized"   
  end 


  # Paypal setExpressCheckout
  def self.setExpressCheckout(billingType, returnURL, cancelURL, amount, description, isRecurring, locale)     

    new                        

    if billingType == "credit-card"
      billingType = "Billing"
    else 
      billingType = "Login"
    end

    if isRecurring    
      varSetExpressCheckoutRequestDetails = createSetExpressCheckoutRecurringRequestDetails(billingType, returnURL, cancelURL, amount, description, isRecurring, locale)
    else
      varSetExpressCheckoutRequestDetails = createSetExpressCheckoutRequestDetails(billingType, returnURL, cancelURL, amount, description, isRecurring, locale)
    end

    @set_express_checkout = @api.build_set_express_checkout(varSetExpressCheckoutRequestDetails)
...

My call to the service is:

I want to initilize the @api attribute and use it whenever I execute one service method. This is my call from the client controller:

expressCheckoutResponse = PaypalService.setExpressCheckout(params[:paymentType], 
                                                             "http://" + Yanpy::IP + "/app/index.html#/boat-booking-payment", 
                                                             "http://" + Yanpy::IP + "/app/index.html#/boat-booking", 
                                                             totalPrice,                                                  
                                                             description,
                                                             false,
                                                             'ES')

Log output:

PaypalService initilized
Completed 500 Internal Server Error in 625ms

NoMethodError (undefined method `build_set_express_checkout' for nil:NilClass):
  lib/paypal_service.rb:30:in `setExpressCheckout'
  app/controllers/bookings_controller.rb:275:in `create'

What am I missing?

1 Answer 1

1

def self.setExpressCheckout this is singleton method of the class PaypalService. Inside of which, @api is an instance variable, you are trying to access, is actually the instance varoable of the class PaypalService, not the instance variable of the instances PaypalService. As you didn't create any object of the class PaypalService, thus @api, which you are trying to aceess still not created, and eve-if you create it, you wouldn't be able to access it from the singleton methods of the class PaypalService, like you did.

Example :

class Foo
  @x = 10
  def self.class_instance_var
    puts @x
  end
  def set_instance_var
    @x = 12
  end
  def instance_var_of_instance
     puts @x
  end
end

foo = Foo.new
foo.set_instance_var
foo.instance_var_of_instance # => 12
Foo.class_instance_var # => 10

What you see, @x is an instance variable, of-course. But one is class Foos instance variable, and the other is instances of Foo, like foos instance variable. Name is same, bot those 2 @x has 2 different owners.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.