Skip to content

Instantly share code, notes, and snippets.

@nbibler
Created January 14, 2009 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbibler/46941 to your computer and use it in GitHub Desktop.
Save nbibler/46941 to your computer and use it in GitHub Desktop.
Using Remit with AmazonFPS
##
# This would be visited by the person ultimately paying for the service or
# product, sometimes referred to as your "end customer".
#
class ClientController < ApplicationController
##
# Generate and redirect the client to the single use payment pipeline, using
# the recipient token collected earlier.
#
def new
redirect_to remit.get_single_use_pipeline({
:caller_reference => "sender-payment-#{Time.now.to_i}",
:recipient_token => recipient_token_for_this_transaction,
:payment_reason => "Payment for Widget",
:transaction_amount => '12.34',
:return_URL => 'http://my.domain.local/client/create'
}).url
end
##
# Check, validate, and store the sender token to capture the payment, or
# charge the transaction immediately. Think of the sender token as being
# similar to a credit card authorization.
#
def create
if response.valid? && response.successful?
# store the sender token to use it later, or just use immediately to
# charge the transaction as follows:
request = returning Remit::Pay::Request.new do |r|
r.recipient_token_id = recipient.token_id
r.sender_token_id = sender.token_id
r.caller_token_id = caller.token_id
r.transaction_amount = Remit::RequestTypes::Amount.new(:currency_code => 'USD', :amount => 12.34)
r.charge_fee_to = Remit::ChargeFeeTo::RECIPIENT
r.caller_reference = "my-first-transaction-#{Time.now.to_i}"
r.meta_data = "Payment for Widget"
r.marketplace_variable_fee = '10'
end
payment_response = remit.pay(request)
if payment_response.successful?
# Profit!!
end
end
end
private
def remit
# same idea as RecipientController.
end
end
##
# Your recipient (3rd party) must follow this link and agree to your
# marketplace terms (10% fee, they pay Amazon fees). Once they have agreed,
# you will receive a "recipient token" which will be used when charging on
# their behalf.
#
class RecipientController < ApplicationController
##
# Generate a Recipient Pipeline URL with custom reference identifiers, a
# 10% maximum marketplace fee (the fee you get for performing the
# transaction on behalf of the 3rd party), the recipient pays Amazon
# transaction fees, and the URL to which the recipient should return.
#
def new
redirect_to remit.get_recipient_pipeline({
:caller_reference => "my-recipient-#{Time.now.to_i}",
:caller_reference_refund => "my-recipient-refund-#{Time.now.to_i}",
:recipient_pays_fee => true,
:max_variable_fee => '10',
:return_URL => 'http://my.domain.local/recipient_return'
}).url
end
##
#
def create
response = Remit::PipelineResponse.new(request.request_uri, AMAZON[:secret_access_key])
if response.valid? && response.successful?
# response.userID
# response.tokenID
# response.refundTokenID
# ... store the token information to use later ...
end
redirect_to root_url
end
private
##
# Initialize a Remit API object with your Amazon credentials (and instruct
# the API whether or not to use the Amazon sandbox [true for sandbox, false
# for production])
#
def remit
@remit ||= Remit::API.new(AMAZON[:access_key_id], AMAZON[:secret_access_key], USE_AMAZON_FPS_SANDBOX)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment