diff --git a/README.md b/README.md index 0478fa8..ab013f7 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ $ gem install cloud_payments ### Configuration +#### Common usage. Global configuration: + ```ruby CloudPayments.configure do |c| c.host = 'http://localhost:3000' # By default, it is https://api.cloudpayments.ru @@ -39,6 +41,18 @@ CloudPayments.configure do |c| end ``` +use can use CloudPayments.client and other resources with these global config. + +#### Local configuration: + +```ruby +config = CloudPayments::Config.configure do |c| + c.host = 'http://localhost:3000' # By default, it is https://api.cloudpayments.ru + c.public_key = '' + c.secret_key = '' +end +``` + ### Test method ```ruby @@ -48,6 +62,8 @@ CloudPayments.client.ping ### Cryptogram-based payments +#### With global configuration: + ```ruby transaction = CloudPayments.client.payments.cards.charge( amount: 120, @@ -95,6 +111,13 @@ transaction.token # => "a4e67841-abb0-42de-a364-d1d8f9f4b3c0" ``` +#### With local configuration: + +```ruby +client = CloudPayment::Client.new(config) +client.payments.cards.charge(...) +``` + ## Webhooks ```ruby diff --git a/lib/cloud_payments/client.rb b/lib/cloud_payments/client.rb index f635caa..089edf5 100644 --- a/lib/cloud_payments/client.rb +++ b/lib/cloud_payments/client.rb @@ -10,8 +10,8 @@ class Client attr_reader :config, :connection - def initialize - @config = CloudPayments.config + def initialize(config = nil) + @config = config || CloudPayments.config @connection = build_connection end diff --git a/lib/cloud_payments/client/serializer/base.rb b/lib/cloud_payments/client/serializer/base.rb index c977625..4a7cb96 100644 --- a/lib/cloud_payments/client/serializer/base.rb +++ b/lib/cloud_payments/client/serializer/base.rb @@ -40,7 +40,7 @@ def convert_keys_from_api(attributes) def convert_keys_to_api(attributes) attributes.each_with_object({}) do |(key, value), result| - value = convert_keys_to_api(value) if value.is_a?(Hash) + value = convert_keys_to_api(value) if value.is_a?(Hash) && (key.to_s != 'json_data') key = key.to_s.gsub(/^[a-z\d]*/){ $&.capitalize } key.gsub!(/(?:_|(\/))([a-z\d]*)/i){ "#{$1}#{$2.capitalize}" } diff --git a/lib/cloud_payments/config.rb b/lib/cloud_payments/config.rb index c453df3..286f51a 100644 --- a/lib/cloud_payments/config.rb +++ b/lib/cloud_payments/config.rb @@ -12,6 +12,12 @@ class Config logger } + def self.configure + config = new + yield config + config + end + def initialize @log = false @serializer = Client::Serializer::MultiJson.new(self) diff --git a/lib/cloud_payments/namespaces/base.rb b/lib/cloud_payments/namespaces/base.rb index f16e974..0e0fc4a 100644 --- a/lib/cloud_payments/namespaces/base.rb +++ b/lib/cloud_payments/namespaces/base.rb @@ -36,7 +36,9 @@ def raise_gateway_error(body) end def raise_reasoned_gateway_error(body) - fail Client::GATEWAY_ERRORS[body[:model][:reason_code]].new(body) if reason_present?(body) + return unless reason_present?(body) + error_class = Client::GATEWAY_ERRORS[body[:model][:reason_code]] + fail error_class.new(body) if error_class end def raise_raw_gateway_error(body) diff --git a/lib/cloud_payments/webhooks.rb b/lib/cloud_payments/webhooks.rb index d5ef61c..b27df4a 100644 --- a/lib/cloud_payments/webhooks.rb +++ b/lib/cloud_payments/webhooks.rb @@ -8,8 +8,8 @@ class HMACError < StandardError; end attr_reader :config - def initialize - @config = CloudPayments.config + def initialize(config = nil) + @config = config || CloudPayments.config @digest = OpenSSL::Digest.new('sha256') @serializer = Client::Serializer::Base.new(config) end