payu_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 931ec6697b3b363bac41725f1ad0d7c9543511a3
4
+ data.tar.gz: 62e64628366cdd28526086a39917615909b065a0
5
+ SHA512:
6
+ metadata.gz: bf2a46f4dee68de7b82d7ba1ebde9cccf06be27ea136d3074d4c21a22da5a649624f84e26c0c8c72921d22a8e6e425b558bb2cfeb398bef8313fb7616ece86d7
7
+ data.tar.gz: 9462c7a7ec77d5bfd63652aa069bda2a7fc4ffc81d6346558466055c91f779ab703ab4e1d5723ae77bff56e78af38ee1280f6162dede3162fbc009ef5ca76384
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.ruby-gemset
11
+ /.ruby-version
data/.rubocop.yml ADDED
@@ -0,0 +1,12 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+
5
+ Metrics/LineLength:
6
+ Max: 120
7
+
8
+ Style/Documentation:
9
+ Enabled: false
10
+
11
+ Style/WordArray:
12
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ - 2.2.5
6
+ - 2.3.1
7
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in payu_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,164 @@
1
+ # PayU REST API
2
+
3
+ Ruby wrapper for PayU REST API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'payu_api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install payu_api
20
+
21
+ ## Usage
22
+
23
+ Create client:
24
+
25
+ ```ruby
26
+ client = PayuAPI::Client.new(
27
+ pos_id: '300046',
28
+ auth_token: 'c8d4b7ac61758704f38ed5564d8c0ae0',
29
+ sandbox: true
30
+ )
31
+ ```
32
+
33
+ Creating a new order:
34
+
35
+ ```ruby
36
+ response = client.create_order(
37
+ continueUrl: 'https://your.eshop.com/orders/5kr-120',
38
+ customerIp: '127.0.0.1',
39
+ description: 'RTV market',
40
+ currencyCode: 'PLN',
41
+ totalAmount: 15000,
42
+ extOrderId: '5kr-120',
43
+ buyer: {
44
+ email: 'john.doe@example.com',
45
+ firstName: 'John',
46
+ lastName: 'Doe',
47
+ language: 'en'
48
+ },
49
+ products: [
50
+ {
51
+ name: 'Wireless Mouse for Laptop',
52
+ unitPrice: 15000,
53
+ quantity: 1
54
+ }
55
+ ]
56
+ )
57
+
58
+ response.success?
59
+ # => true
60
+
61
+ response.order_id
62
+ # => "H9LL64F37H160126GUEST000P01"
63
+
64
+ response.redirect_uri
65
+ # => "https://secure.payu.com/pl/standard/co/summary?sessionId=..."
66
+ ```
67
+
68
+ With error:
69
+
70
+ ```ruby
71
+ response.success?
72
+ # => false
73
+
74
+ response.error?
75
+ # => true
76
+
77
+ response.error_code
78
+ # => "UNAUTHORIZED_REQUEST"
79
+
80
+ response.error_message
81
+ # => "No privileges to perform the request"
82
+ ```
83
+
84
+ Order capture:
85
+
86
+ ```ruby
87
+ response = client.capture(order_id: 'H9LL64F37H160126GUEST000P01')
88
+
89
+ response.success?
90
+ # => true
91
+ ```
92
+
93
+ Order cancellation:
94
+
95
+ ```ruby
96
+ response = client.cancel(order_id: 'H9LL64F37H160126GUEST000P01')
97
+
98
+ response.success?
99
+ # => true
100
+ ```
101
+
102
+ Refund:
103
+
104
+ ```ruby
105
+ response = client.refund(
106
+ order_id: 'H9LL64F37H160126GUEST000P01',
107
+ params: {
108
+ description: 'Refund',
109
+ currencyCode: 'PLN',
110
+ amount: 1000
111
+ }
112
+ )
113
+
114
+ response.success?
115
+ # => true
116
+
117
+ response.refund
118
+ # {
119
+ # refundId: "H9LL64F37H160126GUEST000P02",
120
+ # amount: 1000,
121
+ # currencyCode: "PLN",
122
+ # description: "Refund",
123
+ # creationDateTime: "2016-08-05T15:42:05.241+02:00",
124
+ # status: "PENDING",
125
+ # statusDateTime: "2016-08-05T15:42:05.241+02:00"
126
+ # }
127
+ ```
128
+
129
+ Get order info:
130
+
131
+ ```ruby
132
+ response = client.get_order(order_id: 'H9LL64F37H160126GUEST000P01')
133
+
134
+ response.success?
135
+ # => true
136
+
137
+ response.order
138
+ # {
139
+ # :orderId=>"H9LL64F37H160126GUEST000P01",
140
+ # :orderCreateDate=>"2016-08-05T15:42:05.241+02:00",
141
+ # :customerIp=>"127.0.0.1",
142
+ # :merchantPosId=>"145227",
143
+ # :description=>"RTV market",
144
+ # :currencyCode=>"PLN",
145
+ # :totalAmount=>"15000",
146
+ # :status=>"NEW",
147
+ # :products=>[
148
+ # {
149
+ # :name=>"Wireless Mouse for Laptop",
150
+ # :unitPrice=>"15000",
151
+ # :quantity=>"1"
152
+ # }
153
+ # ]
154
+ # }
155
+ ```
156
+
157
+ ## Contributing
158
+
159
+ Bug reports and pull requests are welcome on GitHub at https://github.com/busfor/payu_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
160
+
161
+
162
+ ## License
163
+
164
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'payu_api'
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
data/bin/setup ADDED
@@ -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
data/lib/payu_api.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'json'
2
+ require 'faraday'
3
+ require 'dry-initializer'
4
+
5
+ require 'payu_api/version'
6
+ require 'payu_api/errors'
7
+ require 'payu_api/request'
8
+ require 'payu_api/response'
9
+ require 'payu_api/responses/get_response'
10
+ require 'payu_api/responses/create_response'
11
+ require 'payu_api/responses/refund_response'
12
+ require 'payu_api/order'
13
+ require 'payu_api/client'
@@ -0,0 +1,29 @@
1
+ module PayuAPI
2
+ class Client
3
+ extend Dry::Initializer::Mixin
4
+
5
+ option :pos_id
6
+ option :auth_token
7
+ option :sandbox, default: proc { false }
8
+
9
+ def create_order(order_params)
10
+ Order.create(client: self, params: order_params)
11
+ end
12
+
13
+ def get_order(order_id:)
14
+ Order.get(client: self, order_id: order_id)
15
+ end
16
+
17
+ def capture(order_id:)
18
+ Order.capture(client: self, order_id: order_id)
19
+ end
20
+
21
+ def cancel(order_id:)
22
+ Order.cancel(client: self, order_id: order_id)
23
+ end
24
+
25
+ def refund(order_id:, params:)
26
+ Order.refund(client: self, order_id: order_id, params: params)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ module PayuAPI
2
+ class Error < StandardError
3
+ end
4
+
5
+ class RequestError < Error
6
+ end
7
+
8
+ class InvalidResponseError < Error
9
+ end
10
+ end
@@ -0,0 +1,41 @@
1
+ module PayuAPI
2
+ class Order
3
+ class << self
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)
7
+ end
8
+
9
+ def create(client:, params:)
10
+ post_params = params.merge(
11
+ merchantPosId: client.pos_id
12
+ )
13
+ request = Request.new(client, :POST, '/api/v2_1/orders', post_params)
14
+ CreateResponse.new(http_response: request.send)
15
+ end
16
+
17
+ def capture(client:, order_id:)
18
+ params = {
19
+ orderId: order_id,
20
+ orderStatus: 'COMPLETED'
21
+ }
22
+ request = Request.new(client, :PUT, "/api/v2_1/orders/#{order_id}/status", params)
23
+ Response.new(http_response: request.send)
24
+ end
25
+
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)
29
+ end
30
+
31
+ def refund(client:, order_id:, params:)
32
+ post_params = {
33
+ orderId: order_id,
34
+ refund: params
35
+ }
36
+ request = Request.new(client, :POST, "/api/v2_1/orders/#{order_id}/refunds", post_params)
37
+ RefundResponse.new(http_response: request.send)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,32 @@
1
+ module PayuAPI
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
+ API_URL = 'https://secure.payu.com/'.freeze
11
+ API_SANDBOX_URL = 'https://secure.snd.payu.com/'.freeze
12
+
13
+ # rubocop:disable Metrics/AbcSize
14
+ def send
15
+ connection = Faraday::Connection.new(api_url)
16
+ connection.public_send(method.to_s.downcase) do |request|
17
+ 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
21
+ end
22
+ rescue Faraday::Error => e
23
+ raise RequestError, e.message
24
+ end
25
+
26
+ private
27
+
28
+ def api_url
29
+ client.sandbox ? API_SANDBOX_URL : API_URL
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,62 @@
1
+ module PayuAPI
2
+ class Response
3
+ extend Dry::Initializer::Mixin
4
+
5
+ option :http_response
6
+
7
+ SUCCESS_HTTP_STATUSES = [200].freeze
8
+ SUCCESS_STATUSES = ['SUCCESS'].freeze
9
+
10
+ def success?
11
+ http_success? && status_success?
12
+ end
13
+
14
+ def error?
15
+ !success?
16
+ end
17
+
18
+ def error_code
19
+ body[:error] || status_code
20
+ end
21
+
22
+ def error_message
23
+ body[:error_description] || status_description
24
+ end
25
+
26
+ private
27
+
28
+ def http_success?
29
+ SUCCESS_HTTP_STATUSES.include?(http_status)
30
+ end
31
+
32
+ def http_status
33
+ http_response.status
34
+ end
35
+
36
+ def status_success?
37
+ SUCCESS_STATUSES.include?(status_code)
38
+ end
39
+
40
+ def status_code
41
+ status[:statusCode]
42
+ end
43
+
44
+ def status_description
45
+ status[:statusDesc]
46
+ end
47
+
48
+ def status
49
+ body[:status] || {}
50
+ end
51
+
52
+ def body
53
+ return unless http_response.body
54
+ @body ||=
55
+ begin
56
+ JSON.parse(http_response.body, symbolize_names: true)
57
+ rescue => e
58
+ raise InvalidResponseError, e.message
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ module PayuAPI
2
+ class CreateResponse < Response
3
+ SUCCESS_HTTP_STATUSES = [200, 302].freeze
4
+ SUCCESS_STATUSES = [
5
+ 'SUCCESS',
6
+ 'WARNING_CONTINUE_3DS',
7
+ 'WARNING_CONTINUE_CVV'
8
+ ].freeze
9
+
10
+ def order_id
11
+ body[:orderId]
12
+ end
13
+
14
+ def redirect_uri
15
+ body[:redirectUri]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module PayuAPI
2
+ class GetResponse < Response
3
+ def order
4
+ body[:orders] && body[:orders][0]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module PayuAPI
2
+ class RefundResponse < Response
3
+ def refund
4
+ body[:refund]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module PayuAPI
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/payu_api.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'payu_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'payu_api'
8
+ spec.version = PayuAPI::VERSION
9
+ spec.authors = ['Roman Khrebtov']
10
+ spec.email = ['roman@alltmb.ru']
11
+
12
+ spec.summary = 'Ruby wrapper for PayU REST API'
13
+ spec.description = 'Ruby wrapper for PayU REST API'
14
+ spec.homepage = 'https://github.com/busfor/payu_api'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'dry-initializer', '~> 0.4'
23
+ spec.add_dependency 'faraday', '~> 0.9'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.12'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'minitest', '~> 5.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.42'
29
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payu_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roman Khrebtov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-initializer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
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: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.42'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.42'
97
+ description: Ruby wrapper for PayU REST API
98
+ email:
99
+ - roman@alltmb.ru
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rubocop.yml"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - lib/payu_api.rb
114
+ - lib/payu_api/client.rb
115
+ - lib/payu_api/errors.rb
116
+ - lib/payu_api/order.rb
117
+ - lib/payu_api/request.rb
118
+ - lib/payu_api/response.rb
119
+ - lib/payu_api/responses/create_response.rb
120
+ - lib/payu_api/responses/get_response.rb
121
+ - lib/payu_api/responses/refund_response.rb
122
+ - lib/payu_api/version.rb
123
+ - payu_api.gemspec
124
+ homepage: https://github.com/busfor/payu_api
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Ruby wrapper for PayU REST API
148
+ test_files: []