Class: Saferpay
- Inherits:
-
Object
- Object
- Saferpay
- Defined in:
- lib/ruby-saferpay.rb
Defined Under Namespace
Classes: AttributeError, ConfigError, Request, TransactionError, 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'
- 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_request ⇒ Object
readonly
Returns the value of attribute current_request.
Instance Method Summary collapse
- #cancel(transaction_id = nil) ⇒ Object
-
#capture(transaction_id = 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, back_reference = nil) ⇒ Object
(also: #lastschrift)
Direct debit on aquierer’s bank account.
- #details(transaction_id = nil) ⇒ Object
-
#initialize(conf_path, account_id, pan, expiry_date, cvc = '', name = '', tolerance = 0) ⇒ Saferpay
constructor
def initialize( account_id = “99867-94913159”, pan = “9451123100000004”, expiry_date = ‘1107’, cvc = ”, name = ”, tolerance = 0 ).
-
#pay(amount, currency = 'EUR', back_reference = nil) ⇒ 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, back_reference = nil) ⇒ Object
- #refund_last ⇒ Object
- #refund_transaction(transaction_id = nil, back_reference = nil) ⇒ Object
- #reserve(amount, currency, back_reference = nil) ⇒ Object
- #valid_param?(param, conditions) ⇒ Boolean
Constructor Details
#initialize(conf_path, account_id, pan, expiry_date, cvc = '', name = '', tolerance = 0) ⇒ Saferpay
def initialize( account_id = “99867-94913159”, pan = “9451123100000004”, expiry_date = ‘1107’, cvc = ”, name = ”, tolerance = 0 )
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ruby-saferpay.rb', line 96 def initialize( conf_path, account_id, pan, expiry_date, cvc = '', name = '', tolerance = 0 ) raise ArgumentError, "Need a valid account_id; was: \"#{account_id}\" (class: #{account_id.class})" unless valid_param?(account_id, {:class => String, :rexp => /^[\d]{5}-[\d]{8}$/}) raise ArgumentError, "Need a valid card number; was: \"#{pan}\" (class: #{pan.class})" unless valid_param?(pan, {:class => String, :rexp => /^[\d]{16}$/}) raise ArgumentError, "Need a valid expiry_date; was: \"#{expiry_date}\" (class: #{expiry_date.class})" unless valid_param?(expiry_date, {:class => String, :rexp => /^[\d]{4}$/}) Saferpay.check_install @current_request = nil @conf_path = conf_path @account_id = account_id @pan = pan @expiry_date= expiry_date @cvc = cvc.gsub(/[\s]/,'').chomp rescue '' @name = name.chomp rescue '' @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.
88 89 90 |
# File 'lib/ruby-saferpay.rb', line 88 def account_id @account_id end |
#current_request ⇒ Object (readonly)
Returns the value of attribute current_request.
89 90 91 |
# File 'lib/ruby-saferpay.rb', line 89 def current_request @current_request end |
Instance Method Details
#cancel(transaction_id = nil) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/ruby-saferpay.rb', line 162 def cancel( transaction_id = nil ) transaction_id = @current_request.transaction_id if transaction_id.nil? raise ArgumentError, "Cancel failed. No transaction to cancel (transaction_id is nil)" if transaction_id.nil? @current_request = Request.new( @conf_path, :cancel, :transaction_id => transaction_id, :accountid => @account_id ) do_direct_cc @current_request.cmd end |
#capture(transaction_id = nil) ⇒ Object
Capture the amount of the transaction with id transaction_id Defaults to capturing the current transaction’s amount.
156 157 158 159 160 |
# File 'lib/ruby-saferpay.rb', line 156 def capture( transaction_id = nil ) transaction_id = @current_request.transaction_id if transaction_id.nil? @current_request = Request.new(@conf_path, :capture, :transaction_id => transaction_id, :accountid => @account_id ) do_direct_cc @current_request.cmd end |
#debit_card_reserve(amount, currency, account_number, blz, back_reference = nil) ⇒ 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
173 174 175 176 177 178 179 180 181 |
# File 'lib/ruby-saferpay.rb', line 173 def debit_card_reserve( amount, currency, account_number, blz, back_reference = nil ) 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 track2 = "\";59#{blz}=#{account_number}\"" # Weird, huh? @current_request = Request.new(@conf_path, :debit_card_reserve, :amount => amount, :currency => currency, :accountid => @account_id, :track2 => track2, :back_reference => back_reference ) do_direct_cc @current_request.cmd end |
#details(transaction_id = nil) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/ruby-saferpay.rb', line 124 def details( transaction_id = nil ) transaction_id = @current_request.transaction_id if transaction_id.nil? raise ArgumentError, "Cannot retrieve transaction details without a transaction id." if transaction_id.nil? req = Request.new(@conf_path, :inquiry, :transaction_id => transaction_id, :accountid => @account_id) do_direct_cc req.cmd end |
#pay(amount, currency = 'EUR', back_reference = nil) ⇒ Object
Convenience method to:
-
Authorize (reserve)
-
Capture
-
Get transacton details
-
Batch clear (needed/wanted? Probably not…) (not implemented)
118 119 120 121 122 |
# File 'lib/ruby-saferpay.rb', line 118 def pay(amount, currency = 'EUR', back_reference = nil ) reserve amount, currency, back_reference 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)
188 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 |
# File 'lib/ruby-saferpay.rb', line 188 def ( amount, currency, description, backlink = 'http://localhost', faillink = 'http://localhost', successlink = 'http://localhost', notifyurl = 'http://localhost') @current_request = TransactionParams.new( :amount => amount, :currency => currency, :description => description, :accountid => @account_id, :backlink => backlink, :faillink => faillink, :successlink => successlink, :notifyurl => notifyurl ) #TODO: fix this! cmd = "#{BASEDIR}#{EXECUTABLE} -payinit -p #{CONFIG} -a #{@current_request.to_s}" logger.debug "#{self.class}#payinit Will execute command:\n#{cmd}" = %x{#{cmd} 2>&1} logger.debug "#{self.class}#payinit result: #{payinit.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, back_reference = nil) ⇒ Object
137 138 139 140 141 |
# File 'lib/ruby-saferpay.rb', line 137 def refund( amount, currency, back_reference = nil ) check_params amount, currency @current_request = Request.new(@conf_path, :refund, :amount => amount, :currency => currency, :accountid => @account_id, :pan => @pan, :exp => @expiry_date, :back_reference => back_reference) do_direct_cc @current_request.cmd end |
#refund_last ⇒ Object
143 144 145 146 |
# File 'lib/ruby-saferpay.rb', line 143 def refund_last raise ArgumentError, "No current request to refund." if @current_request.transaction_id.nil? refund_transaction @current_request.transaction_id end |
#refund_transaction(transaction_id = nil, back_reference = nil) ⇒ Object
148 149 150 151 152 |
# File 'lib/ruby-saferpay.rb', line 148 def refund_transaction( transaction_id = nil, back_reference = nil ) raise ArgumentError, "Cannot refund without transaction_id" if transaction_id.nil? @current_request = Request.new(@conf_path, :refund_transaction, :transaction_id => transaction_id, :back_reference => back_reference, :accountid => @account_id ) do_direct_cc @current_request.cmd end |
#reserve(amount, currency, back_reference = nil) ⇒ Object
131 132 133 134 135 |
# File 'lib/ruby-saferpay.rb', line 131 def reserve( amount, currency, back_reference = nil ) check_params amount, currency @current_request = Request.new(@conf_path, :reserve, :amount => amount, :currency => currency, :accountid => @account_id, :pan => @pan, :exp => @expiry_date, :back_reference => back_reference) do_direct_cc( @current_request.cmd ) end |
#valid_param?(param, conditions) ⇒ Boolean
91 92 93 |
# File 'lib/ruby-saferpay.rb', line 91 def valid_param?(param, conditions) true if param.is_a?( conditions[:class] ) && ( param =~ conditions[:rexp] ) end |