From 89da57c0606fc3f314e2c18339aa06676ff13238 Mon Sep 17 00:00:00 2001 From: Danil Pismenny Date: Mon, 28 Aug 2017 19:24:40 +0300 Subject: [PATCH 1/7] Local configuration --- README.md | 23 +++++++++++++++++++++++ lib/cloud_payments/client.rb | 4 ++-- lib/cloud_payments/config.rb | 6 ++++++ lib/cloud_payments/webhooks.rb | 4 ++-- 4 files changed, 33 insertions(+), 4 deletions(-) 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/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/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 From 85d7abd3f3fad5e2fd9df3c07179d82c1a323cb0 Mon Sep 17 00:00:00 2001 From: Danil Pismenny Date: Fri, 22 Dec 2017 18:57:07 +0300 Subject: [PATCH 2/7] Fix: Return reasoned gateway error only when it is exists --- lib/cloud_payments/namespaces/base.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) From 2f141eb31198f942651eb6232e1a111efeb3ca29 Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Wed, 4 Apr 2018 16:17:38 +0300 Subject: [PATCH 3/7] Update base.rb --- lib/cloud_payments/client/serializer/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cloud_payments/client/serializer/base.rb b/lib/cloud_payments/client/serializer/base.rb index c977625..f3f154e 100644 --- a/lib/cloud_payments/client/serializer/base.rb +++ b/lib/cloud_payments/client/serializer/base.rb @@ -40,8 +40,8 @@ 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 != :data_json) + key = key.to_s.gsub(/^[a-z\d]*/){ $&.capitalize } key.gsub!(/(?:_|(\/))([a-z\d]*)/i){ "#{$1}#{$2.capitalize}" } key.gsub!('/', '::') From 1d21f6e9a8ea35e71baba4e60cc195824cba3dad Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Wed, 4 Apr 2018 16:33:32 +0300 Subject: [PATCH 4/7] Update base.rb --- lib/cloud_payments/client/serializer/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cloud_payments/client/serializer/base.rb b/lib/cloud_payments/client/serializer/base.rb index f3f154e..4a7cb96 100644 --- a/lib/cloud_payments/client/serializer/base.rb +++ b/lib/cloud_payments/client/serializer/base.rb @@ -40,8 +40,8 @@ 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) && (key != :data_json) - + 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}" } key.gsub!('/', '::') From 462aad134deb8061b999f2bdac8af88ad4aca9e9 Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Wed, 4 Apr 2018 17:47:10 +0300 Subject: [PATCH 5/7] Update base.rb --- lib/cloud_payments/client/serializer/base.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cloud_payments/client/serializer/base.rb b/lib/cloud_payments/client/serializer/base.rb index 4a7cb96..331d93b 100644 --- a/lib/cloud_payments/client/serializer/base.rb +++ b/lib/cloud_payments/client/serializer/base.rb @@ -40,7 +40,13 @@ 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) && (key.to_s != 'json_data') + if value.is_a?(Hash) + if key.to_s == 'json_data' + value = MultiJson.dump(value) + else + value = convert_keys_to_api(value) && () + end + end key = key.to_s.gsub(/^[a-z\d]*/){ $&.capitalize } key.gsub!(/(?:_|(\/))([a-z\d]*)/i){ "#{$1}#{$2.capitalize}" } From d1540c510bddc62581b8e4da12e0d3755e0c8d0a Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Wed, 4 Apr 2018 17:50:40 +0300 Subject: [PATCH 6/7] Update base.rb --- lib/cloud_payments/client/serializer/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cloud_payments/client/serializer/base.rb b/lib/cloud_payments/client/serializer/base.rb index 331d93b..50eb335 100644 --- a/lib/cloud_payments/client/serializer/base.rb +++ b/lib/cloud_payments/client/serializer/base.rb @@ -42,9 +42,9 @@ def convert_keys_to_api(attributes) attributes.each_with_object({}) do |(key, value), result| if value.is_a?(Hash) if key.to_s == 'json_data' - value = MultiJson.dump(value) + value = ::MultiJson.dump(value) else - value = convert_keys_to_api(value) && () + value = convert_keys_to_api(value) end end From 0605a275b597cc290f4da7d7ac1ec3457900b9b8 Mon Sep 17 00:00:00 2001 From: Alex Zavgorodnev Date: Thu, 5 Apr 2018 14:43:44 +0300 Subject: [PATCH 7/7] Update base.rb --- lib/cloud_payments/client/serializer/base.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/cloud_payments/client/serializer/base.rb b/lib/cloud_payments/client/serializer/base.rb index 50eb335..4a7cb96 100644 --- a/lib/cloud_payments/client/serializer/base.rb +++ b/lib/cloud_payments/client/serializer/base.rb @@ -40,13 +40,7 @@ def convert_keys_from_api(attributes) def convert_keys_to_api(attributes) attributes.each_with_object({}) do |(key, value), result| - if value.is_a?(Hash) - if key.to_s == 'json_data' - value = ::MultiJson.dump(value) - else - value = convert_keys_to_api(value) - end - end + 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}" }