NewMonarch-remit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/LICENSE +20 -0
  2. data/README +79 -0
  3. data/lib/remit.rb +133 -0
  4. data/lib/remit/cancel_token.rb +18 -0
  5. data/lib/remit/common.rb +87 -0
  6. data/lib/remit/data_types.rb +164 -0
  7. data/lib/remit/discard_results.rb +18 -0
  8. data/lib/remit/error_codes.rb +116 -0
  9. data/lib/remit/fund_prepaid.rb +31 -0
  10. data/lib/remit/get_account_activity.rb +60 -0
  11. data/lib/remit/get_account_balance.rb +29 -0
  12. data/lib/remit/get_all_credit_instruments.rb +18 -0
  13. data/lib/remit/get_all_prepaid_instruments.rb +18 -0
  14. data/lib/remit/get_debt_balance.rb +23 -0
  15. data/lib/remit/get_outstanding_debt_balance.rb +22 -0
  16. data/lib/remit/get_payment_instruction.rb +21 -0
  17. data/lib/remit/get_pipeline.rb +181 -0
  18. data/lib/remit/get_prepaid_balance.rb +23 -0
  19. data/lib/remit/get_results.rb +27 -0
  20. data/lib/remit/get_token_by_caller.rb +19 -0
  21. data/lib/remit/get_token_usage.rb +18 -0
  22. data/lib/remit/get_tokens.rb +20 -0
  23. data/lib/remit/get_total_prepaid_liability.rb +22 -0
  24. data/lib/remit/get_transaction.rb +42 -0
  25. data/lib/remit/install_payment_instruction.rb +22 -0
  26. data/lib/remit/ipn_request.rb +56 -0
  27. data/lib/remit/pay.rb +35 -0
  28. data/lib/remit/pipeline_response.rb +62 -0
  29. data/lib/remit/refund.rb +38 -0
  30. data/lib/remit/reserve.rb +30 -0
  31. data/lib/remit/retry_transaction.rb +18 -0
  32. data/lib/remit/settle.rb +20 -0
  33. data/lib/remit/settle_debt.rb +30 -0
  34. data/lib/remit/subscribe_for_caller_notification.rb +18 -0
  35. data/lib/remit/unsubscribe_for_caller_notification.rb +17 -0
  36. data/lib/remit/write_off_debt.rb +28 -0
  37. data/spec/integrations/get_account_activity_spec.rb +36 -0
  38. data/spec/integrations/get_tokens_spec.rb +38 -0
  39. data/spec/integrations/integrations_helper.rb +8 -0
  40. data/spec/spec_helper.rb +40 -0
  41. data/spec/units/get_pipeline_spec.rb +165 -0
  42. data/spec/units/get_results_spec.rb +47 -0
  43. data/spec/units/ipn_request_spec.rb +34 -0
  44. data/spec/units/pay_spec.rb +70 -0
  45. data/spec/units/units_helper.rb +4 -0
  46. metadata +115 -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,79 @@
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
+ Setup
14
+ ------
15
+ Requires a config file located at #{RAILS_ROOT}/config/amazon_fps.yml. Use the following format.
16
+
17
+ development:
18
+ access_key: <your sandbox access key>
19
+ secret_access_key: <your sandbox secret access key>
20
+ return_base: <your development return url>
21
+ endpoint: 'https://fps.sandbox.amazonaws.com/'
22
+ pipeline: 'https://authorize.payments-sandbox.amazon.com/cobranded-ui/actions/start'
23
+ sandbox: true
24
+ version: '2007-01-08'
25
+
26
+ test:
27
+ access_key: <your sandbox access key>
28
+ secret_access_key: <your sandbox secret access key>
29
+ return_base: <your development return url>
30
+ endpoint: 'https://fps.sandbox.amazonaws.com/'
31
+ pipeline: 'https://authorize.payments-sandbox.amazon.com/cobranded-ui/actions/start'
32
+ sandbox: true
33
+ version: '2007-01-08'
34
+
35
+ production:
36
+ access_key: <your real access key>
37
+ secret_access_key: <your real secret access key>
38
+ return_base: <your production return url>
39
+ version: '2007-01-08'
40
+ endpoint: 'https://fps.amazonaws.com/'
41
+ pipeline: 'https://authorize.payments.amazon.com/cobranded-ui/actions/start'
42
+ sandbox: false
43
+
44
+
45
+
46
+ Users
47
+ -----
48
+
49
+ The following sites are using Remit:
50
+
51
+ * http://www.storenvy.com/
52
+ * http://www.obsidianportal.com/
53
+
54
+
55
+ Sandbox
56
+ -------
57
+ Amazon provides a testing environment for the FPS called a sandbox. You may
58
+ (and should) use the sandbox while testing your application. It can be enabled
59
+ by passing a value of true to the last argument of the API constructor.
60
+
61
+
62
+ Example
63
+ -------
64
+ The following example shows how to load up the API, initialize the service, and
65
+ make a simple call to get the tokens stored on the account:
66
+
67
+ require 'remit'
68
+
69
+ ACCESS_KEY = '<your AWS access key>'
70
+ SECRET_KEY = '<your AWS secret key>'
71
+
72
+ # connect using the API's sandbox mode
73
+ remit = Remit::API.new(ACCESS_KEY, SECRET_KEY, true)
74
+
75
+ response = remit.get_tokens
76
+ puts response.tokens.first.token_id
77
+
78
+
79
+ Copyright (c) 2007-2008 Tyler Hunt, released under the MIT license
data/lib/remit.rb ADDED
@@ -0,0 +1,133 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ require 'openssl'
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'date'
7
+ require 'base64'
8
+ require 'erb'
9
+
10
+ require 'rubygems'
11
+ require 'relax'
12
+
13
+ require 'remit/common'
14
+ require 'remit/data_types'
15
+
16
+ require 'remit/cancel_token'
17
+ require 'remit/discard_results'
18
+ require 'remit/error_codes'
19
+ require 'remit/fund_prepaid'
20
+ require 'remit/get_account_activity'
21
+ require 'remit/get_account_balance'
22
+ require 'remit/get_all_credit_instruments'
23
+ require 'remit/get_all_prepaid_instruments'
24
+ require 'remit/get_debt_balance'
25
+ require 'remit/get_outstanding_debt_balance'
26
+ require 'remit/get_payment_instruction'
27
+ require 'remit/get_pipeline'
28
+ require 'remit/get_prepaid_balance'
29
+ require 'remit/get_results'
30
+ require 'remit/get_token_usage'
31
+ require 'remit/get_tokens'
32
+ require 'remit/get_token_by_caller'
33
+ require 'remit/get_total_prepaid_liability'
34
+ require 'remit/get_transaction'
35
+ require 'remit/install_payment_instruction'
36
+ require 'remit/ipn_request'
37
+ require 'remit/pay'
38
+ require 'remit/pipeline_response'
39
+ require 'remit/refund'
40
+ require 'remit/reserve'
41
+ require 'remit/retry_transaction'
42
+ require 'remit/settle'
43
+ require 'remit/settle_debt'
44
+ require 'remit/subscribe_for_caller_notification'
45
+ require 'remit/unsubscribe_for_caller_notification'
46
+ require 'remit/write_off_debt'
47
+
48
+ module Remit
49
+ class API < Relax::Service
50
+ include CancelToken
51
+ include DiscardResults
52
+ include FundPrepaid
53
+ include GetAccountActivity
54
+ include GetAccountBalance
55
+ include GetAllCreditInstruments
56
+ include GetAllPrepaidInstruments
57
+ include GetDebtBalance
58
+ include GetOutstandingDebtBalance
59
+ include GetPaymentInstruction
60
+ include GetPipeline
61
+ include GetPrepaidBalance
62
+ include GetResults
63
+ include GetTokenUsage
64
+ include GetTokens
65
+ include GetTokenByCaller
66
+ include GetTotalPrepaidLiability
67
+ include GetTransaction
68
+ include InstallPaymentInstruction
69
+ include Pay
70
+ include Refund
71
+ include Reserve
72
+ include RetryTransaction
73
+ include Settle
74
+ include SettleDebt
75
+ include SubscribeForCallerNotification
76
+ include UnsubscribeForCallerNotification
77
+ include WriteOffDebt
78
+
79
+ attr_reader :pipeline
80
+ attr_reader :access_key
81
+ attr_reader :secret_key
82
+
83
+
84
+ path = File.expand_path "#{RAILS_ROOT}/config/amazon_fps.yml" # Should generate amazon_fps.yml file on install
85
+ h = YAML.load_file path rescue raise "No config file round at /config/amazon_fps.yml!"
86
+ config = h[RAILS_ENV].symbolize_keys
87
+ # set default params and set AWS keys
88
+ ACCESS_KEY = config[:access_key]
89
+ SECRET_ACCESS_KEY = config[:secret_access_key]
90
+ ENDPOINT = config[:endpoint]
91
+ RETURN_BASE = config[:return_base]
92
+ PIPELINE = config[:pipeline]
93
+ SANDBOX = config[:sandbox]
94
+ API_VERSION = config[:version]
95
+ SIGNATURE_VERSION = 1
96
+
97
+ def initialize
98
+ super(ENDPOINT)
99
+ @pipeline = PIPELINE
100
+ @access_key = ACCESS_KEY
101
+ @secret_key = SECRET_ACCESS_KEY
102
+ end
103
+
104
+ private
105
+
106
+ def new_query(query = {})
107
+ SignedQuery.new(@endpoint, @secret_key, query)
108
+ end
109
+
110
+ def default_query
111
+ new_query({
112
+ :AWSAccessKeyId => @access_key,
113
+ :SignatureVersion => SIGNATURE_VERSION,
114
+ :Version => API_VERSION,
115
+ :Timestamp => Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
116
+ })
117
+ end
118
+
119
+ def query(request)
120
+ query = super
121
+ query[:Signature] = sign(query)
122
+ query
123
+ end
124
+
125
+ def sign(values)
126
+ keys = values.keys.sort { |a, b| a.to_s.downcase <=> b.to_s.downcase }
127
+ signature = keys.inject('') do |signature, key|
128
+ signature += key.to_s + values[key].to_s
129
+ end
130
+ Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, @secret_key, signature)).strip
131
+ end
132
+ end
133
+ end
@@ -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,87 @@
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
+ def node_name(name, namespace = nil)
23
+ super(name.to_s.gsub(/(^|_)(.)/) { $2.upcase }, namespace)
24
+ end
25
+ end
26
+
27
+ class Response < BaseResponse
28
+ parameter :request_id
29
+ attr_accessor :status
30
+ attr_accessor :errors
31
+
32
+ def initialize(xml)
33
+ super
34
+
35
+ if is?(:Response) and has?(:Errors)
36
+ @errors = elements(:Errors).collect do |error|
37
+ Error.new(error)
38
+ end
39
+ else
40
+ @status = text_value(element(:Status))
41
+ @errors = elements('errors/errors').collect do |error|
42
+ ServiceError.new(error)
43
+ end if not successful?
44
+ end
45
+ end
46
+
47
+ def successful?
48
+ @status == ResponseStatus::SUCCESS
49
+ end
50
+
51
+ def node_name(name, namespace = nil)
52
+ super(name.to_s.split('/').collect{ |tag|
53
+ tag.gsub(/(^|_)(.)/) { $2.upcase }
54
+ }.join('/'), namespace)
55
+ end
56
+
57
+ end
58
+
59
+ class SignedQuery < Relax::Query
60
+ def initialize(uri, secret_key, query = {})
61
+ super(query)
62
+ @uri = URI.parse(uri.to_s)
63
+ @secret_key = secret_key
64
+ end
65
+
66
+ def sign
67
+ delete_if { |key, value| key == :awsSignature }
68
+ store(:awsSignature, Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA1.new, @secret_key, "#{@uri.path}?#{to_s(false)}".gsub('%20', '+'))).strip)
69
+ end
70
+
71
+ def to_s(signed = true)
72
+ sign if signed
73
+ super()
74
+ end
75
+
76
+ class << self
77
+ def parse(uri, secret_key, query_string)
78
+ query = self.new(uri, secret_key)
79
+ query_string.split('&').each do |parameter|
80
+ key, value = parameter.split('=', 2)
81
+ query[key] = unescape_value(value)
82
+ end
83
+ query
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,164 @@
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 TemporaryDeclinePolicy < BaseResponse
13
+ parameter :temporary_decline_policy_type
14
+ parameter :implicit_retry_timeout_in_mins
15
+ end
16
+
17
+ class DescriptorPolicy < BaseResponse
18
+ parameter :soft_descriptor_type
19
+ parameter :CS_number_of
20
+ end
21
+
22
+ class ChargeFeeTo
23
+ CALLER = 'Caller'
24
+ RECIPIENT = 'Recipient'
25
+ end
26
+
27
+ class Error < BaseResponse
28
+ parameter :code
29
+ parameter :message
30
+ end
31
+
32
+ class InstrumentStatus
33
+ ALL = 'ALL'
34
+ ACTIVE = 'Active'
35
+ INACTIVE = 'Inactive'
36
+ end
37
+
38
+ class PaymentMethods
39
+ BALANCE_TRANSFER = 'abt'
40
+ BANK_ACCOUNT = 'ach'
41
+ CREDIT_CARD = 'credit card'
42
+ PREPAID = 'prepaid'
43
+ DEBT = 'Debt'
44
+ end
45
+
46
+ class ServiceError < BaseResponse
47
+ parameter :error_type
48
+ parameter :is_retriable
49
+ parameter :error_code
50
+ parameter :reason_text
51
+
52
+ class ErrorType
53
+ SYSTEM = 'System'
54
+ BUSINESS = 'Business'
55
+ end
56
+ end
57
+
58
+ class ResponseStatus
59
+ SUCCESS = 'Success'
60
+ FAILURE = 'Failure'
61
+ end
62
+
63
+ class Token < BaseResponse
64
+ parameter :token_id
65
+ parameter :friendly_name
66
+ parameter :status
67
+ parameter :date_installed, :type => :time
68
+ parameter :caller_installed
69
+ parameter :caller_reference
70
+ parameter :token_type
71
+ parameter :old_token_id
72
+ parameter :payment_reason
73
+
74
+ class TokenStatus
75
+ ACTIVE = 'Active'
76
+ INACTIVE = 'Inactive'
77
+ end
78
+ end
79
+
80
+ class TokenUsageLimit < BaseResponse
81
+ parameter :count
82
+ parameter :limit
83
+ parameter :last_reset_amount
84
+ parameter :last_reset_count
85
+ parameter :last_reset_time_stamp
86
+ end
87
+
88
+ class TransactionResponse < BaseResponse
89
+ parameter :transaction_id
90
+ parameter :status
91
+ parameter :status_detail
92
+ parameter :new_sender_token_usage, :type => TokenUsageLimit
93
+
94
+ %w(reserved success failure initiated reinitiated temporary_decline).each do |status_name|
95
+ define_method("#{status_name}?") do
96
+ self.status == Remit::TransactionStatus.const_get(status_name.sub('_', '').upcase)
97
+ end
98
+ end
99
+ end
100
+
101
+ class TransactionStatus
102
+ RESERVED = 'Reserved'
103
+ SUCCESS = 'Success'
104
+ FAILURE = 'Failure'
105
+ INITIATED = 'Initiated'
106
+ REINITIATED = 'Reinitiated'
107
+ TEMPORARYDECLINE = 'TemporaryDecline'
108
+ end
109
+
110
+ class TokenType
111
+ SINGLE_USE = 'SingleUse'
112
+ MULTI_USE = 'MultiUse'
113
+ RECURRING = 'Recurring'
114
+ UNRESTRICTED = 'Unrestricted'
115
+ end
116
+
117
+ class PipelineName
118
+ SINGLE_USE = 'SingleUse'
119
+ MULTI_USE = 'MultiUse'
120
+ RECURRING = 'Recurring'
121
+ RECIPIENT = 'Recipient'
122
+ SETUP_PREPAID = 'SetupPrepaid'
123
+ SETUP_POSTPAID = 'SetupPostpaid'
124
+ end
125
+
126
+ class PipelineStatusCode
127
+ CALLER_EXCEPTION = 'CE' # problem with your code
128
+ SYSTEM_ERROR = 'SE' # system error, try again
129
+ SUCCESS_ABT = 'SA' # successful payment with Amazon balance
130
+ SUCCESS_ACH = 'SB' # successful payment with bank transfer
131
+ SUCCESS_CC = 'SC' # successful payment with credit card
132
+ ABORTED = 'A' # user aborted payment
133
+ PAYMENT_METHOD_MISMATCH = 'PE' # user does not have payment method requested
134
+ PAYMENT_METHOD_UNSUPPORTED = 'NP' # account doesn't support requested payment method
135
+ INVALID_CALLER = 'NM' # you are not a valid 3rd party caller to the transaction
136
+ SUCCESS_RECIPIENT_TOKEN_INSTALLED = 'SR'
137
+ end
138
+
139
+ module RequestTypes
140
+ class Amount < Remit::Request
141
+ parameter :amount
142
+ parameter :currency_code
143
+ end
144
+
145
+ class TemporaryDeclinePolicy < Remit::Request
146
+ parameter :temporary_decline_policy_type
147
+ parameter :implicit_retry_timeout_in_mins
148
+ end
149
+
150
+ class DescriptorPolicy < Remit::Request
151
+ parameter :soft_descriptor_type
152
+ parameter :CS_number_of
153
+ end
154
+ end
155
+
156
+ class Operation
157
+ PAY = "Pay"
158
+ REFUND = "Refund"
159
+ SETTLE = "Settle"
160
+ SETTLE_DEBT = "SettleDebt"
161
+ WRITE_OFF_DEBT = "WriteOffDebt"
162
+ FUND_PREPAID = "FundPrepaid"
163
+ end
164
+ end