class PaymentGateway
Constants
- CLOSE
- INIT
- PROD_HOST
- REST_API
constants forbasic URLS and method names
- RESULT
- TEST_HOST
Attributes
Public Class Methods
# File lib/payment_gateway.rb, line 80 def self.config @@config end
config through hash
# File lib/payment_gateway.rb, line 63 def self.configure(opts = {}) opts.each { |k,v| @@config[k.to_sym] = v if @@valid_config_keys.include?(k.to_sym) } end
config through yaml file
# File lib/payment_gateway.rb, line 68 def self.configure_with(path_to_yaml_file) begin config = YAML::load(IO.read(path_to_yaml_file)) rescue Errno::ENOENT log(:warning, "YAML configuration file couldn't be found. Using defaults."); return rescue Psych::SyntaxError log(:warning, "YAML configuration file contains invalid syntax. Using defaults."); return end configure(config) end
# File lib/payment_gateway.rb, line 84 def initialize(provider = @@config[:provider]) self.provider = provider end
Public Instance Methods
The close
instance method finalizes the payment in one of the following ways: 1) cancel the authorization hold on the customer's credit or debit card, or 2) submit the transaction and thus effectively charging the customer's card.
Attributes¶ ↑
-
transaction_id mandatory PG transaction id, returned by init
-
approve (optional, boolean) cancel, or submit, defaults to true
Parameters¶ ↑
-
logger (optional) = logger to use (must implement
log(request_param_string, response_param_string)
Example¶ ↑
pg = PaymentGateway.new pg.transaction_id = '1234hfajl' pg.approve = false pg.close
# File lib/payment_gateway.rb, line 185 def close(logger = DefaultLogger.new) request_hash = { 'TransactionId' => transaction_id.to_s, 'Approved' => approved.to_s || true } result = submit_request(CLOSE, request_hash) logger.log(request_hash.to_s, result.to_s) return result rescue => e logger.log(request_hash.to_s, result.to_s) raise e end
The init
instance method initializes the payment transaction at PaymentGateway
. Returns response as a hash.
Attributes¶ ↑
-
provider optional, initialize sets it to defaults
-
response_url mandatory, relative to the app host
-
amount amount to charge the customer
-
order_id optional, vendor-specific id of the order
-
user_id optional, vendor-specific id of the user
Parameters¶ ↑
-
logger optional logger to use (must implement
log(request_param_string, response_param_string)
)
Example¶ ↑
pg = PaymentGateway.new pg.provider = provider pg.response_url = some_url pg.amount = 3000 pg.currency = "HUF" pg.order_id = "order123" pg.user_id = "user123" pg.language = "HU" response = pg.init(logger)
# File lib/payment_gateway.rb, line 108 def init(logger = DefaultLogger.new) request_hash = { 'ProviderName' => provider, 'StoreName' => @@config[:store], 'ResponseUrl' => CGI::escape(@@config[:app_host] + response_url), 'Amount' => amount.to_s, 'OrderId' => order_id.to_s, 'UserId' => user_id.to_s, 'Currency' => @@config[:currency], 'Language' => @@config[:language], 'AutoCommit' => (@@config[:auto_commit_providers].include?(provider).to_s if !@@config[:auto_commit_not_implemented].include?(provider)) } result = submit_request(INIT, request_hash) logger.log(request_hash.to_s, result.to_s) return result rescue => e logger.log(request_hash.to_s, result.to_s) raise e end
The result
instance method queries the status of the payment from PG, returns result in a hash.
Attributes¶ ↑
-
transaction_id mandatory, PG transaction id, returned by init
Parameters¶ ↑
-
logger (optional) = logger to use (must implement
log(request_param_string, response_param_string)
)
Example¶ ↑
pg = PaymentGateway.new pg.transaction_id = '123456789abc' result = pg.result
# File lib/payment_gateway.rb, line 155 def result(logger = DefaultLogger.new) request_hash = { 'TransactionId' => transaction_id.to_s } result = submit_request(RESULT, request_hash) logger.log(request_hash.to_s, result.to_s) return result rescue => e logger.log(request_hash.to_s, result.to_s) raise e end
The start
instance method composes the url the user has to be redirected to.
Attributes¶ ↑
-
transaction_id mandatory
Example¶ ↑
pg = PaymentGateway.new
pg.transaction_id = '123456789abc' redirect_to pg.start
# File lib/payment_gateway.rb, line 139 def start(logger = DefaultLogger.new) url = 'http://' + @@config[:header_host] + '/Start?TransactionId=' + transaction_id.to_s logger.log(url, nil) return url end
Private Instance Methods
submit_request
takes the method name and the request parameters as a hash, uses RestClient to submit the request to the PaymentGateway
REST API, and returs the parsed response as hash.
Parameters¶ ↑
-
method = method name, eg.: 'Init'
-
request_hash = request parameters as hash
Example¶ ↑
submit_request('Result', {'TransactionId' => '123456789abc'})
# File lib/payment_gateway.rb, line 212 def submit_request(method, request_hash) header_key = Base64.encode64(@@config[:store] + ':' + @@config[:api_key]) response = RestClient.post( @@config[:header_host] + REST_API, {:method => method, :json => request_hash.to_json}, :accept => :json, :Authorization => "Basic #{header_key}") JSON.load(response).to_hash end