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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +9 -6
  4. data/.yardopts +1 -0
  5. data/CHANGELOG.md +33 -1
  6. data/README.md +22 -26
  7. data/constants.rb.enc +0 -0
  8. data/lib/yandex_money/api.rb +3 -185
  9. data/lib/yandex_money/api/version.rb +1 -1
  10. data/lib/yandex_money/config.rb +26 -0
  11. data/lib/yandex_money/exceptions.rb +14 -4
  12. data/lib/yandex_money/external_payment.rb +78 -0
  13. data/lib/yandex_money/wallet.rb +215 -0
  14. data/spec/account_info_spec.rb +15 -14
  15. data/spec/auth_spec.rb +14 -41
  16. data/spec/cards_spec.rb +20 -18
  17. data/spec/config_spec.rb +50 -0
  18. data/spec/exceptions_spec.rb +55 -0
  19. data/spec/fixtures/vcr_cassettes/accept_incoming_transfer_with_protection_code.yml +3 -5
  20. data/spec/fixtures/vcr_cassettes/accept_incoming_transfer_with_protection_code_with_wrong_code.yml +6 -8
  21. data/spec/fixtures/vcr_cassettes/get_account_info.yml +5 -5
  22. data/spec/fixtures/vcr_cassettes/get_instance_id_fail.yml +2 -840
  23. data/spec/fixtures/vcr_cassettes/get_instance_id_success.yml +4 -840
  24. data/spec/fixtures/vcr_cassettes/get_operation_details.yml +199 -16
  25. data/spec/fixtures/vcr_cassettes/get_operation_history.yml +149 -150
  26. data/spec/fixtures/vcr_cassettes/get_operation_history_with_params.yml +9 -9
  27. data/spec/fixtures/vcr_cassettes/get_token_from_authorization_code.yml +15 -842
  28. data/spec/fixtures/vcr_cassettes/get_wrong_operation_details.yml +4 -4
  29. data/spec/fixtures/vcr_cassettes/init_without_client_secret.yml +719 -711
  30. data/spec/fixtures/vcr_cassettes/initialize_with_token.yml +7 -878
  31. data/spec/fixtures/vcr_cassettes/insufficient_scope_error.yml +40 -0
  32. data/spec/fixtures/vcr_cassettes/invalid_request_error.yml +40 -0
  33. data/spec/fixtures/vcr_cassettes/process_external_payment.yml +80 -6
  34. data/spec/fixtures/vcr_cassettes/process_payment_to_an_account_with_failure.yml +4 -6
  35. data/spec/fixtures/vcr_cassettes/reject_payment.yml +3 -5
  36. data/spec/fixtures/vcr_cassettes/reject_payment_fail.yml +3 -5
  37. data/spec/fixtures/vcr_cassettes/request_external_payment.yml +43 -10
  38. data/spec/fixtures/vcr_cassettes/request_payment_to_an_account_with_failure.yml +5 -7
  39. data/spec/fixtures/vcr_cassettes/success_process_payment_to_an_account.yml +6 -8
  40. data/spec/fixtures/vcr_cassettes/success_request_payment_to_an_account.yml +7 -9
  41. data/spec/fixtures/vcr_cassettes/unauthorized_error.yml +41 -0
  42. data/spec/fixtures/vcr_cassettes/unauthorized_exception.yml +3 -3
  43. data/spec/logger_spec.rb +0 -18
  44. data/spec/payments_spec.rb +20 -30
  45. data/spec/spec_helper.rb +12 -3
  46. data/spec/support/constants.example.rb +13 -0
  47. data/yandex-money-sdk.gemspec +2 -1
  48. metadata +42 -18
  49. data/lib/yandex_money/logger/default.rb +0 -17
  50. data/lib/yandex_money/logger/empty.rb +0 -10
  51. data/spec/fixtures/vcr_cassettes/init_with_client_secret.yml +0 -839
  52. data/spec/fixtures/vcr_cassettes/obtain_token_for_get_account_info.yml +0 -877
  53. data/spec/fixtures/vcr_cassettes/obtain_token_for_making_payments_to_an_account.yml +0 -875
  54. data/spec/fixtures/vcr_cassettes/obtain_token_for_payments_from_bank_cards_without_authorization.yml +0 -839
  55. data/spec/fixtures/vcr_cassettes/token_with_client_secret.yml +0 -875
  56. data/spec/support/constants.rb +0 -6
@@ -0,0 +1,215 @@
1
+ require "httparty"
2
+ require "recursive-open-struct"
3
+
4
+ module YandexMoney
5
+ # Payments from the Yandex.Money wallet
6
+ class Wallet
7
+ include HTTParty
8
+
9
+ base_uri YandexMoney.config.money_url
10
+ default_timeout 30
11
+
12
+ attr_accessor :token
13
+
14
+ def initialize(token)
15
+ @token = token
16
+ end
17
+
18
+ # Getting information about the status of the user account.
19
+ #
20
+ # @see http://api.yandex.com/money/doc/dg/reference/account-info.xml
21
+ # @see https://tech.yandex.ru/money/doc/dg/reference/account-info-docpage
22
+ #
23
+ # @return [RecursiveOpenStruct] Account information
24
+ #
25
+ # @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.
26
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
27
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
28
+ # @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.
29
+ def account_info
30
+ RecursiveOpenStruct.new send_request("/api/account-info", recurse_over_arrays: true).parsed_response
31
+ end
32
+
33
+ # Returns operation history of a user's wallet
34
+ #
35
+ # @see http://api.yandex.com/money/doc/dg/reference/operation-history.xml
36
+ # @see https://tech.yandex.ru/money/doc/dg/reference/operation-history-docpage/
37
+ #
38
+ # @param options [Hash] A hash with filter parameters according to documetation
39
+ # @return [Array<RecursiveOpenStruct>] An array containing user's wallet operations.
40
+ #
41
+ # @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.
42
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
43
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
44
+ # @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.
45
+ def operation_history(options=nil)
46
+ history = RecursiveOpenStruct.new(
47
+ send_request("/api/operation-history", options).parsed_response
48
+ )
49
+ history.operations = history.operations.map do |operation|
50
+ RecursiveOpenStruct.new operation
51
+ end
52
+ history
53
+ end
54
+
55
+ # Returns details of operation specified by operation_id
56
+ #
57
+ # @see http://api.yandex.com/money/doc/dg/reference/operation-details.xml
58
+ # @see https://tech.yandex.ru/money/doc/dg/reference/operation-details-docpage/
59
+ #
60
+ # @param operation_id [String] A operation identifier
61
+ # @return [RecursiveOpenStruct] All details of requested operation.
62
+ #
63
+ # @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.
64
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
65
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
66
+ # @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.
67
+ def operation_details(operation_id)
68
+ request = send_request("/api/operation-details", operation_id: operation_id)
69
+ RecursiveOpenStruct.new request.parsed_response
70
+ end
71
+
72
+ # Requests a payment
73
+ #
74
+ # @see http://api.yandex.com/money/doc/dg/reference/request-payment.xml
75
+ # @see https://tech.yandex.ru/money/doc/dg/reference/request-payment-docpage/
76
+ #
77
+ # @param options [Hash] A method's parameters. Check out docs for more information
78
+ # @return [RecursiveOpenStruct] `payment_id` and additional information about a recipient and payer.
79
+ #
80
+ # @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.
81
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
82
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
83
+ # @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.
84
+ def request_payment(options)
85
+ send_payment_request("/api/request-payment", options)
86
+ end
87
+
88
+ # Confirms a payment that was created using the request-payment method.
89
+ #
90
+ # @see http://api.yandex.com/money/doc/dg/reference/process-payment.xml
91
+ # @see https://tech.yandex.ru/money/doc/dg/reference/process-payment-docpage/
92
+ #
93
+ # @param options [Hash] A method's parameters. Check out docs for more information
94
+ # @return [RecursiveOpenStruct] A status of payment and additional steps for authorization (if needed)
95
+ #
96
+ # @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.
97
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
98
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
99
+ # @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.
100
+ def process_payment(options)
101
+ send_payment_request("/api/process-payment", options)
102
+ end
103
+
104
+ # Accepts incoming transfer with a protection code or deferred transfer
105
+ #
106
+ # @see http://api.yandex.com/money/doc/dg/reference/incoming-transfer-accept.xml
107
+ # @see https://tech.yandex.ru/money/doc/dg/reference/incoming-transfer-accept-docpage/
108
+ #
109
+ # @param operation_id [String] A operation identifier
110
+ # @param protection_code [String] Secret code of four decimal digits. Specified for an incoming transfer proteced by a secret code. Omitted for deferred transfers
111
+ # @return [RecursiveOpenStruct] An information about operation result.
112
+ #
113
+ # @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.
114
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
115
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
116
+ # @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.
117
+ def incoming_transfer_accept(operation_id, protection_code = nil)
118
+ uri = "/api/incoming-transfer-accept"
119
+ if protection_code
120
+ request_body = {
121
+ operation_id: operation_id,
122
+ protection_code: protection_code
123
+ }
124
+ else
125
+ request_body = { operation_id: operation_id }
126
+ end
127
+ RecursiveOpenStruct.new send_request("/api/incoming-transfer-accept", request_body)
128
+ end
129
+
130
+ # Rejects incoming transfer with a protection code or deferred transfer
131
+ #
132
+ # @see http://api.yandex.com/money/doc/dg/reference/incoming-transfer-reject.xml
133
+ # @see https://tech.yandex.ru/money/doc/dg/reference/incoming-transfer-reject-docpage/
134
+ #
135
+ # @param operation_id [String] A operation identifier
136
+ # @return [RecursiveOpenStruct] An information about operation result.
137
+ #
138
+ # @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.
139
+ # @raise [YandexMoney::UnauthorizedError] Nonexistent, expired, or revoked token specified.
140
+ # @raise [YandexMoney::InsufficientScopeError] The token does not have permissions for the requested operation.
141
+ # @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.
142
+ def incoming_transfer_reject(operation_id)
143
+ RecursiveOpenStruct.new send_request("/api/incoming-transfer-reject", operation_id: operation_id)
144
+ end
145
+
146
+ # Request a authorization URL
147
+ #
148
+ # @note For the authorization request, the user is redirected to the Yandex.Money authorization page. The user enters his login and password, reviews the list of requested permissions and payment limits, and either approves or rejects the application's authorization request.
149
+ # @note The authorization result is returned as an HTTP 302 Redirect. The application must process the HTTP Redirect response.
150
+ # @note Attention! If a user repeats the application authorization with the same value for the client_id parameter, the previous authorization is canceled.
151
+ #
152
+ # @param client_id [String] The client_id that was assigned to the application during registration
153
+ # @param redirect_uri [String] URI that the OAuth server sends the authorization result to. Must have a string value that exactly matches the redirect_uri parameter specified in the application registration data. Any additional parameters required for the application can be added at the end of the string.
154
+ # @param scope [String] A list of requested permissions. Items in the list are separated by a space. List items are case-sensitive.
155
+ #
156
+ # @return [String] Url to user must be redirected
157
+ def self.build_obtain_token_url(client_id, redirect_uri, scope)
158
+ uri = "#{YandexMoney.config.sp_money_url}/oauth/authorize"
159
+ options = {
160
+ client_id: client_id,
161
+ response_type: "code",
162
+ redirect_uri: redirect_uri,
163
+ scope: scope
164
+ }
165
+ HTTParty.post(uri, body: options).request.path.to_s
166
+ end
167
+
168
+ # Access token request
169
+ #
170
+ # @see http://api.yandex.com/money/doc/dg/reference/obtain-access-token.xml
171
+ # @see https://tech.yandex.ru/money/doc/dg/reference/obtain-access-token-docpage
172
+ #
173
+ # @note If authorization was completed successfully, the application should immediately exchange the temporary authorization code for an access token. To do this, a request containing the temporary authorization code must be sent to the Yandex.Money OAuth server.
174
+ #
175
+ # @param client_id [String] The client_id that was assigned to the application during registration
176
+ # @param code [String] Temporary token (authorization code)
177
+ # @param redirect_uri [String] URI that the OAuth server sends the authorization result to. The value of this parameter must exactly match the redirect_uri value from the previous "authorize" call
178
+ # @param client_secret [String] A secret word for verifying the application's authenticity. Specified if the service is registered with the option to verify authenticity
179
+ # @return [String] Access token
180
+ def self.get_access_token(client_id, code, redirect_uri, client_secret=nil)
181
+ uri = "#{YandexMoney.config.sp_money_url}/oauth/token"
182
+ options = {
183
+ code: code,
184
+ client_id: client_id,
185
+ grant_type: "authorization_code",
186
+ redirect_uri: redirect_uri
187
+ }
188
+ options[:client_secret] = client_secret if client_secret
189
+ response = HTTParty.post(uri, body: options).parsed_response
190
+ response["access_token"]
191
+ end
192
+
193
+ protected
194
+
195
+ def send_request(uri, options = nil)
196
+ request = self.class.post(uri, headers: {
197
+ "Authorization" => "Bearer #{@token}",
198
+ "Content-Type" => "application/x-www-form-urlencoded"
199
+ }, body: options)
200
+ case request.response.code
201
+ when "400" then raise YandexMoney::InvalidRequestError.new request.response
202
+ when "401" then raise YandexMoney::UnauthorizedError.new request.response
203
+ when "403" then raise YandexMoney::InsufficientScopeError.new request.response
204
+ when "500" then raise YandexMoney::ServerError
205
+ else
206
+ request
207
+ end
208
+ end
209
+
210
+ def send_payment_request(uri, options)
211
+ request = send_request(uri, options)
212
+ RecursiveOpenStruct.new request.parsed_response
213
+ end
214
+ end
215
+ end
@@ -3,13 +3,14 @@ require "spec_helper"
3
3
  describe "get account info" do
4
4
  before :all do
5
5
  VCR.use_cassette "obtain token for get account info" do
6
- @api = YandexMoney::Api.new(
7
- client_id: CLIENT_ID,
8
- redirect_uri: REDIRECT_URI,
9
- scope: "account-info operation-history operation-details"
10
- )
11
- @api.code = "39041180F6631E2B56DD0058F75A34C7504226178A45D624313495ECD417DCC3AA6CBF1B010E65BB09F3F9EB5AE63452129BAE2B732B7457C33BE6B2039B7B60A8058D2A387729A601DC817BBFB27CB0CC2D65E3C70997D981AC0E31F18CF32C0675DFD461E2F5C5639B75AC0E5074CE64FCF4546447BBDC566E3459FB1B3C3B"
12
- @api.obtain_token
6
+ @api = YandexMoney::Wallet.new(ACCESS_TOKEN)
7
+ end
8
+ end
9
+
10
+ it "should return recursive open struct" do
11
+ VCR.use_cassette "get account info" do
12
+ info = @api.account_info
13
+ expect(info.avatar.url).to start_with "https://avatars.yandex.net/"
13
14
  end
14
15
  end
15
16
 
@@ -17,7 +18,7 @@ describe "get account info" do
17
18
  it "should return account info" do
18
19
  VCR.use_cassette "get account info" do
19
20
  info = @api.account_info
20
- expect(info.account).to eq("41001565326286")
21
+ expect(info.account).to eq WALLET_NUMBER
21
22
  end
22
23
  end
23
24
 
@@ -25,7 +26,7 @@ describe "get account info" do
25
26
  it "should return operation history" do
26
27
  VCR.use_cassette "get operation history" do
27
28
  history = @api.operation_history
28
- expect(history.operations.count).to eq 30
29
+ expect(history.operations.count).to be_between(1, 30).inclusive
29
30
  end
30
31
  end
31
32
 
@@ -40,23 +41,23 @@ describe "get account info" do
40
41
  describe "operation details" do
41
42
  it "should return valid operation details" do
42
43
  VCR.use_cassette "get operation details" do
43
- details = @api.operation_details "462449992116028008"
44
+ history = @api.operation_history
45
+ operation_id = history.operations.first.operation_id
46
+ details = @api.operation_details operation_id
44
47
  expect(details.status).to eq "success"
45
48
  end
46
49
  end
47
50
 
48
51
  it "raise unauthorized exception when wrong token" do
49
52
  VCR.use_cassette "unauthorized exception" do
50
- @api = YandexMoney::Api.new(
51
- token: "wrong"
52
- )
53
+ @api = YandexMoney::Wallet.new("wrong_token")
53
54
  expect { @api.operation_details "462449992116028008" }.to raise_error YandexMoney::UnauthorizedError
54
55
  end
55
56
  end
56
57
 
57
58
  it "should raise exception if operation_id is wrong" do
58
59
  VCR.use_cassette "get wrong operation details" do
59
- expect { @api.operation_details "unknown" }.to raise_error YandexMoney::ApiError
60
+ expect(@api.operation_details("unknown").error).to eq "illegal_param_operation_id"
60
61
  end
61
62
  end
62
63
  end
@@ -4,59 +4,32 @@ describe "Application authorization flow" do
4
4
  # http://api.yandex.ru/money/doc/dg/reference/request-access-token.xml
5
5
  it "should properly initialize without client_secret" do
6
6
  VCR.use_cassette "init without client secret" do
7
- api = YandexMoney::Api.new(
8
- client_id: CLIENT_ID,
9
- redirect_uri: REDIRECT_URI,
10
- scope: "account-info operation-history"
7
+ url = YandexMoney::Wallet.build_obtain_token_url(
8
+ CLIENT_ID,
9
+ REDIRECT_URI,
10
+ "account-info operation-history"
11
11
  )
12
- expect(api.client_url).to start_with("https://money.yandex.ru/select-wallet.xml?requestid=")
12
+ expect(url).to start_with("https://money.yandex.ru/select-wallet.xml?requestid=")
13
13
  end
14
14
  end
15
15
 
16
16
  # http://api.yandex.ru/money/doc/dg/reference/request-access-token.xml
17
- it "should get token with usage of client_secret" do
18
- VCR.use_cassette "token with client secret" do
19
- api = YandexMoney::Api.new(
20
- client_id: "2CB2BBF0788E79A1537437CFD37B15A3E21DAAEE5CD0AE8981118C1CFC6F376A",
21
- redirect_uri: REDIRECT_URI,
22
- scope: "account-info operation-history"
23
- )
24
- api.code = "94D93C28E1B27B02A36D495B01BB410EB415EF0152FA30837DEAF307773EDB9734B46954292458D3E5B0E8ABD70C1AF1EE71D64648FB65E7DA8ADAC961970709C3BEBA1AF949F73BAEA2134D386D2ED31CA3F001A45EA05A614432A196A6BEA7B042A0ABDA6E62BA108F864FC400286F388A41454DD961A4A782BF32A80F3816"
25
- api.obtain_token("6FAFA896BD2C77E21E240081CDFF3B007451876AB9C186DE2AD2EDDCE29CE3E1BCC1A2789B53583F3398ACD8127A61851357C5D3F444D58F8B5F0AA4F78F088D")
26
- expect(api.token).to start_with("410011285611534")
27
- end
28
- end
29
-
30
- # http://api.yandex.ru/money/doc/dg/reference/obtain-access-token.xml
31
17
  it "should get token from code" do
32
18
  VCR.use_cassette "get token from authorization code" do
33
- api = YandexMoney::Api.new(
34
- client_id: CLIENT_ID,
35
- redirect_uri: REDIRECT_URI,
36
- scope: "account-info operation-history"
37
- )
38
- api.code = "736557BEBF03A1867FFF179F8FADF0F33841471B31BD9ECF2AC59480D0F123475E6261300B1FED6CDB7C067EF4C5E9CC860A31839D52FA5950B5CAFD18C29FE0A31D2F53618D000BAA7C733B2A143C148C4631EFDEAB29151A5EA92B6B099AAEE31A82BF9F7C13EC2E8EAFF44F62D1326EDEBDA4668631ACC367967DDA57F875"
39
- api.obtain_token
40
- expect(api.token).to start_with("41001565326286.D206A82773387134BB25CF89A85256EA")
19
+ expect(
20
+ YandexMoney::Wallet.get_access_token(
21
+ CLIENT_ID,
22
+ "SOME CODE",
23
+ REDIRECT_URI
24
+ )
25
+ ).to be nil
41
26
  end
42
27
  end
43
28
 
44
29
  it "could be initialized with token" do
45
30
  VCR.use_cassette "initialize with token" do
46
- token_api = YandexMoney::Api.new(
47
- client_id: CLIENT_ID,
48
- redirect_uri: REDIRECT_URI,
49
- scope: "account-info"
50
- )
51
- token_api.code = "7E0C87AE3AFAE96E87FDD780A034FD696264A68CEE8CD9BFDD9F247D32AF6AD1FABC6733D1AFAD65FD08C8112C9D1E8BE80D996F6CF026916154A7A59B431D7A2D02E5845192A2956348C46D85799622DCFEDF4F2DEB80EA81BC1EA5D8874065D0E3828C556754CA60D3B281A1F530DED7B74F415EE8C6B703D34F4A53AE5F90"
52
- token_api.obtain_token
53
- api = YandexMoney::Api.new(token: token_api.token)
54
- expect(api.account_info.account).to eq("41001565326286")
31
+ api = YandexMoney::Wallet.new(ACCESS_TOKEN)
32
+ expect(api.account_info.account).to eq WALLET_NUMBER
55
33
  end
56
34
  end
57
-
58
- it "should raise exception while trying to obtain token without code" do
59
- api = YandexMoney::Api.new(client_id: CLIENT_ID)
60
- expect { api.obtain_token }.to raise_error YandexMoney::FieldNotSetError
61
- end
62
35
  end
@@ -7,28 +7,25 @@ describe "Payments from bank cards without authorization" do
7
7
  end
8
8
  it "should fail when try to register an instance of application without connected market" do
9
9
  VCR.use_cassette "get instance id fail" do
10
- @api = YandexMoney::Api.new(
11
- client_id: nil
12
- )
13
- expect { @api.get_instance_id }.to raise_error YandexMoney::ApiError
10
+ expect(
11
+ YandexMoney::ExternalPayment.get_instance_id(nil).status
12
+ ).to eq "refused"
14
13
  end
15
14
  end
16
15
 
17
16
  it "should register an instance of application" do
18
17
  VCR.use_cassette "get instance id success" do
19
- @api = YandexMoney::Api.new(
20
- client_id: CLIENT_ID
21
- )
22
- expect(@api.get_instance_id).to eq(INSTANCE_ID)
18
+ expect(
19
+ YandexMoney::ExternalPayment.get_instance_id(CLIENT_ID)
20
+ .instance_id.length
21
+ ).to eq 64
23
22
  end
24
23
  end
25
24
 
26
25
  it "should request external payment" do
27
26
  VCR.use_cassette "request external payment" do
28
- @api = YandexMoney::Api.new(
29
- client_id: CLIENT_ID,
30
- instance_id: INSTANCE_ID
31
- )
27
+ instance_id = YandexMoney::ExternalPayment.get_instance_id(CLIENT_ID)
28
+ @api = YandexMoney::ExternalPayment.new(instance_id)
32
29
  expect(@api.request_external_payment({
33
30
  pattern_id: "p2p",
34
31
  to: "410011285611534",
@@ -40,13 +37,18 @@ describe "Payments from bank cards without authorization" do
40
37
 
41
38
  it "should process external payment" do
42
39
  VCR.use_cassette "process external payment" do
43
- @api = YandexMoney::Api.new(
44
- instance_id: INSTANCE_ID
45
- )
40
+ instance_id = YandexMoney::ExternalPayment.get_instance_id(CLIENT_ID)
41
+ @api = YandexMoney::ExternalPayment.new(instance_id)
42
+ request_id = @api.request_external_payment(
43
+ pattern_id: "p2p",
44
+ to: "410011285611534",
45
+ amount_due: "1.00",
46
+ message: "test"
47
+ ).request_id
46
48
  expect(@api.process_external_payment({
47
- request_id: REQUEST_ID,
48
- ext_auth_success_uri: "http://drakmail.ru/success",
49
- ext_auth_fail_uri: "http://drakmail.ru/fail"
49
+ request_id: request_id,
50
+ ext_auth_success_uri: "http://127.0.0.1:4567/success",
51
+ ext_auth_fail_uri: "http://127.0.0.1:4567/fail"
50
52
  }).status).to eq("ext_auth_required")
51
53
  end
52
54
  end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe "YandexMoney config" do
4
+ after :all do
5
+ YandexMoney.reset_config
6
+ end
7
+
8
+ let(:default_money_url) { "https://money.yandex.ru" }
9
+ let(:default_sp_money_url) { "https://sp-money.yandex.ru" }
10
+
11
+ it "should have methods: config, configure, reset_config" do
12
+ yandex = class_double("YandexMoney")
13
+ expect(yandex).to receive_messages([:config, :configure, :reset_config])
14
+ yandex.config & yandex.configure & yandex.reset_config
15
+ end
16
+
17
+ it "should yield block in configure" do
18
+ expect {|b| YandexMoney.configure(&b)}.to yield_control
19
+ end
20
+
21
+ it "should return a YandexMoney::Config instance" do
22
+ expect(YandexMoney.config).to be_a_kind_of(YandexMoney::Config)
23
+ end
24
+
25
+ it "should reset_config" do
26
+ YandexMoney.configure { |c| c.money_url = "http://example1.com" }
27
+ YandexMoney.reset_config
28
+ expect(YandexMoney.config.money_url).to eq(default_money_url)
29
+ end
30
+
31
+ it "should have correct defaults" do
32
+ expect(YandexMoney.config.money_url).to eq(default_money_url)
33
+ expect(YandexMoney.config.sp_money_url).to eq(default_sp_money_url)
34
+ end
35
+
36
+ it "can write config url directly" do
37
+ YandexMoney.config.money_url = "http://example3.com"
38
+ expect(YandexMoney.config.money_url).to eq "http://example3.com"
39
+ end
40
+
41
+ it "should correct configure hosts addresses" do
42
+ YandexMoney.configure do |config|
43
+ config.money_url = "http://example1.com"
44
+ config.sp_money_url = "http://example2.com"
45
+ end
46
+ expect(YandexMoney.config.money_url).to eq "http://example1.com"
47
+ expect(YandexMoney.config.sp_money_url).to eq "http://example2.com"
48
+ end
49
+
50
+ end