Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы предложил более нативный вариант:

config = CloudPayments::Config.new do |c|
  c.bla_bla_bla ...
end

и CloudPayments::Config.new(hash) и CloudPayments::Config.new(file_name)

ну и комбинированный вариант

config = CloudPayments::Config.new(hash_or_string) do |c|
  c.bla_bla_bla ...
end

То есть если передан hash, то используем его в качестве конфига, если передана строка, то используем ее в как имя YAML файла в котором лежит кофиг

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config = CloudPayments::Config.new 

Не хотелось бы менять API уже существующего класса (который работает до нашего PR). Именно поэтому я добавил отдельный метод configure иначе этот gem потеряет обратную совместимость.

c.host = 'http://localhost:3000' # By default, it is https://api.cloudpayments.ru
c.public_key = ''
c.secret_key = ''
end
```

### Test method

```ruby
Expand All @@ -48,6 +62,8 @@ CloudPayments.client.ping

### Cryptogram-based payments

#### With global configuration:

```ruby
transaction = CloudPayments.client.payments.cards.charge(
amount: 120,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_payments/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/cloud_payments/client/serializer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }
Expand Down
6 changes: 6 additions & 0 deletions lib/cloud_payments/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/cloud_payments/namespaces/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_payments/webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down