Class: Omnipay::Adapters::SampleAdapter
- Inherits:
-
Object
- Object
- Omnipay::Adapters::SampleAdapter
- Defined in:
- lib/omnipay/sample_adapter.rb
Overview
This is a sample Omnipay adapter implementation for a fictive payment provider. You can use it as a boilerplate to develop your own.
This adapter will take two arguments in its configuration :
-
api_key [String - mandatory] : given by the payment provider, used for authenticating to their API
-
sandbox [Boolean - optional] : whether the payment should be done in a sandbox or with the real payment page
It may then be set up like this in a rack application
Omnipay.use_gateway(
:uid => 'my_gateway',
:adapter => Omnipay::Adapters::SampleAdapter,
:config => {
:api_key => 'my-api-key',
:sandbox => (ENV['RACK_ENV'] != 'production')
}
)
Let’s say our payment provider needs to be given the birthdate of the buyer for every payment. It also accepts a locale to display the payment page in It means that the redirection to the payment provider will be called like this :
my_gateway = Omnipay.gateways.find('my_gateway')
my_gateway.payment_redirection({
:amount => 1295, # Amount in cents, mandatory for every adapter
:host => 'http://www.my-website.com', # Also mandatory for every adapter, used to compute the redirect_url
:birthdate => current_user.birthdate, # Custom field for this adapter class
:locale => 'fr' # Optional field for this adapter class
})
Instance Method Summary collapse
-
#callback_hash(params) ⇒ Hash
Analyze the redirection from the payment provider, to determine if the payment is a success, and its details.
-
#initialize(config = {}) ⇒ SampleAdapter
constructor
The adapters initialization.
-
#request_phase(amount, callback_url, params = {}) ⇒ Array
Responsible for determining the redirection to the payment page for a given amount.
Constructor Details
#initialize(config = {}) ⇒ SampleAdapter
The adapters initialization
35 36 37 38 39 40 |
# File 'lib/omnipay/sample_adapter.rb', line 35 def initialize(config = {}) @api_key = config[:api_key] raise ArgumentError.new("Missing api_key") unless @api_key @mode = (config[:sandbox] == false) ? 'production' : 'sandbox' end |
Instance Method Details
#callback_hash(params) ⇒ Hash
Analyze the redirection from the payment provider, to determine if the payment is a success, and its details
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/omnipay/sample_adapter.rb', line 80 def callback_hash(params) transaction_id = params[:transaction_id] transaction = fetch_transaction(transaction_id) if !transaction return { :success => false, :error => Omnipay::INVALID_RESPONSE, :error_message => "No transaction found with id #{transaction_id}"} end if transaction.success { :success => true, :transaction_id => transaction_id } else if transaction.canceled { :success => false, :error => Omnipay::CANCELATION } else { :success => false, :error => Omnipay::PAYMENT_REFUSED, :error_message => "Transaction #{transaction_id} was not successful : #{transaction.error}" } end end end |
#request_phase(amount, callback_url, params = {}) ⇒ Array
Responsible for determining the redirection to the payment page for a given amount
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/omnipay/sample_adapter.rb', line 54 def request_phase(amount, callback_url, params={}) # Check our params birthdate = params[:birthdate] && params[:birthdate].to_date raise ArgumentError.new('parameter birthdate must be given') if birthdate.blank? locale = params[:locale] || 'en' # Build the transaction (this is where the provider-specific code happens) amount_in_dollar = (amount.to_f / 100).round(2) payment_params = build_transaction_for_provider(amount, birthdate, locale, callback_url) # Return the redirection details (method, url, params) return ['GET', 'http://my-provider-payment-endpoint.com', payment_params] end |