qiwi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activesupport'
4
+ gem 'activemodel'
5
+ gem 'nokogiri'
6
+ gem 'faraday'
7
+ gem 'rack'
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem 'webmock'
13
+ gem "rspec", "~> 2.11.0"
14
+ gem "yard", "~> 0.7"
15
+ gem "bundler", "~> 1.0"
16
+ gem "jeweler", "~> 1.8.4"
17
+ gem 'guard-rspec'
18
+ gem 'rb-fsevent', '~> 0.9.1'
19
+ gem 'growl'
20
+ end
21
+
22
+ gem 'simplecov', :require => false, :group => :test
@@ -0,0 +1,85 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.8)
5
+ activesupport (= 3.2.8)
6
+ builder (~> 3.0.0)
7
+ activesupport (3.2.8)
8
+ i18n (~> 0.6)
9
+ multi_json (~> 1.0)
10
+ addressable (2.3.2)
11
+ builder (3.0.4)
12
+ coderay (1.0.8)
13
+ crack (0.3.1)
14
+ diff-lcs (1.1.3)
15
+ faraday (0.8.4)
16
+ multipart-post (~> 1.1)
17
+ git (1.2.5)
18
+ growl (1.0.3)
19
+ guard (1.5.2)
20
+ listen (>= 0.4.2)
21
+ lumberjack (>= 1.0.2)
22
+ pry (>= 0.9.10)
23
+ thor (>= 0.14.6)
24
+ guard-rspec (2.1.1)
25
+ guard (>= 1.1)
26
+ rspec (~> 2.11)
27
+ i18n (0.6.1)
28
+ jeweler (1.8.4)
29
+ bundler (~> 1.0)
30
+ git (>= 1.2.5)
31
+ rake
32
+ rdoc
33
+ json (1.7.5)
34
+ listen (0.5.3)
35
+ lumberjack (1.0.2)
36
+ method_source (0.8.1)
37
+ multi_json (1.3.6)
38
+ multipart-post (1.1.5)
39
+ nokogiri (1.5.5)
40
+ pry (0.9.10)
41
+ coderay (~> 1.0.5)
42
+ method_source (~> 0.8)
43
+ slop (~> 3.3.1)
44
+ rack (1.4.1)
45
+ rake (0.9.2.2)
46
+ rb-fsevent (0.9.2)
47
+ rdoc (3.12)
48
+ json (~> 1.4)
49
+ rspec (2.11.0)
50
+ rspec-core (~> 2.11.0)
51
+ rspec-expectations (~> 2.11.0)
52
+ rspec-mocks (~> 2.11.0)
53
+ rspec-core (2.11.1)
54
+ rspec-expectations (2.11.3)
55
+ diff-lcs (~> 1.1.3)
56
+ rspec-mocks (2.11.3)
57
+ simplecov (0.7.1)
58
+ multi_json (~> 1.0)
59
+ simplecov-html (~> 0.7.1)
60
+ simplecov-html (0.7.1)
61
+ slop (3.3.3)
62
+ thor (0.16.0)
63
+ webmock (1.8.11)
64
+ addressable (>= 2.2.7)
65
+ crack (>= 0.1.7)
66
+ yard (0.8.3)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ activemodel
73
+ activesupport
74
+ bundler (~> 1.0)
75
+ faraday
76
+ growl
77
+ guard-rspec
78
+ jeweler (~> 1.8.4)
79
+ nokogiri
80
+ rack
81
+ rb-fsevent (~> 0.9.1)
82
+ rspec (~> 2.11.0)
83
+ simplecov
84
+ webmock
85
+ yard (~> 0.7)
@@ -0,0 +1,5 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/qiwi/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Roman Shterenzon
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # qiwi
2
+
3
+ A framework for integration with the Qiwi payments platform.
4
+
5
+ You just need to provide the authentication details and transaction handler in you configuration,
6
+ and you're set.
7
+ For example, in Rails that would go into config/initializers/qiwi.rb:
8
+
9
+ ```ruby
10
+ Qiwi.configure do |config|
11
+ config.login = 'mylogin'
12
+ config.password = 'secret'
13
+ config.logger = Rails.logger
14
+ config.transaction_handler = lambda do |txn|
15
+ # The finder should respond to :find_by_txn, maybe an ActiveRecord model
16
+ txn.finder = PendingTransaction
17
+ # Observers that will be notified when the transaction is committed.
18
+ # See Observable
19
+ txn.add_observer(PendingTransaction, :commit_transaction)
20
+ txn.add_observer(TransactionMailer, :transaction)
21
+ end
22
+ end
23
+ ```
24
+
25
+ Example commit_transaction implementation:
26
+
27
+ ```ruby
28
+ class PendingTransaction < ActiveRecord::Base
29
+ # @param [Qiwi::Transaction] txn
30
+ def self.commit_transaction(txn)
31
+ if txn.valid?
32
+ txn.persisted.update_attribute(:approved, true)
33
+ else
34
+ # Notify the user by e-mail
35
+ end
36
+ # Rescue from exceptions, so other observers will still get an update
37
+ rescue => e
38
+ logger.error e.message
39
+ end
40
+ end
41
+ ```
42
+
43
+ It exposes the /qiwi endpoint which can be consumed by the Qiwi service.
44
+
45
+ Calling the Qiwi service is as simple as:
46
+
47
+ ```ruby
48
+ client = Qiwi::Client.new
49
+ # See Qiwi::Request::CreateBill#initialize for all the parameters
50
+ client.create_bill(user: 'user', amount: 1000.0, comment: 'comment', txn: 'txnid')
51
+ client.check_bill(txn: 'txnid')
52
+ client.cancel_bill(txn: 'txnid')
53
+ client.get_bill_list(date_from: Time.now - 1209600, date_to: Time.now, status: 50)
54
+ ```
55
+
56
+ ## Contributing to qiwi
57
+
58
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
59
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
60
+ * Fork the project.
61
+ * Start a feature/bugfix branch.
62
+ * Commit and push until you are happy with your contribution.
63
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
64
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
65
+
66
+ ## Copyright
67
+
68
+ Copyright (c) 2012 Roman Shterenzon. See LICENSE.txt for
69
+ further details.
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "qiwi"
18
+ gem.homepage = "http://github.com/romanbsd/qiwi"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Qiwi payments solution}
21
+ gem.description = gem.summary
22
+ gem.email = "romanbsd@yahoo.com"
23
+ gem.authors = ["Roman Shterenzon"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ mount Qiwi::Engine => '/qiwi'
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'qiwi/config'
2
+ require 'qiwi/client'
3
+ require 'qiwi/engine' if defined?(Rails)
4
+
5
+ module Qiwi
6
+ VERSION = File.read(File.expand_path('../../VERSION', __FILE__)).strip.freeze
7
+
8
+ def self.logger
9
+ config.logger
10
+ end
11
+ end
@@ -0,0 +1,107 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <wsdl:definitions name="IShopClientWSService" targetNamespace="http://client.ishop.mw.ru/" xmlns:tns="http://client.ishop.mw.ru/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
3
+ <wsdl:types>
4
+ <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://client.ishop.mw.ru/" attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://client.ishop.mw.ru/">
5
+ <xs:element name="updateBill" type="tns:updateBill"/>
6
+ <xs:element name="updateBillResponse" type="tns:updateBillResponse"/>
7
+ <xs:complexType name="updateBill">
8
+ <xs:sequence>
9
+ <xs:element minOccurs="0" name="login" type="xs:string"/>
10
+ <xs:element minOccurs="0" name="password" type="xs:string"/>
11
+ <xs:element minOccurs="0" name="txn" type="xs:string"/>
12
+ <xs:element name="status" type="xs:int"/>
13
+ </xs:sequence>
14
+ </xs:complexType>
15
+ <xs:complexType name="updateBillResponse">
16
+ <xs:sequence>
17
+ <xs:element name="updateBillResult" type="xs:int"/>
18
+ </xs:sequence>
19
+ </xs:complexType>
20
+ <xs:element name="updateBillExt" type="tns:updateBillExt"/>
21
+ <xs:element name="updateBillExtResponse" type="tns:updateBillExtResponse"/>
22
+
23
+ <xs:complexType name="Param">
24
+ <xs:sequence>
25
+ <xs:element name="name" nillable="false" type="xsd:string"/>
26
+ <xs:element name="value" nillable="false" type="xsd:string"/>
27
+ </xs:sequence>
28
+ </xs:complexType>
29
+ <xs:complexType name="ArrayOfParams">
30
+ <xs:sequence>
31
+ <xs:element maxOccurs="unbounded" minOccurs="0" name="item" type="tns:Param"/>
32
+ </xs:sequence>
33
+ </xs:complexType>
34
+
35
+ <xs:complexType name="updateBillExt">
36
+ <xs:sequence>
37
+ <xs:element minOccurs="0" name="login" type="xs:string"/>
38
+ <xs:element minOccurs="0" name="password" type="xs:string"/>
39
+ <xs:element minOccurs="0" name="txn" type="xs:string"/>
40
+ <xs:element name="status" type="xs:int"/>
41
+ <xs:element name="params" nillable="false" type="tns:ArrayOfParams"/>
42
+ </xs:sequence>
43
+ </xs:complexType>
44
+ <xs:complexType name="updateBillExtResponse">
45
+ <xs:sequence>
46
+ <xs:element name="updateBillExtResult" type="xs:int"/>
47
+ </xs:sequence>
48
+ </xs:complexType>
49
+ </xs:schema>
50
+ </wsdl:types>
51
+ <wsdl:message name="updateBillResponse">
52
+ <wsdl:part name="parameters" element="tns:updateBillResponse">
53
+ </wsdl:part>
54
+ </wsdl:message>
55
+ <wsdl:message name="updateBill">
56
+ <wsdl:part name="parameters" element="tns:updateBill">
57
+ </wsdl:part>
58
+ </wsdl:message>
59
+ <wsdl:message name="updateBillExtResponse">
60
+ <wsdl:part name="parameters" element="tns:updateBillExtResponse">
61
+ </wsdl:part>
62
+ </wsdl:message>
63
+ <wsdl:message name="updateBillExt">
64
+ <wsdl:part name="parameters" element="tns:updateBillExt">
65
+ </wsdl:part>
66
+ </wsdl:message>
67
+ <wsdl:portType name="IShopClientWS">
68
+ <wsdl:operation name="updateBill">
69
+ <wsdl:input name="updateBill" message="tns:updateBill">
70
+ </wsdl:input>
71
+ <wsdl:output name="updateBillResponse" message="tns:updateBillResponse">
72
+ </wsdl:output>
73
+ </wsdl:operation>
74
+ <wsdl:operation name="updateBillExt">
75
+ <wsdl:input name="updateBillExt" message="tns:updateBillExt">
76
+ </wsdl:input>
77
+ <wsdl:output name="updateBillExtResponse" message="tns:updateBillExtResponse">
78
+ </wsdl:output>
79
+ </wsdl:operation>
80
+ </wsdl:portType>
81
+ <wsdl:binding name="IShopClientWSServiceSoapBinding" type="tns:IShopClientWS">
82
+ <soap12:binding style="document" transport="http://www.w3.org/2003/05/soap/bindings/HTTP/"/>
83
+ <wsdl:operation name="updateBill">
84
+ <soap12:operation soapAction="" style="document"/>
85
+ <wsdl:input name="updateBill">
86
+ <soap12:body use="literal"/>
87
+ </wsdl:input>
88
+ <wsdl:output name="updateBillResponse">
89
+ <soap12:body use="literal"/>
90
+ </wsdl:output>
91
+ </wsdl:operation>
92
+ <wsdl:operation name="updateBillExt">
93
+ <soap12:operation soapAction="" style="document"/>
94
+ <wsdl:input name="updateBillExt">
95
+ <soap12:body use="literal"/>
96
+ </wsdl:input>
97
+ <wsdl:output name="updateBillExtResponse">
98
+ <soap12:body use="literal"/>
99
+ </wsdl:output>
100
+ </wsdl:operation>
101
+ </wsdl:binding>
102
+ <wsdl:service name="IShopClientWSService">
103
+ <wsdl:port name="IShopClientWSPort" binding="tns:IShopClientWSServiceSoapBinding">
104
+ <soap12:address location="http://localhost:9090/IShopClientWSPort"/>
105
+ </wsdl:port>
106
+ </wsdl:service>
107
+ </wsdl:definitions>
@@ -0,0 +1,106 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'active_model'
3
+ require 'nokogiri'
4
+ require 'faraday'
5
+ require 'qiwi/config'
6
+ require 'qiwi/request'
7
+
8
+ module Qiwi
9
+
10
+ class Client
11
+ ENDPOINT = 'https://ishop.qiwi.ru/services/ishop'.freeze
12
+ HEADERS = {'Content-Type' => 'application/soap+xml'}.freeze
13
+
14
+ attr_reader :login, :password, :endpoint
15
+ def initialize(login = nil, password = nil, endpoint = nil)
16
+ @login = login || Qiwi.config.login
17
+ @password = password || Qiwi.config.password
18
+ @endpoint = endpoint || Qiwi.config.endpoint || ENDPOINT
19
+ raise ArgumentError.new("Missing login or password") unless @login and @password
20
+ end
21
+
22
+ [:create_bill, :cancel_bill, :check_bill, :get_bill_list].each do |method|
23
+ class_eval <<-EOF, __FILE__, __LINE__ + 1
24
+ def #{method}(params)
25
+ request = Request::#{method.to_s.classify}.new(self, params)
26
+ if request.valid?
27
+ perform(request)
28
+ else
29
+ raise InvalidRequestError.new(request.errors)
30
+ end
31
+ end
32
+ EOF
33
+ end
34
+
35
+ # Advanced methods
36
+ # :create_bill_ccy
37
+ # :cancel_bill_payed_amount
38
+ # :create_bill_ext
39
+
40
+ private
41
+
42
+ def perform(request)
43
+ body = request.body
44
+ conn = Faraday.new
45
+ response = conn.post(@endpoint, body.to_xml, HEADERS)
46
+ unless response.success?
47
+ handle_error(response)
48
+ return
49
+ end
50
+ xml = Nokogiri::XML(response.body).remove_namespaces!
51
+ request.result_from_xml(xml)
52
+ # rescue
53
+ # raise ServerError.new(response)
54
+ end
55
+
56
+ def handle_error(response)
57
+ el = nil
58
+ begin
59
+ xml = Nokogiri::XML(response.body).remove_namespaces!
60
+ xpath = '/Envelope/Body/Fault'
61
+ el = xml.at(xpath)
62
+ rescue
63
+ # If it's an invalid XML response
64
+ raise ServerError.new(response)
65
+ end
66
+
67
+ raise ServerError.new(response) unless el
68
+ code = el.xpath('Code/Value').text
69
+ string = el.xpath('Reason/Text').text
70
+ raise SOAPError.new(code, string)
71
+ end
72
+
73
+ end
74
+
75
+
76
+ class SOAPError < StandardError
77
+ attr_reader :code
78
+ def initialize(code, string)
79
+ @code = code
80
+ super(string)
81
+ end
82
+ end
83
+
84
+ class InvalidRequestError < StandardError
85
+ # @param [ActiveModel::Errors] errors
86
+ def initialize(errors)
87
+ super(errors.full_messages.join("\n"))
88
+ end
89
+ end
90
+
91
+ class ServerError < StandardError
92
+ attr_reader :response
93
+ def initialize(response)
94
+ super(response.body)
95
+ end
96
+ end
97
+
98
+ end
99
+
100
+ if $0 == __FILE__
101
+ client = Qiwi::Client.new('test', 'pass', 'http://localhost:8088/mock')
102
+ puts client.create_bill(user: 'user', amount: 1000, comment: 'comment', txn: 'txn').inspect
103
+ puts client.cancel_bill(txn: 'txn').inspect
104
+ puts client.check_bill(txn: 'txn').inspect
105
+ puts client.get_bill_list(date_from: Time.now - 1209600, date_to: Time.now, status: 50)
106
+ end