Module: AmazonFlexPay

Defined in:
lib/amazon_flex_pay.rb,
lib/amazon_flex_pay/api.rb,
lib/amazon_flex_pay/model.rb,
lib/amazon_flex_pay/signing.rb,
lib/amazon_flex_pay/pipelines.rb

Defined Under Namespace

Modules: API, DataTypes, Enumerations, Pipelines Classes: Model

Constant Summary collapse

VERSION =
'0.9.13'
API_VERSION =
'2011-09-20'
PIPELINE_VERSION =
'2009-01-09'
UNSAFE =

:nodoc:

/[^A-Za-z0-9_.~-]/

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.access_keyObject

Returns the value of attribute access_key.



26
27
28
# File 'lib/amazon_flex_pay.rb', line 26

def access_key
  @access_key
end

.api_endpointObject

The URL used for API calls.

Defaults to the sandbox unless you set it explicitly or call go_live!.



32
33
34
# File 'lib/amazon_flex_pay.rb', line 32

def api_endpoint
  @api_endpoint ||= 'https://fps.sandbox.amazonaws.com/'
end

.pipeline_endpointObject

The URL used for pipeline redirects.

Defaults to the sandbox unless you set it explicitly or call go_live!.



40
41
42
# File 'lib/amazon_flex_pay.rb', line 40

def pipeline_endpoint
  @pipeline_endpoint ||= 'https://authorize.payments-sandbox.amazon.com/cobranded-ui/actions/start'
end

.secret_keyObject

Returns the value of attribute secret_key.



27
28
29
# File 'lib/amazon_flex_pay.rb', line 27

def secret_key
  @secret_key
end

Class Method Details

.cancel(transaction_id, options = {}) ⇒ Object



10
11
12
# File 'lib/amazon_flex_pay/api.rb', line 10

def cancel(transaction_id, options = {})
  API::Cancel.new(options.merge(:transaction_id => transaction_id)).submit
end

.cancel_token(token_id, options = {}) ⇒ Object



17
18
19
# File 'lib/amazon_flex_pay/api.rb', line 17

def cancel_token(token_id, options = {})
  API::CancelToken.new(options.merge(:token_id => token_id)).submit
end

.edit_token_pipeline(caller_reference, options = {}) ⇒ Object

Creates a pipeline that may be used to change the payment method of a token.

Note that this does not allow changing a token’s limits or recipients or really anything but the method.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/EditTokenPipeline.html



11
12
13
# File 'lib/amazon_flex_pay/pipelines.rb', line 11

def edit_token_pipeline(caller_reference, options = {})
  AmazonFlexPay::Pipelines::EditToken.new(options.merge(:caller_reference => caller_reference))
end

.escape(value) ⇒ Object

Amazon is very specific about which chars should be escaped, and which should not.



37
38
39
40
# File 'lib/amazon_flex_pay/signing.rb', line 37

def escape(value) #:nodoc:
  # note that URI.escape(' ') => '%20', and CGI.escape(' ') => '+'
  URI.escape(value.to_s, UNSAFE)
end

.get_account_activity(start_date, end_date, options = {}) ⇒ Object

Searches through transactions on your account. Helpful if you want to compare vs your own records, or print an admin report.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAccountManagementGuide/GetAccountActivity.html



25
26
27
# File 'lib/amazon_flex_pay/api.rb', line 25

def (start_date, end_date, options = {})
  API::GetAccountActivity.new(options.merge(:start_date => start_date, :end_date => end_date)).submit
end

.get_account_balanceObject



32
33
34
# File 'lib/amazon_flex_pay/api.rb', line 32

def 
  API::GetAccountBalance.new.submit
end

.get_recipient_verification_status(recipient_token_id) ⇒ Object



39
40
41
# File 'lib/amazon_flex_pay/api.rb', line 39

def get_recipient_verification_status(recipient_token_id)
  API::GetRecipientVerificationStatus.new(:recipient_token_id => recipient_token_id).submit
end

.get_token_by_caller_reference(ref) ⇒ Object

Returns information about a token’s state from a caller reference.

This is the closest thing that Amazon’s API has to getting the status of a pipeline. Provide the caller reference for your pipeline (you are saving caller references, right?), and if this returns a token you can assume the pipeline finished successfully.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/GetTokensByCaller.html



50
51
52
# File 'lib/amazon_flex_pay/api.rb', line 50

def get_token_by_caller_reference(ref)
  API::GetTokenByCaller.new(:caller_reference => ref).submit
end

.get_token_by_id(id) ⇒ Object

Returns information about a token’s state from a token id.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/GetTokensByCaller.html



57
58
59
# File 'lib/amazon_flex_pay/api.rb', line 57

def get_token_by_id(id)
  API::GetTokenByCaller.new(:token_id => id).submit
end

.get_token_usage(id) ⇒ Object

Returns information about how much of the token has been used, and what remains.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAccountManagementGuide/GetTokenUsage.html



64
65
66
# File 'lib/amazon_flex_pay/api.rb', line 64

def get_token_usage(id)
  API::GetTokenUsage.new(:token_id => id).submit
end

.get_tokens(options = {}) ⇒ Object

Returns all of your tokens. Note that when you send someone through a recipient pipeline, that registers a recipient token on their account, not yours. And when you send someone through a single use pipeline, that registers a sender token on _their_account, not yours.

So basically this will only return recipient tokens where you are the recipient, or sender tokens where you are the sender.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAccountManagementGuide/GetTokens.html



76
77
78
# File 'lib/amazon_flex_pay/api.rb', line 76

def get_tokens(options = {})
  API::GetTokens.new(options).submit
end

.get_transaction(id) ⇒ Object

Returns all of Amazon’s details about a transaction, such as its status and when it began and finished processing.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAccountManagementGuide/GetTransaction.html



84
85
86
# File 'lib/amazon_flex_pay/api.rb', line 84

def get_transaction(id)
  API::GetTransaction.new(:transaction_id => id).submit
end

.get_transaction_status(id) ⇒ Object

Returns the current status of the transaction. Note that this information is also available from get_transaction.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAccountManagementGuide/GetTransactionStatus.html



92
93
94
# File 'lib/amazon_flex_pay/api.rb', line 92

def get_transaction_status(id)
  API::GetTransactionStatus.new(:transaction_id => id).submit
end

.go_live!Object

By default all API calls and pipeline redirects are in the Amazon Payments sandbox.

Call AmazonFlexPay.go_live! to enable live transactions and real money in this environment.



48
49
50
51
# File 'lib/amazon_flex_pay.rb', line 48

def go_live!
  self.api_endpoint = 'https://fps.amazonaws.com/'
  self.pipeline_endpoint = 'https://authorize.payments.amazon.com/cobranded-ui/actions/start'
end

.multi_use_pipeline(caller_reference, options = {}) ⇒ Object

Creates a pipeline that will authorize you to send money from the user multiple times.

This is also necessary to create sender tokens that are valid for a long period of time, even if you only plan to collect from the token once.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/MultiUsePipeline.html



21
22
23
# File 'lib/amazon_flex_pay/pipelines.rb', line 21

def multi_use_pipeline(caller_reference, options = {})
  AmazonFlexPay::Pipelines::MultiUse.new(options.merge(:caller_reference => caller_reference))
end

.pay(value, currency, sender_token_id, caller_reference, options = {}) ⇒ Object

Begins a Pay request for a sender token. If you are not also the recipient (this is a three-party marketplace transaction) then you must also specify the recipient token.

Sign up for Instant Payment Notifications to be told when this has finished.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/Pay.html



102
103
104
105
106
107
108
# File 'lib/amazon_flex_pay/api.rb', line 102

def pay(value, currency, sender_token_id, caller_reference, options = {})
  API::Pay.new(options.merge(
    :transaction_amount => {:value => value, :currency_code => currency},
    :sender_token_id => sender_token_id,
    :caller_reference => caller_reference
  )).submit
end

.query_string(params, prefix = nil) ⇒ Object

Flattens a possibly-nested hash into a query string for Amazon. With Amazon, nested hashes are flattened with a period, as follows:

AmazonFlexPay.query_string(:foo => {:hello => 'world'})
=> "foo.hello=world"


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/amazon_flex_pay/signing.rb', line 23

def query_string(params, prefix = nil) #:nodoc:
  prefix = "#{prefix}." if prefix
  params.keys.sort { |a, b| a.to_s <=> b.to_s }.collect do |key|
    case val = params[key]
      when Hash
      query_string(val, key)
      else
      "#{prefix}#{key}=#{escape val}"
    end
  end.join('&')
end

.recipient_pipeline(caller_reference, options = {}) ⇒ Object

Creates a pipeline that will authorize you to send money to the user.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/CBUIapiMerchant.html



28
29
30
# File 'lib/amazon_flex_pay/pipelines.rb', line 28

def recipient_pipeline(caller_reference, options = {})
  AmazonFlexPay::Pipelines::Recipient.new(options.merge(:caller_reference => caller_reference))
end

.refund(transaction_id, caller_reference, options = {}) ⇒ Object

Refunds a transaction. By default it will refund the entire transaction, but you can provide an amount for partial refunds.

Sign up for Instant Payment Notifications to be told when this has finished.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/Refund.html



129
130
131
# File 'lib/amazon_flex_pay/api.rb', line 129

def refund(transaction_id, caller_reference, options = {})
  API::Refund.new(options.merge(:transaction_id => transaction_id, :caller_reference => caller_reference)).submit
end

.reserve(value, currency, sender_token_id, caller_reference, options = {}) ⇒ Object

Begins a Reserve request for the sender token. Very similar to pay.

Sign up for Instant Payment Notifications to be told when this has finished.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/Reserve.html



115
116
117
118
119
120
121
# File 'lib/amazon_flex_pay/api.rb', line 115

def reserve(value, currency, sender_token_id, caller_reference, options = {})
  API::Reserve.new(options.merge(
    :transaction_amount => {:value => value, :currency_code => currency},
    :sender_token_id => sender_token_id,
    :caller_reference => caller_reference
  )).submit
end

.settle(transaction_id, options = {}) ⇒ Object

If you have a Reserve transaction, use this to Settle (capture) it.

Sign up for Instant Payment Notifications to be told when this has finished.

See docs.amazonwebservices.com/AmazonFPS/latest/FPSAdvancedGuide/Settle.html



138
139
140
# File 'lib/amazon_flex_pay/api.rb', line 138

def settle(transaction_id, options = {})
  API::Settle.new(options.merge(:reserve_transaction_id => transaction_id)).submit
end

.signature(endpoint, params) ⇒ Object

Returns a signature for the given URL and parameters.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/amazon_flex_pay/signing.rb', line 4

def signature(endpoint, params)
  uri = URI.parse(endpoint)

  signable_string = [
    'GET',
    uri.host,
    uri.path,
    query_string(params)
  ].join("\n")

  Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, AmazonFlexPay.secret_key, signable_string)).strip
end

.single_use_pipeline(caller_reference, options = {}) ⇒ Object

Creates a pipeline that will authorize you to send money from the user one time.

Note that if this payment fails, you must create another pipeline to get another token.

See docs.amazonwebservices.com/AmazonFPS/2010-08-28/FPSBasicGuide/SingleUsePipeline.html



37
38
39
# File 'lib/amazon_flex_pay/pipelines.rb', line 37

def single_use_pipeline(caller_reference, options = {})
  AmazonFlexPay::Pipelines::SingleUse.new(options.merge(:caller_reference => caller_reference))
end

.verify_request(request) ⇒ Object

This is how you verify IPNs and pipeline responses. Pass the entire request object.



144
145
146
147
148
149
150
151
# File 'lib/amazon_flex_pay/api.rb', line 144

def verify_request(request)
  verify_signature(
    # url without query string
    request.protocol + request.host_with_port + request.path,
    # raw parameters
    request.get? ? request.query_string : request.raw_post
  )
end

.verify_signature(url, params) ⇒ Object

Verifies the signature embedded in the params string for the given url.

Please use verify_request instead to make sure the URL and params remain properly formatted.



156
157
158
# File 'lib/amazon_flex_pay/api.rb', line 156

def verify_signature(url, params)
  API::VerifySignature.new(:url_end_point => url, :http_parameters => params).submit
end