Class: Saferpay
- Inherits:
-
Object
- Object
- Saferpay
- Defined in:
- lib/ruby-saferpay.rb
Defined Under Namespace
Classes: AttributeError, ConfigError, TransactionError, TransactionParams, WireProtocolError
Constant Summary collapse
- LOGNAME =
File.basename(__FILE__, '.rb')
- LOGDIR =
File.dirname(__FILE__)+'/log'
- BASEDIR =
TODO: re-work this. Use a “.saferpayrc” file with config info?
'/opt/saferpay/'- EXECUTABLE =
'saferpay'- CONFIG =
BASEDIR- CURRENCIES =
Supported currencies. Please note that these are the ones Saferpay supports; and not necessarily the ones your account support. You might encounter “unsupported currency” errors even if the currency is among the following. (Test account supports at least USD, EUR, CHF)
['CHF','CZK','DKK','EUR','GBP','PLN','SEK','USD']
Instance Attribute Summary collapse
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#current_transaction ⇒ Object
readonly
Returns the value of attribute current_transaction.
Class Method Summary collapse
-
.check_install ⇒ Object
Check installation.
-
.logger ⇒ Object
:nodoc:.
-
.setup_logging ⇒ Object
Setup logging for class.
Instance Method Summary collapse
- #cancel(transaction_id = nil, token = nil) ⇒ Object
-
#capture(transaction_id = nil, token = nil, params = nil) ⇒ Object
Capture the amount of the transaction with id transaction_id Defaults to capturing the current transaction’s amount.
-
#debit_card_reserve(amount, currency, account_number, blz) ⇒ Object
(also: #lastschrift)
Direct debit on aquierer’s bank account.
- #details(transaction_id = nil) ⇒ Object
-
#initialize(account_id = "99867-94913159", pan = "9451123100000004", expiry_date = nil, cvc = nil, name = nil, tolerance = 0) ⇒ Saferpay
constructor
A new instance of Saferpay.
-
#pay(amount, currency = 'EUR') ⇒ Object
Convenience method to: 1.
-
#payinit(amount, currency, description, backlink = 'http://localhost', faillink = 'http://localhost', successlink = 'http://localhost', notifyurl = 'http://localhost') ⇒ Object
Create and send a payinit message.
- #refund(amount, currency) ⇒ Object
- #refund_last ⇒ Object
- #refund_transaction(transaction_id) ⇒ Object
- #reserve(amount, currency) ⇒ Object
Constructor Details
#initialize(account_id = "99867-94913159", pan = "9451123100000004", expiry_date = nil, cvc = nil, name = nil, tolerance = 0) ⇒ Saferpay
Returns a new instance of Saferpay.
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby-saferpay.rb', line 87 def initialize( account_id = "99867-94913159", pan = "9451123100000004", expiry_date = nil, cvc = nil, name = nil, tolerance = 0 ) Saferpay.check_install @current_transaction = nil @account_id = account_id @pan = pan @expiry_date= expiry_date @cvc = cvc @name = name @tolerance = tolerance # Amount tolerance in percent. The finally captured amount is AMOUNT + TOLERANCE end |
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
84 85 86 |
# File 'lib/ruby-saferpay.rb', line 84 def account_id @account_id end |
#current_transaction ⇒ Object (readonly)
Returns the value of attribute current_transaction.
85 86 87 |
# File 'lib/ruby-saferpay.rb', line 85 def current_transaction @current_transaction end |
Class Method Details
.check_install ⇒ Object
Check installation
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/ruby-saferpay.rb', line 306 def self.check_install #:nodoc: unless File.exist?(BASEDIR+EXECUTABLE) raise ConfigError, "No saferpay executable in \"#{BASEDIR}#{EXECUTABLE}\"" end unless File.executable?(BASEDIR+EXECUTABLE) raise ConfigError, "Saferpay binary in \"#{BASEDIR}#{EXECUTABLE}\" is not executable" end result = %x{#{BASEDIR+EXECUTABLE} --help} # saferpay --help has an exit code of "1", so can't check for success... #unless $?.success? unless result =~ /^SAFERPAY COMMAND LINE UTILITY/ raise ConfigError, "Cannot execute \"#{BASEDIR+EXECUTABLE} --help\". Result: #{result}\nExit code: #{$?.inspect}" end unless File.exist?(BASEDIR+'config.xml') raise ConfigError, "No config.xml file. Need to run \"saferpay -config\" maybe?" end # Should contain: <IDP MSGTYPE="SetupResponse" GXID="0A5365AB-BCBD-4D72-88C3-32BFD5F09912" CUSTOMERID="99867" VERSION="1" VTAUTOURL="https://www.saferpay.com/user/setup.asp" VTURL="https://www.saferpay.com/vt/Pay.asp" VTKEYID="1-0" CAPTUREURL="https://www.saferpay.com/vt/capture.asp" VTSCRIPTURL="http://www.saferpay.com/OpenSaferpayScript.asp"/> conf = File.read(BASEDIR+'config.xml') unless conf =~ /<IDP MSGTYPE="SetupResponse"/ raise ConfigError, "config.xml looks borked" end unless File.directory? BASEDIR+'keys' raise ConfigError, "No keys directory; configuration needed" end unless File.exist?(BASEDIR+'keys/current') raise ConfigError, "No current key in keys directory" end true end |
.logger ⇒ Object
:nodoc:
287 288 289 |
# File 'lib/ruby-saferpay.rb', line 287 def self.logger #:nodoc: @@loger ||= Saferpay.setup_logging end |
.setup_logging ⇒ Object
Setup logging for class
292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/ruby-saferpay.rb', line 292 def self.setup_logging #:nodoc: @@logger = Log4r::Logger.new LOGNAME @@logger.outputters << Log4r::Outputter.stdout begin op = Log4r::FileOutputter.new LOGNAME, :filename => "#{LOGDIR}/#{LOGNAME}.log", :trunc => false op.formatter = Log4r::PatternFormatter.new(:pattern => "[%c, %d, %l] :: %M") @@logger.outputters << op rescue StandardError @@logger.debug("CANNOT WRITE LOGFILE TO \"#{LOGDIR}/#{LOGNAME}.log\". Proceeding anyway.") end @@logger end |
Instance Method Details
#cancel(transaction_id = nil, token = nil) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/ruby-saferpay.rb', line 159 def cancel( transaction_id = nil, token = nil ) transaction_id = @current_transaction.transaction_id if transaction_id.nil? token = @current_transaction.token if token.nil? params = "-a ACTION Cancel" capture transaction_id, token, params end |
#capture(transaction_id = nil, token = nil, params = nil) ⇒ Object
Capture the amount of the transaction with id transaction_id Defaults to capturing the current transaction’s amount.
152 153 154 155 156 157 |
# File 'lib/ruby-saferpay.rb', line 152 def capture( transaction_id = nil, token = nil, params = nil ) transaction_id = @current_transaction.transaction_id if transaction_id.nil? token = @current_transaction.token if token.nil? cmd = "#{BASEDIR}#{EXECUTABLE} -capt -p #{CONFIG} -i #{transaction_id} -t \"#{token}\" #{params}" do_direct_cc cmd end |
#debit_card_reserve(amount, currency, account_number, blz) ⇒ Object Also known as: lastschrift
Direct debit on aquierer’s bank account. This will always fail with the test account. Takes an account number (10 digits) and a mysterious ‘BLZ’ number (8 digits). This is a very common payment method in Germany and Austria
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby-saferpay.rb', line 171 def debit_card_reserve( amount, currency, account_number, blz ) check_params amount, currency raise AttributeError, "Account number is a ten digit number" unless account_number.length == 10 raise AttributeError, "BLZ is a eight digit number" unless blz.length == 8 @current_transaction = TransactionParams.new( :amount => amount, :currency => currency, :accountid => @account_id, :track2 => "\";59#{blz}=#{account_number}\"" ) do_direct_cc end |
#details(transaction_id = nil) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/ruby-saferpay.rb', line 109 def details( transaction_id = nil ) transaction_id = @current_transaction.transaction_id if transaction_id.nil? cmd = "#{BASEDIR}#{EXECUTABLE} -exec -p #{CONFIG} -m Inquiry -a ID #{transaction_id} -a TYPE Transaction -a TOKEN \"(not used)\"" #logger.debug "#{self.class}#details COMMAND: #{cmd}" do_direct_cc cmd end |
#pay(amount, currency = 'EUR') ⇒ Object
Convenience method to:
-
Authorize (reserve)
-
Capture
-
Batch clear (needed/wanted? Probably not…)
103 104 105 106 107 |
# File 'lib/ruby-saferpay.rb', line 103 def pay(amount, currency = 'EUR' ) reserve amount, currency capture details end |
#payinit(amount, currency, description, backlink = 'http://localhost', faillink = 'http://localhost', successlink = 'http://localhost', notifyurl = 'http://localhost') ⇒ Object
Create and send a payinit message. For use with the Virtual Terminal (VT). Unfinished (meaning: it works, but need tests and some love and also a couple of more methods to handle the paycomplete and other types of messages. Take it for what it is: a stub)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/ruby-saferpay.rb', line 189 def ( amount, currency, description, backlink = 'http://localhost', faillink = 'http://localhost', successlink = 'http://localhost', notifyurl = 'http://localhost') @current_transaction = TransactionParams.new( :amount => amount, :currency => currency, :description => description, :accountid => @account_id, :backlink => backlink, :faillink => faillink, :successlink => successlink, :notifyurl => notifyurl ) cmd = "#{BASEDIR}#{EXECUTABLE} -payinit -p #{CONFIG} -a #{@current_transaction.to_s}" logger.debug "#{self.class}#payinit Will execute command:\n#{cmd}" = %x{#{cmd} 2>&1} logger.debug "#{self.class}#payinit result: #{.inspect}" unless $?.success? logger.error "#{self.class}#payinit FAILED error status: #{$?.inspect}" return false else logger.debug "#{self.class}#payinit Saferpay URI generated" end uri = URI.parse( ) logger.debug "#{self.class}#payinit Built an uri object. Host: #{uri.host}, port: #{uri.port}" http = Net::HTTP.new( uri.host, uri.port ) http.use_ssl = true result = http.start do |http| logger.debug "#{self.class}#payinit GETting uri: #{uri.request_uri}" http.get(uri.request_uri) end unless result.is_a? Net::HTTPOK logger.error "#{self.class}#payinit Cannot reach saferpay site." # TODO: Should re-raise here? return false end # TODO: this suck if result.body =~ /missing ACCOUNTID attribute/ raise AttributeError, 'Missing ACCOUNTID attribute' end case result.body when /missing ACCOUNTID attribute/ raise AttributeError, 'Missing ACCOUNTID attribute' when /missing BACKLINK attribute/ raise AttributeError, 'Missing BACKLINK attribute' when /missing FAILLINK attribute/ raise AttributeError, 'Missing FAILLINK attribute' when /missing SUCCESSLINK|NOTIFYURL attribute/ raise AttributeError, 'Missing SUCCESSLINK|NOTIFYURL attribute' end result end |
#refund(amount, currency) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ruby-saferpay.rb', line 128 def refund( amount, currency ) check_params amount, currency @current_transaction = TransactionParams.new( :amount => amount, :currency => currency, :accountid => @account_id, :pan => @pan, :exp => @expiry_date, :action => "Credit" ) do_direct_cc end |
#refund_last ⇒ Object
141 142 143 |
# File 'lib/ruby-saferpay.rb', line 141 def refund_last refund_transaction @current_transaction.transaction_id end |
#refund_transaction(transaction_id) ⇒ Object
145 146 147 148 |
# File 'lib/ruby-saferpay.rb', line 145 def refund_transaction( transaction_id ) cmd = "#{BASEDIR}#{EXECUTABLE} -capt -p #{CONFIG} -i #{transaction_id} -t \"(not used)\"" do_direct_cc cmd end |
#reserve(amount, currency) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby-saferpay.rb', line 116 def reserve( amount, currency ) check_params amount, currency @current_transaction = TransactionParams.new( :amount => amount, :currency => currency, :accountid => @account_id, :pan => @pan, :exp => @expiry_date ) do_direct_cc end |