tylerhunt-remit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/LICENSE +20 -0
  2. data/README +38 -0
  3. data/lib/remit/cancel_token.rb +18 -0
  4. data/lib/remit/common.rb +88 -0
  5. data/lib/remit/data_types.rb +107 -0
  6. data/lib/remit/discard_results.rb +18 -0
  7. data/lib/remit/fund_prepaid.rb +31 -0
  8. data/lib/remit/get_account_activity.rb +60 -0
  9. data/lib/remit/get_account_balance.rb +29 -0
  10. data/lib/remit/get_all_credit_instruments.rb +18 -0
  11. data/lib/remit/get_all_prepaid_instruments.rb +18 -0
  12. data/lib/remit/get_debt_balance.rb +23 -0
  13. data/lib/remit/get_outstanding_debt_balance.rb +22 -0
  14. data/lib/remit/get_payment_instruction.rb +21 -0
  15. data/lib/remit/get_pipeline.rb +123 -0
  16. data/lib/remit/get_prepaid_balance.rb +23 -0
  17. data/lib/remit/get_results.rb +26 -0
  18. data/lib/remit/get_token_by_caller.rb +19 -0
  19. data/lib/remit/get_token_usage.rb +18 -0
  20. data/lib/remit/get_tokens.rb +20 -0
  21. data/lib/remit/get_total_prepaid_liability.rb +22 -0
  22. data/lib/remit/get_transaction.rb +42 -0
  23. data/lib/remit/install_payment_instruction.rb +22 -0
  24. data/lib/remit/pay.rb +34 -0
  25. data/lib/remit/refund.rb +38 -0
  26. data/lib/remit/reserve.rb +30 -0
  27. data/lib/remit/retry_transaction.rb +18 -0
  28. data/lib/remit/settle.rb +20 -0
  29. data/lib/remit/settle_debt.rb +30 -0
  30. data/lib/remit/subscribe_for_caller_notification.rb +18 -0
  31. data/lib/remit/unsubscribe_for_caller_notification.rb +17 -0
  32. data/lib/remit/write_off_debt.rb +28 -0
  33. data/lib/remit.rb +123 -0
  34. data/spec/get_account_activity_spec.rb +36 -0
  35. data/spec/get_pipeline_spec.rb +106 -0
  36. data/spec/get_tokens_spec.rb +38 -0
  37. data/spec/spec_helper.rb +26 -0
  38. metadata +102 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007-2008 Tyler Hunt
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,38 @@
1
+ Remit
2
+ =====
3
+
4
+ This API provides access to the Amazon Flexible Payment Service (FPS). After
5
+ trying to get the SOAP version of the API written, I began working on this REST
6
+ version to provide a cohesive means of access to all of the functionality of
7
+ the FPS without having to get dirty dealing with SOAP requests.
8
+
9
+ I hope you enjoy using it as much as I've enjoyed writing it. I'm interested to
10
+ hear what sort of uses you find for it. If you find any bugs, let me know (or
11
+ better yet, submit a patch).
12
+
13
+
14
+ Sandbox
15
+ -------
16
+ Amazon provides a testing environment for the FPS called a sandbox. You may
17
+ (and should) use the sandbox while testing your application. It can be enabled
18
+ by passing a value of true to the last argument of the API constructor.
19
+
20
+
21
+ Example
22
+ -------
23
+ The following example shows how to load up the API, initialize the service, and
24
+ make a simple call to get the tokens stored on the account:
25
+
26
+ require 'remit'
27
+
28
+ ACCESS_KEY = '<your AWS access key>'
29
+ SECRET_KEY = '<your AWS secret key>'
30
+
31
+ # connect using the API's sandbox mode
32
+ remit = Remit::API.new(ACCESS_KEY, SECRET_KEY, true)
33
+
34
+ response = remit.get_tokens
35
+ puts response.tokens.first.token_id
36
+
37
+
38
+ Copyright (c) 2007-2008 Tyler Hunt, released under the MIT license
@@ -0,0 +1,18 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module CancelToken
5
+ class Request < Remit::Request
6
+ action :CancelToken
7
+ parameter :token_id
8
+ parameter :reason_text
9
+ end
10
+
11
+ class Response < Remit::Response
12
+ end
13
+
14
+ def cancel_token(request = Request.new)
15
+ call(request, Response)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,88 @@
1
+ require 'base64'
2
+ require 'erb'
3
+ require 'uri'
4
+
5
+ require 'rubygems'
6
+ require 'relax'
7
+
8
+ module Remit
9
+ class Request < Relax::Request
10
+ def self.action(name)
11
+ parameter :action, :value => name
12
+ end
13
+
14
+ protected
15
+
16
+ def convert_key(key)
17
+ key.to_s.gsub(/(^|_)(.)/) { $2.upcase }.to_sym
18
+ end
19
+ end
20
+
21
+ class BaseResponse < Relax::Response
22
+ private
23
+
24
+ def node_name(name)
25
+ name.to_s.gsub(/(^|_)(.)/) { $2.upcase }
26
+ end
27
+ end
28
+
29
+ class Response < BaseResponse
30
+ parameter :request_id
31
+ attr_accessor :status
32
+ attr_accessor :errors
33
+
34
+ def initialize(xml)
35
+ super
36
+
37
+ if is?(:Response) and has?(:Errors)
38
+ @errors = elements(:Errors).collect do |error|
39
+ Error.new(error)
40
+ end
41
+ else
42
+ @status = text_value(element(:Status))
43
+ @errors = elements('errors/errors').collect do |error|
44
+ ServiceError.new(error)
45
+ end if not successful?
46
+ end
47
+ end
48
+
49
+ def successful?
50
+ @status == ResponseStatus::SUCCESS
51
+ end
52
+
53
+ private
54
+
55
+ def node_name(name)
56
+ name.to_s.gsub(/(^|_)(.)/) { $2.upcase }
57
+ end
58
+ end
59
+
60
+ class SignedQuery < Relax::Query
61
+ def initialize(uri, secret_key, query = {})
62
+ super(query)
63
+ @uri = URI.parse(uri.to_s)
64
+ @secret_key = secret_key
65
+ end
66
+
67
+ def sign
68
+ delete_if { |key, value| key == :awsSignature }
69
+ store(:awsSignature, Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, @secret_key, "#{@uri.path}?#{to_s(false)}".gsub('%20', '+'))).strip)
70
+ end
71
+
72
+ def to_s(signed = true)
73
+ sign if signed
74
+ super()
75
+ end
76
+
77
+ class << self
78
+ def parse(uri, secret_key, query_string)
79
+ query = self.new(uri, secret_key)
80
+ query_string.split('&').each do |parameter|
81
+ key, value = parameter.split('=')
82
+ query[key] = unescape_value(value)
83
+ end
84
+ query
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require 'relax'
3
+
4
+ require 'remit/common'
5
+
6
+ module Remit
7
+ class Amount < BaseResponse
8
+ parameter :currency_code
9
+ parameter :amount, :type => :float
10
+ end
11
+
12
+ class ChargeFeeTo
13
+ CALLER = 'Caller'
14
+ RECIPIENT = 'Recipient'
15
+ end
16
+
17
+ class Error < BaseResponse
18
+ parameter :code
19
+ parameter :message
20
+ end
21
+
22
+ class InstrumentStatus
23
+ ALL = 'ALL'
24
+ ACTIVE = 'Active'
25
+ INACTIVE = 'Inactive'
26
+ end
27
+
28
+ class PaymentMethods
29
+ BALANCE_TRANSFER = 'abt'
30
+ BANK_ACCOUNT = 'ach'
31
+ CREDIT_CARD = 'credit card'
32
+ PREPAID = 'prepaid'
33
+ DEBT = 'Debt'
34
+ end
35
+
36
+ class ServiceError < BaseResponse
37
+ parameter :error_type
38
+ parameter :is_retriable
39
+ parameter :error_code
40
+ parameter :reason_text
41
+
42
+ class ErrorType
43
+ SYSTEM = 'System'
44
+ BUSINESS = 'Business'
45
+ end
46
+ end
47
+
48
+ class ResponseStatus
49
+ SUCCESS = 'Success'
50
+ FAILURE = 'Failure'
51
+ end
52
+
53
+ class Token < BaseResponse
54
+ parameter :token_id
55
+ parameter :friendly_name
56
+ parameter :status
57
+ parameter :date_installed, :type => :time
58
+ parameter :caller_installed
59
+ parameter :caller_reference
60
+ parameter :token_type
61
+ parameter :old_token_id
62
+ parameter :payment_reason
63
+
64
+ class TokenStatus
65
+ ACTIVE = 'Active'
66
+ INACTIVE = 'Inactive'
67
+ end
68
+ end
69
+
70
+ class TokenUsageLimit < BaseResponse
71
+ parameter :count
72
+ parameter :limit
73
+ parameter :last_reset_amount
74
+ parameter :last_reset_count
75
+ parameter :last_reset_time_stamp
76
+ end
77
+
78
+ class TransactionResponse < BaseResponse
79
+ parameter :transaction_id
80
+ parameter :status
81
+ parameter :status_detail
82
+ parameter :new_sender_token_usage, :type => TokenUsageLimit
83
+ end
84
+
85
+ class TokenType
86
+ SINGLE_USE = 'SingleUse'
87
+ MULTI_USE = 'MultiUse'
88
+ RECURRING = 'Recurring'
89
+ UNRESTRICTED = 'Unrestricted'
90
+ end
91
+
92
+ class PipelineName
93
+ SINGLE_USE = 'SingleUse'
94
+ MULTI_USE = 'MultiUse'
95
+ RECURRING = 'Recurring'
96
+ RECIPIENT = 'Recipient'
97
+ SETUP_PREPAID = 'SetupPrepaid'
98
+ SETUP_POSTPAID = 'SetupPostpaid'
99
+ end
100
+
101
+ module RequestTypes
102
+ class Amount < Remit::Request
103
+ parameter :amount
104
+ parameter :currency_code
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,18 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module DiscardResults
5
+ class Request < Remit::Request
6
+ action :DiscardResults
7
+ parameter :transaction_ids
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ parameter :discard_errors
12
+ end
13
+
14
+ def discard_results(request = Request.new)
15
+ call(request, Response)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module FundPrepaid
5
+ class Request < Remit::Request
6
+ action :FundPrepaid
7
+ parameter :transaction_ids
8
+ parameter :caller_description
9
+ parameter :caller_reference
10
+ parameter :caller_token_id
11
+ parameter :charge_fee_to
12
+ parameter :funding_amount
13
+ parameter :meta_data
14
+ parameter :prepaid_instrument_id
15
+ parameter :recipient_description
16
+ parameter :recipient_reference
17
+ parameter :sender_description
18
+ parameter :sender_reference
19
+ parameter :sender_token_id
20
+ parameter :transaction_date
21
+ end
22
+
23
+ class Response < Remit::Response
24
+ parameter :transaction_response, :type => TransactionResponse
25
+ end
26
+
27
+ def fund_prepaid(request = Request.new)
28
+ call(request, Response)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,60 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetAccountActivity
5
+ class Request < Remit::Request
6
+ action :GetAccountActivity
7
+ parameter :start_date
8
+ parameter :end_date
9
+ parameter :max_batch_size
10
+ parameter :sort_order_by_date
11
+ parameter :response_group
12
+ parameter :operation
13
+ parameter :payment_method
14
+ parameter :role
15
+ parameter :status
16
+ end
17
+
18
+ class Response < Remit::Response
19
+ class Transaction < Remit::BaseResponse
20
+ class TransactionPart < Remit::BaseResponse
21
+ parameter :account_id
22
+ parameter :role
23
+ parameter :name
24
+ parameter :reference
25
+ parameter :description
26
+ parameter :fee_paid, :type => Amount
27
+ end
28
+
29
+ parameter :caller_name
30
+ parameter :caller_token_id
31
+ parameter :caller_transaction_date, :type => :time
32
+ parameter :date_completed, :type => :time
33
+ parameter :date_received, :type => :time
34
+ parameter :error_code
35
+ parameter :error_detail
36
+ parameter :error_message
37
+ parameter :fees, :type => Amount
38
+ parameter :meta_data
39
+ parameter :operation
40
+ parameter :original_transaction_id
41
+ parameter :payment_method
42
+ parameter :recipient_name
43
+ parameter :sender_name
44
+ parameter :sender_token_id
45
+ parameter :status
46
+ parameter :transaction_amount, :type => Amount
47
+ parameter :transaction_id
48
+ parameter :transaction_parts, :collection => TransactionPart
49
+ end
50
+
51
+ parameter :response_batch_size
52
+ parameter :transactions, :collection => Transaction
53
+ parameter :start_time_for_next_transaction, :type => :time
54
+ end
55
+
56
+ def get_account_activity(request = Request.new)
57
+ call(request, Response)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetAccountBalance
5
+ class Request < Remit::Request
6
+ action :GetAccountBalance
7
+ end
8
+
9
+ class Response < Remit::Response
10
+ class AccountBalance < Remit::BaseResponse
11
+ class AvailableBalances < Remit::BaseResponse
12
+ parameter :disburse_balance, :type => Amount
13
+ parameter :refund_balance, :type => Amount
14
+ end
15
+
16
+ parameter :total_balance, :type => Amount
17
+ parameter :pending_in_balance, :type => Amount
18
+ parameter :pending_out_balance, :type => Amount
19
+ parameter :available_balances, :type => AvailableBalances
20
+ end
21
+
22
+ parameter :account_balance, :type => AccountBalance
23
+ end
24
+
25
+ def get_account_balance(request = Request.new)
26
+ call(request, Response)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetAllCreditInstruments
5
+ class Request < Remit::Request
6
+ action :GetAllCreditInstruments
7
+ parameter :instrument_status
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ parameter :credit_instrument_ids
12
+ end
13
+
14
+ def get_all_credit_instruments(request = Request.new)
15
+ call(request, Response)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetAllPrepaidInstruments
5
+ class Request < Remit::Request
6
+ action :GetAllPrepaidInstruments
7
+ parameter :instrument_status
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ parameter :prepaid_instrument_ids
12
+ end
13
+
14
+ def get_all_prepaid_instruments(request = Request.new)
15
+ call(request, Response)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetDebtBalance
5
+ class Request < Remit::Request
6
+ action :GetDebtBalance
7
+ parameter :credit_instrument_id
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ class DebtBalance < Remit::BaseResponse
12
+ parameter :available_balance, :type => Amount
13
+ parameter :pending_out_balance, :type => Amount
14
+ end
15
+
16
+ parameter :debt_balance, :type => DebtBalance
17
+ end
18
+
19
+ def get_debt_balance(request = Request.new)
20
+ call(request, Response)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetOutstandingDebtBalance
5
+ class Request < Remit::Request
6
+ action :GetOutStandingDebtBalance
7
+ end
8
+
9
+ class Response < Remit::Response
10
+ class OutstandingDebtBalance < Remit::BaseResponse
11
+ parameter :outstanding_balance, :type => Amount
12
+ parameter :pending_out_balance, :type => Amount
13
+ end
14
+
15
+ parameter :outstanding_debt, :type => OutstandingDebtBalance
16
+ end
17
+
18
+ def get_outstanding_debt_balance(request = Request.new)
19
+ call(request, Response)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetPaymentInstruction
5
+ class Request < Remit::Request
6
+ action :GetPaymentInstruction
7
+ parameter :token_id
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ parameter :token, :type => Token
12
+ parameter :payment_instruction
13
+ parameter :account_id
14
+ parameter :token_friendly_name
15
+ end
16
+
17
+ def get_payment_instruction(request = Request.new)
18
+ call(request, Response)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,123 @@
1
+ require 'erb'
2
+
3
+ require 'remit/common'
4
+
5
+ module Remit
6
+ module GetPipeline
7
+ class Pipeline
8
+ @parameters = []
9
+ attr_reader :parameters
10
+
11
+ class << self
12
+ # Create the parameters hash for the subclass.
13
+ def inherited(subclass) #:nodoc:
14
+ subclass.instance_variable_set('@parameters', [])
15
+ end
16
+
17
+ def parameter(name)
18
+ attr_accessor name
19
+ @parameters << name
20
+ end
21
+
22
+ def convert_key(key)
23
+ key.to_s.gsub(/_(.)/) { $1.upcase }.to_sym
24
+ end
25
+
26
+ # Returns a hash of all of the parameters for this request, including
27
+ # those that are inherited.
28
+ def parameters #:nodoc:
29
+ (superclass.respond_to?(:parameters) ? superclass.parameters : []) + @parameters
30
+ end
31
+ end
32
+
33
+ attr_reader :api
34
+
35
+ parameter :pipeline_name
36
+ parameter :return_URL
37
+ parameter :caller_key
38
+
39
+ def initialize(api, pipeline, options)
40
+ @api = api
41
+ @pipeline = pipeline
42
+
43
+ options.each do |k,v|
44
+ self.send("#{k}=", v)
45
+ end
46
+ end
47
+
48
+ def url
49
+ uri = URI.parse(@pipeline)
50
+
51
+ query = {}
52
+ self.class.parameters.each do |p|
53
+ val = self.send(p)
54
+
55
+ # Convert Time values to seconds from Epoch
56
+ val = val.to_i if val.class == Time
57
+
58
+ query[self.class.convert_key(p.to_s)] = val
59
+ end
60
+
61
+ # Remove any unused optional parameters
62
+ query.reject! { |key, value| value.nil? }
63
+
64
+ uri.query = SignedQuery.new(@api.pipeline, @api.secret_key, query).to_s
65
+ uri.to_s
66
+ end
67
+ end
68
+
69
+ class SingleUsePipeline < Pipeline
70
+ parameter :caller_reference
71
+ parameter :payment_reason
72
+ parameter :payment_method
73
+ parameter :transaction_amount
74
+ parameter :recipient_token
75
+ end
76
+
77
+ class RecurringUsePipeline < Pipeline
78
+ parameter :caller_reference
79
+ parameter :payment_reason
80
+ parameter :recipient_token
81
+ parameter :transaction_amount
82
+ parameter :validity_start # Time or seconds from Epoch
83
+ parameter :validity_expiry # Time or seconds from Epoch
84
+ parameter :payment_method
85
+ parameter :recurring_period
86
+ end
87
+
88
+ class PostpaidPipeline < Pipeline
89
+ parameter :caller_reference_sender
90
+ parameter :caller_reference_settlement
91
+ parameter :payment_reason
92
+ parameter :payment_method
93
+ parameter :validity_start # Time or seconds from Epoch
94
+ parameter :validity_expiry # Time or seconds from Epoch
95
+ parameter :credit_limit
96
+ parameter :global_amount_limit
97
+ parameter :usage_limit_type1
98
+ parameter :usage_limit_period1
99
+ parameter :usage_limit_value1
100
+ parameter :usage_limit_type2
101
+ parameter :usage_limit_period2
102
+ parameter :usage_limit_value2
103
+ end
104
+
105
+ def get_single_use_pipeline(options)
106
+ self.get_pipeline(SingleUsePipeline, options)
107
+ end
108
+
109
+ def get_recurring_use_pipeline(options)
110
+ self.get_pipeline(RecurringUsePipeline, options)
111
+ end
112
+
113
+ def get_postpaid_pipeline(options)
114
+ self.get_pipeline(PostpaidPipeline, options)
115
+ end
116
+
117
+ def get_pipeline(pipeline_subclass, options)
118
+ pipeline = pipeline_subclass.new(self, @pipeline, {
119
+ :caller_key => @access_key
120
+ }.merge(options))
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,23 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetPrepaidBalance
5
+ class Request < Remit::Request
6
+ action :GetPrepaidBalance
7
+ parameter :prepaid_instrument_id
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ class PrepaidBalance < Remit::BaseResponse
12
+ parameter :available_balance, :type => Amount
13
+ parameter :pending_in_balance, :type => Amount
14
+ end
15
+
16
+ parameter :prepaid_balance, :type => PrepaidBalance
17
+ end
18
+
19
+ def get_prepaid_balance(request = Request.new)
20
+ call(request, Response)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ require 'remit/common'
2
+
3
+ module Remit
4
+ module GetResults
5
+ class Request < Remit::Request
6
+ action :GetResults
7
+ parameter :operation
8
+ parameter :max_results_count
9
+ end
10
+
11
+ class Response < Remit::Response
12
+ class TransactionResults < Remit::BaseResponse
13
+ parameter :transaction_id
14
+ parameter :operation_type
15
+ parameter :caller_reference
16
+ parameter :transaction_status
17
+ end
18
+
19
+ parameter :transaction_results, :type => TransactionResults
20
+ end
21
+
22
+ def get_results(request = Request.new)
23
+ call(request, Response)
24
+ end
25
+ end
26
+ end