-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
secure_pay_au.rb
298 lines (239 loc) · 8.87 KB
/
secure_pay_au.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
require 'rexml/document'
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class SecurePayAuGateway < Gateway
API_VERSION = 'xml-4.2'
PERIODIC_API_VERSION = 'spxml-3.0'
class_attribute :test_periodic_url, :live_periodic_url
self.test_url = 'https://test.api.securepay.com.au/xmlapi/payment'
self.live_url = 'https://api.securepay.com.au/xmlapi/payment'
self.test_periodic_url = 'https://test.securepay.com.au/xmlapi/periodic'
self.live_periodic_url = 'https://api.securepay.com.au/xmlapi/periodic'
self.supported_countries = ['AU']
self.supported_cardtypes = %i[visa master american_express diners_club jcb]
# The homepage URL of the gateway
self.homepage_url = 'http://securepay.com.au'
# The name of the gateway
self.display_name = 'SecurePay'
class_attribute :request_timeout
self.request_timeout = 60
self.money_format = :cents
self.default_currency = 'AUD'
# 0 Standard Payment
# 4 Refund
# 6 Client Reversal (Void)
# 10 Preauthorise
# 11 Preauth Complete (Advice)
TRANSACTIONS = {
purchase: 0,
authorization: 10,
capture: 11,
void: 6,
refund: 4
}
PERIODIC_ACTIONS = {
add_triggered: 'add',
remove_triggered: 'delete',
trigger: 'trigger'
}
PERIODIC_TYPES = {
add_triggered: 4,
remove_triggered: nil,
trigger: nil
}
SUCCESS_CODES = %w[00 08 11 16 77]
def initialize(options = {})
requires!(options, :login, :password)
super
end
def purchase(money, credit_card_or_stored_id, options = {})
if credit_card_or_stored_id.respond_to?(:number)
requires!(options, :order_id)
commit :purchase, build_purchase_request(money, credit_card_or_stored_id, options)
else
options[:billing_id] = credit_card_or_stored_id.to_s
commit_periodic(build_periodic_item(:trigger, money, nil, options))
end
end
def authorize(money, credit_card, options = {})
requires!(options, :order_id)
commit :authorization, build_purchase_request(money, credit_card, options)
end
def capture(money, reference, options = {})
commit :capture, build_reference_request(money, reference)
end
def refund(money, reference, options = {})
commit :refund, build_reference_request(money, reference)
end
def credit(money, reference, options = {})
ActiveMerchant.deprecated CREDIT_DEPRECATION_MESSAGE
refund(money, reference)
end
def void(reference, options = {})
commit :void, build_reference_request(nil, reference)
end
def store(creditcard, options = {})
requires!(options, :billing_id, :amount)
commit_periodic(build_periodic_item(:add_triggered, options[:amount], creditcard, options))
end
def unstore(identification, options = {})
options[:billing_id] = identification
commit_periodic(build_periodic_item(:remove_triggered, options[:amount], nil, options))
end
def supports_scrubbing?
true
end
def scrub(transcript)
transcript.
gsub(%r((<merchantID>).+(</merchantID>)), '\1[FILTERED]\2').
gsub(%r((<password>).+(</password>)), '\1[FILTERED]\2').
gsub(%r((<cardNumber>).+(</cardNumber>)), '\1[FILTERED]\2').
gsub(%r((<cvv>).+(</cvv>)), '\1[FILTERED]\2')
end
private
def build_purchase_request(money, credit_card, options)
xml = Builder::XmlMarkup.new
currency = options[:currency] || currency(money)
xml.tag! 'amount', localized_amount(money, currency)
xml.tag! 'currency', currency
xml.tag! 'purchaseOrderNo', options[:order_id].to_s.gsub(/[ ']/, '')
xml.tag! 'CreditCardInfo' do
xml.tag! 'cardNumber', credit_card.number
xml.tag! 'expiryDate', expdate(credit_card)
xml.tag! 'cvv', credit_card.verification_value if credit_card.verification_value?
end
xml.target!
end
def build_reference_request(money, reference)
xml = Builder::XmlMarkup.new
transaction_id, order_id, preauth_id, original_amount = reference.split('*')
xml.tag! 'amount', (money ? amount(money) : original_amount)
xml.tag! 'currency', options[:currency] || currency(money)
xml.tag! 'txnID', transaction_id
xml.tag! 'purchaseOrderNo', order_id
xml.tag! 'preauthID', preauth_id
xml.target!
end
def build_request(action, body)
xml = Builder::XmlMarkup.new
xml.instruct!
xml.tag! 'SecurePayMessage' do
xml.tag! 'MessageInfo' do
xml.tag! 'messageID', SecureRandom.hex(15)
xml.tag! 'messageTimestamp', generate_timestamp
xml.tag! 'timeoutValue', request_timeout
xml.tag! 'apiVersion', API_VERSION
end
xml.tag! 'MerchantInfo' do
xml.tag! 'merchantID', @options[:login]
xml.tag! 'password', @options[:password]
end
xml.tag! 'RequestType', 'Payment'
xml.tag! 'Payment' do
xml.tag! 'TxnList', 'count' => 1 do
xml.tag! 'Txn', 'ID' => 1 do
xml.tag! 'txnType', TRANSACTIONS[action]
xml.tag! 'txnSource', 23
xml << body
end
end
end
end
xml.target!
end
def commit(action, request)
response = parse(ssl_post(test? ? self.test_url : self.live_url, build_request(action, request)))
Response.new(
success?(response),
message_from(response),
response,
test: test?,
authorization: authorization_from(response)
)
end
def build_periodic_item(action, money, credit_card, options)
xml = Builder::XmlMarkup.new
xml.tag! 'actionType', PERIODIC_ACTIONS[action]
xml.tag! 'clientID', options[:billing_id].to_s
if credit_card
xml.tag! 'CreditCardInfo' do
xml.tag! 'cardNumber', credit_card.number
xml.tag! 'expiryDate', expdate(credit_card)
xml.tag! 'cvv', credit_card.verification_value if credit_card.verification_value?
end
end
xml.tag! 'amount', amount(money)
xml.tag! 'periodicType', PERIODIC_TYPES[action] if PERIODIC_TYPES[action]
xml.target!
end
def build_periodic_request(body)
xml = Builder::XmlMarkup.new
xml.instruct!
xml.tag! 'SecurePayMessage' do
xml.tag! 'MessageInfo' do
xml.tag! 'messageID', SecureRandom.hex(15)
xml.tag! 'messageTimestamp', generate_timestamp
xml.tag! 'timeoutValue', request_timeout
xml.tag! 'apiVersion', PERIODIC_API_VERSION
end
xml.tag! 'MerchantInfo' do
xml.tag! 'merchantID', @options[:login]
xml.tag! 'password', @options[:password]
end
xml.tag! 'RequestType', 'Periodic'
xml.tag! 'Periodic' do
xml.tag! 'PeriodicList', 'count' => 1 do
xml.tag! 'PeriodicItem', 'ID' => 1 do
xml << body
end
end
end
end
xml.target!
end
def commit_periodic(request)
my_request = build_periodic_request(request)
response = parse(ssl_post(test? ? self.test_periodic_url : self.live_periodic_url, my_request))
Response.new(
success?(response),
message_from(response),
response,
test: test?,
authorization: authorization_from(response)
)
end
def success?(response)
SUCCESS_CODES.include?(response[:response_code])
end
def authorization_from(response)
[response[:txn_id], response[:purchase_order_no], response[:preauth_id], response[:amount]].join('*')
end
def message_from(response)
response[:response_text] || response[:status_description]
end
def expdate(credit_card)
"#{format(credit_card.month, :two_digits)}/#{format(credit_card.year, :two_digits)}"
end
def parse(body)
xml = REXML::Document.new(body)
response = {}
xml.root.elements.to_a.each do |node|
parse_element(response, node)
end
response
end
def parse_element(response, node)
if node.has_elements?
node.elements.each { |element| parse_element(response, element) }
else
response[node.name.underscore.to_sym] = node.text
end
end
# YYYYDDMMHHNNSSKKK000sOOO
def generate_timestamp
time = Time.now.utc
time.strftime("%Y%d%m%H%M%S#{time.usec}+000")
end
end
end
end