payoneer-rb 1.0.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
+ SHA256:
3
+ metadata.gz: 6f5be0cce0d48ed1d8eb4686046eff0a6a756fbcba0e31a756cbe0c728a9640d
4
+ data.tar.gz: e9cb9ed894823c95fdf64680a53c11131dcee08d9e22db16c3ea2c965b262de2
5
+ SHA512:
6
+ metadata.gz: caffc29dd685ba87d5e40f5832db06536416f5ad84f823284b7c143e466250091475da8889ecc73540e5c7186d614438d8647988b62afb921802d21a6402e039
7
+ data.tar.gz: 913bd4543b18a7d756e83ec7c1b17717e2ce51ffc947dc0be361cab4a37c2c444de7e57a11511ff3744c902dde5b443637274677ab5297a7aaecef1bfc7aa464
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+
3
+ ## 1.0.0
4
+ - Initial version
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Amir Zametica
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,41 @@
1
+ # Installation
2
+ Add gem to your Gemfile and run bundle
3
+ ```ruby
4
+ gem 'payoneer-rb'
5
+ ```
6
+
7
+ # Usage
8
+ ## Configuration
9
+ All of the following properties have to be set in order for gem to work properly
10
+ ```ruby
11
+ # config/initializers/payoneer.rb
12
+
13
+ Payoneer.configure do |c|
14
+ c.environment = # payoneer env (:sandbox (default) | :production)
15
+ c.callback_url = # callback url used for sending auth codes (Optional)
16
+ c.program_id = # program id
17
+ end
18
+ ```
19
+
20
+ ## Examples
21
+
22
+ ### Create signup url
23
+ ```ruby
24
+ Payoneer::Payee.create_link(params: { payee_id: '<payee_id>' })
25
+ ```
26
+
27
+ ### Create a payout
28
+ ```ruby
29
+ Payoneer::Payout.create(
30
+ payment_id: '<internal_payment_ref>',
31
+ payee_id: '<payee_id>',
32
+ amount: 170.45,
33
+ description: '',
34
+ currency: 'USD'
35
+ )
36
+ ```
37
+
38
+ ### Check the program balance
39
+ ```ruby
40
+ Payoneer::Program.balance
41
+ ```
data/Rakefile ADDED
@@ -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,42 @@
1
+ module Payoneer
2
+ module Configuration
3
+ def configure
4
+ yield self
5
+ end
6
+
7
+ ENV_URLS = {
8
+ sandbox: {
9
+ api: 'https://api.sandbox.payoneer.com/v4',
10
+ auth: 'https://login.sandbox.payoneer.com/api/v2/oauth2'
11
+ },
12
+ production: {
13
+ api: 'https://api.payoneer.com/v4',
14
+ auth: 'https://login.payoneer.com/api/v2/oauth2'
15
+ }
16
+ }.freeze
17
+
18
+ def self.authorize_url
19
+ "#{auth_url}/authorize?"\
20
+ "redirect_uri=#{callback_url}&"\
21
+ 'scope=read%20write%20openid%20personal-details&'\
22
+ 'response_type=code'
23
+ end
24
+
25
+ def self.api_url
26
+ ENV_URLS[environment][:api]
27
+ end
28
+
29
+ def self.auth_url
30
+ ENV_URLS[environment][:auth]
31
+ end
32
+
33
+ mattr_accessor :environment
34
+ @@environment = :sandbox
35
+
36
+ mattr_accessor :callback_url
37
+ @@callback_url = nil
38
+
39
+ mattr_accessor :program_id
40
+ @@program_id = nil
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ module Payoneer
2
+ class Error < StandardError
3
+ attr_reader :description, :details, :code
4
+
5
+ def initialize(description: '', details: {}, code: 0)
6
+ super(description)
7
+ @description = description
8
+ @details = details
9
+ @code = code
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module Payoneer
2
+ module Account
3
+ extend Payoneer::RemoteApi
4
+
5
+ def details(account_id:, **args)
6
+ return unless args[:access_token] && account_id
7
+
8
+ get_details(args[:access_token], account_id)
9
+ end
10
+
11
+ private
12
+
13
+ def get_details(access_token, account_id)
14
+ get(
15
+ path: "/accounts/#{account_id}/details",
16
+ options: {
17
+ access_token: access_token
18
+ }
19
+ )
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,66 @@
1
+ module Payoneer
2
+ module Auth
3
+ extend Payoneer::RemoteApi
4
+
5
+ def application_token(client_id:, client_secret:)
6
+ token(
7
+ body: {
8
+ grant_type: 'client_credentials',
9
+ scope: 'read write',
10
+ client_id: client_id,
11
+ client_secret: client_secret
12
+ }
13
+ )
14
+ end
15
+
16
+ def access_token(code:, client_id:, client_secret:)
17
+ token(
18
+ body: {
19
+ grant_type: 'authorization_code',
20
+ redirect_uri: Payoneer::Configuration.callback_url,
21
+ code: code,
22
+ client_id: client_id,
23
+ client_secret: client_secret
24
+ }
25
+ )
26
+ end
27
+
28
+ def refresh_token(refresh_token:, client_id:, client_secret:)
29
+ token(
30
+ body: {
31
+ grant_type: 'refresh_token',
32
+ refresh_token: refresh_token,
33
+ client_id: client_id,
34
+ client_secret: client_secret
35
+ }
36
+ )
37
+ end
38
+
39
+ def revoke_token(token:, client_id:, client_secret:)
40
+ token(
41
+ path: '/revoke',
42
+ body: {
43
+ token_type_hint: 'access_token',
44
+ token: token,
45
+ client_id: client_id,
46
+ client_secret: client_secret,
47
+ }
48
+ )
49
+ end
50
+
51
+ private
52
+
53
+ def token(body:, path: '/token')
54
+ post(
55
+ path: path,
56
+ body: body,
57
+ options: {
58
+ base_url: Payoneer::Configuration.auth_url,
59
+ headers: {
60
+ 'Content-Type' => 'application/x-www-form-urlencoded'
61
+ }
62
+ }
63
+ )
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,53 @@
1
+ module Payoneer
2
+ module Payee
3
+ extend Payoneer::RemoteApi
4
+
5
+ def create_link(params: {}, **args)
6
+ post(
7
+ path: "/programs/#{Payoneer::Configuration.program_id}/payees/registration-link",
8
+ body: registration_params(params),
9
+ options: args
10
+ )
11
+ end
12
+
13
+ def status(payee_id:, **args)
14
+ get(
15
+ path: "/programs/#{Payoneer::Configuration.program_id}/payees/#{payee_id}/status",
16
+ options: args
17
+ )
18
+ end
19
+
20
+ def release(payee_id:, **args)
21
+ delete(
22
+ path: "/programs/#{Payoneer::Configuration.program_id}/payees/#{payee_id}",
23
+ options: args
24
+ )
25
+ end
26
+
27
+ def details(payee_id:, **args)
28
+ get(
29
+ path: "/programs/#{Payoneer::Configuration.program_id}/payees/#{payee_id}/details",
30
+ options: args
31
+ )
32
+ end
33
+
34
+ private
35
+
36
+ def registration_params(params)
37
+ registration_params = {
38
+ payee_id: params[:payee_id],
39
+ already_have_an_account: params[:existing] || false,
40
+ }
41
+
42
+ if params[:consent]
43
+ registration_params.merge!(
44
+ redirect_url: "#{Payoneer::Configuration.authorize_url}&\
45
+ client_id=#{params[:client_id]}&\
46
+ state=#{params[:user_id]}"
47
+ )
48
+ end
49
+
50
+ registration_params
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,66 @@
1
+ module Payoneer
2
+ module Payout
3
+ extend Payoneer::RemoteApi
4
+
5
+ def create(payment_id:, payee_id:, amount:, description:, currency: 'USD', **args)
6
+ submit_payout(
7
+ {
8
+ payments: [
9
+ {
10
+ client_reference_id: payment_id,
11
+ payee_id: payee_id,
12
+ amount: amount,
13
+ description: description,
14
+ currency: currency
15
+ }
16
+ ]
17
+ },
18
+ args
19
+ )
20
+ end
21
+
22
+ def status(payment_id:, **args)
23
+ payout_status(payment_id, args)
24
+ end
25
+
26
+ private
27
+
28
+ def submit_payout(params, args)
29
+ post(
30
+ path: "/programs/#{Payoneer::Configuration.program_id}/masspayouts",
31
+ body: params,
32
+ options: {
33
+ response_params: {
34
+ payment_id: params[:payments][0][:client_reference_id]
35
+ },
36
+ **args
37
+ }
38
+ )
39
+ end
40
+
41
+ def payout_status(payment_id, args)
42
+ get(
43
+ path: "/programs/#{Payoneer::Configuration.program_id}/payouts"\
44
+ "/#{payment_id}/status",
45
+ options: {
46
+ serializer: Status,
47
+ response_params: {
48
+ payment_id: payment_id
49
+ },
50
+ **args
51
+ }
52
+ )
53
+ rescue Payoneer::Error => e
54
+ Status.convert(
55
+ {
56
+ status: 'Failed',
57
+ payment_id: payment_id,
58
+ error: {
59
+ description: e.description,
60
+ reason: e.details
61
+ }
62
+ }
63
+ )
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,12 @@
1
+ module Payoneer
2
+ module Program
3
+ extend Payoneer::RemoteApi
4
+
5
+ def balance(**args)
6
+ get(
7
+ path: "/programs/#{Payoneer::Configuration.program_id}/balance",
8
+ options: args
9
+ )
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Payoneer
2
+ module Util
3
+ extend self
4
+
5
+ def parse_account_id(id_token)
6
+ payload = id_token.split('.')[1]
7
+ payload_decoded = Base64.decode64(payload)
8
+ JSON.parse(payload_decoded)['account_id']
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,106 @@
1
+ require 'httparty'
2
+
3
+ module Payoneer
4
+ module RemoteApi
5
+ def self.extended(base)
6
+ return unless base.is_a? Module
7
+
8
+ base.extend base
9
+ end
10
+
11
+ def post(path:, body: {}, options: {})
12
+ request(
13
+ method: :post,
14
+ path: path,
15
+ body: body,
16
+ options: options
17
+ )
18
+ end
19
+
20
+ def get(path:, options: {})
21
+ request(
22
+ method: :get,
23
+ path: path,
24
+ options: options
25
+ )
26
+ end
27
+
28
+ def put(path:, body: {}, options: {})
29
+ request(
30
+ method: :put,
31
+ path: path,
32
+ body: body,
33
+ options: options
34
+ )
35
+ end
36
+
37
+ def delete(path:, body: {}, options: {})
38
+ request(
39
+ method: :delete,
40
+ path: path,
41
+ body: body,
42
+ options: options
43
+ )
44
+ end
45
+
46
+ private
47
+
48
+ def request(method:, path:, body: {}, options: {})
49
+ body = parse(
50
+ HTTParty.send(
51
+ method,
52
+ "#{options[:base_url] || Payoneer::Configuration.api_url}#{path}",
53
+ body: request_body(body, options),
54
+ headers: options[:headers] || headers(options)
55
+ )
56
+ )
57
+
58
+ convert(body['result'] || body, options)
59
+ rescue HTTParty::Error => e
60
+ raise Payoneer::Error.new(description: e.message)
61
+ end
62
+
63
+ def parse(response)
64
+ body = JSON.parse(response.body)
65
+
66
+ unless response.code == 200
67
+ raise Payoneer::Error.new(
68
+ description: body['error_description'] || 'Server Error',
69
+ details: body['error_details'],
70
+ code: response.code
71
+ )
72
+ end
73
+
74
+ body
75
+ end
76
+
77
+ def convert(result, options)
78
+ result = { result: result } unless result.is_a? Hash
79
+
80
+ serializer = if options[:serializer]&.superclass == Payoneer::Response
81
+ options[:serializer]
82
+ else
83
+ Payoneer::Response
84
+ end
85
+
86
+ result.merge!(options[:response_params]) if options[:response_params].is_a? Hash
87
+ serializer.convert(result)
88
+ end
89
+
90
+ def request_body(body, options)
91
+ content_type = options.stringify_keys.dig('headers', 'Content-Type')
92
+ return body if content_type && content_type != 'application/json'
93
+
94
+ body.to_json
95
+ end
96
+
97
+ def headers(options)
98
+ headers = {
99
+ 'Content-Type' => 'application/json'
100
+ }
101
+
102
+ headers.merge!('Authorization' => "Bearer #{options[:access_token]}") if options[:access_token]
103
+ headers
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,19 @@
1
+ module Payoneer::Payout
2
+ class Status < Payoneer::Response
3
+ def rejected?
4
+ reason_code.present?
5
+ end
6
+
7
+ def cancelled?
8
+ status == 'Cancelled'
9
+ end
10
+
11
+ def success?
12
+ status == 'Transferred'
13
+ end
14
+
15
+ def pending?
16
+ %w[Pending Pending \Payee].include? status
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ module Payoneer
2
+ class Response
3
+ attr_reader :body
4
+
5
+ delegate :[], to: :body
6
+
7
+ def self.convert(response)
8
+ case response
9
+ when Array
10
+ response.map { |i| convert(i) }
11
+ when Hash
12
+ new(response)
13
+ else
14
+ response
15
+ end
16
+ end
17
+
18
+ def initialize(response = {})
19
+ @body = {}.with_indifferent_access
20
+
21
+ add_methods(response.keys)
22
+ update_attributes(response)
23
+ end
24
+
25
+ private
26
+
27
+ def add_methods(keys)
28
+ instance_eval do
29
+ keys.each do |k|
30
+ self.class.send(:define_method, k.to_s.underscore) { @body[k] }
31
+ end
32
+ end
33
+ end
34
+
35
+ def update_attributes(response)
36
+ response.each { |k, v| @body[k] = v }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Payoneer
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1 @@
1
+ require "#{File.dirname(__FILE__)}/payoneer"
data/lib/payoneer.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'active_support'
2
+ require 'active_support/time'
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+
5
+ require 'payoneer/version'
6
+ require 'payoneer/configuration'
7
+ require 'payoneer/remote_api'
8
+ require 'payoneer/error'
9
+
10
+ require 'payoneer/responses/response'
11
+ require 'payoneer/responses/payout/status'
12
+
13
+ require 'payoneer/modules/account'
14
+ require 'payoneer/modules/auth'
15
+ require 'payoneer/modules/payee'
16
+ require 'payoneer/modules/payout'
17
+ require 'payoneer/modules/program'
18
+ require 'payoneer/modules/util'
19
+
20
+ module Payoneer
21
+ extend Configuration
22
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('lib/payoneer/version', __dir__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'payoneer-rb'
5
+ s.version = Payoneer::VERSION
6
+ s.platform = Gem::Platform::RUBY
7
+ s.license = 'MIT'
8
+ s.authors = ['Amir Zametica']
9
+ s.email = ['amirzametica@gmail.com']
10
+ s.description = 'This library provides integration with Payoneer V4 API'
11
+ s.summary = 'Payoneer V4 integration'
12
+ s.homepage = 'https://github.com/zametica/payoneer-rb'
13
+
14
+ s.files = Dir.glob('{lib, spec}/**/*')
15
+ s.files += %w[CHANGELOG.md LICENSE README.md]
16
+ s.files += %w[Rakefile payoneer-rb.gemspec]
17
+
18
+ s.require_path = 'lib'
19
+
20
+ s.add_dependency 'httparty', '~> 0.20', '>= 0.20.0'
21
+ s.add_dependency 'activesupport', '>= 5.2.4'
22
+
23
+ s.add_development_dependency 'bundler', '~> 2.3.0', '>= 2.3.0'
24
+ s.add_development_dependency 'rake', '~> 10.0'
25
+ s.add_development_dependency 'rspec', '~> 3.0'
26
+ s.add_development_dependency 'pry', '~> 0.14.0'
27
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payoneer-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Amir Zametica
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.20'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.20.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.20'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.20.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 5.2.4
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 5.2.4
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 2.3.0
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.3.0
67
+ - !ruby/object:Gem::Dependency
68
+ name: rake
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '10.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '10.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '3.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: pry
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 0.14.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.14.0
109
+ description: This library provides integration with Payoneer V4 API
110
+ email:
111
+ - amirzametica@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files: []
115
+ files:
116
+ - CHANGELOG.md
117
+ - LICENSE
118
+ - README.md
119
+ - Rakefile
120
+ - lib/payoneer-rb.rb
121
+ - lib/payoneer.rb
122
+ - lib/payoneer/configuration.rb
123
+ - lib/payoneer/error.rb
124
+ - lib/payoneer/modules/account.rb
125
+ - lib/payoneer/modules/auth.rb
126
+ - lib/payoneer/modules/payee.rb
127
+ - lib/payoneer/modules/payout.rb
128
+ - lib/payoneer/modules/program.rb
129
+ - lib/payoneer/modules/util.rb
130
+ - lib/payoneer/remote_api.rb
131
+ - lib/payoneer/responses/payout/status.rb
132
+ - lib/payoneer/responses/response.rb
133
+ - lib/payoneer/version.rb
134
+ - payoneer-rb.gemspec
135
+ homepage: https://github.com/zametica/payoneer-rb
136
+ licenses:
137
+ - MIT
138
+ metadata: {}
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.3.7
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Payoneer V4 integration
158
+ test_files: []