payu_api 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 931ec6697b3b363bac41725f1ad0d7c9543511a3
4
- data.tar.gz: 62e64628366cdd28526086a39917615909b065a0
3
+ metadata.gz: f03e3bb9b1d9013eabac126fb67bca990a2c81ee
4
+ data.tar.gz: 4694854d9e6ca75a321db2d8149803e44f415874
5
5
  SHA512:
6
- metadata.gz: bf2a46f4dee68de7b82d7ba1ebde9cccf06be27ea136d3074d4c21a22da5a649624f84e26c0c8c72921d22a8e6e425b558bb2cfeb398bef8313fb7616ece86d7
7
- data.tar.gz: 9462c7a7ec77d5bfd63652aa069bda2a7fc4ffc81d6346558466055c91f779ab703ab4e1d5723ae77bff56e78af38ee1280f6162dede3162fbc009ef5ca76384
6
+ metadata.gz: f2c394eeb1ab1d44aac453984ffda95e2085a0f9a353d5180db20633fe3b1f6cb880b758384da8aa0532b291db9a16010b9aa01c65233fd5336a094220ba4af7
7
+ data.tar.gz: d935e7aa2f10f1406ca61c808b38ea36b257acf2dd20d83375bb82f057abc6a0271abf4c7fdb267e5f81867d80dc492fbb283a8b9e12c3ff86fc914a2bc53c2c
data/.rubocop.yml CHANGED
@@ -10,3 +10,10 @@ Style/Documentation:
10
10
 
11
11
  Style/WordArray:
12
12
  Enabled: false
13
+
14
+ Style/ClassAndModuleChildren:
15
+ Enabled: false
16
+
17
+ Metrics/MethodLength:
18
+ Exclude:
19
+ - 'test/**/*'
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # PayU REST API
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/payu_api.svg)](https://badge.fury.io/rb/payu_api)
4
+ [![Build Status](https://travis-ci.org/busfor/payu_api.svg?branch=master)](https://travis-ci.org/busfor/payu_api)
5
+
3
6
  Ruby wrapper for PayU REST API.
4
7
 
5
8
  ## Installation
@@ -20,14 +23,25 @@ Or install it yourself as:
20
23
 
21
24
  ## Usage
22
25
 
26
+ Get OAuth token:
27
+
28
+ ```ruby
29
+ response = PayuAPI.authorize(pos_id: '300046', key: '098f6b...', sandbox: true)
30
+
31
+ response.success?
32
+ # => true
33
+
34
+ response.auth_token
35
+ # => "c8d4b7..."
36
+
37
+ response.expires_in
38
+ # => 43199
39
+ ```
40
+
23
41
  Create client:
24
42
 
25
43
  ```ruby
26
- client = PayuAPI::Client.new(
27
- pos_id: '300046',
28
- auth_token: 'c8d4b7ac61758704f38ed5564d8c0ae0',
29
- sandbox: true
30
- )
44
+ client = PayuAPI::Client.new(pos_id: '300046', auth_token: 'c8d4b7...', sandbox: true)
31
45
  ```
32
46
 
33
47
  Creating a new order:
data/lib/payu_api.rb CHANGED
@@ -5,9 +5,19 @@ require 'dry-initializer'
5
5
  require 'payu_api/version'
6
6
  require 'payu_api/errors'
7
7
  require 'payu_api/request'
8
+ require 'payu_api/api_request'
9
+ require 'payu_api/auth_request'
8
10
  require 'payu_api/response'
11
+ require 'payu_api/responses/auth_response'
9
12
  require 'payu_api/responses/get_response'
10
13
  require 'payu_api/responses/create_response'
11
14
  require 'payu_api/responses/refund_response'
12
15
  require 'payu_api/order'
13
16
  require 'payu_api/client'
17
+
18
+ module PayuAPI
19
+ def self.authorize(pos_id:, key:, sandbox: false)
20
+ request = AuthRequest.new(pos_id: pos_id, key: key, sandbox: sandbox)
21
+ AuthResponse.new(http_response: request.call)
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ module PayuAPI
2
+ class ApiRequest < Request
3
+ extend Dry::Initializer::Mixin
4
+
5
+ param :client
6
+ param :method
7
+ param :url
8
+ param :params, default: proc { nil }
9
+
10
+ private
11
+
12
+ def sandbox?
13
+ client.sandbox
14
+ end
15
+
16
+ def headers
17
+ {
18
+ 'Content-Type' => 'application/json',
19
+ 'Authorization' => "Bearer #{client.auth_token}"
20
+ }
21
+ end
22
+
23
+ def body
24
+ JSON.generate(params) if params
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ module PayuAPI
2
+ class AuthRequest < Request
3
+ extend Dry::Initializer::Mixin
4
+
5
+ option :pos_id
6
+ option :key
7
+ option :sandbox, default: proc { false }
8
+
9
+ private
10
+
11
+ def sandbox?
12
+ sandbox
13
+ end
14
+
15
+ def method
16
+ :POST
17
+ end
18
+
19
+ def url
20
+ '/pl/standard/user/oauth/authorize'
21
+ end
22
+
23
+ def headers
24
+ { 'Content-Type' => 'application/x-www-form-urlencoded' }
25
+ end
26
+
27
+ def body
28
+ "grant_type=client_credentials&client_id=#{pos_id}&client_secret=#{key}"
29
+ end
30
+ end
31
+ end
@@ -2,16 +2,16 @@ module PayuAPI
2
2
  class Order
3
3
  class << self
4
4
  def get(client:, order_id:)
5
- request = Request.new(client: client, method: 'GET', url: "/api/v2_1/orders/#{order_id}")
6
- GetResponse.new(http_response: request.send)
5
+ request = ApiRequest.new(client: client, method: 'GET', url: "/api/v2_1/orders/#{order_id}")
6
+ GetResponse.new(http_response: request.call)
7
7
  end
8
8
 
9
9
  def create(client:, params:)
10
10
  post_params = params.merge(
11
11
  merchantPosId: client.pos_id
12
12
  )
13
- request = Request.new(client, :POST, '/api/v2_1/orders', post_params)
14
- CreateResponse.new(http_response: request.send)
13
+ request = ApiRequest.new(client, :POST, '/api/v2_1/orders', post_params)
14
+ CreateResponse.new(http_response: request.call)
15
15
  end
16
16
 
17
17
  def capture(client:, order_id:)
@@ -19,13 +19,13 @@ module PayuAPI
19
19
  orderId: order_id,
20
20
  orderStatus: 'COMPLETED'
21
21
  }
22
- request = Request.new(client, :PUT, "/api/v2_1/orders/#{order_id}/status", params)
23
- Response.new(http_response: request.send)
22
+ request = ApiRequest.new(client, :PUT, "/api/v2_1/orders/#{order_id}/status", params)
23
+ Response.new(http_response: request.call)
24
24
  end
25
25
 
26
26
  def cancel(client:, order_id:)
27
- request = Request.new(client, :DELETE, "/api/v2_1/orders/#{order_id}")
28
- Response.new(http_response: request.send)
27
+ request = ApiRequest.new(client, :DELETE, "/api/v2_1/orders/#{order_id}")
28
+ Response.new(http_response: request.call)
29
29
  end
30
30
 
31
31
  def refund(client:, order_id:, params:)
@@ -33,8 +33,8 @@ module PayuAPI
33
33
  orderId: order_id,
34
34
  refund: params
35
35
  }
36
- request = Request.new(client, :POST, "/api/v2_1/orders/#{order_id}/refunds", post_params)
37
- RefundResponse.new(http_response: request.send)
36
+ request = ApiRequest.new(client, :POST, "/api/v2_1/orders/#{order_id}/refunds", post_params)
37
+ RefundResponse.new(http_response: request.call)
38
38
  end
39
39
  end
40
40
  end
@@ -1,23 +1,17 @@
1
1
  module PayuAPI
2
2
  class Request
3
- extend Dry::Initializer::Mixin
4
-
5
- param :client
6
- param :method
7
- param :url
8
- param :params, default: proc { nil }
9
-
10
3
  API_URL = 'https://secure.payu.com/'.freeze
11
4
  API_SANDBOX_URL = 'https://secure.snd.payu.com/'.freeze
12
5
 
13
6
  # rubocop:disable Metrics/AbcSize
14
- def send
7
+ def call
15
8
  connection = Faraday::Connection.new(api_url)
16
9
  connection.public_send(method.to_s.downcase) do |request|
17
10
  request.url url
18
- request.headers['Content-Type'] = 'application/json'
19
- request.headers['Authorization'] = "Bearer #{client.auth_token}"
20
- request.body = JSON.generate(params) if params
11
+ request.body = body if body
12
+ headers.each do |key, value|
13
+ request.headers[key] = value
14
+ end
21
15
  end
22
16
  rescue Faraday::Error => e
23
17
  raise RequestError, e.message
@@ -26,7 +20,27 @@ module PayuAPI
26
20
  private
27
21
 
28
22
  def api_url
29
- client.sandbox ? API_SANDBOX_URL : API_URL
23
+ sandbox? ? API_SANDBOX_URL : API_URL
24
+ end
25
+
26
+ def sandbox?
27
+ raise NotImplementedError
28
+ end
29
+
30
+ def method
31
+ raise NotImplementedError
32
+ end
33
+
34
+ def url
35
+ raise NotImplementedError
36
+ end
37
+
38
+ def headers
39
+ {}
40
+ end
41
+
42
+ def body
43
+ nil
30
44
  end
31
45
  end
32
46
  end
@@ -16,25 +16,27 @@ module PayuAPI
16
16
  end
17
17
 
18
18
  def error_code
19
+ return unless error?
19
20
  body[:error] || status_code
20
21
  end
21
22
 
22
23
  def error_message
24
+ return unless error?
23
25
  body[:error_description] || status_description
24
26
  end
25
27
 
26
28
  private
27
29
 
28
30
  def http_success?
29
- SUCCESS_HTTP_STATUSES.include?(http_status)
31
+ self.class::SUCCESS_HTTP_STATUSES.include?(http_status)
30
32
  end
31
33
 
32
34
  def http_status
33
- http_response.status
35
+ @http_status ||= http_response.status
34
36
  end
35
37
 
36
38
  def status_success?
37
- SUCCESS_STATUSES.include?(status_code)
39
+ self.class::SUCCESS_STATUSES.include?(status_code)
38
40
  end
39
41
 
40
42
  def status_code
@@ -50,13 +52,17 @@ module PayuAPI
50
52
  end
51
53
 
52
54
  def body
53
- return unless http_response.body
55
+ return unless raw_body
54
56
  @body ||=
55
57
  begin
56
- JSON.parse(http_response.body, symbolize_names: true)
58
+ JSON.parse(raw_body, symbolize_names: true)
57
59
  rescue => e
58
60
  raise InvalidResponseError, e.message
59
61
  end
60
62
  end
63
+
64
+ def raw_body
65
+ @raw_body ||= http_response.body
66
+ end
61
67
  end
62
68
  end
@@ -0,0 +1,15 @@
1
+ module PayuAPI
2
+ class AuthResponse < Response
3
+ def success?
4
+ http_success?
5
+ end
6
+
7
+ def auth_token
8
+ body[:access_token]
9
+ end
10
+
11
+ def expires_in
12
+ body[:expires_in]
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module PayuAPI
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
data/payu_api.gemspec CHANGED
@@ -25,5 +25,8 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'bundler', '~> 1.12'
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
27
  spec.add_development_dependency 'minitest', '~> 5.0'
28
+ spec.add_development_dependency 'webmock', '~> 2.1'
29
+ spec.add_development_dependency 'simplecov', '~> 0.12'
28
30
  spec.add_development_dependency 'rubocop', '~> 0.42'
31
+ spec.add_development_dependency 'byebug'
29
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: payu_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Khrebtov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-08 00:00:00.000000000 Z
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-initializer
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.12'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.12'
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: rubocop
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +122,20 @@ dependencies:
94
122
  - - "~>"
95
123
  - !ruby/object:Gem::Version
96
124
  version: '0.42'
125
+ - !ruby/object:Gem::Dependency
126
+ name: byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
97
139
  description: Ruby wrapper for PayU REST API
98
140
  email:
99
141
  - roman@alltmb.ru
@@ -111,11 +153,14 @@ files:
111
153
  - bin/console
112
154
  - bin/setup
113
155
  - lib/payu_api.rb
156
+ - lib/payu_api/api_request.rb
157
+ - lib/payu_api/auth_request.rb
114
158
  - lib/payu_api/client.rb
115
159
  - lib/payu_api/errors.rb
116
160
  - lib/payu_api/order.rb
117
161
  - lib/payu_api/request.rb
118
162
  - lib/payu_api/response.rb
163
+ - lib/payu_api/responses/auth_response.rb
119
164
  - lib/payu_api/responses/create_response.rb
120
165
  - lib/payu_api/responses/get_response.rb
121
166
  - lib/payu_api/responses/refund_response.rb