promisepay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7dcb61a47f74888de0ae910a8d72760ff8bfec29
4
+ data.tar.gz: 75b9d99aead5fbc6aaef117ca81fc61e6540f1a9
5
+ SHA512:
6
+ metadata.gz: 7da53336edb95c96705fcc4f87d57a1295e7b595f2c58d8a58e434e8f925bb8fb54ed81bbab9895eda4c657c0c3297659787732049520d7139e59f7333427811
7
+ data.tar.gz: c5e43c9926709b9f108387f93f238af7c1e7d9e2dfa99df75493e49fd9edb6d3e49ce85dba0bc81e890866cd0f7f62bbd146af2da4514b6ff65acabdce39cd9c
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Romain Vigo Benia
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,163 @@
1
+ # The PromisePay Ruby Gem
2
+ [![Gem Version](https://badge.fury.io/rb/promisepay.svg)](http://badge.fury.io/rb/promisepay)
3
+ [![Build Status](https://travis-ci.org/PromisePay/promisepay-ruby.svg?branch=develop)](https://travis-ci.org/PromisePay/promisepay-ruby)
4
+ [![Coverage Status](https://coveralls.io/repos/PromisePay/promisepay-ruby/badge.svg?branch=develop)](https://coveralls.io/r/PromisePay/promisepay-ruby?branch=develop)
5
+ [![Code Climate](https://codeclimate.com/github/PromisePay/promisepay-ruby/badges/gpa.svg)](https://codeclimate.com/github/PromisePay/promisepay-ruby)
6
+
7
+ A Ruby interface to the Promisepay API.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'promisepay'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install promisepay
24
+
25
+ ## Usage
26
+
27
+ Before interacting with Promispay API you need to generate an access token.
28
+
29
+ See http://docs.promisepay.com/v2.2/docs/request_token for more information.
30
+
31
+ ### Client
32
+
33
+ Create a client as following:
34
+
35
+ ```ruby
36
+ require 'promisepay'
37
+
38
+ client = Promisepay::Client.new(username: 'YOUR_USERNAME', token: 'YOUR_TOKEN')
39
+ ```
40
+
41
+ Alternatively Promisepay Gem handles client configuration through environment variables.
42
+
43
+ ```ruby
44
+ ENV['PROMISEPAY_USERNAME'] = 'YOUR_USERNAME'
45
+ ENV['PROMISEPAY_TOKEN'] = 'YOUR_TOKEN'
46
+ ```
47
+
48
+ ```ruby
49
+ require 'promisepay'
50
+
51
+ client = Promisepay::Client.new()
52
+ ```
53
+
54
+ _All configurable parameters are available in the Client Configuration section of this README._
55
+
56
+ ### Calls
57
+
58
+ The example below is translating [use case #1](http://docs.promisepay.com/v2.2/docs/charge-a-buyer-hold-funds-in-escrow-then-release-t) available on Promispay online documentation.
59
+
60
+ ```ruby
61
+ # 1- Create buyer
62
+ buyer = client.users.create(
63
+ id: '123456',
64
+ first_name: 'test',
65
+ last_name: 'buyer',
66
+ email: 'buyer@test.com',
67
+ address_line1: '48 collingwood',
68
+ state: 'vic',
69
+ city: 'Mel',
70
+ zip: '3000',
71
+ country: 'AUS'
72
+ )
73
+
74
+ # 2- Create buyer's card account
75
+ buyer_card_account = client.card_accounts.create(
76
+ user_id: buyer.id,
77
+ full_name: 'test Buyer',
78
+ number: '4111 1111 1111 1111',
79
+ expiry_month: Time.now.month,
80
+ expiry_year: Time.now.year + 1,
81
+ cvv: '123'
82
+ )
83
+
84
+ # 3- Create seller
85
+ seller = client.users.create(
86
+ id: '123457',
87
+ first_name: 'test',
88
+ last_name: 'seller',
89
+ email: 'seller@test.com',
90
+ mobile: '+61416452321',
91
+ address_line1: 'abc',
92
+ state: 'vic',
93
+ city: 'Mel',
94
+ zip: '3000',
95
+ country: 'AUS'
96
+ )
97
+
98
+ # 4- Create seller's bank account
99
+ seller_bank_account = client.bank_accounts.create(
100
+ user_id: seller.id,
101
+ bank_name: 'Nab',
102
+ account_name: 'test seller',
103
+ routing_number: '22222222',
104
+ account_number: '1234567890',
105
+ account_type: 'savings',
106
+ holder_type: 'personal',
107
+ country: 'AUS'
108
+ )
109
+
110
+ # 5. Create seller's disbursement account
111
+ seller.disbursement_account(seller_bank_account.id)
112
+
113
+ # 6. Create fee
114
+ fee = client.fees.create(
115
+ name: 'test fee for 5 AUD',
116
+ fee_type_id: '1',
117
+ amount: '75',
118
+ to: 'seller'
119
+ )
120
+
121
+ # 7. Create item and link buyer, seller and fee to it
122
+ item = client.items.create(
123
+ id: '12345',
124
+ name: 'test item for 5AUD',
125
+ amount: '500',
126
+ payement_type: '1',
127
+ buyer_id: buyer.id,
128
+ seller_id: seller.id,
129
+ fee_id: fee.id,
130
+ description: '5AUD transfer'
131
+ )
132
+
133
+ # 8. Pay for item using the buyer's CC account
134
+ item.make_payment(
135
+ user_id: buyer.id,
136
+ account_id: buyer_card_account.id
137
+ )
138
+
139
+ # 9. Buyer releases payment
140
+ item.release_payment(user_id: buyer.id)
141
+
142
+ # 10. Check payment status at any time (optional)
143
+ item.status
144
+ ```
145
+
146
+ _Check out the [online documentation](http://promisepay.github.io/promisepay-ruby/) to get a list of available resources and methods._
147
+
148
+ ## Client Configuration
149
+
150
+ The following parameters are configurable through the client:
151
+
152
+ * `:username` / `ENV['PROMISEPAY_USERNAME']`: username for [basic authentication](http://docs.promisepay.com/v2.2/docs/overview-2)
153
+ * `:token` / `ENV['PROMISEPAY_TOKEN']`: token for [basic authentication](http://docs.promisepay.com/v2.2/docs/overview-2)
154
+ * `:environment` / `ENV['PROMISEPAY_ENVIRONMENT']`: API [environment](http://docs.promisepay.com/v2.2/docs/environments) to use (default: 'test')
155
+ * `:api_domain` / `ENV['PROMISEPAY_API_DOMAIN']`: API domain name to use (default: 'api.promisepay.com')
156
+
157
+ ## Contributing
158
+
159
+ 1. Fork it ( https://github.com/PromisePay/promisepay-ruby/fork )
160
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
161
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
162
+ 4. Push to the branch (`git push origin my-new-feature`)
163
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,13 @@
1
+ require_relative 'promisepay/client'
2
+ require_relative 'promisepay/default'
3
+ require_relative 'promisepay/error'
4
+ require_relative 'promisepay/version'
5
+
6
+ # Ruby toolkit for the Promisepay API
7
+ module Promisepay
8
+ class << self
9
+ include Promisepay::Configurable
10
+ end
11
+ end
12
+
13
+ Promisepay.setup
@@ -0,0 +1,144 @@
1
+ require_relative 'configurable'
2
+ require_relative 'models/base_model'
3
+ require_relative 'models/account'
4
+ require_relative 'models/bank_account'
5
+ require_relative 'models/card_account'
6
+ require_relative 'models/fee'
7
+ require_relative 'models/item'
8
+ require_relative 'models/paypal_account'
9
+ require_relative 'models/transaction'
10
+ require_relative 'models/user'
11
+ require_relative 'resources/base_resource'
12
+ require_relative 'resources/account_resource'
13
+ require_relative 'resources/bank_account_resource'
14
+ require_relative 'resources/card_account_resource'
15
+ require_relative 'resources/fee_resource'
16
+ require_relative 'resources/item_resource'
17
+ require_relative 'resources/paypal_account_resource'
18
+ require_relative 'resources/transaction_resource'
19
+ require_relative 'resources/user_resource'
20
+ require 'json'
21
+ require 'faraday'
22
+
23
+ module Promisepay
24
+ # Client for the Promisepay API
25
+ #
26
+ # @see http://docs.promisepay.com/v2.2/docs/overview
27
+ class Client
28
+ include Promisepay::Configurable
29
+
30
+ def initialize(options = {})
31
+ # Use options passed in, but fall back to module defaults
32
+ Promisepay::Configurable.keys.each do |key|
33
+ instance_variable_set(
34
+ :"@#{key}", options[key] || Promisepay.instance_variable_get(:"@#{key}")
35
+ )
36
+ end
37
+ end
38
+
39
+ # Create a new Faraday connection
40
+ #
41
+ # @return [Faraday::Connection]
42
+ def connection
43
+ Faraday.new(url: @api_endpoint) do |builder|
44
+ builder.request :basic_auth, @username, @token
45
+ builder.adapter :net_http
46
+ end
47
+ end
48
+
49
+ # Make a HTTP GET request
50
+ #
51
+ # @param url [String] The path, relative to {#api_endpoint}
52
+ # @param parameters [Hash] Query params for request
53
+ # @return [Faraday::Response]
54
+ def get(url, parameters = {})
55
+ response = connection.get("#{api_endpoint}#{url}", parameters)
56
+ on_complete(response)
57
+ response
58
+ end
59
+
60
+ # Make a HTTP POST request
61
+ #
62
+ # @param url [String] The path, relative to {#api_endpoint}
63
+ # @param parameters [Hash] Query params for request
64
+ # @return [Faraday::Response]
65
+ def post(url, parameters = {})
66
+ response = connection.post do |req|
67
+ req.url "#{api_endpoint}#{url}"
68
+ req.headers['Content-Type'] = 'application/json'
69
+ req.body = parameters.to_json
70
+ end
71
+ on_complete(response)
72
+ response
73
+ end
74
+
75
+ # Make a HTTP PATCH request
76
+ #
77
+ # @param url [String] The path, relative to {#api_endpoint}
78
+ # @param parameters [Hash] Query params for request
79
+ # @return [Faraday::Response]
80
+ def patch(url, parameters = {})
81
+ response = connection.patch do |req|
82
+ req.url "#{api_endpoint}#{url}"
83
+ req.headers['Content-Type'] = 'application/json'
84
+ req.body = parameters.to_json
85
+ end
86
+ on_complete(response)
87
+ response
88
+ end
89
+
90
+ # Make a HTTP DELETE request
91
+ #
92
+ # @param url [String] The path, relative to {#api_endpoint}
93
+ # @param parameters [Hash] Query params for request
94
+ # @return [Faraday::Response]
95
+ def delete(url, parameters = {})
96
+ response = connection.delete do |req|
97
+ req.url "#{api_endpoint}#{url}"
98
+ req.headers['Content-Type'] = 'application/json'
99
+ req.body = parameters.to_json
100
+ end
101
+ on_complete(response)
102
+ response
103
+ end
104
+
105
+ # Available resources for {Client}
106
+ #
107
+ # @return [Hash]
108
+ def self.resources
109
+ {
110
+ bank_accounts: BankAccountResource,
111
+ card_accounts: CardAccountResource,
112
+ fees: FeeResource,
113
+ items: ItemResource,
114
+ paypal_accounts: PaypalAccountResource,
115
+ transactions: TransactionResource,
116
+ users: UserResource
117
+ }
118
+ end
119
+
120
+ # Catch calls for resources
121
+ #
122
+ def method_missing(name, *args, &block)
123
+ if self.class.resources.keys.include?(name)
124
+ resources[name] ||= self.class.resources[name].new(self)
125
+ resources[name]
126
+ else
127
+ super
128
+ end
129
+ end
130
+
131
+ # Resources being currently used
132
+ #
133
+ # @return [Hash]
134
+ def resources
135
+ @resources ||= {}
136
+ end
137
+
138
+ private
139
+
140
+ def on_complete(response)
141
+ fail Promisepay::Error.from_response(response) unless response.success?
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,49 @@
1
+ module Promisepay
2
+ # Configuration options for {Client}, defaulting to values in {Default}.
3
+ module Configurable
4
+ # @!attribute api_domain
5
+ # @return [String] Promisepay API domain name. default: api.promisepay.com
6
+ # @!attribute environment
7
+ # @see http://docs.promisepay.com/v2.2/docs/environments
8
+ # @return [String] Promisepay environment. default: test
9
+ # @!attribute token
10
+ # @see http://docs.promisepay.com/v2.2/docs/overview-2
11
+ # @return [String] Promisepay token for Basic Authentication.
12
+ # @!attribute username
13
+ # @see http://docs.promisepay.com/v2.2/docs/overview-2
14
+ # @return [String] Promisepay username for Basic Authentication.
15
+
16
+ attr_accessor :api_domain, :environment, :token, :username
17
+
18
+ class << self
19
+ # List of configurable keys for {Promisepay::Client}.
20
+ #
21
+ # @return [Array] of option keys
22
+ def keys
23
+ @keys ||= [
24
+ :environment,
25
+ :api_domain,
26
+ :token,
27
+ :username
28
+ ]
29
+ end
30
+ end
31
+
32
+ # Reset configuration options to default values.
33
+ def reset!
34
+ Promisepay::Configurable.keys.each do |key|
35
+ instance_variable_set(:"@#{key}", Promisepay::Default.options[key])
36
+ end
37
+ self
38
+ end
39
+ alias_method :setup, :reset!
40
+
41
+ # API endpoint to be used by {Promisepay::Client}.
42
+ # Built from {#environment} and {#api_domain}
43
+ #
44
+ # @return [String]
45
+ def api_endpoint
46
+ "https://#{@environment}.#{@api_domain}/"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,47 @@
1
+ module Promisepay
2
+ # Default configuration options for {Client}
3
+ module Default
4
+ # Default API domain
5
+ API_DOMAIN = 'api.promisepay.com'.freeze
6
+
7
+ # Default environment
8
+ ENVIRONMENT = 'test'.freeze
9
+
10
+ class << self
11
+ # Configuration options.
12
+ #
13
+ # @return [Hash]
14
+ def options
15
+ Hash[Promisepay::Configurable.keys.map { |key| [key, send(key)] }]
16
+ end
17
+
18
+ # Default environment from ENV or {ENVIRONMENT}.
19
+ #
20
+ # @return [String]
21
+ def environment
22
+ ENV['PROMISEPAY_ENVIRONMENT'] || ENVIRONMENT
23
+ end
24
+
25
+ # Default API domain from ENV or {API_DOMAIN}.
26
+ #
27
+ # @return [String]
28
+ def api_domain
29
+ ENV['PROMISEPAY_API_DOMAIN'] || API_DOMAIN
30
+ end
31
+
32
+ # Default token from ENV.
33
+ #
34
+ # @return [String]
35
+ def token
36
+ ENV['PROMISEPAY_TOKEN']
37
+ end
38
+
39
+ # Default username from ENV.
40
+ #
41
+ # @return [String]
42
+ def username
43
+ ENV['PROMISEPAY_USERNAME']
44
+ end
45
+ end
46
+ end
47
+ end