lib/pesapal/merchant.rb in pesapal-1.3.0 vs lib/pesapal/merchant.rb in pesapal-1.5.0

- old
+ new

@@ -20,55 +20,41 @@ def api_endpoints @api_endpoints end - def mode - @mode + def env + @env end def params - @params + @params ||= nil end def post_xml - @post_xml + @post_xml ||= nil end def token_secret - @token_secret + @token_secret ||= nil end public # constructor - def initialize(mode = Rails.env, path_to_file = nil) - - # initialize - @params = nil - @post_xml = nil - @token_secret = nil - - set_mode mode - - # set the credentials if we have specified a path from which we - # will access a YAML file with the configurations - unless path_to_file.nil? - set_configuration_from_yaml path_to_file + def initialize(env = :auto) + set_env env + if defined?(Rails) + set_configuration Rails.application.config.pesapal_credentials + else + set_configuration end - end # generate pesapal order url (often iframed) def generate_order_url - # check if the config is empty, if yes, we try load what was set by the - # initializer into Pesapal.config - if config.empty? - set_configuration Pesapal.config[@mode] - end - # build xml with input data, the format is standard so no editing is # required @post_xml = Pesapal::Post::generate_post_xml @order_details # initialize setting of @params (oauth_signature left empty) @@ -84,16 +70,10 @@ end # query the details of the transaction def query_payment_details(merchant_reference, transaction_tracking_id) - # check if the config is empty, if yes, we try load what was set by the - # initializer into Pesapal.config - if config.empty? - set_configuration Pesapal.config[@mode] - end - # initialize setting of @params (oauth_signature left empty) @params = Pesapal::Details::set_parameters(@config[:consumer_key], merchant_reference, transaction_tracking_id) # generate oauth signature and add signature to the request parameters @params[:oauth_signature] = Pesapal::Oauth::generate_oauth_signature("GET", @api_endpoints[:querypaymentdetails], @params, @config[:consumer_secret], @token_secret) @@ -113,16 +93,10 @@ end # query the status of the transaction def query_payment_status(merchant_reference, transaction_tracking_id = nil) - # check if the config is empty, if yes, we try load what was set by the - # initializer into Pesapal.config - if config.empty? - set_configuration Pesapal.config[@mode] - end - # initialize setting of @params (oauth_signature left empty) @params = Pesapal::Status::set_parameters(@config[:consumer_key], merchant_reference, transaction_tracking_id) # generate oauth signature and add signature to the request parameters @params[:oauth_signature] = Pesapal::Oauth::generate_oauth_signature("GET", @api_endpoints[:querypaymentstatus], @params, @config[:consumer_secret], @token_secret) @@ -136,17 +110,21 @@ # return the string result of what we want response["pesapal_response_data"][0] end - # set mode when called - def set_mode(mode = Rails.env) - - # convert symbol to string and downcase - @mode = mode.to_s.downcase - - # set api endpoints depending on the mode + # set env when called + def set_env(env = :auto) + env = env.to_s.downcase + if env == 'development' + @env = 'development' + elsif env == 'production' + @env = 'production' + else + @env = 'development' + @env = Rails.env if defined?(Rails) + end set_endpoints end # listen to ipn response def ipn_listener(notification_type, merchant_reference, transaction_tracking_id) @@ -169,11 +147,11 @@ private # set endpoints def set_endpoints - if @mode == 'production' + if @env == 'production' @api_domain = 'https://www.pesapal.com' else @api_domain = 'http://demo.pesapal.com' end @@ -188,39 +166,13 @@ # set the configuration @config = { :callback_url => 'http://0.0.0.0:3000/pesapal/callback', :consumer_key => '<YOUR_CONSUMER_KEY>', :consumer_secret => '<YOUR_CONSUMER_SECRET>' - } + } valid_config_keys = @config.keys consumer_details.each { |k,v| @config[k.to_sym] = v if valid_config_keys.include? k.to_sym } - end - - # set configuration through yaml file - def set_configuration_from_yaml(path_to_file) - - if File.exist?(path_to_file) - - # load file, read it and parse the YAML - begin - loaded_config = YAML::load(IO.read(path_to_file)) - rescue Errno::ENOENT - logger.info("YAML configuration file couldn't be found. Using defaults."); return - rescue Psych::SyntaxError - logger.info("YAML configuration file contains invalid syntax. Using defaults."); return - end - - # pick the correct settings depending on the the mode and set it - # appropriately. this file is expected to have the settings for - # development and production - set_configuration loaded_config[@mode] - - else - - # in this case default values will be set - set_configuration - end end end end