remit2 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/LICENSE +20 -0
  2. data/README.markdown +26 -0
  3. data/lib/remit2.rb +119 -0
  4. data/lib/remit2/data_types.rb +142 -0
  5. data/lib/remit2/error_codes.rb +118 -0
  6. data/lib/remit2/get_pipeline.rb +195 -0
  7. data/lib/remit2/ipn_request.rb +45 -0
  8. data/lib/remit2/operations/cancel_subscription_and_refund.rb +26 -0
  9. data/lib/remit2/operations/cancel_token.rb +16 -0
  10. data/lib/remit2/operations/discard_results.rb +16 -0
  11. data/lib/remit2/operations/fund_prepaid.rb +29 -0
  12. data/lib/remit2/operations/get_account_activity.rb +58 -0
  13. data/lib/remit2/operations/get_account_balance.rb +27 -0
  14. data/lib/remit2/operations/get_all_credit_instruments.rb +16 -0
  15. data/lib/remit2/operations/get_all_prepaid_instruments.rb +16 -0
  16. data/lib/remit2/operations/get_debt_balance.rb +21 -0
  17. data/lib/remit2/operations/get_outstanding_debt_balance.rb +20 -0
  18. data/lib/remit2/operations/get_payment_instruction.rb +19 -0
  19. data/lib/remit2/operations/get_prepaid_balance.rb +21 -0
  20. data/lib/remit2/operations/get_results.rb +25 -0
  21. data/lib/remit2/operations/get_token_by_caller.rb +23 -0
  22. data/lib/remit2/operations/get_token_usage.rb +16 -0
  23. data/lib/remit2/operations/get_tokens.rb +18 -0
  24. data/lib/remit2/operations/get_total_prepaid_liability.rb +20 -0
  25. data/lib/remit2/operations/get_transaction.rb +40 -0
  26. data/lib/remit2/operations/install_payment_instruction.rb +20 -0
  27. data/lib/remit2/operations/pay.rb +38 -0
  28. data/lib/remit2/operations/refund.rb +28 -0
  29. data/lib/remit2/operations/reserve.rb +28 -0
  30. data/lib/remit2/operations/retry_transaction.rb +16 -0
  31. data/lib/remit2/operations/settle.rb +18 -0
  32. data/lib/remit2/operations/settle_debt.rb +28 -0
  33. data/lib/remit2/operations/write_off_debt.rb +26 -0
  34. data/lib/remit2/pipeline_response.rb +52 -0
  35. data/lib/remit2/request.rb +12 -0
  36. data/lib/remit2/response.rb +54 -0
  37. data/lib/remit2/signature.rb +35 -0
  38. data/spec/spec_helper.rb +32 -0
  39. data/spec/units/cancel_subscription_and_refund_spec.rb +26 -0
  40. data/spec/units/get_pipeline_spec.rb +164 -0
  41. data/spec/units/ipn_request_spec.rb +35 -0
  42. data/spec/units/pay_spec.rb +67 -0
  43. data/spec/units/remit_spec.rb +25 -0
  44. data/spec/units/units_helper.rb +4 -0
  45. metadata +119 -0
@@ -0,0 +1,40 @@
1
+ module Remit
2
+ module GetTransaction
3
+ class Request < Remit::Request
4
+ action :GetTransaction
5
+ parameter :transaction_id, :required => true
6
+ end
7
+
8
+ class Response < Remit::Response
9
+ class TransactionDetail < Remit::BaseResponse
10
+ parameter :caller_name
11
+ parameter :caller_token_id
12
+ parameter :caller_transaction_date, :type => :time
13
+ parameter :date_completed, :type => :time
14
+ parameter :date_received, :type => :time
15
+ parameter :error_code
16
+ parameter :error_message
17
+ parameter :fees, :type => Amount
18
+ parameter :meta_data
19
+ parameter :operation
20
+ parameter :payment_method
21
+ parameter :recipient_name
22
+ parameter :recipient_token_id
23
+ parameter :related_transactions
24
+ parameter :sender_name
25
+ parameter :sender_token_id
26
+ parameter :status
27
+ parameter :status_history
28
+ parameter :transaction_amount, :type => Amount
29
+ parameter :transaction_id
30
+ parameter :transaction_parts
31
+ end
32
+
33
+ parameter :transaction, :type => TransactionDetail
34
+ end
35
+
36
+ def get_transaction(request = Request.new)
37
+ call(request, Response)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ module Remit
2
+ module InstallPaymentInstruction
3
+ class Request < Remit::Request
4
+ action :InstallPaymentInstruction
5
+ parameter :payment_instruction, :required => true
6
+ parameter :caller_reference, :required => true
7
+ parameter :token_friendly_name
8
+ parameter :token_type, :required => true
9
+ parameter :payment_reason
10
+ end
11
+
12
+ class Response < Remit::Response
13
+ parameter :token_id
14
+ end
15
+
16
+ def install_payment_instruction(request)
17
+ call(request, Response)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ # Updated for API Version 2008-09-17
2
+ module Remit
3
+ module Pay
4
+ class Request < Remit::Request
5
+ action :Pay
6
+ parameter :caller_description
7
+ parameter :caller_reference, :required => true
8
+ parameter :charge_fee_to
9
+ parameter :descriptor_policy, :type => Remit::RequestTypes::DescriptorPolicy
10
+ parameter :marketplace_fixed_fee, :type => Remit::RequestTypes::Amount
11
+ parameter :marketplace_variable_fee
12
+ parameter :meta_data
13
+ parameter :recipient_description
14
+ parameter :recipient_reference
15
+ parameter :recipient_token_id
16
+ parameter :sender_description
17
+ parameter :sender_reference
18
+ parameter :sender_token_id, :required => true
19
+ parameter :transaction_amount, :type => Remit::RequestTypes::Amount, :required => true
20
+ parameter :transaction_date
21
+ parameter :transaction_timeout_in_mins
22
+ end
23
+
24
+ class InnerResponse < Remit::BaseResponse
25
+ parameter :transaction_id
26
+ parameter :transaction_status
27
+ end
28
+
29
+ class Response < Remit::Response
30
+ parameter :inner, :element => "PayResult", :type => InnerResponse
31
+ inner_parameters :transaction_id, :transaction_status
32
+ end
33
+
34
+ def pay(request = Request.new)
35
+ call(request, Response)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,28 @@
1
+ # Updated for API Version 2008-09-17
2
+ module Remit
3
+ module Refund
4
+ class Request < Remit::Request
5
+ action :Refund
6
+ parameter :caller_description
7
+ parameter :caller_reference, :required => true
8
+ parameter :refund_amount, :type => Remit::RequestTypes::Amount
9
+ parameter :transaction_id, :required => true
10
+ parameter :marketplace_refund_policy
11
+
12
+ # The RefundAmount parameter has multiple components. It is specified on the query string like
13
+ # so: RefundAmount.Amount=XXX&RefundAmount.CurrencyCode=YYY
14
+ def convert_complex_key(key, parameter)
15
+ "#{convert_key(key).to_s}.#{convert_key(parameter).to_s}"
16
+ end
17
+ end
18
+
19
+ class Response < Remit::Response
20
+ parser :rexml
21
+ parameter :refund_result, :type => RefundResult
22
+ end
23
+
24
+ def refund(request = Request.new)
25
+ call(request, Response)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module Remit
2
+ module Reserve
3
+ class Request < Remit::Request
4
+ action :Reserve
5
+ parameter :recipient_token_id, :required => true
6
+ parameter :sender_token_id, :required => true
7
+ parameter :caller_token_id, :required => true
8
+ parameter :sender_reference
9
+ parameter :recipient_reference
10
+ parameter :caller_reference, :required => true
11
+ parameter :transaction_date
12
+ parameter :transaction_amount, :type => Remit::RequestTypes::Amount, :required => true
13
+ parameter :charge_fee_to, :required => true
14
+ parameter :sender_description
15
+ parameter :recipient_description
16
+ parameter :caller_description
17
+ parameter :meta_data
18
+ end
19
+
20
+ class Response < Remit::Response
21
+ parameter :transaction_response, :type => TransactionResponse
22
+ end
23
+
24
+ def reserve(request = Request.new)
25
+ call(request, Response)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ module Remit
2
+ module RetryTransaction
3
+ class Request < Remit::Request
4
+ action :RetryTransaction
5
+ parameter :original_transaction_id, :required => true
6
+ end
7
+
8
+ class Response < Remit::Response
9
+ parameter :transaction_response, :type => TransactionResponse
10
+ end
11
+
12
+ def retry_transaction(request = Request.new)
13
+ call(request, Response)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module Remit
2
+ module Settle
3
+ class Request < Remit::Request
4
+ action :Settle
5
+ parameter :reserve_transaction_id, :required => true
6
+ parameter :transaction_amount, :type => Remit::RequestTypes::Amount
7
+ parameter :transaction_date
8
+ end
9
+
10
+ class Response < Remit::Response
11
+ parameter :transaction_response, :type => TransactionResponse
12
+ end
13
+
14
+ def settle(request = Request.new)
15
+ call(request, Response)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ module Remit
2
+ module SettleDebt
3
+ class Request < Remit::Request
4
+ action :SettleDebt
5
+ parameter :caller_description
6
+ parameter :caller_reference, :required => true
7
+ parameter :caller_token_id, :required => true
8
+ parameter :charge_fee_to, :required => true
9
+ parameter :credit_instrument_id, :required => true
10
+ parameter :meta_data
11
+ parameter :recipient_description
12
+ parameter :recipient_reference
13
+ parameter :sender_description
14
+ parameter :sender_reference
15
+ parameter :sender_token_id, :required => true
16
+ parameter :settlement_amount, :type => Remit::RequestTypes::Amount, :required => true
17
+ parameter :transaction_date
18
+ end
19
+
20
+ class Response < Remit::Response
21
+ parameter :transaction_response, :type => TransactionResponse
22
+ end
23
+
24
+ def settle_debt(request = Request.new)
25
+ call(request, Response)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module Remit
2
+ module WriteOffDebt
3
+ class Request < Remit::Request
4
+ action :WriteOffDebt
5
+ parameter :caller_token_id, :required => true
6
+ parameter :credit_instrument_id, :required => true
7
+ parameter :adjustment_amount, :required => true
8
+ parameter :transaction_date
9
+ parameter :sender_reference
10
+ parameter :caller_reference, :required => true
11
+ parameter :recipient_reference
12
+ parameter :sender_description
13
+ parameter :recipient_description
14
+ parameter :caller_description
15
+ parameter :meta_data
16
+ end
17
+
18
+ class Response < Remit::Response
19
+ parameter :transaction_response, :type => TransactionResponse
20
+ end
21
+
22
+ def write_off_debt(request = Request.new)
23
+ call(request, Response)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ module Remit
2
+ class PipelineResponse
3
+ def initialize(uri, secret_key)
4
+ @uri = URI.parse(uri)
5
+ @secret_key = secret_key
6
+ end
7
+
8
+ # Returns +true+ if the response is correctly signed (awsSignature).
9
+ #
10
+ #--
11
+ # The unescape_value method is used here because the awsSignature value
12
+ # pulled from the request is filtered through the same method.
13
+ #++
14
+ def valid?
15
+ return false unless given_signature
16
+ Relax::Query.unescape_value(correct_signature) == given_signature
17
+ end
18
+
19
+ # Returns +true+ if the response returns a successful state.
20
+ def successful?
21
+ [
22
+ Remit::PipelineStatusCode::SUCCESS_ABT,
23
+ Remit::PipelineStatusCode::SUCCESS_ACH,
24
+ Remit::PipelineStatusCode::SUCCESS_CC,
25
+ Remit::PipelineStatusCode::SUCCESS_RECIPIENT_TOKEN_INSTALLED
26
+ ].include?(request_query[:status])
27
+ end
28
+
29
+ def method_missing(method, *args) #:nodoc:
30
+ if request_query.has_key?(method.to_sym)
31
+ request_query[method.to_sym]
32
+ else
33
+ super
34
+ end
35
+ end
36
+
37
+ def request_query(reload = false)
38
+ @query ||= Remit::SignedQuery.parse(@uri, @secret_key, @uri.query || '')
39
+ end
40
+ private :request_query
41
+
42
+ def given_signature
43
+ request_query[:awsSignature]
44
+ end
45
+ private :given_signature
46
+
47
+ def correct_signature
48
+ Remit::SignedQuery.new(@uri.path, @secret_key, request_query).sign
49
+ end
50
+ private :correct_signature
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ module Remit
2
+ class Request < Relax::Request
3
+ def self.action(name)
4
+ parameter :action, :value => name
5
+ end
6
+
7
+ def convert_key(key)
8
+ key.to_s.gsub(/(^|_)(.)/) { $2.upcase }.to_sym
9
+ end
10
+ protected :convert_key
11
+ end
12
+ end
@@ -0,0 +1,54 @@
1
+ module Remit
2
+ class BaseResponse < Relax::Response
3
+ def node_name(name, namespace=nil)
4
+ super(name.to_s.gsub(/(^|_)(.)/) { $2.upcase }, namespace)
5
+ end
6
+ end
7
+
8
+ class ResponseMetadata < BaseResponse
9
+ parameter :request_id
10
+ end
11
+
12
+ class Response < BaseResponse
13
+ parameter :response_metadata, :type => ResponseMetadata
14
+ attr_accessor :errors
15
+
16
+ def initialize(xml)
17
+ super
18
+ @errors = []
19
+
20
+ if is?(:Response) && has?(:Errors)
21
+ @errors = elements('Errors/Error').collect do |error|
22
+ Error.new(error)
23
+ end
24
+ end
25
+ end
26
+
27
+ def successful?
28
+ @errors.empty?
29
+ end
30
+
31
+ def request_id
32
+ # If successful it's in the ResponseMetadata, if failure, it's in the base
33
+ # response. Very irritating.
34
+ if successful?
35
+ response_metadata.request_id
36
+ else
37
+ elements('RequestId')[0].to_s
38
+ end
39
+ end
40
+
41
+ def node_name(name, namespace=nil)
42
+ super(name.to_s.split('/').collect{ |tag|
43
+ tag.gsub(/(^|_)(.)/) { $2.upcase }
44
+ }.join('/'), namespace)
45
+ end
46
+
47
+ # Easily exposes parameters that are buried in the InnerResponse
48
+ def self.inner_parameters(*fields)
49
+ fields.each do |inner_field_name|
50
+ define_method(inner_field_name) { inner.send(inner_field_name) }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,35 @@
1
+ module Remit
2
+ module Signature
3
+ def sign(secret_key, endpoint, http_verb, params)
4
+ s = string_to_sign(endpoint, http_verb, params)
5
+ Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, secret_key, s)).strip
6
+ end
7
+
8
+ def string_to_sign(endpoint, http_verb, values)
9
+ uri = URI::parse(endpoint.to_s)
10
+
11
+ path = uri.path
12
+ path = "/" if (path.nil? || path.empty?)
13
+ path = urlencode(path).gsub("%2F", "/")
14
+
15
+ host = uri.host.downcase
16
+
17
+ # Explicitly remove the Signature param if found
18
+ sorted_keys = values.keys.reject{ |k| k.to_s == "Signature" }.sort { |x,y| x.to_s <=> y.to_s }
19
+ converted = sorted_keys.collect do |k|
20
+ v = values[k]
21
+ s = urlencode(k)
22
+ s << "=#{urlencode(v)}" unless v.nil?
23
+ s
24
+ end
25
+ params = converted.join("&")
26
+
27
+ "#{http_verb}\n#{host}\n#{path}\n#{params}"
28
+ end
29
+
30
+ # Convert a string into URL encoded form that Amazon accepts
31
+ def urlencode(plaintext)
32
+ CGI.escape(plaintext.to_s).gsub("+", "%20").gsub("%7E", "~")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/remit2'
5
+
6
+ def remit
7
+ @remit ||= Remit::API.new(ACCESS_KEY, SECRET_KEY, true)
8
+ end
9
+
10
+ describe 'a successful response', :shared => true do
11
+ it 'should not have any errors' do
12
+ @response.errors.should be_empty
13
+ end
14
+
15
+ it 'should have a request ID' do
16
+ @response.request_id.should_not be_nil
17
+ end
18
+ end
19
+
20
+ describe 'a failed response', :shared => true do
21
+ it "is not successful" do
22
+ @response.should_not be_successful
23
+ end
24
+
25
+ it "has a request id" do
26
+ @response.request_id.should_not be_empty
27
+ end
28
+
29
+ it "has errors" do
30
+ @response.errors.should_not be_empty
31
+ end
32
+ end