yandex-money-client 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +8 -0
  5. data/.yardopts +1 -0
  6. data/CHANGELOG.md +43 -0
  7. data/Gemfile +11 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +159 -0
  10. data/Rakefile +7 -0
  11. data/lib/yandex_money/api.rb +10 -0
  12. data/lib/yandex_money/api/version.rb +5 -0
  13. data/lib/yandex_money/client.rb +56 -0
  14. data/lib/yandex_money/config.rb +26 -0
  15. data/lib/yandex_money/exceptions.rb +71 -0
  16. data/lib/yandex_money/external_payment.rb +81 -0
  17. data/lib/yandex_money/wallet.rb +215 -0
  18. data/spec/account_info_spec.rb +64 -0
  19. data/spec/auth_spec.rb +35 -0
  20. data/spec/cards_spec.rb +55 -0
  21. data/spec/config_spec.rb +50 -0
  22. data/spec/exceptions_spec.rb +55 -0
  23. data/spec/fixtures/vcr_cassettes/accept_incoming_transfer_with_protection_code.yml +40 -0
  24. data/spec/fixtures/vcr_cassettes/accept_incoming_transfer_with_protection_code_with_wrong_code.yml +40 -0
  25. data/spec/fixtures/vcr_cassettes/get_account_info.yml +40 -0
  26. data/spec/fixtures/vcr_cassettes/get_instance_id_fail.yml +38 -0
  27. data/spec/fixtures/vcr_cassettes/get_instance_id_success.yml +38 -0
  28. data/spec/fixtures/vcr_cassettes/get_operation_details.yml +233 -0
  29. data/spec/fixtures/vcr_cassettes/get_operation_history.yml +186 -0
  30. data/spec/fixtures/vcr_cassettes/get_operation_history_with_params.yml +46 -0
  31. data/spec/fixtures/vcr_cassettes/get_token_from_authorization_code.yml +48 -0
  32. data/spec/fixtures/vcr_cassettes/get_wrong_operation_details.yml +40 -0
  33. data/spec/fixtures/vcr_cassettes/init_without_client_secret.yml +843 -0
  34. data/spec/fixtures/vcr_cassettes/initialize_with_token.yml +40 -0
  35. data/spec/fixtures/vcr_cassettes/insufficient_scope_error.yml +40 -0
  36. data/spec/fixtures/vcr_cassettes/invalid_request_error.yml +40 -0
  37. data/spec/fixtures/vcr_cassettes/process_external_payment.yml +114 -0
  38. data/spec/fixtures/vcr_cassettes/process_payment_to_an_account_with_failure.yml +41 -0
  39. data/spec/fixtures/vcr_cassettes/reject_payment.yml +40 -0
  40. data/spec/fixtures/vcr_cassettes/reject_payment_fail.yml +40 -0
  41. data/spec/fixtures/vcr_cassettes/request_external_payment.yml +79 -0
  42. data/spec/fixtures/vcr_cassettes/request_payment_to_an_account_with_failure.yml +41 -0
  43. data/spec/fixtures/vcr_cassettes/success_process_payment_to_an_account.yml +40 -0
  44. data/spec/fixtures/vcr_cassettes/success_request_payment_to_an_account.yml +41 -0
  45. data/spec/fixtures/vcr_cassettes/unauthorized_error.yml +41 -0
  46. data/spec/fixtures/vcr_cassettes/unauthorized_exception.yml +41 -0
  47. data/spec/logger_spec.rb +1 -0
  48. data/spec/payments_spec.rb +80 -0
  49. data/spec/spec_helper.rb +27 -0
  50. data/spec/support/constants.example.rb +13 -0
  51. data/yandex-money-client.gemspec +31 -0
  52. metadata +269 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b73afb2b13858ae3727698d5de80c8f25ada821
4
+ data.tar.gz: 4936ec98237be831dcc118f840e5d8c836ae7416
5
+ SHA512:
6
+ metadata.gz: 406e026ae829d294b3f2dbf33da04c398598a6c4fce325b0c9fa09e0e9f9762918ed42b792c79d32fd57b86c3e70e9522629f0ff2cebf8fa5ef017bcb4792c30
7
+ data.tar.gz: 1aa34078ce103a657061ff1d0161eb13f79ba8f8c9cf45b9266b12dc971303aa52e26d0cb6206c0de9bde9959f860eca66e6539d835c632558ae046a77a8da2b
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .DS_Store
24
+ *.swp
25
+ spec/support/constants.rb
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ matrix:
4
+ include:
5
+ - rvm: 2.3.0
6
+ - rvm: 2.2
7
+ before_install:
8
+ - cp spec/support/constants.example.rb spec/support/constants.rb
@@ -0,0 +1 @@
1
+ --markup markdown
@@ -0,0 +1,43 @@
1
+ # 1.0.5 (April 3, 2015)
2
+
3
+ Release of 1.0.5 version.
4
+
5
+ # 1.0.4pre (December 9, 2014)
6
+
7
+ Now you could set API url through config:
8
+
9
+ ```
10
+ YandexMoney.configure do |config|
11
+ config.money_url = "https://money.yandex.ru"
12
+ config.sp_money_url = "https://sp-money.yandex.ru"
13
+ end
14
+ ```
15
+
16
+ # 1.0.3pre (November 25, 2014)
17
+
18
+ Some API changes - now exceptions raised only for 4xx and 500 error codes, changed returned values.
19
+
20
+ # 1.0.2pre (November 10, 2014)
21
+
22
+ Big refactoring of specs, they are can run without vcr cassettes with success, small fixes in README, list of operations in operations history now are OpenStruct, not hash.
23
+
24
+ # 1.0.1pre (November 7, 2014)
25
+
26
+ Change OpenStruct to RecursiveOpenStruct in all responses.
27
+
28
+ # 1.0.0pre (October 29, 2014)
29
+
30
+ Totally new API (like in other implementations), temporally removed logger, removed `spec/support/constants` from git (for travis now it encrypted).
31
+
32
+ # 0.11.0 (October 14, 2014)
33
+
34
+ Disable handling of HTTP errors inside gem in `process_payment` method.
35
+ Add support for logging.
36
+
37
+ # 0.10.0 (September 20, 2014)
38
+
39
+ Exceptions now is divided by classes, now it lives in `lib/yandex_money/exceptions.rb` for details.
40
+
41
+ # 0.9.5 (September 14, 2014)
42
+
43
+ Changed API for initialization and `obtain_token` method. Now secret key only needed in `obtain_token` method.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :development do
4
+ gem "pry", require: false
5
+ end
6
+
7
+ gemspec
8
+
9
+ group :metrics do
10
+ gem "coveralls", require: false
11
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Maslov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,159 @@
1
+ [![Build Status](https://travis-ci.org/serverscom/yandex-money-client.svg)](https://travis-ci.org/yandex-money/yandex-money-client)
2
+ [![Gem Version](https://badge.fury.io/rb/yandex-money-client.svg)](http://badge.fury.io/rb/yandex-money-client)
3
+ [![Dependency Status](https://gemnasium.com/serverscom/yandex-money-client.svg)](https://gemnasium.com/serverscom/yandex-money-client)
4
+
5
+ # Ruby Yandex.Money API Client
6
+
7
+ This gem is a fork of [Yandex Money SDK](https://github.com/yandex-money/yandex-money-sdk-ruby)
8
+
9
+ ## Requirements
10
+
11
+ Supported ruby versions: 1.9.3, 2.0, 2.1, 2.2, 2.3, jruby, rbx-2
12
+
13
+ ## Links
14
+
15
+ 1. Yandex.Money API page: [Ru](http://api.yandex.ru/money/),
16
+ [En](http://api.yandex.com/money/)
17
+
18
+ ## Getting started
19
+
20
+ ### Installation
21
+
22
+
23
+ Add this line to your Gemfile:
24
+
25
+ ```ruby
26
+ gem 'yandex-money-client'
27
+ ```
28
+
29
+ And then execute:
30
+
31
+ ```
32
+ bundle
33
+ ```
34
+
35
+ Or install it manually with:
36
+
37
+ ```
38
+ gem install yandex-money-client
39
+ ```
40
+
41
+ Next, require it in application:
42
+
43
+ ```ruby
44
+ require 'yandex_money/api'
45
+ ```
46
+
47
+
48
+ ### Payments from the Yandex.Money wallet
49
+
50
+ Using Yandex.Money API requires following steps
51
+
52
+ 1. Obtain token URL and redirect user's browser to Yandex.Money service.
53
+ Note: `client_id`, `redirect_uri`, `client_secret` are constants that you get,
54
+ when [register](https://sp-money.yandex.ru/myservices/new.xml) app in Yandex.Money API.
55
+
56
+ ```ruby
57
+ auth_url = YandexMoney::Wallet.build_obtain_token_url(
58
+ CLIENT_ID,
59
+ REDIRECT_URI,
60
+ "account-info operation-history" # SCOPE
61
+ )
62
+ ```
63
+
64
+ 2. After that, user fills Yandex.Money HTML form and user is redirected back to
65
+ `REDIRECT_URI?code=CODE`.
66
+
67
+ 3. You should immediately exchange `CODE` with `ACCESS_TOKEN`.
68
+
69
+ ```ruby
70
+ access_token = YandexMoney::Wallet.get_access_token(
71
+ CLIENT_ID,
72
+ CODE,
73
+ REDIRECT_URI
74
+ )
75
+ # or, if client secret defined:
76
+ access_token = YandexMoney::Wallet.get_access_token(
77
+ CLIENT_ID,
78
+ CODE,
79
+ REDIRECT_URI,
80
+ CLIENT_SECRET
81
+ )
82
+ ```
83
+
84
+ If `access_token` couldn't be obtained, `YandexMoney::ApiError` expection will be raised.
85
+
86
+
87
+ 4. Now you can use Yandex.Money API.
88
+
89
+ ```ruby
90
+ api = YandexMoney::Wallet.new(access_token)
91
+ account_info = api.account_info
92
+ balance = account_info.balance # and so on
93
+
94
+ request_options = {
95
+ "pattern_id": "p2p",
96
+ "to": "410011161616877",
97
+ "amount_due": "0.02",
98
+ "comment": "test payment comment from yandex-money-python",
99
+ "message": "test payment message from yandex-money-python",
100
+ "label": "testPayment",
101
+ "test_payment": true,
102
+ "test_result": "success"
103
+ };
104
+ request_result = api.request_payment(request_options)
105
+ # check status
106
+
107
+ process_payment = api.process_payment({
108
+ request_id: request_result.request_id,
109
+ })
110
+ # check result
111
+ if process_payment.status == "success"
112
+ # show success page
113
+ else
114
+ # something went wrong
115
+ end
116
+ ```
117
+
118
+ ### Payments from bank cards without authorization
119
+
120
+ 1. Fetch instantce-id(ussually only once for every client. You can store
121
+ result in DB).
122
+
123
+ ```ruby
124
+ instance_id = YandexMoney::ExternalPayment.get_instance_id(CLIENT_ID)
125
+ ```
126
+
127
+ 2. Make request payment
128
+
129
+ ```ruby
130
+ api = YandexMoney::ExternalPayment.new(INSTANCE_ID)
131
+ response = api.request_external_payment({
132
+ pattern_id: "p2p",
133
+ to: "410011285611534",
134
+ amount_due: "1.00",
135
+ message: "test"
136
+ })
137
+ if response.status == "success"
138
+ request_id = response.request_id
139
+ else
140
+ # throw exception
141
+ end
142
+ ```
143
+
144
+ 3. Process the request with process-payment.
145
+
146
+ ```ruby
147
+ api = YandexMoney::ExternalPayment.new(INSTANCE_ID)
148
+ result = api.process_external_payment({
149
+ request_id: REQUEST_ID,
150
+ ext_auth_success_uri: "http://example.com/success",
151
+ ext_auth_fail_uri: "http://example.com/fail"
152
+ })
153
+ # process result according to docs
154
+ ```
155
+
156
+
157
+ ## Running tests
158
+
159
+ Fill values in `spec/support/constants.rb` file (example could be found in `spec/support/constants.example.rb`) and after this just run tests with `bundle exec rake` command.
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,10 @@
1
+ require "recursive-open-struct"
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+
5
+ require "yandex_money/api/version"
6
+ require "yandex_money/config"
7
+ require "yandex_money/exceptions"
8
+ require 'yandex_money/client'
9
+ require "yandex_money/wallet"
10
+ require "yandex_money/external_payment"
@@ -0,0 +1,5 @@
1
+ module YandexMoney
2
+ class Api
3
+ VERSION = "1.0.6".freeze
4
+ end
5
+ end
@@ -0,0 +1,56 @@
1
+ module YandexMoney
2
+ module Client
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def base_uri(base)
9
+ @base_uri = base
10
+ end
11
+
12
+ def default_timeout(timeout)
13
+ @default_timeout = timeout
14
+ end
15
+
16
+ def post(uri, options = {})
17
+ conn = Faraday.new(build_url(uri)) do |conn|
18
+ conn.response :json, :content_type => /\bjson$/
19
+
20
+ conn.adapter Faraday.default_adapter
21
+ end
22
+
23
+ conn.post do |req|
24
+ if options[:headers].is_a?(Hash)
25
+ options[:headers].each do |key, value|
26
+ req[key] = value
27
+ end
28
+ end
29
+
30
+ if options[:body]
31
+ case req.headers['Content-Type']
32
+ when 'application/x-www-form-urlencoded'
33
+ req.body = URI.encode_www_form(options[:body])
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def build_url(uri)
40
+ if @base_uri.nil?
41
+ uri.to_s
42
+ else
43
+ URI.join(@base_uri, uri).to_s
44
+ end
45
+ end
46
+ end
47
+
48
+ class Basement
49
+ include YandexMoney::Client
50
+ end
51
+
52
+ def self.post(*args)
53
+ Basement.post(*args)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,26 @@
1
+ module YandexMoney
2
+ class << self
3
+ attr_writer :config
4
+
5
+ def config
6
+ @config ||= Config.new
7
+ end
8
+
9
+ def configure
10
+ yield(config)
11
+ end
12
+
13
+ def reset_config
14
+ @config = Config.new
15
+ end
16
+ end
17
+
18
+ class Config
19
+ attr_accessor :money_url, :sp_money_url
20
+
21
+ def initialize
22
+ @money_url = "https://money.yandex.ru"
23
+ @sp_money_url = "https://sp-money.yandex.ru"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,71 @@
1
+ module YandexMoney
2
+ class Error < StandardError; end
3
+
4
+ class ErrorWithMessage < Error
5
+ def initialize(error)
6
+ @err = error
7
+ end
8
+
9
+ def message
10
+ if @err["www-authenticate"]
11
+ "#{@err.msg} - #{@err["www-authenticate"]}"
12
+ else
13
+ @err.msg
14
+ end
15
+ end
16
+ end
17
+
18
+ class InsufficientScopeError < ErrorWithMessage; end
19
+
20
+ class InvalidRequestError < ErrorWithMessage; end
21
+
22
+ class UnauthorizedError < ErrorWithMessage; end
23
+
24
+ class ServerError < Error; end
25
+
26
+ class FieldNotSetError < Error
27
+ def initialize(field)
28
+ @field = field
29
+ end
30
+
31
+ def message
32
+ "Field '#{@field}' not set!"
33
+ end
34
+ end
35
+
36
+ class ApiError < Error
37
+ def initialize(msg)
38
+ @msg = msg
39
+ end
40
+
41
+ def message
42
+ @msg.gsub(/_/, " ").capitalize
43
+ end
44
+ end
45
+
46
+ class AcceptTransferError < ApiError
47
+ def initialize(msg, attemps = nil)
48
+ @msg = msg
49
+ @attemps = attemps
50
+ end
51
+
52
+ def message
53
+ if @attemps
54
+ super + ", attemps available: #{@attemps}"
55
+ else
56
+ super
57
+ end
58
+ end
59
+ end
60
+
61
+ class ExternalPaymentProgressError < ApiError
62
+ def initialize(msg, next_retry)
63
+ @msg = msg
64
+ @next_retry = next_retry
65
+ end
66
+
67
+ def message
68
+ super + ", next_retry: #{@next_retry}"
69
+ end
70
+ end
71
+ end