ap_ruby_sdk 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 +7 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +50 -0
- data/README.md +123 -0
- data/Rakefile +10 -0
- data/ap_ruby_sdk.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/ap_ruby_sdk.rb +148 -0
- data/lib/ap_ruby_sdk/api_operations/create.rb +16 -0
- data/lib/ap_ruby_sdk/api_operations/list.rb +20 -0
- data/lib/ap_ruby_sdk/api_operations/retrieve.rb +17 -0
- data/lib/ap_ruby_sdk/api_resource.rb +26 -0
- data/lib/ap_ruby_sdk/base_model.rb +31 -0
- data/lib/ap_ruby_sdk/collection.rb +33 -0
- data/lib/ap_ruby_sdk/customer.rb +27 -0
- data/lib/ap_ruby_sdk/errors/ap_error.rb +19 -0
- data/lib/ap_ruby_sdk/errors/api_error.rb +4 -0
- data/lib/ap_ruby_sdk/errors/authentication_error.rb +4 -0
- data/lib/ap_ruby_sdk/errors/invalid_parameter_error.rb +16 -0
- data/lib/ap_ruby_sdk/errors/payment_error.rb +4 -0
- data/lib/ap_ruby_sdk/pagination.rb +9 -0
- data/lib/ap_ruby_sdk/payment.rb +17 -0
- data/lib/ap_ruby_sdk/period.rb +8 -0
- data/lib/ap_ruby_sdk/phone_verification.rb +15 -0
- data/lib/ap_ruby_sdk/plan.rb +22 -0
- data/lib/ap_ruby_sdk/preauthorization.rb +26 -0
- data/lib/ap_ruby_sdk/redirect_urls.rb +8 -0
- data/lib/ap_ruby_sdk/refund.rb +26 -0
- data/lib/ap_ruby_sdk/refund_reason.rb +9 -0
- data/lib/ap_ruby_sdk/subscription.rb +35 -0
- data/lib/ap_ruby_sdk/transaction.rb +95 -0
- data/lib/ap_ruby_sdk/util.rb +77 -0
- data/lib/ap_ruby_sdk/version.rb +3 -0
- data/lib/ap_ruby_sdk/void.rb +26 -0
- metadata +149 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6c8404c265b754d10f4b2f8680c7fa00e05583cf
|
4
|
+
data.tar.gz: f01f791948c985f3a98d71021eac419623983662
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8e1e3ae3345815a621cdd926756ac283b53410e1bc6279b712db580df4f066df614f0e525cca97aa59c7b0e1911f58fbcccfdc192474d814a864a11559d41184
|
7
|
+
data.tar.gz: ec8b3b9abf52879ccc6857a84193abe349b05b600adadb6a7f9fcd909aac3d21949e45fbdf3fb49108c10f94afcb036513e9a733238f7daa1fe0cab110eb2f0d
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ap_ruby_sdk (0.1.0)
|
5
|
+
oj
|
6
|
+
rest-client (~> 1.8)
|
7
|
+
webmock
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
addressable (2.4.0)
|
13
|
+
crack (0.4.3)
|
14
|
+
safe_yaml (~> 1.0.0)
|
15
|
+
domain_name (0.5.20160310)
|
16
|
+
unf (>= 0.0.5, < 1.0.0)
|
17
|
+
hashdiff (0.3.0)
|
18
|
+
http-cookie (1.0.2)
|
19
|
+
domain_name (~> 0.5)
|
20
|
+
mime-types (2.99.2)
|
21
|
+
minitest (5.9.0)
|
22
|
+
multi_json (1.12.1)
|
23
|
+
netrc (0.11.0)
|
24
|
+
oj (2.15.1)
|
25
|
+
rake (10.5.0)
|
26
|
+
rest-client (1.8.0)
|
27
|
+
http-cookie (>= 1.0.2, < 2.0)
|
28
|
+
mime-types (>= 1.16, < 3.0)
|
29
|
+
netrc (~> 0.7)
|
30
|
+
safe_yaml (1.0.4)
|
31
|
+
unf (0.1.4)
|
32
|
+
unf_ext
|
33
|
+
unf_ext (0.0.7.2)
|
34
|
+
webmock (2.0.3)
|
35
|
+
addressable (>= 2.3.6)
|
36
|
+
crack (>= 0.3.2)
|
37
|
+
hashdiff
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
ap_ruby_sdk!
|
44
|
+
bundler (~> 1.8)
|
45
|
+
minitest
|
46
|
+
multi_json
|
47
|
+
oj
|
48
|
+
rake (~> 10.0)
|
49
|
+
rest-client
|
50
|
+
webmock
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# ApRubySdk
|
2
|
+
|
3
|
+
Alternative Payments ruby gem sdk. Accept local payments from all over the world
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ap_ruby_sdk'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ap_ruby_sdk
|
20
|
+
|
21
|
+
In your initializing files add line:
|
22
|
+
|
23
|
+
if Rails.env.production?
|
24
|
+
ApRubySdk.api_key = 'Live key provided from your AP account'
|
25
|
+
else
|
26
|
+
ApRubySdk.api_key = 'Test key provided from your AP account'
|
27
|
+
end
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
For usage and examples check `http://www.alternativepayments.com/support/api/` or sample-application on our open-source repo `https://github.com/AlternativePayments/ap-ruby-sdk`
|
32
|
+
Example of creating new customer:
|
33
|
+
|
34
|
+
customer = ApRubySdk::Customer.create(
|
35
|
+
{
|
36
|
+
'firstName' => 'John',
|
37
|
+
'lastName' => 'Doe',
|
38
|
+
'email' => params[:email],
|
39
|
+
'address' => 'Rutledge Ave 409',
|
40
|
+
'city' => 'Folsom',
|
41
|
+
'zip' => '19033',
|
42
|
+
'country' => 'US',
|
43
|
+
'state' => 'PA',
|
44
|
+
'phone' => '55555555555',
|
45
|
+
'created' => '2016-03-24T15:19:10.7800694Z'
|
46
|
+
}
|
47
|
+
)
|
48
|
+
|
49
|
+
Accessing object's attributes:
|
50
|
+
|
51
|
+
customer.firstName
|
52
|
+
=> John
|
53
|
+
|
54
|
+
If you want to work with JSON:
|
55
|
+
|
56
|
+
customer.to_json
|
57
|
+
|
58
|
+
Same goes for complex objects like Transaction.
|
59
|
+
Create SEPA transaction:
|
60
|
+
|
61
|
+
customer = ApRubySdk::Customer.new(
|
62
|
+
'id' => 'cus_bd838e3611d34d598',
|
63
|
+
'firstName' => 'John',
|
64
|
+
'lastName' => 'Doe',
|
65
|
+
'email' => 'john@doe.com',
|
66
|
+
'country' => 'DE'
|
67
|
+
)
|
68
|
+
|
69
|
+
payment = ApRubySdk::Payment.new(
|
70
|
+
'paymentOption' => 'SEPA',
|
71
|
+
'holder' => 'John Doe',
|
72
|
+
'iban' => 'BE88271080782541'
|
73
|
+
)
|
74
|
+
|
75
|
+
transaction = ApRubySdk::Transaction.create(
|
76
|
+
'customer' => customer,
|
77
|
+
'payment' => payment,
|
78
|
+
'amount' => 500,
|
79
|
+
'currency' => 'EUR',
|
80
|
+
'description' => 'test sepa php sdk',
|
81
|
+
'merchantPassThruData' => 'test_sepa_123',
|
82
|
+
'iPAddress' => '127.0.0.1'
|
83
|
+
)
|
84
|
+
|
85
|
+
Access customer:
|
86
|
+
|
87
|
+
transaction.customer.firstName
|
88
|
+
|
89
|
+
You can also create objects using only JSON:
|
90
|
+
|
91
|
+
transaction = ApRubySdk::Transaction.create(
|
92
|
+
'customer' => {
|
93
|
+
'id' => 'cus_bd838e3611d34d598',
|
94
|
+
'firstName' => 'John',
|
95
|
+
'lastName' => 'Doe',
|
96
|
+
'email' => 'john@doe.com',
|
97
|
+
'country' => 'DE'
|
98
|
+
},
|
99
|
+
'payment' => {
|
100
|
+
'paymentOption' => 'SEPA',
|
101
|
+
'holder' => 'John Doe',
|
102
|
+
'iban' => 'BE88271080782541'
|
103
|
+
},
|
104
|
+
'amount' => 500,
|
105
|
+
'currency' => 'EUR',
|
106
|
+
'description' => 'test sepa php sdk',
|
107
|
+
'merchantPassThruData' => 'test_sepa_123',
|
108
|
+
'iPAddress' => '127.0.0.1'
|
109
|
+
)
|
110
|
+
|
111
|
+
## Development
|
112
|
+
|
113
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
114
|
+
|
115
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
116
|
+
|
117
|
+
## Contributing
|
118
|
+
|
119
|
+
1. Fork it ( https://github.com/AlternativePayments/ap-ruby-sdk/fork )
|
120
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
121
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
122
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
123
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/ap_ruby_sdk.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ap_ruby_sdk/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ap_ruby_sdk'
|
8
|
+
spec.version = ApRubySdk::VERSION
|
9
|
+
spec.authors = ['Marjan, Nenad']
|
10
|
+
spec.email = ['marjan.stojanovic90@gmail.com, nenad.bozic@smartcat.io']
|
11
|
+
|
12
|
+
spec.summary = %q{Alternative Payments ruby gem sdk. Accept local payments from all over the world}
|
13
|
+
spec.homepage = 'http://www.alternativepayments.com/'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'rest-client', '~> 1.8'
|
22
|
+
spec.add_dependency 'webmock'
|
23
|
+
spec.add_dependency 'oj'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.8'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ap_ruby_sdk"
|
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
data/lib/ap_ruby_sdk.rb
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'oj'
|
4
|
+
require 'multi_json'
|
5
|
+
|
6
|
+
# Version
|
7
|
+
require 'ap_ruby_sdk/version'
|
8
|
+
|
9
|
+
# AP errors
|
10
|
+
require 'ap_ruby_sdk/errors/ap_error'
|
11
|
+
require 'ap_ruby_sdk/errors/api_error'
|
12
|
+
require 'ap_ruby_sdk/errors/authentication_error'
|
13
|
+
require 'ap_ruby_sdk/errors/invalid_parameter_error'
|
14
|
+
require 'ap_ruby_sdk/errors/payment_error'
|
15
|
+
|
16
|
+
# AP operations
|
17
|
+
require 'ap_ruby_sdk/api_operations/create'
|
18
|
+
require 'ap_ruby_sdk/api_operations/list'
|
19
|
+
require 'ap_ruby_sdk/api_operations/retrieve'
|
20
|
+
|
21
|
+
# AP Resources
|
22
|
+
require 'ap_ruby_sdk/util'
|
23
|
+
require 'ap_ruby_sdk/base_model'
|
24
|
+
require 'ap_ruby_sdk/collection'
|
25
|
+
require 'ap_ruby_sdk/api_resource'
|
26
|
+
require 'ap_ruby_sdk/customer'
|
27
|
+
require 'ap_ruby_sdk/payment'
|
28
|
+
require 'ap_ruby_sdk/transaction'
|
29
|
+
require 'ap_ruby_sdk/void'
|
30
|
+
require 'ap_ruby_sdk/refund'
|
31
|
+
require 'ap_ruby_sdk/redirect_urls'
|
32
|
+
require 'ap_ruby_sdk/phone_verification'
|
33
|
+
require 'ap_ruby_sdk/plan'
|
34
|
+
require 'ap_ruby_sdk/period'
|
35
|
+
require 'ap_ruby_sdk/refund_reason'
|
36
|
+
require 'ap_ruby_sdk/subscription'
|
37
|
+
require 'ap_ruby_sdk/pagination'
|
38
|
+
require 'ap_ruby_sdk/preauthorization'
|
39
|
+
|
40
|
+
module ApRubySdk
|
41
|
+
@api_base = 'https://api.alternativepayments.com/api'
|
42
|
+
|
43
|
+
class << self
|
44
|
+
attr_accessor :api_key, :api_base
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.request(method, url, api_key, params={}, headers={})
|
48
|
+
unless api_key ||= @api_key
|
49
|
+
raise AuthenticationError.new('No API key provided.')
|
50
|
+
end
|
51
|
+
|
52
|
+
url = api_url(url)
|
53
|
+
|
54
|
+
if method.to_s.downcase.to_sym == :get
|
55
|
+
# Make params into GET params
|
56
|
+
url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
|
57
|
+
payload = nil
|
58
|
+
elsif method.to_s.downcase.to_sym == :delete
|
59
|
+
# Make params into expected DELETE params
|
60
|
+
elsif method.to_s.downcase.to_sym == :put
|
61
|
+
# Make params into expected PUT params
|
62
|
+
else
|
63
|
+
# Make params into POST params
|
64
|
+
payload = MultiJson.dump(params, mode: :compat)
|
65
|
+
end
|
66
|
+
|
67
|
+
request_opts = {
|
68
|
+
:headers => request_headers(api_key).update(headers),
|
69
|
+
:open_timeout => 30,
|
70
|
+
:timeout => 80,
|
71
|
+
:method => method,
|
72
|
+
:payload => payload,
|
73
|
+
:url => url
|
74
|
+
}
|
75
|
+
begin
|
76
|
+
response = execute_request(request_opts)
|
77
|
+
rescue RestClient::ExceptionWithResponse => e
|
78
|
+
if e.http_code and e.http_body
|
79
|
+
parse_api_error(e.http_code, e.http_body)
|
80
|
+
else
|
81
|
+
raise APIError.new(message + "\n\n(Network error: #{e.message})")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
parse(response)
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.api_url(url='')
|
89
|
+
@api_base + url
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def self.uri_encode(params)
|
95
|
+
Util.flatten_params(params).
|
96
|
+
map { |k, v| "#{k}=#{Util.url_encode(v)}" }.join('&')
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.request_headers(api_key)
|
100
|
+
{
|
101
|
+
:user_agent => "AlternativePayments Ruby SDK v#{ApRubySdk::VERSION}",
|
102
|
+
:authorization => "Basic #{Base64.strict_encode64(api_key)}",
|
103
|
+
:content_type => :json
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.execute_request(opts)
|
108
|
+
RestClient::Request.execute(opts)
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.parse(response)
|
112
|
+
begin
|
113
|
+
response = MultiJson.load(response.body)
|
114
|
+
rescue MultiJson::DecodeError
|
115
|
+
raise_general_error(response.code, response.body)
|
116
|
+
end
|
117
|
+
|
118
|
+
Util.symbolize_names(response)
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.raise_general_error(response_code, response_body)
|
122
|
+
raise APIError.new("Invalid response object from API: #{response_body.inspect} " +
|
123
|
+
"(HTTP response code was #{response_code})", response_code)
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.parse_api_error(response_code, response_body)
|
127
|
+
begin
|
128
|
+
error = MultiJson.load(response_body)
|
129
|
+
error = Util.symbolize_names(error)
|
130
|
+
|
131
|
+
raise ApError.new if error[:Type].nil? # if there is no type
|
132
|
+
|
133
|
+
rescue MultiJson::DecodeError, ApError
|
134
|
+
raise_general_error(response_code, response_body)
|
135
|
+
end
|
136
|
+
|
137
|
+
case error[:Type]
|
138
|
+
when 'payment_error'
|
139
|
+
raise PaymentError.new(error[:Message], response_code, error[:Code])
|
140
|
+
when 'api_error'
|
141
|
+
raise APIError.new(error[:Message], response_code, error[:Code])
|
142
|
+
when 'invalid_parameter_error'
|
143
|
+
raise InvalidParameterError.new(error[:Message], response_code, error[:Code], error[:Param])
|
144
|
+
else
|
145
|
+
raise APIError.new(error[:Message], response_code, error[:Code]);
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
module ApiOperations
|
3
|
+
module Create
|
4
|
+
module ClassMethods
|
5
|
+
def create(params={}, url_prefix=nil, api_key=nil)
|
6
|
+
response = ApRubySdk.request(:post, url_with_prefix(url_prefix), api_key, params)
|
7
|
+
Util.convert_to_ap_object(response, self.url)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
module ApiOperations
|
3
|
+
module List
|
4
|
+
module ClassMethods
|
5
|
+
def all(pagination_options={}, url_prefix=nil, api_key=nil)
|
6
|
+
response = ApRubySdk.request(:get, "#{url_with_prefix(url_prefix)}/", api_key, pagination_options)
|
7
|
+
list = Util.convert_to_ap_object(response[self.list_members], self.url)
|
8
|
+
ApRubySdk::Collection.new(
|
9
|
+
'items' => list,
|
10
|
+
'pagination' => response[:pagination]
|
11
|
+
)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.included(base)
|
16
|
+
base.extend(ClassMethods)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
module ApiOperations
|
3
|
+
module Retrieve
|
4
|
+
module ClassMethods
|
5
|
+
def retrieve(id, retrieve_options={}, url_prefix=nil, api_key=nil)
|
6
|
+
response = ApRubySdk.request(:get, "#{url_with_prefix(url_prefix)}/#{id}", api_key, retrieve_options)
|
7
|
+
Util.convert_to_ap_object(response, self.url)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(base)
|
12
|
+
base.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class ApiResource < BaseModel
|
3
|
+
|
4
|
+
attr_accessor :mode
|
5
|
+
|
6
|
+
def self.url
|
7
|
+
raise NotImplementedError.new('APIResource is an abstract class. You should use it\'s subclasses (Customer, Payment, etc.)')
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.list_members
|
11
|
+
raise NotImplementedError.new('APIResource is an abstract class. You should use it\'s subclasses (Customer, Payment, etc.)')
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.url_with_prefix(url_prefix)
|
15
|
+
if url_prefix.nil?
|
16
|
+
self.url
|
17
|
+
else
|
18
|
+
"#{url_prefix}#{self.url}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.construct_object(response)
|
23
|
+
self.new(response)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class BaseModel
|
3
|
+
attr_accessor :id,
|
4
|
+
:mode,
|
5
|
+
:created,
|
6
|
+
:updated
|
7
|
+
|
8
|
+
def initialize(attributes={})
|
9
|
+
self.attributes = attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
def attributes=(attributes)
|
13
|
+
if attributes.respond_to?(:each)
|
14
|
+
attributes.each do |key, value|
|
15
|
+
writer = (key == 'type' ? 'kind=' : "#{key}=")
|
16
|
+
if respond_to?(writer)
|
17
|
+
send(writer, value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
attributes
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_json
|
26
|
+
hash = {}
|
27
|
+
instance_variables.each { |var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
|
28
|
+
hash.to_json
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Collection
|
3
|
+
attr_accessor :pagination,
|
4
|
+
:items
|
5
|
+
|
6
|
+
def pagination=(pagination)
|
7
|
+
@pagination = Pagination.new(pagination)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(attributes={})
|
11
|
+
self.attributes = attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def attributes=(attributes)
|
15
|
+
if attributes.respond_to?(:each)
|
16
|
+
attributes.each do |key, value|
|
17
|
+
writer = (key == 'type' ? 'kind=' : "#{key}=")
|
18
|
+
if respond_to?(writer)
|
19
|
+
send(writer, value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
else
|
23
|
+
attributes
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_json
|
28
|
+
hash = {}
|
29
|
+
instance_variables.each { |var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
|
30
|
+
hash.to_json
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Customer < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
include ApRubySdk::ApiOperations::List
|
5
|
+
include ApRubySdk::ApiOperations::Retrieve
|
6
|
+
|
7
|
+
attr_accessor :firstName,
|
8
|
+
:lastName,
|
9
|
+
:email,
|
10
|
+
:address,
|
11
|
+
:address2,
|
12
|
+
:city,
|
13
|
+
:zip,
|
14
|
+
:country,
|
15
|
+
:state,
|
16
|
+
:phone,
|
17
|
+
:birthDate
|
18
|
+
|
19
|
+
def self.url
|
20
|
+
'/customers'
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.list_members
|
24
|
+
:customers
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class ApError < StandardError
|
3
|
+
attr_reader :message
|
4
|
+
attr_reader :http_status
|
5
|
+
attr_reader :error_code
|
6
|
+
|
7
|
+
def initialize(message=nil, http_status=nil, error_code=nil)
|
8
|
+
@message = message
|
9
|
+
@http_status = http_status
|
10
|
+
@error_code = error_code
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_s
|
14
|
+
status_string = @http_status.nil? ? '' : "(Status #{@http_status})"
|
15
|
+
error_code = @error_code.nil? ? '' : "Error code #{@error_code}"
|
16
|
+
"#{status_string}\n#{@message}\n#{error_code}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class InvalidParameterError < ApError
|
3
|
+
attr_reader :parameter
|
4
|
+
|
5
|
+
def initialize(message=nil, http_status=nil, error_code=nil, parameter=nil)
|
6
|
+
super(message, http_status, error_code)
|
7
|
+
@parameter = parameter
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
message = super
|
12
|
+
parameter = @parameter.nil? ? '' : "Wrong parameter #{@parameter}"
|
13
|
+
"#{message}\n#{parameter}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Plan < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
include ApRubySdk::ApiOperations::Retrieve
|
5
|
+
include ApRubySdk::ApiOperations::List
|
6
|
+
|
7
|
+
attr_accessor :interval,
|
8
|
+
:period,
|
9
|
+
:amount,
|
10
|
+
:currency,
|
11
|
+
:name,
|
12
|
+
:description
|
13
|
+
|
14
|
+
def self.url
|
15
|
+
'/plans'
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.list_members
|
19
|
+
:plans
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Preauthorization < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
|
5
|
+
attr_accessor :customer,
|
6
|
+
:payment,
|
7
|
+
:amount,
|
8
|
+
:currency
|
9
|
+
|
10
|
+
def customer=(customer)
|
11
|
+
@customer = Customer.new(customer)
|
12
|
+
end
|
13
|
+
|
14
|
+
def payment=(payment)
|
15
|
+
@payment = Payment.new(payment)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.url
|
19
|
+
'/preauthorizations'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.list_members
|
23
|
+
:preauthorizations
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Refund < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
include ApRubySdk::ApiOperations::Retrieve
|
5
|
+
include ApRubySdk::ApiOperations::List
|
6
|
+
|
7
|
+
attr_accessor :amount,
|
8
|
+
:currency,
|
9
|
+
:originalTransactionId,
|
10
|
+
:originalTransaction,
|
11
|
+
:status,
|
12
|
+
:reason
|
13
|
+
|
14
|
+
def originalTransaction=(originalTransaction)
|
15
|
+
@originalTransaction = Transaction.new(originalTransaction)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.url
|
19
|
+
'/refunds'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.list_members
|
23
|
+
:refundTransactions
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Subscription < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
include ApRubySdk::ApiOperations::List
|
5
|
+
include ApRubySdk::ApiOperations::Retrieve
|
6
|
+
|
7
|
+
attr_accessor :customer,
|
8
|
+
:customerId,
|
9
|
+
:payment,
|
10
|
+
:paymentId,
|
11
|
+
:plan,
|
12
|
+
:planId,
|
13
|
+
:status
|
14
|
+
|
15
|
+
def customer=(customer)
|
16
|
+
@customer = Customer.new(customer)
|
17
|
+
end
|
18
|
+
|
19
|
+
def payment=(payment)
|
20
|
+
@payment = Payment.new(payment)
|
21
|
+
end
|
22
|
+
|
23
|
+
def plan=(plan)
|
24
|
+
@plan = Plan.new(plan)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.url
|
28
|
+
'/subscriptions'
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.list_members
|
32
|
+
:subscriptions
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Transaction < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
include ApRubySdk::ApiOperations::List
|
5
|
+
include ApRubySdk::ApiOperations::Retrieve
|
6
|
+
|
7
|
+
attr_accessor :customer,
|
8
|
+
:customerId,
|
9
|
+
:payment,
|
10
|
+
:token,
|
11
|
+
:amount,
|
12
|
+
:currency,
|
13
|
+
:merchantPassThruData,
|
14
|
+
:merchantTransactionId,
|
15
|
+
:description,
|
16
|
+
:ipAddress,
|
17
|
+
:status,
|
18
|
+
:redirectUrls,
|
19
|
+
:redirectUrl,
|
20
|
+
:phoneverification,
|
21
|
+
:preauthorization
|
22
|
+
|
23
|
+
def customer=(customer)
|
24
|
+
@customer = Customer.new(customer)
|
25
|
+
end
|
26
|
+
|
27
|
+
def payment=(payment)
|
28
|
+
@payment = Payment.new(payment)
|
29
|
+
end
|
30
|
+
|
31
|
+
def redirectUrls=(redirectUrls)
|
32
|
+
@redirectUrls = RedirectUrls.new(redirectUrls)
|
33
|
+
end
|
34
|
+
|
35
|
+
def phoneverification=(phoneverification)
|
36
|
+
@phoneverification = PhoneVerification.new(phoneverification)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.retrieve_void(void_id=nil, transaction_id=self.id)
|
40
|
+
Void.retrieve(void_id, {}, "#{self.url}/#{transaction_id}")
|
41
|
+
end
|
42
|
+
|
43
|
+
def retrieve_void(void_id=nil, transaction_id=self.id)
|
44
|
+
self.class.retrieve_void(void_id, transaction_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.void(reason='', transaction_id=self.id)
|
48
|
+
Void.create({:reason => reason}, "#{self.url}/#{transaction_id}")
|
49
|
+
end
|
50
|
+
|
51
|
+
def void(reason='', transaction_id=self.id)
|
52
|
+
self.class.void(reason, transaction_id)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.voids(transaction_id=self.id)
|
56
|
+
Void.all({}, "#{self.url}/#{transaction_id}")
|
57
|
+
end
|
58
|
+
|
59
|
+
def voids(transaction_id=self.id)
|
60
|
+
self.class.voids(transaction_id)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.retrieve_refund(refund_id=nil, transaction_id=self.id)
|
64
|
+
Refund.retrieve(refund_id, {}, "#{self.url}/#{transaction_id}")
|
65
|
+
end
|
66
|
+
|
67
|
+
def retrieve_refund(refund_id=nil, transaction_id=self.id)
|
68
|
+
self.class.retrieve_refund(refund_id, transaction_id)
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.refund(reason='', transaction_id=self.id)
|
72
|
+
Refund.create({:reason => reason}, "#{self.url}/#{transaction_id}")
|
73
|
+
end
|
74
|
+
|
75
|
+
def refund(reason='', transaction_id=self.id)
|
76
|
+
self.class.refund(reason, transaction_id)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.refunds(transaction_id=self.id)
|
80
|
+
Refund.all({}, "#{self.url}/#{transaction_id}")
|
81
|
+
end
|
82
|
+
|
83
|
+
def refunds(transaction_id=self.id)
|
84
|
+
self.class.refunds(transaction_id)
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.url
|
88
|
+
'/transactions'
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.list_members
|
92
|
+
:transactions
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
module Util
|
3
|
+
def self.symbolize_names(object)
|
4
|
+
case object
|
5
|
+
when Hash
|
6
|
+
new = {}
|
7
|
+
object.each do |key, value|
|
8
|
+
key = (key.to_sym rescue key) || key
|
9
|
+
new[key] = symbolize_names(value)
|
10
|
+
end
|
11
|
+
new
|
12
|
+
when Array
|
13
|
+
object.map { |value| symbolize_names(value) }
|
14
|
+
else
|
15
|
+
object
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.url_encode(key)
|
20
|
+
URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.flatten_params(params, parent_key=nil)
|
24
|
+
result = []
|
25
|
+
params.each do |key, value|
|
26
|
+
calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
|
27
|
+
if value.is_a?(Hash)
|
28
|
+
result += flatten_params(value, calculated_key)
|
29
|
+
elsif value.is_a?(Array)
|
30
|
+
result += flatten_params_array(value, calculated_key)
|
31
|
+
else
|
32
|
+
result << [calculated_key, value]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
result
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.flatten_params_array(value, calculated_key)
|
39
|
+
result = []
|
40
|
+
value.each do |elem|
|
41
|
+
if elem.is_a?(Hash)
|
42
|
+
result += flatten_params(elem, calculated_key)
|
43
|
+
elsif elem.is_a?(Array)
|
44
|
+
result += flatten_params_array(elem, calculated_key)
|
45
|
+
else
|
46
|
+
result << ["#{calculated_key}[]", elem]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
result
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.convert_to_ap_object(response, url)
|
53
|
+
case response
|
54
|
+
when Array
|
55
|
+
response.map { |object| convert_to_ap_object(object, url) }
|
56
|
+
when Hash
|
57
|
+
object_classes.fetch(url, ApiResource).construct_object(response)
|
58
|
+
else
|
59
|
+
response
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
def self.object_classes
|
65
|
+
@object_classes ||= {
|
66
|
+
'/customers' => Customer,
|
67
|
+
'/transactions' => Transaction,
|
68
|
+
'/phoneverification' => PhoneVerification,
|
69
|
+
'/voids' => Void,
|
70
|
+
'/plans' => Plan,
|
71
|
+
'/refunds' => Refund,
|
72
|
+
'/subscriptions' => Subscription,
|
73
|
+
'/preauthorizations' => Preauthorization
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ApRubySdk
|
2
|
+
class Void < ApiResource
|
3
|
+
include ApRubySdk::ApiOperations::Create
|
4
|
+
include ApRubySdk::ApiOperations::Retrieve
|
5
|
+
include ApRubySdk::ApiOperations::List
|
6
|
+
|
7
|
+
attr_accessor :amount,
|
8
|
+
:currency,
|
9
|
+
:originalTransactionId,
|
10
|
+
:originalTransaction,
|
11
|
+
:status,
|
12
|
+
:reason
|
13
|
+
|
14
|
+
def originalTransaction=(originalTransaction)
|
15
|
+
@originalTransaction = Transaction.new(originalTransaction)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.url
|
19
|
+
'/voids'
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.list_members
|
23
|
+
:voidTransactions
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ap_ruby_sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marjan, Nenad
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: webmock
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: oj
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- marjan.stojanovic90@gmail.com, nenad.bozic@smartcat.io
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- ap_ruby_sdk.gemspec
|
95
|
+
- bin/console
|
96
|
+
- bin/setup
|
97
|
+
- lib/ap_ruby_sdk.rb
|
98
|
+
- lib/ap_ruby_sdk/api_operations/create.rb
|
99
|
+
- lib/ap_ruby_sdk/api_operations/list.rb
|
100
|
+
- lib/ap_ruby_sdk/api_operations/retrieve.rb
|
101
|
+
- lib/ap_ruby_sdk/api_resource.rb
|
102
|
+
- lib/ap_ruby_sdk/base_model.rb
|
103
|
+
- lib/ap_ruby_sdk/collection.rb
|
104
|
+
- lib/ap_ruby_sdk/customer.rb
|
105
|
+
- lib/ap_ruby_sdk/errors/ap_error.rb
|
106
|
+
- lib/ap_ruby_sdk/errors/api_error.rb
|
107
|
+
- lib/ap_ruby_sdk/errors/authentication_error.rb
|
108
|
+
- lib/ap_ruby_sdk/errors/invalid_parameter_error.rb
|
109
|
+
- lib/ap_ruby_sdk/errors/payment_error.rb
|
110
|
+
- lib/ap_ruby_sdk/pagination.rb
|
111
|
+
- lib/ap_ruby_sdk/payment.rb
|
112
|
+
- lib/ap_ruby_sdk/period.rb
|
113
|
+
- lib/ap_ruby_sdk/phone_verification.rb
|
114
|
+
- lib/ap_ruby_sdk/plan.rb
|
115
|
+
- lib/ap_ruby_sdk/preauthorization.rb
|
116
|
+
- lib/ap_ruby_sdk/redirect_urls.rb
|
117
|
+
- lib/ap_ruby_sdk/refund.rb
|
118
|
+
- lib/ap_ruby_sdk/refund_reason.rb
|
119
|
+
- lib/ap_ruby_sdk/subscription.rb
|
120
|
+
- lib/ap_ruby_sdk/transaction.rb
|
121
|
+
- lib/ap_ruby_sdk/util.rb
|
122
|
+
- lib/ap_ruby_sdk/version.rb
|
123
|
+
- lib/ap_ruby_sdk/void.rb
|
124
|
+
homepage: http://www.alternativepayments.com/
|
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.6
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Alternative Payments ruby gem sdk. Accept local payments from all over the
|
148
|
+
world
|
149
|
+
test_files: []
|