openbill-ruby 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -1
- data/README.md +21 -47
- data/lib/openbill.rb +5 -9
- data/lib/openbill/configuration.rb +8 -0
- data/lib/openbill/database.rb +14 -0
- data/lib/openbill/registry.rb +36 -0
- data/lib/openbill/service.rb +26 -15
- data/lib/openbill/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e596b1c45d1e6de254938e905bbf454058bf7df
|
4
|
+
data.tar.gz: 9b6bfc5f5e89ac5c1479e3f3de6e4aba3ecc1619
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a27ab2112aed4d8f0fbf228d5570cacdb7d1ea71eee4e9927e90e07b90f0dc4e9e4deea520b0dcdaf6dd46552df1abdbb609632051023d2f3817f8890294e55
|
7
|
+
data.tar.gz: 023a1cf6968b662d6337aff8aa0a2b97077a01b9d31601b5b9f5d2a7324af27a4b6396a033c8130a2e92f43f6765ea256e9184cc2bc360b875eb9080cd8c318c
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -20,50 +20,26 @@ Migrate database:
|
|
20
20
|
|
21
21
|
Add database configurartion to `./config/initializers/openbill.rb`
|
22
22
|
|
23
|
-
```ruby
|
24
|
-
Openbill.config.database = ActiveRecord::Base.connection.instance_variable_get('@config');
|
25
|
-
```
|
26
|
-
|
27
23
|
## Usage
|
28
24
|
|
29
25
|
### Создание системных счетов
|
30
26
|
|
31
|
-
|
32
|
-
Рекомендую создать такой сервис для работы с системными счетами:
|
33
|
-
|
34
27
|
```ruby
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
class << self
|
39
|
-
def payments
|
40
|
-
account :payments, 'Счет с которого поступает оплата'
|
41
|
-
end
|
28
|
+
Openbill.config.database =
|
29
|
+
ActiveRecord::Base.connection.instance_variable_get('@config');
|
42
30
|
|
43
|
-
def subscriptions
|
44
|
-
account :subscriptions, 'Абонентская плата'
|
45
|
-
end
|
46
31
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
#
|
51
|
-
def account(path, details)
|
52
|
-
# Создаем uri аккаунта из его названия
|
53
|
-
uri = Openbill.generate_uri NS, path
|
54
|
-
|
55
|
-
Openbill.get_account_by_uri(uri) ||
|
56
|
-
Openbill.create_account(uri, details: details)
|
57
|
-
end
|
58
|
-
end
|
32
|
+
SystemRegistry = Openbill::Registry.new Openbill.current do |registry|
|
33
|
+
registry.define :payments, 'Счет с которого поступает оплата'
|
34
|
+
registry.define :subscriptions, 'Абонентская плата'
|
59
35
|
end
|
60
36
|
```
|
61
37
|
|
62
|
-
Таким образом в
|
38
|
+
Таким образом в системом реестре появляется два счета `SystemRegistry[:payments]` и `SystemRegistry[:subscriptions]`
|
63
39
|
|
64
40
|
### Поддержка биллинга со стороны клиента (автоматическое создание клиентского счета):
|
65
41
|
|
66
|
-
```
|
42
|
+
```ruby
|
67
43
|
module AccountBilling
|
68
44
|
extend ActiveSupport::Concern
|
69
45
|
|
@@ -74,21 +50,7 @@ module AccountBilling
|
|
74
50
|
delegate :amount, to: :billing_account
|
75
51
|
|
76
52
|
def billing_account
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
private
|
81
|
-
|
82
|
-
def account_uri
|
83
|
-
Openbill.generate_uri :accounts, id
|
84
|
-
end
|
85
|
-
|
86
|
-
def find_billing
|
87
|
-
Openbill.current.get_account_by_uri account_uri
|
88
|
-
end
|
89
|
-
|
90
|
-
def attach_billing
|
91
|
-
Openbill.current.create_account account_uri
|
53
|
+
Openbill.current.account([:accounts, id])
|
92
54
|
end
|
93
55
|
end
|
94
56
|
```
|
@@ -102,10 +64,22 @@ end
|
|
102
64
|
|
103
65
|
```ruby
|
104
66
|
Openbill.current.make_transaction(
|
67
|
+
// Счет списания
|
105
68
|
from_account_id: Billing.payment.id,
|
69
|
+
|
70
|
+
// Счет зачисление
|
106
71
|
to_account_id: user.billing_account.id,
|
107
|
-
|
72
|
+
|
73
|
+
// Уникальный идентификатор транзакции
|
74
|
+
uri: Openbill.current.generate_uri(:transactions, 123),
|
75
|
+
|
76
|
+
// Сумма транзакции. Валюта должна совпадать с обоими счетами
|
77
|
+
amount: Money(123, 'RUB'),
|
78
|
+
|
79
|
+
// Не обязательное текстовое описание транзакции
|
108
80
|
details: 'Оплата такая-то',
|
81
|
+
|
82
|
+
// hash с данными транзакции для структурированного поиска в дальнейшем
|
109
83
|
meta: { key: 'value' } // не обязательно
|
110
84
|
)
|
111
85
|
```
|
data/lib/openbill.rb
CHANGED
@@ -4,6 +4,8 @@ module Openbill
|
|
4
4
|
autoload :Account, 'openbill/account'
|
5
5
|
autoload :Transaction, 'openbill/transaction'
|
6
6
|
autoload :Configuration, 'openbill/configuration'
|
7
|
+
autoload :Registry, 'openbill/registry'
|
8
|
+
autoload :Database, 'openbill/database'
|
7
9
|
|
8
10
|
ACCOUNTS_TABLE_NAME = :openbill_accounts
|
9
11
|
TRANSACTIONS_TABLE_NAME = :openbill_transactions
|
@@ -21,16 +23,10 @@ module Openbill
|
|
21
23
|
Configuration.instance
|
22
24
|
end
|
23
25
|
|
24
|
-
def generate_uri(resource, id)
|
25
|
-
"obp://local/#{resource}/#{id}"
|
26
|
-
end
|
27
|
-
|
28
|
-
def create_connection(&block)
|
29
|
-
@create_connection_block = block
|
30
|
-
end
|
31
|
-
|
32
26
|
def current
|
33
|
-
@current
|
27
|
+
return @current if @current
|
28
|
+
|
29
|
+
@current = Openbill::Service.new config
|
34
30
|
end
|
35
31
|
end
|
36
32
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Openbill
|
2
|
+
class Database
|
3
|
+
MAX_CONNECTIONS = 10
|
4
|
+
|
5
|
+
def initialize(db_config)
|
6
|
+
@db_config = db_config
|
7
|
+
@db = Sequel.connect db_config, logger: logger, max_connections: MAX_CONNECTIONS
|
8
|
+
@db.extension :pagination
|
9
|
+
@db.extension :pg_hstore
|
10
|
+
end
|
11
|
+
|
12
|
+
delegate :logger, to: Rails
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Openbill
|
2
|
+
class Registry
|
3
|
+
SYSTEM_NS = :system
|
4
|
+
AccountNotFound = Class.new StandardError
|
5
|
+
|
6
|
+
def initialize(service, system_ns = SYSTEM_NS)
|
7
|
+
fail("Must be a Openbill::Service #{service}") unless service.is_a? Openbill::Service
|
8
|
+
@service = service
|
9
|
+
@system_ns = system_ns
|
10
|
+
@accounts = {}
|
11
|
+
yield self
|
12
|
+
@accounts.freeze
|
13
|
+
return @accounts
|
14
|
+
rescue
|
15
|
+
@system_ns = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
# Находит, или создает аккаунт с указанным именем
|
19
|
+
#
|
20
|
+
def define(name, options)
|
21
|
+
@accounts[name] = service.account([system_ns, name], options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def [](name)
|
25
|
+
@account[name]
|
26
|
+
end
|
27
|
+
|
28
|
+
def find(name)
|
29
|
+
[name] || raise(AccountNotFound, name)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :service, :system_ns
|
35
|
+
end
|
36
|
+
end
|
data/lib/openbill/service.rb
CHANGED
@@ -3,25 +3,42 @@ require 'uri'
|
|
3
3
|
require 'money'
|
4
4
|
|
5
5
|
module Openbill
|
6
|
-
MAX_CONNECTIONS = 10
|
7
|
-
DEFAULT_CURRENCY = 'RUB'.freeze
|
8
6
|
|
9
7
|
class Service
|
10
|
-
|
11
|
-
|
8
|
+
PROTO = 'obp'.freeze
|
9
|
+
HOST = 'local'.freeze
|
10
|
+
|
11
|
+
def initialize(config)
|
12
|
+
@config = config
|
13
|
+
@database = Openbill::Database.new config.database
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_uri(resource, id)
|
17
|
+
"#{PROTO}://#{HOST}/#{resource}/#{id}"
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param uri - uri аккаунта в виде строки: "odp://local/resource/id"
|
21
|
+
# или в виде массива [resource, id]
|
22
|
+
#
|
23
|
+
# @param options - опции применяемые для создания аккаунта (см create_account)
|
24
|
+
#
|
25
|
+
def account(uri, options = {})
|
26
|
+
uri = prepare_uri uri
|
27
|
+
get_account_by_uri(uri) || create_account(uri, options)
|
12
28
|
end
|
13
29
|
|
14
30
|
def get_account_by_uri(uri)
|
31
|
+
uri = prepare_uri uri
|
15
32
|
Openbill::Account[uri: uri]
|
16
33
|
end
|
17
34
|
|
18
|
-
def create_account(uri, currency:
|
35
|
+
def create_account(uri, currency: nil, details: nil, meta: {})
|
19
36
|
uri = prepare_uri uri
|
20
37
|
Openbill::Account.create(
|
21
38
|
uri: uri,
|
22
39
|
details: details,
|
23
40
|
meta: meta,
|
24
|
-
amount_currency: currency
|
41
|
+
amount_currency: currency || config.default_currency
|
25
42
|
)
|
26
43
|
end
|
27
44
|
|
@@ -43,25 +60,19 @@ module Openbill
|
|
43
60
|
|
44
61
|
private
|
45
62
|
|
46
|
-
attr_reader :
|
63
|
+
attr_reader :database, :config
|
47
64
|
|
48
65
|
delegate :logger, to: Rails
|
49
66
|
|
50
67
|
def prepare_amount(amount)
|
51
68
|
return amount if amount.is_a? Money
|
52
|
-
return Money.new(amount,
|
69
|
+
return Money.new(amount, config.default_currency) if amount.is_a? Fixnum
|
53
70
|
raise "amount parameter (#{amount}) must be a Money or a Fixnum"
|
54
71
|
end
|
55
72
|
|
56
73
|
def prepare_uri(uri)
|
74
|
+
uri = generate_uri uri.first, uri.second if uri.is_a? Array
|
57
75
|
URI(uri).to_s # Парсим и валидируем заодно
|
58
76
|
end
|
59
|
-
|
60
|
-
def init_db(db_config)
|
61
|
-
@db_config = db_config
|
62
|
-
@db = Sequel.connect db_config, logger: logger, max_connections: MAX_CONNECTIONS
|
63
|
-
@db.extension :pagination
|
64
|
-
@db.extension :pg_hstore
|
65
|
-
end
|
66
77
|
end
|
67
78
|
end
|
data/lib/openbill/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openbill-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danil Pismenny
|
@@ -73,7 +73,9 @@ files:
|
|
73
73
|
- lib/openbill.rb
|
74
74
|
- lib/openbill/account.rb
|
75
75
|
- lib/openbill/configuration.rb
|
76
|
+
- lib/openbill/database.rb
|
76
77
|
- lib/openbill/engine.rb
|
78
|
+
- lib/openbill/registry.rb
|
77
79
|
- lib/openbill/service.rb
|
78
80
|
- lib/openbill/transaction.rb
|
79
81
|
- lib/openbill/version.rb
|