yandex-money-sdk 0.11.0 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +9 -6
- data/.yardopts +1 -0
- data/CHANGELOG.md +33 -1
- data/README.md +22 -26
- data/constants.rb.enc +0 -0
- data/lib/yandex_money/api.rb +3 -185
- data/lib/yandex_money/api/version.rb +1 -1
- data/lib/yandex_money/config.rb +26 -0
- data/lib/yandex_money/exceptions.rb +14 -4
- data/lib/yandex_money/external_payment.rb +78 -0
- data/lib/yandex_money/wallet.rb +215 -0
- data/spec/account_info_spec.rb +15 -14
- data/spec/auth_spec.rb +14 -41
- data/spec/cards_spec.rb +20 -18
- data/spec/config_spec.rb +50 -0
- data/spec/exceptions_spec.rb +55 -0
- data/spec/fixtures/vcr_cassettes/accept_incoming_transfer_with_protection_code.yml +3 -5
- data/spec/fixtures/vcr_cassettes/accept_incoming_transfer_with_protection_code_with_wrong_code.yml +6 -8
- data/spec/fixtures/vcr_cassettes/get_account_info.yml +5 -5
- data/spec/fixtures/vcr_cassettes/get_instance_id_fail.yml +2 -840
- data/spec/fixtures/vcr_cassettes/get_instance_id_success.yml +4 -840
- data/spec/fixtures/vcr_cassettes/get_operation_details.yml +199 -16
- data/spec/fixtures/vcr_cassettes/get_operation_history.yml +149 -150
- data/spec/fixtures/vcr_cassettes/get_operation_history_with_params.yml +9 -9
- data/spec/fixtures/vcr_cassettes/get_token_from_authorization_code.yml +15 -842
- data/spec/fixtures/vcr_cassettes/get_wrong_operation_details.yml +4 -4
- data/spec/fixtures/vcr_cassettes/init_without_client_secret.yml +719 -711
- data/spec/fixtures/vcr_cassettes/initialize_with_token.yml +7 -878
- data/spec/fixtures/vcr_cassettes/insufficient_scope_error.yml +40 -0
- data/spec/fixtures/vcr_cassettes/invalid_request_error.yml +40 -0
- data/spec/fixtures/vcr_cassettes/process_external_payment.yml +80 -6
- data/spec/fixtures/vcr_cassettes/process_payment_to_an_account_with_failure.yml +4 -6
- data/spec/fixtures/vcr_cassettes/reject_payment.yml +3 -5
- data/spec/fixtures/vcr_cassettes/reject_payment_fail.yml +3 -5
- data/spec/fixtures/vcr_cassettes/request_external_payment.yml +43 -10
- data/spec/fixtures/vcr_cassettes/request_payment_to_an_account_with_failure.yml +5 -7
- data/spec/fixtures/vcr_cassettes/success_process_payment_to_an_account.yml +6 -8
- data/spec/fixtures/vcr_cassettes/success_request_payment_to_an_account.yml +7 -9
- data/spec/fixtures/vcr_cassettes/unauthorized_error.yml +41 -0
- data/spec/fixtures/vcr_cassettes/unauthorized_exception.yml +3 -3
- data/spec/logger_spec.rb +0 -18
- data/spec/payments_spec.rb +20 -30
- data/spec/spec_helper.rb +12 -3
- data/spec/support/constants.example.rb +13 -0
- data/yandex-money-sdk.gemspec +2 -1
- metadata +42 -18
- data/lib/yandex_money/logger/default.rb +0 -17
- data/lib/yandex_money/logger/empty.rb +0 -10
- data/spec/fixtures/vcr_cassettes/init_with_client_secret.yml +0 -839
- data/spec/fixtures/vcr_cassettes/obtain_token_for_get_account_info.yml +0 -877
- data/spec/fixtures/vcr_cassettes/obtain_token_for_making_payments_to_an_account.yml +0 -875
- data/spec/fixtures/vcr_cassettes/obtain_token_for_payments_from_bank_cards_without_authorization.yml +0 -839
- data/spec/fixtures/vcr_cassettes/token_with_client_secret.yml +0 -875
- data/spec/support/constants.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 478c50bf64ad5fb25f23a2729b39f102f76e14af
|
4
|
+
data.tar.gz: 22ef302f42e010a897513e38bcbfb7035655fc42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 03fa5e4c1a9c14b3ccee965056f963fe2e99e9d43cfef5ac9fbd64c51caeae9ac8696bc90f59da4ec93312b71f45413a098774a5b963ad644497f291d1a23091
|
7
|
+
data.tar.gz: 29b8a373243901e0ab736c3fd81845c5138e04fbaf2637b05fa31d45b0f717cabf4ef3812cae448718b21d0c7d94cb889774192e1b41a19ab8ac8a927863be34
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,13 +1,16 @@
|
|
1
1
|
language: ruby
|
2
|
-
script:
|
2
|
+
script: rake
|
3
3
|
rvm:
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0.0
|
6
|
+
- 2.1.0
|
7
7
|
matrix:
|
8
8
|
include:
|
9
|
-
|
10
|
-
|
9
|
+
- rvm: jruby-19mode
|
10
|
+
- rvm: rbx-2
|
11
11
|
fast_finish: true
|
12
12
|
branches:
|
13
13
|
only: master
|
14
|
+
before_install:
|
15
|
+
- openssl aes-256-cbc -K $encrypted_b75809a34a66_key -iv $encrypted_b75809a34a66_iv
|
16
|
+
-in constants.rb.enc -out spec/support/constants.rb -d
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,38 @@
|
|
1
|
-
# 0.
|
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)
|
2
33
|
|
3
34
|
Disable handling of HTTP errors inside gem in `process_payment` method.
|
35
|
+
Add support for logging.
|
4
36
|
|
5
37
|
# 0.10.0 (September 20, 2014)
|
6
38
|
|
data/README.md
CHANGED
@@ -54,12 +54,11 @@ Note: `client_id`, `redirect_uri`, `client_secret` are constants that you get,
|
|
54
54
|
when [register](https://sp-money.yandex.ru/myservices/new.xml) app in Yandex.Money API.
|
55
55
|
|
56
56
|
```ruby
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
57
|
+
auth_url = YandexMoney::Wallet.build_obtain_token_url(
|
58
|
+
CLIENT_ID,
|
59
|
+
REDIRECT_URI,
|
60
|
+
"account-info operation-history" # SCOPE
|
61
61
|
)
|
62
|
-
auth_url = api.client_url
|
63
62
|
```
|
64
63
|
|
65
64
|
2. After that, user fills Yandex.Money HTML form and user is redirected back to
|
@@ -68,21 +67,27 @@ when [register](https://sp-money.yandex.ru/myservices/new.xml) app in Yandex.Mon
|
|
68
67
|
3. You should immediately exchange `CODE` with `ACCESS_TOKEN`.
|
69
68
|
|
70
69
|
```ruby
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
access_token = YandexMoney::Wallet.get_access_token(
|
71
|
+
CLIENT_ID,
|
72
|
+
CODE,
|
73
|
+
REDIRECT_URI
|
75
74
|
)
|
76
|
-
api.code = params[:code] # obtained code
|
77
|
-
access_token = api.obtain_token
|
78
75
|
# or, if client secret defined:
|
79
|
-
|
76
|
+
access_token = YandexMoney::Wallet.get_access_token(
|
77
|
+
CLIENT_ID,
|
78
|
+
CODE,
|
79
|
+
REDIRECT_URI,
|
80
|
+
CLIENT_SECRET
|
81
|
+
)
|
80
82
|
```
|
81
83
|
|
84
|
+
If `access_token` couldn't be obtained, `YandexMoney::ApiError` expection will be raised.
|
85
|
+
|
86
|
+
|
82
87
|
4. Now you can use Yandex.Money API.
|
83
88
|
|
84
89
|
```ruby
|
85
|
-
api = YandexMoney::
|
90
|
+
api = YandexMoney::Wallet.new(access_token)
|
86
91
|
account_info = api.account_info
|
87
92
|
balance = account_info.balance # and so on
|
88
93
|
|
@@ -116,22 +121,13 @@ when [register](https://sp-money.yandex.ru/myservices/new.xml) app in Yandex.Mon
|
|
116
121
|
result in DB).
|
117
122
|
|
118
123
|
```ruby
|
119
|
-
|
120
|
-
response = api.get_instance_id # returns string, contains instance id
|
121
|
-
if reponse.status == "success"
|
122
|
-
instance_id = response.instance_id
|
123
|
-
else
|
124
|
-
# throw exception
|
125
|
-
end
|
124
|
+
instance_id = YandexMoney::ExternalPayment.get_instance_id(CLIENT_ID)
|
126
125
|
```
|
127
126
|
|
128
127
|
2. Make request payment
|
129
128
|
|
130
129
|
```ruby
|
131
|
-
api = YandexMoney::
|
132
|
-
client_id: CLIENT_ID,
|
133
|
-
instance_id: INSTANCE_ID
|
134
|
-
)
|
130
|
+
api = YandexMoney::ExternalPayment.new(INSTANCE_ID)
|
135
131
|
response = api.request_external_payment({
|
136
132
|
pattern_id: "p2p",
|
137
133
|
to: "410011285611534",
|
@@ -148,7 +144,7 @@ result in DB).
|
|
148
144
|
3. Process the request with process-payment.
|
149
145
|
|
150
146
|
```ruby
|
151
|
-
api = YandexMoney::
|
147
|
+
api = YandexMoney::ExternalPayment.new(INSTANCE_ID)
|
152
148
|
result = api.process_external_payment({
|
153
149
|
request_id: REQUEST_ID,
|
154
150
|
ext_auth_success_uri: "http://example.com/success",
|
@@ -160,4 +156,4 @@ result in DB).
|
|
160
156
|
|
161
157
|
## Running tests
|
162
158
|
|
163
|
-
|
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.
|
data/constants.rb.enc
ADDED
Binary file
|
data/lib/yandex_money/api.rb
CHANGED
@@ -1,187 +1,5 @@
|
|
1
1
|
require "yandex_money/api/version"
|
2
|
+
require "yandex_money/config"
|
2
3
|
require "yandex_money/exceptions"
|
3
|
-
require "yandex_money/
|
4
|
-
require "yandex_money/
|
5
|
-
require "httparty"
|
6
|
-
require "uri"
|
7
|
-
require "ostruct"
|
8
|
-
|
9
|
-
module YandexMoney
|
10
|
-
class Api
|
11
|
-
include HTTParty
|
12
|
-
|
13
|
-
base_uri "https://sp-money.yandex.ru"
|
14
|
-
default_timeout 30
|
15
|
-
|
16
|
-
attr_accessor :client_url, :code, :token, :instance_id
|
17
|
-
|
18
|
-
# Returns url to get token
|
19
|
-
def initialize(options)
|
20
|
-
# TOKEN provided
|
21
|
-
@logger = options[:logger] || YandexMoney::Logger::Empty.new(STDOUT)
|
22
|
-
if options.length == 1 && options[:token] != nil
|
23
|
-
@token = options[:token]
|
24
|
-
else
|
25
|
-
@client_id = options[:client_id]
|
26
|
-
@redirect_uri = options[:redirect_uri]
|
27
|
-
@instance_id = options[:instance_id]
|
28
|
-
if options[:scope] != nil
|
29
|
-
@client_url = send_authorize_request(
|
30
|
-
client_id: @client_id,
|
31
|
-
response_type: "code",
|
32
|
-
redirect_uri: @redirect_uri,
|
33
|
-
scope: options[:scope]
|
34
|
-
)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# obtains and saves token from code
|
40
|
-
def obtain_token(client_secret = nil)
|
41
|
-
raise YandexMoney::FieldNotSetError.new(:code) if @code == nil
|
42
|
-
uri = "/oauth/token"
|
43
|
-
options = {
|
44
|
-
code: @code,
|
45
|
-
client_id: @client_id,
|
46
|
-
grant_type: "authorization_code",
|
47
|
-
redirect_uri: @redirect_url
|
48
|
-
}
|
49
|
-
options[:client_secret] = client_secret if client_secret
|
50
|
-
@logger.info("url: https://sp-money.yandex.ru#{uri}, params: #{URI.encode_www_form options}")
|
51
|
-
response = self.class.post(uri, body: options).parsed_response
|
52
|
-
@token = response["access_token"]
|
53
|
-
end
|
54
|
-
|
55
|
-
# obtains account info
|
56
|
-
def account_info
|
57
|
-
check_token
|
58
|
-
OpenStruct.new send_request("/api/account-info").parsed_response
|
59
|
-
end
|
60
|
-
|
61
|
-
# obtains operation history
|
62
|
-
def operation_history(options=nil)
|
63
|
-
check_token
|
64
|
-
OpenStruct.new send_request("/api/operation-history", options).parsed_response
|
65
|
-
end
|
66
|
-
|
67
|
-
# obtains operation details
|
68
|
-
def operation_details(operation_id)
|
69
|
-
check_token
|
70
|
-
request = send_request("/api/operation-details", operation_id: operation_id)
|
71
|
-
details = OpenStruct.new request.parsed_response
|
72
|
-
if details.error
|
73
|
-
raise YandexMoney::ApiError.new details.error
|
74
|
-
else
|
75
|
-
details
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
# basic request payment method
|
80
|
-
def request_payment(options)
|
81
|
-
send_payment_request("/api/request-payment", options)
|
82
|
-
end
|
83
|
-
|
84
|
-
# basic process payment method
|
85
|
-
def process_payment(options)
|
86
|
-
send_payment_request("/api/process-payment", options)
|
87
|
-
end
|
88
|
-
|
89
|
-
def get_instance_id
|
90
|
-
request = send_request("/api/instance-id", client_id: @client_id)
|
91
|
-
if request["status"] == "refused"
|
92
|
-
raise YandexMoney::ApiError.new request["error"]
|
93
|
-
else
|
94
|
-
request["instance_id"]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def incoming_transfer_accept(operation_id, protection_code = nil)
|
99
|
-
uri = "/api/incoming-transfer-accept"
|
100
|
-
if protection_code
|
101
|
-
request_body = {
|
102
|
-
operation_id: operation_id,
|
103
|
-
protection_code: protection_code
|
104
|
-
}
|
105
|
-
else
|
106
|
-
request_body = { operation_id: operation_id }
|
107
|
-
end
|
108
|
-
request = send_request("/api/incoming-transfer-accept", request_body)
|
109
|
-
|
110
|
-
if request["status"] == "refused"
|
111
|
-
raise YandexMoney::AcceptTransferError.new request["error"], request["protection_code_attempts_available"]
|
112
|
-
else
|
113
|
-
true
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def incoming_transfer_reject(operation_id)
|
118
|
-
request = send_request("/api/incoming-transfer-reject", operation_id: operation_id)
|
119
|
-
|
120
|
-
if request["status"] == "refused"
|
121
|
-
raise YandexMoney::ApiError.new request["error"]
|
122
|
-
else
|
123
|
-
true
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def request_external_payment(payment_options)
|
128
|
-
payment_options[:instance_id] ||= @instance_id
|
129
|
-
request = send_request("/api/request-external-payment", payment_options)
|
130
|
-
if request["status"] == "refused"
|
131
|
-
raise YandexMoney::ApiError.new request["error"]
|
132
|
-
else
|
133
|
-
OpenStruct.new request.parsed_response
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def process_external_payment(payment_options)
|
138
|
-
payment_options[:instance_id] ||= @instance_id
|
139
|
-
request = send_request("/api/process-external-payment", payment_options)
|
140
|
-
if request["status"] == "refused"
|
141
|
-
raise YandexMoney::ApiError.new request["error"]
|
142
|
-
elsif request["status"] == "in_progress"
|
143
|
-
raise YandexMoney::ExternalPaymentProgressError.new request["error"], request["next_retry"]
|
144
|
-
else
|
145
|
-
OpenStruct.new request.parsed_response
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
private
|
150
|
-
|
151
|
-
def send_request(uri, options = nil)
|
152
|
-
@logger.info("url: https://money.yandex.ru#{uri}, bearer: #{@logger.mask_token(@token)}, params: #{URI.encode_www_form options || {}}")
|
153
|
-
request = self.class.post(uri, base_uri: "https://money.yandex.ru", headers: {
|
154
|
-
"Authorization" => "Bearer #{@token}",
|
155
|
-
"Content-Type" => "application/x-www-form-urlencoded"
|
156
|
-
}, body: options)
|
157
|
-
@logger.info("response: #{request.response.body}")
|
158
|
-
|
159
|
-
case request.response.code
|
160
|
-
when "403" then raise YandexMoney::InsufficientScopeError
|
161
|
-
when "401" then raise YandexMoney::UnauthorizedError.new request["www-authenticate"]
|
162
|
-
else request
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
def send_payment_request(uri, options)
|
167
|
-
check_token
|
168
|
-
request = send_request(uri, options)
|
169
|
-
response = OpenStruct.new request.parsed_response
|
170
|
-
if response.error
|
171
|
-
raise YandexMoney::ApiError.new response.error
|
172
|
-
else
|
173
|
-
response
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
def check_token
|
178
|
-
raise YandexMoney::FieldNotSetError.new(:token) unless @token
|
179
|
-
end
|
180
|
-
|
181
|
-
def send_authorize_request(options)
|
182
|
-
uri = "/oauth/authorize"
|
183
|
-
@logger.info("url: https://money.yandex.ru#{uri}, params: #{URI.encode_www_form options || {}}")
|
184
|
-
self.class.post(uri, body: options).request.path.to_s
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
4
|
+
require "yandex_money/wallet"
|
5
|
+
require "yandex_money/external_payment"
|
@@ -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
|
@@ -1,18 +1,28 @@
|
|
1
1
|
module YandexMoney
|
2
2
|
class Error < StandardError; end
|
3
3
|
|
4
|
-
class
|
5
|
-
|
6
|
-
class UnauthorizedError < Error
|
4
|
+
class ErrorWithMessage < Error
|
7
5
|
def initialize(error)
|
8
6
|
@err = error
|
9
7
|
end
|
10
8
|
|
11
9
|
def message
|
12
|
-
@err
|
10
|
+
if @err["www-authenticate"]
|
11
|
+
"#{@err.msg} - #{@err["www-authenticate"]}"
|
12
|
+
else
|
13
|
+
@err.msg
|
14
|
+
end
|
13
15
|
end
|
14
16
|
end
|
15
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
|
+
|
16
26
|
class FieldNotSetError < Error
|
17
27
|
def initialize(field)
|
18
28
|
@field = field
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module YandexMoney
|
2
|
+
# Payments from bank cards without authorization
|
3
|
+
#
|
4
|
+
# @see http://api.yandex.com/money/doc/dg/reference/process-external-payments.xml
|
5
|
+
# @see https://tech.yandex.ru/money/doc/dg/reference/process-external-payments-docpage/
|
6
|
+
class ExternalPayment
|
7
|
+
def initialize(instance_id)
|
8
|
+
@instance_id = instance_id
|
9
|
+
end
|
10
|
+
|
11
|
+
# Registers instance of application
|
12
|
+
#
|
13
|
+
# @see http://api.yandex.com/money/doc/dg/reference/instance-id.xml
|
14
|
+
# @see https://tech.yandex.ru/money/doc/dg/reference/instance-id-docpage/
|
15
|
+
#
|
16
|
+
# @param client_id [String] An identifier of application
|
17
|
+
#
|
18
|
+
# @raise [YandexMoney::InvalidRequestError] HTTP request does not conform to protocol format. Unable to parse HTTP request, or the Authorization header is missing or has an invalid value.
|
19
|
+
# @raise [YandexMoney::ServerError] A technical error occurs (the server responds with the HTTP code 500 Internal Server Error). The application should repeat the request with the same parameters later.
|
20
|
+
#
|
21
|
+
# @return [RecursiveOpenStruct] A status of operation
|
22
|
+
def self.get_instance_id(client_id)
|
23
|
+
request = send_external_payment_request("/api/instance-id", client_id: client_id)
|
24
|
+
RecursiveOpenStruct.new request.parsed_response
|
25
|
+
end
|
26
|
+
|
27
|
+
# Requests a external payment
|
28
|
+
#
|
29
|
+
# @see http://api.yandex.com/money/doc/dg/reference/request-external-payment.xml
|
30
|
+
# @see https://tech.yandex.ru/money/doc/dg/reference/request-external-payment-docpage/
|
31
|
+
#
|
32
|
+
# @param payment_options [Hash] Method's parameters. Check out docs for more information.
|
33
|
+
#
|
34
|
+
# @raise [YandexMoney::InvalidRequestError] HTTP request does not conform to protocol format. Unable to parse HTTP request, or the Authorization header is missing or has an invalid value.
|
35
|
+
# @raise [YandexMoney::ServerError] A technical error occurs (the server responds with the HTTP code 500 Internal Server Error). The application should repeat the request with the same parameters later.
|
36
|
+
#
|
37
|
+
# @return [RecursiveOpenStruct] A struct, containing `payment_id` and additional information about a recipient and payer
|
38
|
+
def request_external_payment(payment_options)
|
39
|
+
payment_options[:instance_id] = @instance_id
|
40
|
+
request = self.class.send_external_payment_request("/api/request-external-payment", payment_options)
|
41
|
+
RecursiveOpenStruct.new request.parsed_response
|
42
|
+
end
|
43
|
+
|
44
|
+
# Confirms a payment that was created using the request-extenral-payment method
|
45
|
+
#
|
46
|
+
# @see http://api.yandex.com/money/doc/dg/reference/process-external-payment.xml
|
47
|
+
# @see https://tech.yandex.ru/money/doc/dg/reference/process-external-payment-docpage/
|
48
|
+
#
|
49
|
+
# @param payment_options [Hash] Method's parameters. Check out docs for more information.
|
50
|
+
#
|
51
|
+
# @raise [YandexMoney::InvalidRequestError] HTTP request does not conform to protocol format. Unable to parse HTTP request, or the Authorization header is missing or has an invalid value.
|
52
|
+
# @raise [YandexMoney::ServerError] A technical error occurs (the server responds with the HTTP code 500 Internal Server Error). The application should repeat the request with the same parameters later.
|
53
|
+
#
|
54
|
+
# @return [RecursiveOpenStruct] A status of payment and additional steps for authorization (if needed)
|
55
|
+
def process_external_payment(payment_options)
|
56
|
+
payment_options[:instance_id] = @instance_id
|
57
|
+
request = self.class.send_external_payment_request("/api/process-external-payment", payment_options)
|
58
|
+
RecursiveOpenStruct.new request.parsed_response
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def self.send_external_payment_request(uri, options)
|
64
|
+
request = HTTParty.post(uri, base_uri: YandexMoney.config.money_url, headers: {
|
65
|
+
"Content-Type" => "application/x-www-form-urlencoded"
|
66
|
+
}, body: options)
|
67
|
+
case request.response.code
|
68
|
+
when "400" then raise YandexMoney::InvalidRequestError.new request.response
|
69
|
+
when "401" then raise YandexMoney::UnauthorizedError.new request.response
|
70
|
+
when "403" then raise YandexMoney::InsufficientScopeError request.response
|
71
|
+
when "500" then raise YandexMoney::ServerError
|
72
|
+
else
|
73
|
+
request
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|