lib/pesapal/merchant.rb in pesapal-0.0.3 vs lib/pesapal/merchant.rb in pesapal-0.1.0
- old
+ new
@@ -1,34 +1,34 @@
module Pesapal
class Merchant
- attr_accessor :callback_url, :credentials, :order_details
+ attr_accessor :config, :order_details
attr_reader :api_domain, :api_endpoints
def api_domain
@api_domain
end
def api_endpoints
@api_endpoints
end
- def callback_url
- @callback_url
+ def config
+ @config
end
- def credentials
- @credentials
- end
-
def order_details
@order_details
end
private
+ def mode
+ @mode
+ end
+
def params
@params
end
def post_xml
@@ -40,47 +40,36 @@
end
public
# constructor
- def initialize(mode = :development)
+ def initialize(mode = :development, path_to_file = "#{Rails.root}/config/pesapal.yml")
- # convert symbol to string and downcase
- mode.to_s.downcase!
-
# initialize
@params = nil
@post_xml = nil
@token_secret = nil
- # set the credentials from the config (if initializers/pesapal.rb
- # exists they should have set these values)
- @credentials = nil
+ # convert symbol to string and downcase
+ @mode = "#{mode.to_s.downcase}"
- # set the callback url that the iframe will respond to
- @callback_url = 'http://0.0.0.0:3000/pesapal/callback'
+ # set the credentials
+ set_configuration_from_yaml path_to_file
# set api endpoints depending on the mode
- @api_endpoints = {}
- if mode == 'development'
- set_endpoints_development
- elseif mode == 'production'
- set_endpoints_production
- else
- set_endpoints_development
- end
+ set_endpoints
end
# generate pesapal order url (often iframed)
def generate_order_url
# 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) ... this gene
- @params = Pesapal::Post::set_parameters(@callback_url, @credentials[:consumer_key], @post_xml)
+ @params = Pesapal::Post::set_parameters(@credentials[:callback_url], @credentials[:consumer_key], @post_xml)
# generate oauth signature and add signature to the request parameters
@params[:oauth_signature] = Pesapal::Oauth::generate_oauth_signature("GET", @api_endpoints[:postpesapaldirectorderv4], @params, @credentials[:consumer_secret], @token_secret)
# change params (with signature) to a query string
@@ -89,26 +78,62 @@
"#{@api_endpoints[:postpesapaldirectorderv4]}?#{query_string}"
end
private
- # set all endpoint for use in development mode
- def set_endpoints_development
- @api_domain = 'http://demo.pesapal.com'
- set_endpoints @api_domain
+ # set endpoints
+ def set_endpoints
+
+ if @mode == 'production'
+ @api_domain = 'https://www.pesapal.com'
+ else
+ @api_domain = 'http://demo.pesapal.com'
+ end
+
+ @api_endpoints = {}
+ @api_endpoints[:postpesapaldirectorderv4] = "#{@api_domain}/API/PostPesapalDirectOrderV4"
+ @api_endpoints[:querypaymentstatus] = "#{@api_domain}/API/QueryPaymentStatus"
+ @api_endpoints[:querypaymentstatusbymerchantref] = "#{@api_domain}/API/QueryPaymentStatus"
+ @api_endpoints[:querypaymentdetails] = "#{@api_domain}/API/QueryPaymentDetails"
end
- # set all enpoints for use in production mode
- def set_endpoints_production
- @api_domain = "https://www.pesapal.com"
- set_endpoints @api_domain
+ # set credentialts through hash, uses default if nothing is input
+ def set_configuration(consumer_details = {})
+
+ # 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 endpoints
- def set_endpoints(domain_string)
- @api_endpoints[:postpesapaldirectorderv4] = "#{domain_string}/API/PostPesapalDirectOrderV4"
- @api_endpoints[:querypaymentstatus] = "#{domain_string}/API/QueryPaymentStatus"
- @api_endpoints[:querypaymentstatusbymerchantref] = "#{domain_string}/API/QueryPaymentStatus"
- @api_endpoints[:querypaymentdetails] = "#{domain_string}/API/QueryPaymentDetails"
+ # 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