current_user
current_user

Reputation: 1212

include in Rails Controller

I was trying Ruby on Rails integration with the Authorize.Net API from https://github.com/AuthorizeNet/sample-code-ruby/blob/master/AcceptSuite/create-an-accept-payment-transaction.rb. When I try to run this ruby code in a method of a custom controller

   class PaymentController < ApplicationController

     def create_an_accept_payment_transaction
         include AuthorizeNet::API
         config = YAML.load_file(File.dirname(__FILE__) + "/../credentials.yml")

         transaction = Transaction.new(config['api_login_id'], config['api_transaction_key'], :gateway => :sandbox)

         request = CreateTransactionRequest.new

        ...............
        if response.messages.resultCode == MessageTypeEnum::Ok
        ................
     end
   end

I was getting the following errors

NoMethodError (undefined method `include' for #<PaymentController:0x000000000296d118>)

and

 NameError (uninitialized constant PaymentController::MessageTypeEnum
 Did you mean?  MessagePack):

But the same ruby code is working without any issue in a method in an ActiveRecord model.WHY ?

class Payment < ApplicationRecord

def self.payment_transaction(token, amount)
      require 'yaml'
      require 'authorizenet' 
      require 'securerandom'
      include AuthorizeNet::API
      config = YAML.load_file(File.dirname(__FILE__) + "/../credentials.yml")
      transaction = Transaction.new(config['api_login_id'], config['api_transaction_key'], :gateway => :sandbox)
     request = CreateTransactionRequest.new
      request.transactionRequest = TransactionRequestType.new()
    ..................
 end
 end    

I could also run this ruby code without any issues with

    $ ruby create-an-accept-payment-transaction.rb

Upvotes: 0

Views: 109

Answers (1)

eikes
eikes

Reputation: 5071

The payment_transaction is a class method, whereas create_an_accept_payment_transaction is an instance method. The include AuthorizeNet::API call works in the class not in the instance.

Try this instead:

require 'authorizenet' 
class PaymentController < ApplicationController  
  include AuthorizeNet::API
end

Upvotes: 2

Related Questions