From 36ee07087025b2af42d5ec35f2eb6cceeadb0d2f Mon Sep 17 00:00:00 2001 From: Alexey Lapitsky Date: Sun, 2 Nov 2014 18:50:38 +0100 Subject: [PATCH 1/6] no monkeypatching --- lib/okpay.rb | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/okpay.rb b/lib/okpay.rb index a10fac3..d29e5c1 100644 --- a/lib/okpay.rb +++ b/lib/okpay.rb @@ -1,23 +1,21 @@ require 'savon' -module Okpay +module Okpay class API class Client - - class ::String - def camelize - self.gsub!(/^[a-z]|[\s_]+[a-z]/) { |a| a.upcase } - self.gsub!(/[\s_]/, '') - self - end + + def okpay_camelize(str) + result = str.gsub(/^[a-z]|[\s_]+[a-z]/) { |a| a.upcase } + result = result.gsub!(/[\s_]/, '') + result end - + DEFAULTS = { :wsdl => File.join(File.dirname(File.expand_path(__FILE__)), '../config/wsdl.xml') } - + METHODS = { - :get_date_time => [], + :get_date_time => [], :wallet_get_balance => [], :wallet_get_currency_balance => [:currency], :send_money => [:reciever, :currency, :amount, :comment, :is_receiver_pays_fees, :invoice], @@ -28,34 +26,34 @@ def camelize :withdraw_to_ecurrency => [:payment_method, :pay_system_account, :amount, :currency, :fees_from_amount, :invoice], :withdraw_to_ecurrency_calculate => [:payment_method, :amount, :currency, :fees_from_amount] } - + def initialize(wallet_id, api_key, options={}) @api_key = api_key.strip @wallet_id = wallet_id.strip @config = DEFAULTS.merge! options @soap_client = Savon.client(wsdl: @config[:wsdl]) end - + METHODS.each_pair do |method,args| class_eval %{ def #{method}(#{args.join(',')}) message = {"WalletID" => @wallet_id, "SecurityToken" => security_token} #{args}.each do |arg| - message[arg.to_s.camelize] = eval arg.to_s + message[okpay_camelize(arg.to_s)] = eval arg.to_s end response = @soap_client.call(:#{method}, message: message) response.body[:#{method}_response][:#{method}_result] end } end - + private - + def security_token okpay_timestamp = Time.now.utc.strftime('%Y%m%d:%H') Digest::SHA256.hexdigest("#{@api_key}:#{okpay_timestamp}").to_s.upcase end - + end end end From 4bf0d6b5eede3aec888884fa3e8c43abd96decfe Mon Sep 17 00:00:00 2001 From: Alexey Lapitsky Date: Tue, 4 Nov 2014 21:49:59 +0100 Subject: [PATCH 2/6] update wsdl.xml --- config/wsdl.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/wsdl.xml b/config/wsdl.xml index f2a69c8..1071055 100644 --- a/config/wsdl.xml +++ b/config/wsdl.xml @@ -1 +1 @@ -481632 \ No newline at end of file +481632 \ No newline at end of file From 067463216ea2bfa10a3019fcabc750cdf264163f Mon Sep 17 00:00:00 2001 From: Alexey Lapitsky Date: Tue, 4 Nov 2014 22:18:01 +0100 Subject: [PATCH 3/6] fix okpay_camelize --- lib/okpay.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/okpay.rb b/lib/okpay.rb index d29e5c1..563ed49 100644 --- a/lib/okpay.rb +++ b/lib/okpay.rb @@ -6,7 +6,7 @@ class Client def okpay_camelize(str) result = str.gsub(/^[a-z]|[\s_]+[a-z]/) { |a| a.upcase } - result = result.gsub!(/[\s_]/, '') + result = result.gsub(/[\s_]/, '') result end From 0b6792307b543cdcb971ec6598dce7855b0b8298 Mon Sep 17 00:00:00 2001 From: Alexey Lapitsky Date: Tue, 4 Nov 2014 22:27:02 +0100 Subject: [PATCH 4/6] fix a typo --- lib/okpay.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/okpay.rb b/lib/okpay.rb index 563ed49..06dea20 100644 --- a/lib/okpay.rb +++ b/lib/okpay.rb @@ -18,7 +18,7 @@ def okpay_camelize(str) :get_date_time => [], :wallet_get_balance => [], :wallet_get_currency_balance => [:currency], - :send_money => [:reciever, :currency, :amount, :comment, :is_receiver_pays_fees, :invoice], + :send_money => [:receiver, :currency, :amount, :comment, :is_receiver_pays_fees, :invoice], :account_check => [:account], :transaction_get => [:txn_id, :invoice], :transaction_history => [:from, :till, :page_size, :page_number], From bf1b48ca9619ed1133fd01414b9cc1f428fb3bdc Mon Sep 17 00:00:00 2001 From: Alexey Lapitsky Date: Wed, 5 Nov 2014 07:41:01 +0100 Subject: [PATCH 5/6] add account_check --- lib/okpay.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/okpay.rb b/lib/okpay.rb index 06dea20..1e093d1 100644 --- a/lib/okpay.rb +++ b/lib/okpay.rb @@ -15,6 +15,7 @@ def okpay_camelize(str) } METHODS = { + :account_check => [:account], :get_date_time => [], :wallet_get_balance => [], :wallet_get_currency_balance => [:currency], From b1f3e92d9dd559792295fb880af567c5a286b20d Mon Sep 17 00:00:00 2001 From: Alexey Lapitsky Date: Wed, 5 Nov 2014 08:36:39 +0100 Subject: [PATCH 6/6] improve initialization --- lib/okpay.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/okpay.rb b/lib/okpay.rb index 1e093d1..f920ff0 100644 --- a/lib/okpay.rb +++ b/lib/okpay.rb @@ -3,7 +3,6 @@ module Okpay class API class Client - def okpay_camelize(str) result = str.gsub(/^[a-z]|[\s_]+[a-z]/) { |a| a.upcase } result = result.gsub(/[\s_]/, '') @@ -28,14 +27,20 @@ def okpay_camelize(str) :withdraw_to_ecurrency_calculate => [:payment_method, :amount, :currency, :fees_from_amount] } - def initialize(wallet_id, api_key, options={}) - @api_key = api_key.strip - @wallet_id = wallet_id.strip + class_attribute :wallet_id, :api_key + + def initialize(options = {}) + @api_key = options[:api_key] || api_key + @wallet_id = options[:wallet_id] || wallet_id + fail('OKPAY api_key is not configured') unless @api_key + fail('OKPAY wallet_id is not configured') unless @wallet_id + @api_key.strip! + @wallet_id.strip! @config = DEFAULTS.merge! options @soap_client = Savon.client(wsdl: @config[:wsdl]) end - METHODS.each_pair do |method,args| + METHODS.each_pair do |method, args| class_eval %{ def #{method}(#{args.join(',')}) message = {"WalletID" => @wallet_id, "SecurityToken" => security_token} @@ -54,7 +59,6 @@ def security_token okpay_timestamp = Time.now.utc.strftime('%Y%m%d:%H') Digest::SHA256.hexdigest("#{@api_key}:#{okpay_timestamp}").to_s.upcase end - end end end