Class: Omnipay::Adapters::SampleAdapter
- Inherits:
-
Object
- Object
- Omnipay::Adapters::SampleAdapter
- Defined in:
- lib/omnipay/adapters/sample_adapter.rb
Overview
This is a sample Omnipay adapter implementation for a fictive payment gateway. 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 gateway, 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 initialized like this in a rack application
use Omnipay::Gateway,
:uid => 'my-gateway',
:adapter => Omnipay::Adapters::SampleAdapter
:config => {
:api_key => 'my-api-key',
:sandbox => (ENV['RACK_ENV'] != 'production')
}
Instance Method Summary collapse
-
#callback_hash(params) ⇒ Hash
The resulting response hash which will be accessible in the application.
-
#initialize(callback_url, config = {}) ⇒ SampleAdapter
constructor
The adapters initialization.
-
#request_phase(amount, params = {}) ⇒ Array
Responsible for determining the redirection to the payment page for a given amount.
Constructor Details
#initialize(callback_url, config = {}) ⇒ SampleAdapter
The adapters initialization
26 27 28 29 30 31 32 33 |
# File 'lib/omnipay/adapters/sample_adapter.rb', line 26 def initialize(callback_url, config = {}) @callback_url = callback_url @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
Returns the resulting response hash which will be accessible in the application. Must contain the following values :
-
:success (
Boolean) | Did the payment occur or not? -
:amount (
Integer) if successful | The amount in cents actually payed -
:transaction_id (
String) if successful | The id of the transaction. Must match the one returned in the request phase. -
:error (
Symbol) if failed | The reason why the payment was not successful. The available values are :-
Omnipay::CANCELED : The payment didn’t occur because of the user.
-
Omnipay::PAYMENT_REFUSED : The payment didn’t occue because of an error on the gateway’s side.
-
Omnipay::INVALID_RESPONSE : The response from the gateway cannot be parsed. The payment may or may not have occured.
-
-
:error_message (
String) if failed | A more detailed error message / stack trace, for logging purposes.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/omnipay/adapters/sample_adapter.rb', line 83 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, :amount => (transaction.amount*100).to_i, :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, params = {}) ⇒ Array
Responsible for determining the redirection to the payment page for a given amount
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/omnipay/adapters/sample_adapter.rb', line 49 def request_phase(amount, params={}) amount_in_dollar = amount * 1.0 / 100 locale = params[:locale] || 'en' transaction = build_new_transaction(amount_in_dollars, locale) uri = URI(transaction.payment_url) method = 'GET' url = "#{uri.scheme}://#{uri.host}#{uri.path}" get_params = Rack::Utils.parse_query(uri.query) transaction_id = transaction.id return [ method, url, get_params, transaction_id ] end |