Class: Saferpay

Inherits:
Object
  • Object
show all
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'
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

Instance Method Summary collapse

Constructor Details

#initialize(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 )

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby-saferpay.rb', line 95

def initialize( , pan, expiry_date, cvc = '', name = '', tolerance = 0 )
	raise ArgumentError, "Need a valid account_id; was: \"#{}\" (class: #{.class})" unless valid_param?(, {: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
	
	@account_id	=	.gsub(/[\s]/,'').chomp
	@pan				=	pan.gsub(/[\s]/,'').chomp
	@expiry_date=	expiry_date.gsub(/[\s]/,'').chomp
	@cvc				=	cvc.gsub(/[\s]/,'').chomp
	@name				=	name.chomp
	@tolerance	=	tolerance	# Amount tolerance in percent. The finally captured amount is AMOUNT + TOLERANCE
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



87
88
89
# File 'lib/ruby-saferpay.rb', line 87

def 
  @account_id
end

#current_requestObject (readonly)

Returns the value of attribute current_request.



88
89
90
# File 'lib/ruby-saferpay.rb', line 88

def current_request
  @current_request
end

Instance Method Details

#cancel(transaction_id = nil, token = nil) ⇒ Object



159
160
161
162
163
164
# File 'lib/ruby-saferpay.rb', line 159

def cancel( transaction_id = nil, token = nil )
  transaction_id  = @current_request.transaction_id if transaction_id.nil?
  token           = @current_request.token if token.nil?
	@current_request	=	Request.new( :cancel, :transaction_id => transaction_id )
	do_direct_cc @current_request.cmd
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.



153
154
155
156
157
# File 'lib/ruby-saferpay.rb', line 153

def capture( transaction_id = nil, token = nil, params = nil )
  transaction_id =  @current_request.transaction_id if transaction_id.nil?
	@current_request	=	Request.new( :capture, :transaction_id => transaction_id )
	do_direct_cc @current_request.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

Raises:



170
171
172
173
174
175
176
177
178
# File 'lib/ruby-saferpay.rb', line 170

def debit_card_reserve( amount, currency, , blz )
	check_params amount, currency
	raise AttributeError, "Account number is a ten digit number" unless .length == 10
	raise AttributeError, "BLZ is a eight digit number" unless blz.length == 8

	track2	=	"\";59#{blz}=#{}\""	# Weird, huh?
	@current_request	=	Request.new(:debit_card_reserve, :amount => amount, :currency => currency, :accountid => @account_id, :track2 => track2 )
	do_direct_cc @current_request.cmd
end

#details(transaction_id = nil) ⇒ Object

Raises:

  • (ArgumentError)


121
122
123
124
125
126
# File 'lib/ruby-saferpay.rb', line 121

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(:inquiry, :transaction_id => transaction_id)
	do_direct_cc req.cmd
end

#pay(amount, currency = 'EUR') ⇒ Object

Convenience method to:

  1. Authorize (reserve)

  2. Capture

  3. Get transacton details

  4. Batch clear (needed/wanted? Probably not…) (not implemented)



115
116
117
118
119
# File 'lib/ruby-saferpay.rb', line 115

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)



185
186
187
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
# File 'lib/ruby-saferpay.rb', line 185

def payinit( 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
				)
	cmd	=	"#{BASEDIR}#{EXECUTABLE} -payinit -p #{CONFIG} -a #{@current_request.to_s}"
	logger.debug "#{self.class}#payinit Will execute command:\n#{cmd}"
	payinit	=	%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( payinit )
	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



134
135
136
137
138
# File 'lib/ruby-saferpay.rb', line 134

def refund( amount, currency )
	check_params amount, currency
	@current_request	=	Request.new(:refund, :amount => amount, :currency => currency, :accountid => @account_id, :pan => @pan, :exp => @expiry_date)
	do_direct_cc @current_request.cmd
end

#refund_lastObject

Raises:

  • (ArgumentError)


140
141
142
143
# File 'lib/ruby-saferpay.rb', line 140

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) ⇒ Object

Raises:

  • (ArgumentError)


145
146
147
148
149
# File 'lib/ruby-saferpay.rb', line 145

def refund_transaction( transaction_id = nil )
	raise ArgumentError, "Cannot refund without transaction_id" if transaction_id.nil?
	@current_request	=	Request.new(:refund_transaction, :transaction_id => transaction_id)
	do_direct_cc @current_request.cmd
end

#reserve(amount, currency) ⇒ Object



128
129
130
131
132
# File 'lib/ruby-saferpay.rb', line 128

def reserve( amount, currency )
	check_params amount, currency
	@current_request 	=	Request.new(:reserve, :amount => amount, :currency => currency, :accountid => @account_id, :pan => @pan, :exp => @expiry_date)
	do_direct_cc( @current_request.cmd )
end

#valid_param?(param, conditions) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/ruby-saferpay.rb', line 90

def valid_param?(param, conditions)
	 true if param.is_a?( conditions[:class] ) && ( param =~ conditions[:rexp] )
end