promisepay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ module Promisepay
2
+ # Custom error class for rescuing from all Promisepau errors
3
+ class Error < StandardError
4
+ # Returns the appropriate Promisepay::Error subclass based on status
5
+ #
6
+ # @param [Faraday::Response] response Faraday HTTP response
7
+ # @return [Promisepay::Error]
8
+ def self.from_response(response)
9
+ klass = case response.status
10
+ when 400 then Promisepay::BadRequest
11
+ when 401 then Promisepay::Unauthorized
12
+ when 403 then Promisepay::Forbidden
13
+ when 404 then Promisepay::NotFound
14
+ when 405 then Promisepay::MethodNotAllowed
15
+ when 406 then Promisepay::NotAcceptable
16
+ when 409 then Promisepay::Conflict
17
+ when 422 then Promisepay::UnprocessableEntity
18
+ when 400..499 then Promisepay::ClientError
19
+ when 500 then Promisepay::InternalServerError
20
+ when 501 then Promisepay::NotImplemented
21
+ when 502 then Promisepay::BadGateway
22
+ when 503 then Promisepay::ServiceUnavailable
23
+ when 500..599 then Promisepay::ServerError
24
+ end
25
+ (klass) ? klass.new(response) : new(response)
26
+ end
27
+
28
+ def initialize(response = nil)
29
+ @response = response
30
+ super(build_error_message)
31
+ end
32
+
33
+ private
34
+
35
+ def build_error_message
36
+ return nil if @response.nil? || @response.body.nil?
37
+
38
+ json_response = JSON.parse(@response.body)
39
+ message = ''
40
+ message << json_response['message'] if json_response.key?('message')
41
+ if json_response.key?('errors')
42
+ json_response['errors'].each do |attribute, content|
43
+ message << "#{attribute}: #{content}"
44
+ end
45
+ end
46
+
47
+ message
48
+ end
49
+ end
50
+
51
+ # Raised on errors in the 400-499 range
52
+ class ClientError < Error; end
53
+
54
+ # Raised when Promisepay returns a 400 HTTP status code
55
+ class BadRequest < ClientError; end
56
+
57
+ # Raised when Promisepay returns a 401 HTTP status code
58
+ class Unauthorized < ClientError; end
59
+
60
+ # Raised when Promisepay returns a 403 HTTP status code
61
+ class Forbidden < ClientError; end
62
+
63
+ # Raised when Promisepay returns a 404 HTTP status code
64
+ class NotFound < ClientError; end
65
+
66
+ # Raised when Promisepay returns a 405 HTTP status code
67
+ class MethodNotAllowed < ClientError; end
68
+
69
+ # Raised when Promisepay returns a 406 HTTP status code
70
+ class NotAcceptable < ClientError; end
71
+
72
+ # Raised when Promisepay returns a 409 HTTP status code
73
+ class Conflict < ClientError; end
74
+
75
+ # Raised when Promisepay returns a 422 HTTP status code
76
+ class UnprocessableEntity < ClientError; end
77
+
78
+ # Raised on errors in the 500-599 range
79
+ class ServerError < Error; end
80
+
81
+ # Raised when Promisepay returns a 500 HTTP status code
82
+ class InternalServerError < ServerError; end
83
+
84
+ # Raised when Promisepay returns a 501 HTTP status code
85
+ class NotImplemented < ServerError; end
86
+
87
+ # Raised when Promisepay returns a 502 HTTP status code
88
+ class BadGateway < ServerError; end
89
+
90
+ # Raised when Promisepay returns a 503 HTTP status code
91
+ class ServiceUnavailable < ServerError; end
92
+ end
@@ -0,0 +1,5 @@
1
+ module Promisepay
2
+ # Manage Accounts
3
+ class Account < BaseModel
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ module Promisepay
2
+ # Manage Bank accounts
3
+ class BankAccount < Account
4
+ # Get the user the bank account belongs to.
5
+ #
6
+ # @see http://docs.promisepay.com/v2.2/docs/bank_accountsidusers
7
+ #
8
+ # @return [Promisepay::User]
9
+ def user
10
+ response = JSON.parse(@client.get("bank_accounts/#{send(:id)}/users").body)
11
+ Promisepay::User.new(@client, response['users'])
12
+ end
13
+
14
+ # Deletes a bank account for a user on a marketplace.
15
+ # Sets the account to in-active.
16
+ #
17
+ # @see http://docs.promisepay.com/v2.2/docs/bank_accountsid
18
+ #
19
+ # @param mobile_pin [String] Mobile PIN.
20
+ #
21
+ # @return [Boolean]
22
+ def deactivate(mobile_pin)
23
+ @client.delete("bank_accounts/#{id}", mobile_pin: mobile_pin)
24
+ @attributes['active'] = false
25
+ true
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ module Promisepay
2
+ # Base model for all the other models to inherit from
3
+ class BaseModel
4
+ def initialize(client, attributes = {})
5
+ @client = client
6
+ @attributes = stringify_keys(attributes)
7
+ end
8
+
9
+ def method_missing(name, *args, &block)
10
+ if @attributes.key?(name.to_s)
11
+ @attributes[name.to_s]
12
+ else
13
+ super
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def stringify_keys(hash)
20
+ Hash[hash.map { |k, v| [k.to_s, v] }]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module Promisepay
2
+ # Manage Card accounts
3
+ class CardAccount < Account
4
+ # Get the user the card account belongs to.
5
+ #
6
+ # @see http://docs.promisepay.com/v2.2/docs/card_accountsidusers
7
+ #
8
+ # @return [Promisepay::User]
9
+ def user
10
+ response = JSON.parse(@client.get("card_accounts/#{send(:id)}/users").body)
11
+ Promisepay::User.new(@client, response['users'])
12
+ end
13
+
14
+ # Deletes a card account for a user on a marketplace.
15
+ # Sets the account to in-active.
16
+ #
17
+ # @see http://docs.promisepay.com/v2.2/docs/card_accountsid
18
+ #
19
+ # @return [Boolean]
20
+ def deactivate
21
+ @client.delete("card_accounts/#{id}")
22
+ @attributes['active'] = false
23
+ true
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module Promisepay
2
+ # Manage Fees
3
+ class Fee < BaseModel
4
+ end
5
+ end
@@ -0,0 +1,197 @@
1
+ module Promisepay
2
+ # Manage Items
3
+ class Item < BaseModel
4
+ # Update the attributes of an item.
5
+ #
6
+ # @see http://docs.promisepay.com/v2.2/docs/itemsiduseraction
7
+ #
8
+ # @param attributes [Hash] Item's attributes to be updated.
9
+ #
10
+ # @return [self]
11
+ def update(attributes)
12
+ response = JSON.parse(@client.patch("items/#{send(:id)}", attributes).body)
13
+ @attributes = response['items']
14
+ self
15
+ end
16
+
17
+ # Show the item status for a marketplace.
18
+ #
19
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidstatus
20
+ #
21
+ # @return [Hash]
22
+ def status
23
+ response = JSON.parse(@client.get("items/#{send(:id)}/status").body)
24
+ response['items']
25
+ end
26
+
27
+ # Show the buyer detail for a single item for a marketplace.
28
+ #
29
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidbuyers
30
+ #
31
+ # @return [Promisepay::User]
32
+ def buyer
33
+ response = JSON.parse(@client.get("items/#{send(:id)}/buyers").body)
34
+ Promisepay::User.new(@client, response['users'])
35
+ end
36
+
37
+ # Show the seller detail for a single item for a marketplace.
38
+ #
39
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidbuyers
40
+ #
41
+ # @return [Promisepay::User]
42
+ def seller
43
+ response = JSON.parse(@client.get("items/#{send(:id)}/sellers").body)
44
+ Promisepay::User.new(@client, response['users'])
45
+ end
46
+
47
+ # Get fees associated to the item.
48
+ #
49
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidfees
50
+ #
51
+ # @param options [Hash] Optional options.
52
+ # @option options [Integer] :limit Can ask for up to 200 fees. default: 10
53
+ # @option options [Integer] :offset Pagination help. default: 0
54
+ #
55
+ # @return [Array<Promisepay::Fee>]
56
+ def fees(options = {})
57
+ response = JSON.parse(@client.get("items/#{send(:id)}/fees", options).body)
58
+ fees = response.key?('fees') ? response['fees'] : []
59
+ fees.map { |attributes| Promisepay::Fee.new(@client, attributes) }
60
+ end
61
+
62
+ # Get historical transaction for the item.
63
+ #
64
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidtransactions
65
+ #
66
+ # @param options [Hash] Optional options.
67
+ # @option options [Integer] :limit Can ask for up to 200 transactions. default: 10
68
+ # @option options [Integer] :offset Pagination help. default: 0
69
+ #
70
+ # @return [Array<Promisepay::Transaction>]
71
+ def transactions(options = {})
72
+ response = JSON.parse(@client.get("items/#{send(:id)}/transactions", options).body)
73
+ transactions = response.key?('transactions') ? response['transactions'] : []
74
+ transactions.map { |attributes| Promisepay::Transaction.new(@client, attributes) }
75
+ end
76
+
77
+ # Show the wire details for payment.
78
+ #
79
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidwire_details
80
+ #
81
+ # @return [Hash]
82
+ def wire_details
83
+ response = JSON.parse(@client.get("items/#{send(:id)}/wire_details").body)
84
+ response['items']['wire_details']
85
+ end
86
+
87
+ # Show the bpay details details for payment.
88
+ #
89
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidbpay_details
90
+ #
91
+ # @return [Hash]
92
+ def bpay_details
93
+ response = JSON.parse(@client.get("items/#{send(:id)}/bpay_details").body)
94
+ response['items']['bpay_details']
95
+ end
96
+
97
+ # .
98
+ #
99
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidaction
100
+ #
101
+ # @return [Boolean]
102
+ def make_payment(options = {})
103
+ @client.patch("items/#{send(:id)}/make_payment", options).body
104
+ true
105
+ end
106
+
107
+ # .
108
+ #
109
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidrequest_payment
110
+ #
111
+ # @return [Boolean]
112
+ def request_payment(options = {})
113
+ @client.patch("items/#{send(:id)}/request_payment", options).body
114
+ true
115
+ end
116
+
117
+ # .
118
+ #
119
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidrequest_payment
120
+ #
121
+ # @return [Boolean]
122
+ def release_payment(options = {})
123
+ @client.patch("items/#{send(:id)}/release_payment", options).body
124
+ true
125
+ end
126
+
127
+ # .
128
+ #
129
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidrequest_release
130
+ #
131
+ # @return [Boolean]
132
+ def request_release(options = {})
133
+ @client.patch("items/#{send(:id)}/request_release", options).body
134
+ true
135
+ end
136
+
137
+ # .
138
+ #
139
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidacknowledge_wire
140
+ #
141
+ # @return [Boolean]
142
+ def acknowledge_wire(options = {})
143
+ @client.patch("items/#{send(:id)}/acknowledge_wire", options).body
144
+ true
145
+ end
146
+
147
+ # .
148
+ #
149
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidacknowledge_paypal
150
+ #
151
+ # @return [Boolean]
152
+ def acknowledge_paypal(options = {})
153
+ @client.patch("items/#{send(:id)}/acknowledge_paypal", options).body
154
+ true
155
+ end
156
+
157
+ # .
158
+ #
159
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidrevert_wire
160
+ #
161
+ # @return [Boolean]
162
+ def revert_wire(options = {})
163
+ @client.patch("items/#{send(:id)}/revert_wire", options).body
164
+ true
165
+ end
166
+
167
+ # .
168
+ #
169
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidrequest_refund
170
+ #
171
+ # @return [Boolean]
172
+ def request_refund(options = {})
173
+ @client.patch("items/#{send(:id)}/request_refund", options).body
174
+ true
175
+ end
176
+
177
+ # .
178
+ #
179
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidrefund
180
+ #
181
+ # @return [Boolean]
182
+ def refund(options = {})
183
+ @client.patch("items/#{send(:id)}/refund", options).body
184
+ true
185
+ end
186
+
187
+ # .
188
+ #
189
+ # @see http://docs.promisepay.com/v2.2/docs/itemsidcancel
190
+ #
191
+ # @return [Boolean]
192
+ def cancel
193
+ @client.patch("items/#{id}/cancel")
194
+ true
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,26 @@
1
+ module Promisepay
2
+ # Manage Paypal accounts
3
+ class PaypalAccount < Account
4
+ # Get the user the paypal account belongs to.
5
+ #
6
+ # @see http://docs.promisepay.com/v2.2/docs/paypal_accountsidusers
7
+ #
8
+ # @return [Promisepay::User]
9
+ def user
10
+ response = JSON.parse(@client.get("paypal_accounts/#{send(:id)}/users").body)
11
+ Promisepay::User.new(@client, response['users'])
12
+ end
13
+
14
+ # Deletes a Paypal account for a user on a marketplace.
15
+ # Sets the account to in-active.
16
+ #
17
+ # @see http://docs.promisepay.com/v2.2/docs/paypal_accountsid
18
+ #
19
+ # @return [Boolean]
20
+ def deactivate
21
+ @client.delete("paypal_accounts/#{id}")
22
+ @attributes['active'] = false
23
+ true
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ module Promisepay
2
+ # Manage Transactions
3
+ class Transaction < Account
4
+ # .
5
+ #
6
+ # @see https://test.api.promisepay.com/transactions/:id/users
7
+ #
8
+ # @return [Promisepay::User]
9
+ def user
10
+ response = JSON.parse(@client.get("transactions/#{send(:id)}/users").body)
11
+ response.key?('users') ? Promisepay::User.new(@client, response['users']) : nil
12
+ end
13
+
14
+ # Gets a transactions fee details if applicable.
15
+ #
16
+ # @see http://docs.promisepay.com/v2.2/docs/transactionsidfees
17
+ #
18
+ # @return [Promisepay::Fee]
19
+ def fee
20
+ response = JSON.parse(@client.get("transactions/#{send(:id)}/fees").body)
21
+ response.key?('fees') ? Promisepay::Fee.new(@client, response['fees']) : nil
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,63 @@
1
+
2
+ module Promisepay
3
+ # Manage Users
4
+ class User < BaseModel
5
+ # Lists items for a user on a marketplace.
6
+ #
7
+ # @see http://docs.promisepay.com/v2.2/docs/usersiditems
8
+ #
9
+ # @return [Array<Promisepay::Item>]
10
+ def items
11
+ response = JSON.parse(@client.get("users/#{send(:id)}/items").body)
12
+ users = response.key?('items') ? response['items'] : []
13
+ users.map { |attributes| Promisepay::Item.new(@client, attributes) }
14
+ end
15
+
16
+ # Gets Bank account for a user on a marketplace.
17
+ #
18
+ # @see http://docs.promisepay.com/v2.2/docs/usersidbank_accounts
19
+ #
20
+ # @return [Promisepay::BankAccount]
21
+ def bank_account
22
+ response = JSON.parse(@client.get("users/#{send(:id)}/bank_accounts").body)
23
+ Promisepay::BankAccount.new(@client, response['bank_accounts'])
24
+ rescue Promisepay::UnprocessableEntity
25
+ nil
26
+ end
27
+
28
+ # Gets Card account for a user on a marketplace.
29
+ #
30
+ # @see http://docs.promisepay.com/v2.2/docs/usersidcard_accounts
31
+ #
32
+ # @return [Promisepay::CardAccount]
33
+ def card_account
34
+ response = JSON.parse(@client.get("users/#{send(:id)}/card_accounts").body)
35
+ Promisepay::CardAccount.new(@client, response['card_accounts'])
36
+ rescue Promisepay::UnprocessableEntity
37
+ nil
38
+ end
39
+
40
+ # Gets PayPal account for a user on a marketplace.
41
+ #
42
+ # @see http://docs.promisepay.com/v2.2/docs/usersidpaypal_accounts
43
+ #
44
+ # @return [Promisepay::PaypalAccount]
45
+ def paypal_account
46
+ response = JSON.parse(@client.get("users/#{send(:id)}/paypal_accounts").body)
47
+ Promisepay::PaypalAccount.new(@client, response['paypal_accounts'])
48
+ rescue Promisepay::UnprocessableEntity
49
+ nil
50
+ end
51
+
52
+ # Set the disbursement account for a user.
53
+ #
54
+ # @see http://docs.promisepay.com/v2.2/docs/usersiddisbursement_account
55
+ #
56
+ # @return [Boolean]
57
+ def disbursement_account(account_id)
58
+ options = { account_id: account_id }
59
+ JSON.parse(@client.post("users/#{send(:id)}/disbursement_account", options).body)
60
+ true
61
+ end
62
+ end
63
+ end