Class: PaymentGateway
- Inherits:
-
Object
- Object
- PaymentGateway
- Defined in:
- lib/payment_gateway.rb
Overview
Default logger
Constant Summary collapse
- REST_API =
constants forbasic URLS and method names
'/api/rest'
- TEST_HOST =
'test.paymentgateway.hu'
- PROD_HOST =
'paymentgateway.hu'
- INIT =
'Init'
- RESULT =
'Result'
- CLOSE =
'Close'
- @@config =
{ :provider => 'OTP', :store => 'sdk_test', :currency => 'HUF', :language => 'HU', :host => '', :header_host => PROD_HOST, :port => '', :use_ssl => 'true', :auto_commit_providers => ['MPP2'], :auto_commit_not_implemented => ['OTPayMP'], :app_host => '', :api_key => '86af3-80e4f-f8228-9498f-910ad' }
- @@valid_config_keys =
@@config.keys
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#approved ⇒ Object
Returns the value of attribute approved.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#language ⇒ Object
Returns the value of attribute language.
-
#order_id ⇒ Object
Returns the value of attribute order_id.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#response_url ⇒ Object
Returns the value of attribute response_url.
-
#transaction_id ⇒ Object
Returns the value of attribute transaction_id.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Class Method Summary collapse
- .config ⇒ Object
-
.configure(opts = {}) ⇒ Object
config through hash.
-
.configure_with(path_to_yaml_file) ⇒ Object
config through yaml file.
Instance Method Summary collapse
-
#close(logger = DefaultLogger.new) ⇒ Object
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. -
#init(logger = DefaultLogger.new) ⇒ Object
The
init
instance method initializes the payment transaction at PaymentGateway. -
#initialize(provider = ) ⇒ PaymentGateway
constructor
A new instance of PaymentGateway.
-
#result(logger = DefaultLogger.new) ⇒ Object
The
result
instance method queries the status of the payment from PG, returns result in a hash. -
#start(logger = DefaultLogger.new) ⇒ Object
The
start
instance method composes the url the user has to be redirected to.
Constructor Details
#initialize(provider = ) ⇒ PaymentGateway
Returns a new instance of PaymentGateway.
84 85 86 |
# File 'lib/payment_gateway.rb', line 84 def initialize(provider = @@config[:provider]) self.provider = provider end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def amount @amount end |
#approved ⇒ Object
Returns the value of attribute approved.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def approved @approved end |
#currency ⇒ Object
Returns the value of attribute currency.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def currency @currency end |
#language ⇒ Object
Returns the value of attribute language.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def language @language end |
#order_id ⇒ Object
Returns the value of attribute order_id.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def order_id @order_id end |
#provider ⇒ Object
Returns the value of attribute provider.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def provider @provider end |
#response_url ⇒ Object
Returns the value of attribute response_url.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def response_url @response_url end |
#transaction_id ⇒ Object
Returns the value of attribute transaction_id.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def transaction_id @transaction_id end |
#user_id ⇒ Object
Returns the value of attribute user_id.
60 61 62 |
# File 'lib/payment_gateway.rb', line 60 def user_id @user_id end |
Class Method Details
.config ⇒ Object
80 81 82 |
# File 'lib/payment_gateway.rb', line 80 def self.config @@config end |
.configure(opts = {}) ⇒ Object
config through hash
63 64 65 |
# 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 |
.configure_with(path_to_yaml_file) ⇒ Object
config through yaml file
68 69 70 71 72 73 74 75 76 77 78 |
# 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 |
Instance Method Details
#close(logger = DefaultLogger.new) ⇒ Object
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
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# 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 |
#init(logger = DefaultLogger.new) ⇒ Object
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)
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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 |
#result(logger = DefaultLogger.new) ⇒ Object
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
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# 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 |
#start(logger = DefaultLogger.new) ⇒ Object
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
139 140 141 142 143 |
# 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 |