qiwi_observer 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9112ae619d28bc841d4140723508753cf042b3c31268fc6bb693d3144400e2f0
4
+ data.tar.gz: 0a5d8e77d20ad5bd1662dd0c4db7f15f3d1c91704923bfe5e865605620624cd7
5
+ SHA512:
6
+ metadata.gz: 73ddd5921661cfab753a4b568aff1d751b8db0c9a9c4805d517460a9d9998f16d2edeaf532f1603669dfe88cb6b66ed65532fa58ecae431ac48aa2126992224a
7
+ data.tar.gz: 83249f57c49ab70d4fc45264b7bb5628226e369ade1c1537b5106d2af80baabd6ba4daf0a29f3e3d26fe7157b9e35c60be1b71c1a69e7335ee7173157c42593c
@@ -0,0 +1,32 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - master
7
+ push:
8
+ branches:
9
+ - master
10
+
11
+ jobs:
12
+ build:
13
+ name: Build + Publish
14
+ runs-on: ubuntu-latest
15
+
16
+ steps:
17
+ - uses: actions/checkout@master
18
+ - name: Set up Ruby 2.6
19
+ uses: actions/setup-ruby@v1
20
+ with:
21
+ version: 2.6.x
22
+
23
+ - name: Publish to RubyGems
24
+ run: |
25
+ mkdir -p $HOME/.gem
26
+ touch $HOME/.gem/credentials
27
+ chmod 0600 $HOME/.gem/credentials
28
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
29
+ gem build *.gemspec
30
+ gem push *.gem
31
+ env:
32
+ GEM_HOST_API_KEY: ${{secrets.RUBY_GEMS_API_KEY}}
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,183 @@
1
+ # Relaxed.Ruby.Style
2
+ ## Version 2.2
3
+ AllCops:
4
+ Exclude:
5
+ - "app/views/**/*"
6
+ - "db/**/*"
7
+ - "config/**/*"
8
+ - "node_modules/**/*"
9
+ - "bin/**/*"
10
+
11
+ Style/Alias:
12
+ Enabled: false
13
+ StyleGuide: https://relaxed.ruby.style/#stylealias
14
+
15
+ Style/AsciiComments:
16
+ Enabled: false
17
+ StyleGuide: https://relaxed.ruby.style/#styleasciicomments
18
+
19
+ Style/BeginBlock:
20
+ Enabled: false
21
+ StyleGuide: https://relaxed.ruby.style/#stylebeginblock
22
+
23
+ Style/BlockDelimiters:
24
+ Enabled: false
25
+ StyleGuide: https://relaxed.ruby.style/#styleblockdelimiters
26
+
27
+ Style/CommentAnnotation:
28
+ Enabled: false
29
+ StyleGuide: https://relaxed.ruby.style/#stylecommentannotation
30
+
31
+ Style/Documentation:
32
+ Enabled: false
33
+ StyleGuide: https://relaxed.ruby.style/#styledocumentation
34
+
35
+ Layout/DotPosition:
36
+ Enabled: false
37
+ StyleGuide: https://relaxed.ruby.style/#layoutdotposition
38
+
39
+ Style/DoubleNegation:
40
+ Enabled: false
41
+ StyleGuide: https://relaxed.ruby.style/#styledoublenegation
42
+
43
+ Style/EndBlock:
44
+ Enabled: false
45
+ StyleGuide: https://relaxed.ruby.style/#styleendblock
46
+
47
+ Style/FormatString:
48
+ Enabled: false
49
+ StyleGuide: https://relaxed.ruby.style/#styleformatstring
50
+
51
+ Style/IfUnlessModifier:
52
+ Enabled: false
53
+ StyleGuide: https://relaxed.ruby.style/#styleifunlessmodifier
54
+
55
+ Style/Lambda:
56
+ Enabled: false
57
+ StyleGuide: https://relaxed.ruby.style/#stylelambda
58
+
59
+ Style/ModuleFunction:
60
+ Enabled: false
61
+ StyleGuide: https://relaxed.ruby.style/#stylemodulefunction
62
+
63
+ Style/MultilineBlockChain:
64
+ Enabled: false
65
+ StyleGuide: https://relaxed.ruby.style/#stylemultilineblockchain
66
+
67
+ Style/NegatedIf:
68
+ Enabled: true
69
+ StyleGuide: https://relaxed.ruby.style/#stylenegatedif
70
+
71
+ Style/NegatedWhile:
72
+ Enabled: false
73
+ StyleGuide: https://relaxed.ruby.style/#stylenegatedwhile
74
+
75
+ Style/ParallelAssignment:
76
+ Enabled: false
77
+ StyleGuide: https://relaxed.ruby.style/#styleparallelassignment
78
+
79
+ Style/PercentLiteralDelimiters:
80
+ Enabled: false
81
+ StyleGuide: https://relaxed.ruby.style/#stylepercentliteraldelimiters
82
+
83
+ Style/PerlBackrefs:
84
+ Enabled: false
85
+ StyleGuide: https://relaxed.ruby.style/#styleperlbackrefs
86
+
87
+ Style/Semicolon:
88
+ Enabled: false
89
+ StyleGuide: https://relaxed.ruby.style/#stylesemicolon
90
+
91
+ Style/SignalException:
92
+ Enabled: false
93
+ StyleGuide: https://relaxed.ruby.style/#stylesignalexception
94
+
95
+ Style/SingleLineBlockParams:
96
+ Enabled: false
97
+ StyleGuide: https://relaxed.ruby.style/#stylesinglelineblockparams
98
+
99
+ Style/SingleLineMethods:
100
+ Enabled: false
101
+ StyleGuide: https://relaxed.ruby.style/#stylesinglelinemethods
102
+
103
+ Layout/SpaceBeforeBlockBraces:
104
+ Enabled: false
105
+ StyleGuide: https://relaxed.ruby.style/#layoutspacebeforeblockbraces
106
+
107
+ Layout/SpaceInsideParens:
108
+ Enabled: false
109
+ StyleGuide: https://relaxed.ruby.style/#layoutspaceinsideparens
110
+
111
+ Style/SpecialGlobalVars:
112
+ Enabled: false
113
+ StyleGuide: https://relaxed.ruby.style/#stylespecialglobalvars
114
+
115
+ Style/StringLiterals:
116
+ Enabled: false
117
+ StyleGuide: https://relaxed.ruby.style/#stylestringliterals
118
+
119
+ Style/TrailingCommaInArguments:
120
+ Enabled: false
121
+ StyleGuide: https://relaxed.ruby.style/#styletrailingcommainarguments
122
+
123
+ Style/TrailingCommaInArrayLiteral:
124
+ Enabled: false
125
+ StyleGuide: https://relaxed.ruby.style/#styletrailingcommainarrayliteral
126
+
127
+ Style/TrailingCommaInHashLiteral:
128
+ Enabled: false
129
+ StyleGuide: https://relaxed.ruby.style/#styletrailingcommainhashliteral
130
+
131
+ Style/WhileUntilModifier:
132
+ Enabled: false
133
+ StyleGuide: https://relaxed.ruby.style/#stylewhileuntilmodifier
134
+
135
+ Style/WordArray:
136
+ Enabled: false
137
+ StyleGuide: https://relaxed.ruby.style/#stylewordarray
138
+
139
+ Lint/AmbiguousRegexpLiteral:
140
+ Enabled: false
141
+ StyleGuide: https://relaxed.ruby.style/#lintambiguousregexpliteral
142
+
143
+ Lint/AssignmentInCondition:
144
+ Enabled: false
145
+ StyleGuide: https://relaxed.ruby.style/#lintassignmentincondition
146
+
147
+ Style/StringLiterals:
148
+ Enabled: true
149
+ EnforcedStyle: double_quotes
150
+
151
+ Layout/SpaceInLambdaLiteral:
152
+ Enabled: true
153
+ EnforcedStyle: require_space
154
+
155
+ Metrics/AbcSize:
156
+ Enabled: false
157
+
158
+ Metrics/BlockNesting:
159
+ Enabled: false
160
+
161
+ Metrics/ClassLength:
162
+ Enabled: false
163
+
164
+ Metrics/ModuleLength:
165
+ Enabled: false
166
+
167
+ Metrics/CyclomaticComplexity:
168
+ Enabled: false
169
+
170
+ Metrics/LineLength:
171
+ Enabled: false
172
+
173
+ Metrics/MethodLength:
174
+ Enabled: true
175
+
176
+ Metrics/ParameterLists:
177
+ Enabled: false
178
+
179
+ Metrics/PerceivedComplexity:
180
+ Enabled: false
181
+
182
+ Metrics/BlockLength:
183
+ Enabled: false
@@ -0,0 +1 @@
1
+ qiwi_observer
@@ -0,0 +1 @@
1
+ ruby-2.6.2
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ibekable@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in qiwi_observer.gemspec
6
+ gemspec
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ qiwi_observer (0.1.1)
5
+ dry-configurable (~> 0.7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.6.0)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ concurrent-ruby (1.1.5)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ dry-configurable (0.8.3)
17
+ concurrent-ruby (~> 1.0)
18
+ dry-core (~> 0.4, >= 0.4.7)
19
+ dry-core (0.4.7)
20
+ concurrent-ruby (~> 1.0)
21
+ hashdiff (0.4.0)
22
+ public_suffix (3.1.0)
23
+ rake (10.5.0)
24
+ rspec (3.8.0)
25
+ rspec-core (~> 3.8.0)
26
+ rspec-expectations (~> 3.8.0)
27
+ rspec-mocks (~> 3.8.0)
28
+ rspec-core (3.8.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-expectations (3.8.3)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-mocks (3.8.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-support (3.8.0)
37
+ safe_yaml (1.0.5)
38
+ webmock (3.6.0)
39
+ addressable (>= 2.3.6)
40
+ crack (>= 0.3.2)
41
+ hashdiff (>= 0.4.0, < 2.0.0)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 2.0)
48
+ qiwi_observer!
49
+ rake (~> 10.0)
50
+ rspec (~> 3.0)
51
+ webmock (~> 3.3)
52
+
53
+ BUNDLED WITH
54
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Izhanov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,41 @@
1
+ # QiwiObserver
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/qiwi_observer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'qiwi_observer'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install qiwi_observer
21
+
22
+ ## Как работать
23
+ #### Инициализируйте объект
24
+ - request = QiwiObserver::Payments.new(wallet: '7XXXXXXXXXX', token: 'asd23dsvdljoihiscgasdcrpoi')
25
+
26
+ #### Вызовите метод #call на объекте с переданным хэшом, формата {rows: Integer, operation: String, startDate: DateTime, endDate: DateTime}*
27
+ - response = request.call({rows: 15, operation: 'IN'})
28
+
29
+ #### Сделайте проверку, при успешном запросе через метод #short_info получите массив хэшей с информацией платежей (:account_id, :date, :amount, :comment)
30
+ - if response.success?
31
+ - result = response.short_info
32
+ - else
33
+ - redirect_to some_apth flash {error: response.error}
34
+ - end
35
+ - ##### rows: целое число от 1 до 50. Обязательный параметр.
36
+ - ##### Возможные значения параметра operation: ALL, IN, OUT & QIWI_CARD.
37
+ - ##### startDate: Начальная дата поиска платежей. Дату можно указать в любой временной зоне TZD (формат ГГГГ-ММ-ДД'T'чч:мм:ссTZD), однако она должна совпадать с временной зоной в параметре endDate.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "qiwi_observer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ require "net/http"
2
+ require "dry-configurable"
3
+ require "base64"
4
+ require "openssl"
5
+ require "json"
6
+
7
+ require "qiwi_observer/version"
8
+ require "qiwi_observer/response"
9
+ require "qiwi_observer/payments/payments"
10
+ require "qiwi_observer/payments/payments_response"
11
+ require "qiwi_observer/webhook/webhook"
12
+ require "qiwi_observer/webhook/webhook_response"
13
+ require "qiwi_observer/webhook/active_webhook"
14
+ require "qiwi_observer/webhook/webhook_secret_key"
15
+ require "qiwi_observer/webhook/webhook_registration"
16
+ require "qiwi_observer/webhook/webhook_removal"
17
+ require "qiwi_observer/transfer/transfer"
18
+
19
+ module QiwiObserver
20
+ extend Dry::Configurable
21
+
22
+ setting :wallet
23
+ setting :token
24
+ end
@@ -0,0 +1,51 @@
1
+ module QiwiObserver
2
+ class Payments
3
+ API_PATH = "https://edge.qiwi.com/payment-history/v2/persons/".freeze
4
+
5
+ def initialize
6
+ @wallet = QiwiObserver.config.wallet
7
+ @token = QiwiObserver.config.token
8
+
9
+ raise ArgumentError, "Wallet and token must be configure" if @wallet.nil? && @token.nil?
10
+ end
11
+
12
+ def call(args = {})
13
+ uri = linkage(args)
14
+ request = prepare_a_request(uri)
15
+ http = connect_to(uri)
16
+
17
+ response = http.request(request)
18
+
19
+
20
+ return PaymentsResponse.new(success: true, body: response.body) if response.is_a?(Net::HTTPOK)
21
+ return PaymentsResponse.new(success: false, body: [response.code, response.message])
22
+ end
23
+
24
+ private
25
+
26
+ def linkage(args)
27
+ link = URI(API_PATH + "#{@wallet}/payments")
28
+ link.query = URI.encode_www_form(args)
29
+ link
30
+ end
31
+
32
+ def prepare_a_request(uri)
33
+ request = Net::HTTP::Get.new(uri)
34
+ request.initialize_http_header(
35
+ {
36
+ 'Accept' => 'application/json',
37
+ 'Content-Type' => 'application/json',
38
+ 'Authorization' => "Bearer #{@token}"
39
+ }
40
+ )
41
+ request
42
+ end
43
+
44
+ def connect_to(uri)
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.use_ssl = true
47
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
48
+ http
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,44 @@
1
+ module QiwiObserver
2
+ class PaymentsResponse < Response
3
+ attr_reader :value, :error
4
+
5
+ def initialize(success:, body:)
6
+ @success = success
7
+
8
+ if @success
9
+ @value = parse_body(body)
10
+ else
11
+ @error = error_description(body)
12
+ end
13
+ end
14
+
15
+ def success?
16
+ @success
17
+ end
18
+
19
+ def short_info
20
+ output = []
21
+ hash = @value
22
+ hash[:data].each do |tran|
23
+ output << {
24
+ account_id: tran[:account],
25
+ amount: tran[:total][:amount],
26
+ date: tran[:date],
27
+ comment: tran[:comment]
28
+ }
29
+ end
30
+ output
31
+ end
32
+
33
+ private
34
+
35
+ def parse_body(body)
36
+ JSON.parse(body, symbolize_names: true)
37
+ end
38
+
39
+ def error_description(body)
40
+ 'Error ' + body.join(' ')
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ module QiwiObserver
2
+ class Response
3
+ attr_reader :value, :error
4
+
5
+ def initialize(success:, body:)
6
+ @success = success
7
+
8
+ if @success
9
+ @value = parse_body(body)
10
+ else
11
+ @error = error_description(body)
12
+ end
13
+ end
14
+
15
+ def success?
16
+ @success
17
+ end
18
+
19
+ private
20
+
21
+ def parse_body(body)
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def error_description(body)
26
+ raise NotImplementedError
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ module QiwiObserver
2
+ class Transfer
3
+ API_PATH = 'https://edge.qiwi.com/sinap/api/v2/terms/99/payments'
4
+ def initialize
5
+ @wallet = QiwiObserver.config.wallet
6
+ @token = QiwiObserver.config.token
7
+ raise ArgumentError, 'Wallet and token must be configure' if @wallet.nil? && @token.nil?
8
+ end
9
+
10
+ def to_wallet(args={})
11
+ raise ArgumentError, 'Params must be set' unless !args.empty?
12
+ uri = URI(API_PATH)
13
+
14
+ request = Net::HTTP::Post.new(uri)
15
+ request.initialize_http_header(
16
+ {
17
+ 'Accept' => 'application/json',
18
+ 'Content-Type' => 'application/json',
19
+ 'Authorization' => "Bearer #{@token}"
20
+ })
21
+ request.body = body(args).to_json
22
+
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+ http.use_ssl = true
25
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
26
+
27
+ response = http.request(request)
28
+ JSON.parse(response.body, symbolize_names: true)
29
+ end
30
+
31
+
32
+ private
33
+
34
+ def body(args)
35
+ {
36
+ "id" => "11111111111111",
37
+ "sum" => {
38
+ "amount" => args[:amount],
39
+ "currency" => "643"
40
+ },
41
+ "paymentMethod" => {
42
+ "type" => "Account",
43
+ "accountId" => "643"
44
+ },
45
+ "comment" => "test",
46
+ "fields" => {
47
+ "account" => args[:account]
48
+ }
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module QiwiObserver
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiwiObserver
4
+ class ActiveWebhook
5
+ API_PATH = "https://edge.qiwi.com/payment-notifier/v1/hooks/active"
6
+
7
+ def initialize
8
+ @token = QiwiObserver.config.token
9
+ raise ArgumentError, "Token must be configure in QiwiObserver" unless @token
10
+ end
11
+
12
+ def call
13
+ uri = URI(API_PATH)
14
+ request = prepare_a_request(uri)
15
+ http = connect_to_api(uri)
16
+ response = http.request(request)
17
+ return WebhookResponse.new(success: true, body: response.body) if response.is_a?(Net::HTTPOK)
18
+ return WebhookResponse.new(success: false, body: [response.code, response.message])
19
+ end
20
+
21
+ private
22
+
23
+ def prepare_a_request(uri)
24
+ request = Net::HTTP::Get.new(uri)
25
+ request.initialize_http_header(
26
+ {
27
+ "Accept" => "application/json",
28
+ "Content-Type" => "application/json",
29
+ "Authorization" => "Bearer #{@token}"
30
+ }
31
+ )
32
+ request
33
+ end
34
+
35
+ def connect_to_api(uri)
36
+ http = Net::HTTP.new(uri.host, uri.port)
37
+ http.use_ssl = true
38
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
39
+ http
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ module QiwiObserver
2
+ class Webhook
3
+ def initialize(key)
4
+ raise ArgumentError, "Secret key must be set" unless key
5
+
6
+ @key = key
7
+ end
8
+
9
+ def call(params)
10
+ return WebhookResponse.new(success: true, body: params) if params.dig(:test) == true
11
+ return WebhookResponse.new(success: true, body: params) if sign_correct?(params)
12
+
13
+ WebhookResponse.new(success: false, body: params.dig(:payment, :txnId))
14
+ end
15
+
16
+ private
17
+
18
+ def sign_correct?(params)
19
+ sign_fields = concat_sign_fields(params)
20
+ secret_key = Base64.decode64(@key)
21
+ secure_hash = OpenSSL::HMAC.hexdigest("SHA256", secret_key, sign_fields)
22
+ params[:hash] == secure_hash
23
+ end
24
+
25
+ def concat_sign_fields(params)
26
+ first_part = params.dig(:payment, :sum, :currency)
27
+ second_part = params.dig(:payment, :sum, :amount)
28
+ third_part = params.dig(:payment, :type)
29
+ fourth_part = params.dig(:payment, :account)
30
+ fifth_part = params.dig(:payment, :txnId)
31
+ [first_part, second_part, third_part, fourth_part, fifth_part].join("|")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiwiObserver
4
+ class WebhookRegistration
5
+ def initialize(url)
6
+ @token = QiwiObserver.config.token
7
+ @url = url
8
+
9
+ raise ArgumentError, "Token must be configure in QiwiObserver" if @token.nil?
10
+ raise ArgumentError, "URL must be set" unless url
11
+ end
12
+
13
+ def call
14
+ query = URI.encode_www_form({hookType: 1, param: @url, txnType: "2"})
15
+ uri = URI("https://edge.qiwi.com/payment-notifier/v1/hooks?#{query}")
16
+ request = prepare_a_request(uri)
17
+ http = connect_to_api(uri)
18
+ response = http.request(request)
19
+ return WebhookResponse.new(success: true, body: response.body) if response.is_a?(Net::HTTPOK)
20
+ return WebhookResponse.new(success: false, body: [response.code, response.message])
21
+ end
22
+
23
+ private
24
+
25
+ def prepare_a_request(uri)
26
+ request = Net::HTTP::Put.new(uri)
27
+ request.initialize_http_header(
28
+ {
29
+ "Accept" => "application/json",
30
+ "Content-Type" => "application/json",
31
+ "Authorization" => "Bearer #{@token}"
32
+ }
33
+ )
34
+ request
35
+ end
36
+
37
+ def connect_to_api(uri)
38
+ http = Net::HTTP.new(uri.host, uri.port)
39
+ http.use_ssl = true
40
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+ http
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiwiObserver
4
+ class WebhookRemoval
5
+ def initialize(hook_id)
6
+ @token = QiwiObserver.config.token
7
+ @hook_id = hook_id
8
+
9
+ raise ArgumentError, "QiwiObserver must be configure with token" if @token.nil?
10
+ raise ArgumentError, "Hook ID must be set" unless hook_id
11
+ end
12
+
13
+ def call
14
+ uri = URI("https://edge.qiwi.com/payment-notifier/v1/hooks/#{@hook_id}")
15
+ request = prepare_a_request(uri)
16
+ http = connect_to_api(uri)
17
+ response = http.request(request)
18
+ return WebhookResponse.new(success: true, body: response.body) if response.is_a?(Net::HTTPOK)
19
+ return WebhookResponse.new(success: false, body: [response.code, response.message])
20
+ end
21
+
22
+ private
23
+
24
+ def prepare_a_request(uri)
25
+ request = Net::HTTP::Delete.new(uri)
26
+ request.initialize_http_header(
27
+ {
28
+ "Accept" => "application/json",
29
+ "Content-Type" => "application/json",
30
+ "Authorization" => "Bearer #{@token}"
31
+ }
32
+ )
33
+ request
34
+ end
35
+
36
+ def connect_to_api(uri)
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ http.use_ssl = true
39
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
40
+ http
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiwiObserver
4
+ class WebhookResponse < Response
5
+ attr_reader :error, :value
6
+ def initialize(success:, body:)
7
+ @success = success
8
+ if @success
9
+ @value = parse_body(body)
10
+ else
11
+ @error = error_description(body)
12
+ end
13
+ end
14
+
15
+ def success?
16
+ @success
17
+ end
18
+
19
+ private
20
+
21
+ def parse_body(body)
22
+ return parse_hash(body) if body.is_a?(Hash)
23
+ return parse_json(body) if body.is_a?(String)
24
+ end
25
+
26
+ def error_description(body)
27
+ return "This ##{body} transaction is not authenticated" if body.is_a?(String)
28
+ return "Error #{body.first} #{body.last}" if body.is_a?(Array)
29
+ end
30
+
31
+ def parse_hash(body)
32
+ if body[:test] != true
33
+ output = body[:payment]
34
+ output.reject { |key, _val| key == :signFields }.to_h
35
+ else
36
+ body
37
+ end
38
+ end
39
+
40
+ def parse_json(body)
41
+ output = JSON.parse(body).reduce({}) {|result, (key, value)| result.merge({key.to_sym => value})}
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QiwiObserver
4
+ class WebhookSecretKey
5
+ API_PATH =
6
+
7
+ def initialize(hook_id)
8
+ raise ArgumentError, "Hook id must be set" unless hook_id
9
+
10
+ @hook_id = hook_id
11
+ @token = QiwiObserver.config.token
12
+ @api_path = "https://edge.qiwi.com/payment-notifier/v1/hooks/#{hook_id}/key"
13
+ end
14
+
15
+ def call
16
+ uri = URI(@api_path)
17
+ request = prepare_a_request(uri)
18
+ http = connect_to_api(uri)
19
+ response = http.request(request)
20
+ return WebhookResponse.new(success: true, body: response.body) if response.is_a?(Net::HTTPOK)
21
+ return WebhookResponse.new(success: false, body: [response.code, response.message])
22
+ end
23
+
24
+ private
25
+
26
+ def prepare_a_request(uri)
27
+ request = Net::HTTP::Get.new(uri)
28
+ request.initialize_http_header(
29
+ {
30
+ "Accept" => "application/json",
31
+ "Content-Type" => "application/json",
32
+ "Authorization" => "Bearer #{@token}"
33
+ }
34
+ )
35
+ request
36
+ end
37
+
38
+ def connect_to_api(uri)
39
+ http = Net::HTTP.new(uri.host, uri.port)
40
+ http.use_ssl = true
41
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
42
+ http
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "qiwi_observer/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "qiwi_observer"
8
+ spec.version = QiwiObserver::VERSION
9
+ spec.authors = ["Izhanov"]
10
+ spec.email = ["ibekable@gmail.com"]
11
+
12
+ spec.summary = %q{Allows you to automate the receipt of information about
13
+ your account in the service QIWI Wallet®}
14
+ spec.description = %q{Allows you to request information on the user profile,
15
+ payment history, wallet balance, etc.}
16
+ spec.homepage = 'https://github.com/izhanov/qiwi_observer'
17
+ spec.license = "MIT"
18
+
19
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
21
+ # if spec.respond_to?(:metadata)
22
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
23
+ # else
24
+ # raise "RubyGems 2.0 or newer is required to protect against " \
25
+ # "public gem pushes."
26
+ # end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_runtime_dependency "dry-configurable", "~> 0.7"
38
+
39
+ spec.add_development_dependency "webmock", "~> 3.3"
40
+ spec.add_development_dependency "bundler", "~> 2.0"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "rspec", "~> 3.0"
43
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qiwi_observer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Izhanov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: |-
84
+ Allows you to request information on the user profile,
85
+ payment history, wallet balance, etc.
86
+ email:
87
+ - ibekable@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".github/workflows/gempush.yml"
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - ".rubocop.yml"
96
+ - ".ruby-gemset"
97
+ - ".ruby-version"
98
+ - ".travis.yml"
99
+ - CODE_OF_CONDUCT.md
100
+ - Gemfile
101
+ - Gemfile.lock
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - bin/console
106
+ - bin/setup
107
+ - lib/qiwi_observer.rb
108
+ - lib/qiwi_observer/payments/payments.rb
109
+ - lib/qiwi_observer/payments/payments_response.rb
110
+ - lib/qiwi_observer/response.rb
111
+ - lib/qiwi_observer/transfer/transfer.rb
112
+ - lib/qiwi_observer/version.rb
113
+ - lib/qiwi_observer/webhook/active_webhook.rb
114
+ - lib/qiwi_observer/webhook/webhook.rb
115
+ - lib/qiwi_observer/webhook/webhook_registration.rb
116
+ - lib/qiwi_observer/webhook/webhook_removal.rb
117
+ - lib/qiwi_observer/webhook/webhook_response.rb
118
+ - lib/qiwi_observer/webhook/webhook_secret_key.rb
119
+ - qiwi_observer.gemspec
120
+ homepage: https://github.com/izhanov/qiwi_observer
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Allows you to automate the receipt of information about your account in the
143
+ service QIWI Wallet®
144
+ test_files: []