zooz 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,26 @@
1
+ Copyright (c) 2012, Michael Alexander
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
+
24
+ The views and conclusions contained in the software and documentation are those
25
+ of the authors and should not be interpreted as representing official policies,
26
+ either expressed or implied, of Miniand.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ ZooZ Ruby library
2
+ =================
3
+
4
+ The ZooZ Ruby library provides an interface to the ZooZ API. This library is
5
+ purely for API interaction and doesn't provide any Rails helpers. The upcoming
6
+ Rails ZooZ library will provide Rails helpers.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ ```
12
+ gem install zooz
13
+ ```
14
+
15
+ Opening a transaction
16
+ ---------------------
17
+
18
+ ```
19
+ require 'zooz'
20
+
21
+ req = Zooz::Request::Open.new
22
+
23
+ # Run in sandbox mode
24
+ req.sandox = true
25
+
26
+ # Enter your details
27
+ req.unique_id = 'your-unique-id-here'
28
+ req.app_key = 'your-app-key-here'
29
+
30
+ # Set the details of the transaction
31
+ req.currency_code = 'USD'
32
+ req.amount = 99.99
33
+
34
+ # Make the request
35
+ resp = req.request
36
+ unless resp
37
+ raise "Therre was a problem opening a transaction:\n#{req.errors.join(', ')}"
38
+ end
39
+
40
+ # Use the token and session_token to provide an interface to the customer
41
+ token = resp.token
42
+ session_token = resp.session_token
43
+ ```
44
+
45
+ Verifying a transaction
46
+ -----------------------
47
+
48
+ Coming soon.
@@ -0,0 +1,43 @@
1
+ require 'zooz/request'
2
+ require 'zooz/response/open'
3
+ require 'active_support/core_ext/module/delegation'
4
+
5
+ module Zooz
6
+ class Request
7
+ class Open
8
+ attr_accessor :amount, :currency_code, :requestor
9
+ attr_reader :errors
10
+ delegate :sandbox, :sandbox=, :unique_id, :unique_id=, :app_key,
11
+ :app_key=, :response_type, :response_type=, :cmd, :cmd=, :is_sandbox?,
12
+ :to => :requestor
13
+
14
+ def initialize
15
+ @errors = []
16
+ @requestor = Request.new
17
+ @requestor.cmd = 'openTrx'
18
+ end
19
+
20
+ def request
21
+ return false unless valid?
22
+ @requestor.set_param('amount', @amount)
23
+ @requestor.set_param('currencyCode', @currency_code)
24
+ open_response = Response::Open.new
25
+ open_response.request = self
26
+ open_response.response = @requestor.request
27
+ unless open_response.response
28
+ @errors += @requestor.errors
29
+ return false
30
+ end
31
+ open_response
32
+ end
33
+
34
+ def valid?
35
+ @errors = []
36
+ @errors << 'amount is required' if @amount.nil?
37
+ @errors << 'currency_code is required' if @currency_code.nil?
38
+ @errors += @requestor.errors unless @requestor.valid?
39
+ @errors.empty?
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,41 @@
1
+ require 'zooz/request'
2
+ require 'zooz/response/verify'
3
+ require 'active_support/core_ext/module/delegation'
4
+
5
+ module Zooz
6
+ class Request
7
+ class Verify
8
+ attr_accessor :trx_id
9
+ attr_reader :errors
10
+ delegate :sandbox, :sandbox=, :unique_id, :unique_id=, :app_key,
11
+ :app_key=, :response_type, :response_type=, :cmd, :cmd=, :is_sandbox?,
12
+ :to => :requestor
13
+
14
+ def initialize
15
+ @errors = []
16
+ @requestor = Request.new
17
+ @requestor.cmd = 'verifyTrx'
18
+ end
19
+
20
+ def request
21
+ return false unless valid?
22
+ @requestor.set_param('trxId', @trx_id)
23
+ verify_response = Response::Verify.new
24
+ verify_response.request = self
25
+ verify_response.response = @requestor.request
26
+ unless verify_response.response
27
+ @errors += @requestor.errors
28
+ return false
29
+ end
30
+ verify_response
31
+ end
32
+
33
+ def valid?
34
+ @errors = []
35
+ @errors << 'trx_id is required' if @trx_id.nil?
36
+ @errors += @requestor.errors unless @requestor.valid?
37
+ @errors.empty?
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,68 @@
1
+ require 'httparty'
2
+
3
+ # The ZooZ Request class formats and sends requests to the ZooZ API.
4
+ module Zooz
5
+ class Request
6
+ attr_accessor :sandbox, :unique_id, :app_key, :response_type, :cmd
7
+ attr_reader :errors, :params
8
+
9
+ def initialize
10
+ @params = {}
11
+ @errors = []
12
+ @response_type = 'NVP'
13
+ @sandbox = false
14
+ end
15
+
16
+ # Set a request parameter.
17
+ def set_param name, value
18
+ @params[name] = value
19
+ end
20
+
21
+ # Get a request parameter.
22
+ def get_param
23
+ @params[name]
24
+ end
25
+
26
+ # Whether the request will be sent to sandbox.
27
+ def is_sandbox?
28
+ @sandbox == true
29
+ end
30
+
31
+ # Get the URL of the API, based on whether in sandbox mode or not.
32
+ def url
33
+ (is_sandbox? ? 'https://sandbox.zooz.co' : 'https://app.zooz.com') +
34
+ '/mobile/SecuredWebServlet'
35
+ end
36
+
37
+ # Send a request to the server, returns a Zooz::Response object or false.
38
+ # If returning false, the @errors attribute is populated.
39
+ def request
40
+ return false unless valid?
41
+ http_response = HTTParty.post(url, :format => :plain,
42
+ :query => @params.merge({ :cmd => @cmd }),
43
+ :headers => {
44
+ 'ZooZ-Unique-ID' => @unique_id,
45
+ 'ZooZ-App-Key' => @app_key,
46
+ 'ZooZ-Response-Type' => @response_type,
47
+ })
48
+ response = Response.new
49
+ response.request = self
50
+ response.http_response = http_response
51
+ unless response.success?
52
+ @errors = response.errors
53
+ return false
54
+ end
55
+ response
56
+ end
57
+
58
+ # Whether the request object is valid for requesting.
59
+ def valid?
60
+ @errors = []
61
+ @errors << 'unique_id is required' if @unique_id.nil?
62
+ @errors << 'app_key is required' if @app_key.nil?
63
+ @errors << 'cmd is required' if @cmd.nil?
64
+ @errors << 'response_type is required' if @response_type.nil?
65
+ @errors.empty?
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,22 @@
1
+ require 'zooz/response'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module Zooz
5
+ class Response
6
+ class Open
7
+ attr_accessor :request, :response
8
+ delegate :success?, :errors, :parsed_response, :get_parsed_singular,
9
+ :to => :response
10
+ delegate :is_sandbox?, :unique_id, :app_key, :amount, :currency_code,
11
+ :to => :request
12
+
13
+ def token
14
+ get_parsed_singular('token')
15
+ end
16
+
17
+ def session_token
18
+ get_parsed_singular('sessionToken')
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ require 'zooz/response'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module Zooz
5
+ class Response
6
+ class Verify
7
+ attr_accessor :request, :response
8
+ delegate :success?, :errors, :parsed_response, :get_parsed_singular,
9
+ :to => :response
10
+ delegate :is_sandbox?, :unique_id, :app_key, :trx_id, :to => :request
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,54 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'cgi'
3
+
4
+ # The base level ZooZ response class encapsulates responses from the ZooZ API
5
+ # and reports on success and errors.
6
+ module Zooz
7
+ class Response
8
+ attr_accessor :http_response, :request
9
+ attr_reader :errors
10
+ delegate :body, :code, :message, :headers, :to => :http_response,
11
+ :prefix => :http
12
+ delegate :is_sandbox?, :unique_id, :app_key, :to => :request
13
+
14
+ def initialize
15
+ @errors = []
16
+ end
17
+
18
+ # Gets a singular offset from the response data, useful because CGI returns
19
+ # single parameters embedded in an array.
20
+ def get_parsed_singular(offset)
21
+ (parsed_response[offset] || []).first
22
+ end
23
+
24
+ # Get a parsed response as an object from the response text.
25
+ def parsed_response
26
+ CGI::parse(http_body.strip)
27
+ end
28
+
29
+ # Get the ZooZ status code, 0 for success.
30
+ def status_code
31
+ get_parsed_singular('statusCode')
32
+ end
33
+
34
+ # Get the ZooZ error message, returned when status_code is not 0.
35
+ def error_message
36
+ get_parsed_singular('errorMessage')
37
+ end
38
+
39
+ # Whether the request was successful, populates the @errors array on error.
40
+ def success?
41
+ @errors = []
42
+ unless http_code.to_s[0] == '2'
43
+ @errors << "HTTP status #{http_code}: #{http_message}"
44
+ end
45
+ unless status_code
46
+ @errors << "Unable to parse ZooZ response"
47
+ end
48
+ unless status_code == '0'
49
+ @errors << "ZooZ error #{status_code}: #{error_message}"
50
+ end
51
+ @errors.empty?
52
+ end
53
+ end
54
+ end
data/lib/zooz.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Require all requests
2
+ require 'zooz/request/open'
3
+ require 'zooz/request/verify'
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zooz
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Alexander
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: httparty
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.8.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.0
46
+ description: A ZooZ API library for Ruby
47
+ email: beefsack@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/zooz.rb
53
+ - lib/zooz/request/open.rb
54
+ - lib/zooz/request/verify.rb
55
+ - lib/zooz/response/open.rb
56
+ - lib/zooz/response/verify.rb
57
+ - lib/zooz/request.rb
58
+ - lib/zooz/response.rb
59
+ - LICENSE
60
+ - README.md
61
+ homepage: https://github.com/Miniand/zooz-ruby
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.23
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: ZooZ
85
+ test_files: []