projectdx_activemerchant 1.7.1.20100817.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. data/CHANGELOG +530 -0
  2. data/CONTRIBUTORS +142 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +136 -0
  5. data/gem-public_cert.pem +20 -0
  6. data/lib/active_merchant/billing/avs_result.rb +98 -0
  7. data/lib/active_merchant/billing/base.rb +57 -0
  8. data/lib/active_merchant/billing/check.rb +68 -0
  9. data/lib/active_merchant/billing/credit_card.rb +159 -0
  10. data/lib/active_merchant/billing/credit_card_formatting.rb +21 -0
  11. data/lib/active_merchant/billing/credit_card_methods.rb +125 -0
  12. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  13. data/lib/active_merchant/billing/expiry_date.rb +34 -0
  14. data/lib/active_merchant/billing/gateway.rb +163 -0
  15. data/lib/active_merchant/billing/gateways/authorize_net.rb +654 -0
  16. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +736 -0
  17. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +244 -0
  18. data/lib/active_merchant/billing/gateways/beanstream.rb +102 -0
  19. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  20. data/lib/active_merchant/billing/gateways/bogus.rb +102 -0
  21. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  22. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  23. data/lib/active_merchant/billing/gateways/braintree_blue.rb +210 -0
  24. data/lib/active_merchant/billing/gateways/braintree_orange.rb +17 -0
  25. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  26. data/lib/active_merchant/billing/gateways/convenience_pay.rb +191 -0
  27. data/lib/active_merchant/billing/gateways/cyber_source.rb +406 -0
  28. data/lib/active_merchant/billing/gateways/data_cash.rb +593 -0
  29. data/lib/active_merchant/billing/gateways/efsnet.rb +229 -0
  30. data/lib/active_merchant/billing/gateways/elavon.rb +134 -0
  31. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  32. data/lib/active_merchant/billing/gateways/exact.rb +222 -0
  33. data/lib/active_merchant/billing/gateways/first_pay.rb +172 -0
  34. data/lib/active_merchant/billing/gateways/garanti.rb +222 -0
  35. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  36. data/lib/active_merchant/billing/gateways/jetpay.rb +270 -0
  37. data/lib/active_merchant/billing/gateways/linkpoint.rb +449 -0
  38. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +154 -0
  39. data/lib/active_merchant/billing/gateways/merchant_ware.rb +283 -0
  40. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  41. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  42. data/lib/active_merchant/billing/gateways/moneris.rb +205 -0
  43. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  44. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  45. data/lib/active_merchant/billing/gateways/ogone.rb +279 -0
  46. data/lib/active_merchant/billing/gateways/pay_junction.rb +392 -0
  47. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  48. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +207 -0
  49. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  50. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  51. data/lib/active_merchant/billing/gateways/payflow.rb +236 -0
  52. data/lib/active_merchant/billing/gateways/payflow_express.rb +138 -0
  53. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  54. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  55. data/lib/active_merchant/billing/gateways/payment_express.rb +230 -0
  56. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +326 -0
  57. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +38 -0
  58. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  59. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  60. data/lib/active_merchant/billing/gateways/paypal_express.rb +135 -0
  61. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +20 -0
  62. data/lib/active_merchant/billing/gateways/plugnpay.rb +292 -0
  63. data/lib/active_merchant/billing/gateways/psigate.rb +214 -0
  64. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  65. data/lib/active_merchant/billing/gateways/quickpay.rb +213 -0
  66. data/lib/active_merchant/billing/gateways/realex.rb +200 -0
  67. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  68. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  69. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  70. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  71. data/lib/active_merchant/billing/gateways/sage_pay.rb +309 -0
  72. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  73. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  74. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +157 -0
  75. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  76. data/lib/active_merchant/billing/gateways/skip_jack.rb +453 -0
  77. data/lib/active_merchant/billing/gateways/smart_ps.rb +265 -0
  78. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  79. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  80. data/lib/active_merchant/billing/gateways/trust_commerce.rb +418 -0
  81. data/lib/active_merchant/billing/gateways/usa_epay.rb +194 -0
  82. data/lib/active_merchant/billing/gateways/verifi.rb +228 -0
  83. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  84. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  85. data/lib/active_merchant/billing/gateways.rb +18 -0
  86. data/lib/active_merchant/billing/integrations/action_view_helper.rb +79 -0
  87. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  88. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  89. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  90. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  91. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  92. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  93. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  94. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  95. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  96. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  97. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  98. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  99. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  100. data/lib/active_merchant/billing/integrations/helper.rb +93 -0
  101. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  102. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  103. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  104. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  105. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  106. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  107. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  108. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  109. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  110. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  111. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  112. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  113. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  114. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  115. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  116. data/lib/active_merchant/billing/integrations/quickpay.rb +17 -0
  117. data/lib/active_merchant/billing/integrations/return.rb +35 -0
  118. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  119. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  120. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  121. data/lib/active_merchant/billing/integrations/two_checkout.rb +23 -0
  122. data/lib/active_merchant/billing/integrations.rb +29 -0
  123. data/lib/active_merchant/billing/response.rb +32 -0
  124. data/lib/active_merchant/billing.rb +9 -0
  125. data/lib/active_merchant/common/connection.rb +172 -0
  126. data/lib/active_merchant/common/country.rb +319 -0
  127. data/lib/active_merchant/common/error.rb +26 -0
  128. data/lib/active_merchant/common/post_data.rb +24 -0
  129. data/lib/active_merchant/common/posts_data.rb +47 -0
  130. data/lib/active_merchant/common/requires_parameters.rb +16 -0
  131. data/lib/active_merchant/common/utils.rb +18 -0
  132. data/lib/active_merchant/common/validateable.rb +76 -0
  133. data/lib/active_merchant/common.rb +14 -0
  134. data/lib/active_merchant/version.rb +3 -0
  135. data/lib/active_merchant.rb +47 -0
  136. data/lib/activemerchant.rb +1 -0
  137. data/lib/certs/cacert.pem +7815 -0
  138. data/lib/support/gateway_support.rb +58 -0
  139. data/lib/support/outbound_hosts.rb +25 -0
  140. metadata +254 -0
@@ -0,0 +1,406 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ # See the remote and mocked unit test files for example usage. Pay special attention to the contents of the options hash.
4
+ #
5
+ # Initial setup instructions can be found in http://cybersource.com/support_center/implementation/downloads/soap_api/SOAP_toolkits.pdf
6
+ #
7
+ # Debugging
8
+ # If you experience an issue with this gateway be sure to examine the transaction information from a general transaction search inside the CyberSource Business
9
+ # Center for the full error messages including field names.
10
+ #
11
+ # Important Notes
12
+ # * AVS and CVV only work against the production server. You will always get back X for AVS and no response for CVV against the test server.
13
+ # * Nexus is the list of states or provinces where you have a physical presence. Nexus is used to calculate tax. Leave blank to tax everyone.
14
+ # * If you want to calculate VAT for overseas customers you must supply a registration number in the options hash as vat_reg_number.
15
+ # * productCode is a value in the line_items hash that is used to tell CyberSource what kind of item you are selling. It is used when calculating tax/VAT.
16
+ # * All transactions use dollar values.
17
+ class CyberSourceGateway < Gateway
18
+ TEST_URL = 'https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor'
19
+ LIVE_URL = 'https://ics2ws.ic3.com/commerce/1.x/transactionProcessor'
20
+
21
+ # visa, master, american_express, discover
22
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover]
23
+ self.supported_countries = ['US']
24
+ self.default_currency = 'USD'
25
+ self.homepage_url = 'http://www.cybersource.com'
26
+ self.display_name = 'CyberSource'
27
+
28
+ # map credit card to the CyberSource expected representation
29
+ @@credit_card_codes = {
30
+ :visa => '001',
31
+ :master => '002',
32
+ :american_express => '003',
33
+ :discover => '004'
34
+ }
35
+
36
+ # map response codes to something humans can read
37
+ @@response_codes = {
38
+ :r100 => "Successful transaction",
39
+ :r101 => "Request is missing one or more required fields" ,
40
+ :r102 => "One or more fields contains invalid data",
41
+ :r150 => "General failure",
42
+ :r151 => "The request was received but a server time-out occurred",
43
+ :r152 => "The request was received, but a service timed out",
44
+ :r200 => "The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the AVS check",
45
+ :r201 => "The issuing bank has questions about the request",
46
+ :r202 => "Expired card",
47
+ :r203 => "General decline of the card",
48
+ :r204 => "Insufficient funds in the account",
49
+ :r205 => "Stolen or lost card",
50
+ :r207 => "Issuing bank unavailable",
51
+ :r208 => "Inactive card or card not authorized for card-not-present transactions",
52
+ :r209 => "American Express Card Identifiction Digits (CID) did not match",
53
+ :r210 => "The card has reached the credit limit",
54
+ :r211 => "Invalid card verification number",
55
+ :r221 => "The customer matched an entry on the processor's negative file",
56
+ :r230 => "The authorization request was approved by the issuing bank but declined by CyberSource because it did not pass the card verification check",
57
+ :r231 => "Invalid account number",
58
+ :r232 => "The card type is not accepted by the payment processor",
59
+ :r233 => "General decline by the processor",
60
+ :r234 => "A problem exists with your CyberSource merchant configuration",
61
+ :r235 => "The requested amount exceeds the originally authorized amount",
62
+ :r236 => "Processor failure",
63
+ :r237 => "The authorization has already been reversed",
64
+ :r238 => "The authorization has already been captured",
65
+ :r239 => "The requested transaction amount must match the previous transaction amount",
66
+ :r240 => "The card type sent is invalid or does not correlate with the credit card number",
67
+ :r241 => "The request ID is invalid",
68
+ :r242 => "You requested a capture, but there is no corresponding, unused authorization record.",
69
+ :r243 => "The transaction has already been settled or reversed",
70
+ :r244 => "The bank account number failed the validation check",
71
+ :r246 => "The capture or credit is not voidable because the capture or credit information has already been submitted to your processor",
72
+ :r247 => "You requested a credit for a capture that was previously voided",
73
+ :r250 => "The request was received, but a time-out occurred with the payment processor",
74
+ :r254 => "Your CyberSource account is prohibited from processing stand-alone refunds",
75
+ :r255 => "Your CyberSource account is not configured to process the service in the country you specified"
76
+ }
77
+
78
+ # These are the options that can be used when creating a new CyberSource Gateway object.
79
+ #
80
+ # :login => your username
81
+ #
82
+ # :password => the transaction key you generated in the Business Center
83
+ #
84
+ # :test => true sets the gateway to test mode
85
+ #
86
+ # :vat_reg_number => your VAT registration number
87
+ #
88
+ # :nexus => "WI CA QC" sets the states/provinces where you have a physical presense for tax purposes
89
+ #
90
+ # :ignore_avs => true don't want to use AVS so continue processing even if AVS would have failed
91
+ #
92
+ # :ignore_cvv => true don't want to use CVV so continue processing even if CVV would have failed
93
+ def initialize(options = {})
94
+ requires!(options, :login, :password)
95
+ @options = options
96
+ super
97
+ end
98
+
99
+ # Should run against the test servers or not?
100
+ def test?
101
+ @options[:test] || Base.gateway_mode == :test
102
+ end
103
+
104
+ # Request an authorization for an amount from CyberSource
105
+ #
106
+ # You must supply an :order_id in the options hash
107
+ def authorize(money, creditcard, options = {})
108
+ requires!(options, :order_id, :email)
109
+ setup_address_hash(options)
110
+ commit(build_auth_request(money, creditcard, options), options )
111
+ end
112
+
113
+ # Capture an authorization that has previously been requested
114
+ def capture(money, authorization, options = {})
115
+ setup_address_hash(options)
116
+ commit(build_capture_request(money, authorization, options), options)
117
+ end
118
+
119
+ # Purchase is an auth followed by a capture
120
+ # You must supply an order_id in the options hash
121
+ def purchase(money, creditcard, options = {})
122
+ requires!(options, :order_id, :email)
123
+ setup_address_hash(options)
124
+ commit(build_purchase_request(money, creditcard, options), options)
125
+ end
126
+
127
+ def void(identification, options = {})
128
+ commit(build_void_request(identification, options), options)
129
+ end
130
+
131
+ def credit(money, identification, options = {})
132
+ commit(build_credit_request(money, identification, options), options)
133
+ end
134
+
135
+
136
+ # CyberSource requires that you provide line item information for tax calculations
137
+ # If you do not have prices for each item or want to simplify the situation then pass in one fake line item that costs the subtotal of the order
138
+ #
139
+ # The line_item hash goes in the options hash and should look like
140
+ #
141
+ # :line_items => [
142
+ # {
143
+ # :declared_value => '1',
144
+ # :quantity => '2',
145
+ # :code => 'default',
146
+ # :description => 'Giant Walrus',
147
+ # :sku => 'WA323232323232323'
148
+ # },
149
+ # {
150
+ # :declared_value => '6',
151
+ # :quantity => '1',
152
+ # :code => 'default',
153
+ # :description => 'Marble Snowcone',
154
+ # :sku => 'FAKE1232132113123'
155
+ # }
156
+ # ]
157
+ #
158
+ # This functionality is only supported by this particular gateway may
159
+ # be changed at any time
160
+ def calculate_tax(creditcard, options)
161
+ requires!(options, :line_items)
162
+ setup_address_hash(options)
163
+ commit(build_tax_calculation_request(creditcard, options), options)
164
+ end
165
+
166
+ private
167
+ # Create all address hash key value pairs so that we still function if we were only provided with one or two of them
168
+ def setup_address_hash(options)
169
+ options[:billing_address] = options[:billing_address] || options[:address] || {}
170
+ options[:shipping_address] = options[:shipping_address] || {}
171
+ end
172
+
173
+ def build_auth_request(money, creditcard, options)
174
+ xml = Builder::XmlMarkup.new :indent => 2
175
+ add_address(xml, creditcard, options[:billing_address], options)
176
+ add_purchase_data(xml, money, true, options)
177
+ add_creditcard(xml, creditcard)
178
+ add_auth_service(xml)
179
+ add_business_rules_data(xml)
180
+ xml.target!
181
+ end
182
+
183
+ def build_tax_calculation_request(creditcard, options)
184
+ xml = Builder::XmlMarkup.new :indent => 2
185
+ add_address(xml, creditcard, options[:billing_address], options, false)
186
+ add_address(xml, creditcard, options[:shipping_address], options, true)
187
+ add_line_item_data(xml, options)
188
+ add_purchase_data(xml, 0, false, options)
189
+ add_tax_service(xml)
190
+ add_business_rules_data(xml)
191
+ xml.target!
192
+ end
193
+
194
+ def build_capture_request(money, authorization, options)
195
+ order_id, request_id, request_token = authorization.split(";")
196
+ options[:order_id] = order_id
197
+
198
+ xml = Builder::XmlMarkup.new :indent => 2
199
+ add_purchase_data(xml, money, true, options)
200
+ add_capture_service(xml, request_id, request_token)
201
+ add_business_rules_data(xml)
202
+ xml.target!
203
+ end
204
+
205
+ def build_purchase_request(money, creditcard, options)
206
+ xml = Builder::XmlMarkup.new :indent => 2
207
+ add_address(xml, creditcard, options[:billing_address], options)
208
+ add_purchase_data(xml, money, true, options)
209
+ add_creditcard(xml, creditcard)
210
+ add_purchase_service(xml, options)
211
+ add_business_rules_data(xml)
212
+ xml.target!
213
+ end
214
+
215
+ def build_void_request(identification, options)
216
+ order_id, request_id, request_token = identification.split(";")
217
+ options[:order_id] = order_id
218
+
219
+ xml = Builder::XmlMarkup.new :indent => 2
220
+ add_void_service(xml, request_id, request_token)
221
+ xml.target!
222
+ end
223
+
224
+ def build_credit_request(money, identification, options)
225
+ order_id, request_id, request_token = identification.split(";")
226
+ options[:order_id] = order_id
227
+
228
+ xml = Builder::XmlMarkup.new :indent => 2
229
+ add_purchase_data(xml, money, true, options)
230
+ add_credit_service(xml, request_id, request_token)
231
+
232
+ xml.target!
233
+ end
234
+
235
+ def add_business_rules_data(xml)
236
+ xml.tag! 'businessRules' do
237
+ xml.tag!('ignoreAVSResult', 'true') if @options[:ignore_avs]
238
+ xml.tag!('ignoreCVResult', 'true') if @options[:ignore_cvv]
239
+ end
240
+ end
241
+
242
+ def add_line_item_data(xml, options)
243
+ options[:line_items].each_with_index do |value, index|
244
+ xml.tag! 'item', {'id' => index} do
245
+ xml.tag! 'unitPrice', amount(value[:declared_value])
246
+ xml.tag! 'quantity', value[:quantity]
247
+ xml.tag! 'productCode', value[:code] || 'shipping_only'
248
+ xml.tag! 'productName', value[:description]
249
+ xml.tag! 'productSKU', value[:sku]
250
+ end
251
+ end
252
+ end
253
+
254
+ def add_merchant_data(xml, options)
255
+ xml.tag! 'merchantID', @options[:login]
256
+ xml.tag! 'merchantReferenceCode', options[:order_id]
257
+ xml.tag! 'clientLibrary' ,'Ruby Active Merchant'
258
+ xml.tag! 'clientLibraryVersion', '1.0'
259
+ xml.tag! 'clientEnvironment' , 'Linux'
260
+ end
261
+
262
+ def add_purchase_data(xml, money = 0, include_grand_total = false, options={})
263
+ xml.tag! 'purchaseTotals' do
264
+ xml.tag! 'currency', options[:currency] || currency(money)
265
+ xml.tag!('grandTotalAmount', amount(money)) if include_grand_total
266
+ end
267
+ end
268
+
269
+ def add_address(xml, creditcard, address, options, shipTo = false)
270
+ xml.tag! shipTo ? 'shipTo' : 'billTo' do
271
+ xml.tag! 'firstName', creditcard.first_name
272
+ xml.tag! 'lastName', creditcard.last_name
273
+ xml.tag! 'street1', address[:address1]
274
+ xml.tag! 'street2', address[:address2]
275
+ xml.tag! 'city', address[:city]
276
+ xml.tag! 'state', address[:state]
277
+ xml.tag! 'postalCode', address[:zip]
278
+ xml.tag! 'country', address[:country]
279
+ xml.tag! 'email', options[:email]
280
+ end
281
+ end
282
+
283
+ def add_creditcard(xml, creditcard)
284
+ xml.tag! 'card' do
285
+ xml.tag! 'accountNumber', creditcard.number
286
+ xml.tag! 'expirationMonth', format(creditcard.month, :two_digits)
287
+ xml.tag! 'expirationYear', format(creditcard.year, :four_digits)
288
+ xml.tag!('cvNumber', creditcard.verification_value) unless (@options[:ignore_cvv] || creditcard.verification_value.blank? )
289
+ xml.tag! 'cardType', @@credit_card_codes[card_brand(creditcard).to_sym]
290
+ end
291
+ end
292
+
293
+ def add_tax_service(xml)
294
+ xml.tag! 'taxService', {'run' => 'true'} do
295
+ xml.tag!('nexus', @options[:nexus]) unless @options[:nexus].blank?
296
+ xml.tag!('sellerRegistration', @options[:vat_reg_number]) unless @options[:vat_reg_number].blank?
297
+ end
298
+ end
299
+
300
+ def add_auth_service(xml)
301
+ xml.tag! 'ccAuthService', {'run' => 'true'}
302
+ end
303
+
304
+ def add_capture_service(xml, request_id, request_token)
305
+ xml.tag! 'ccCaptureService', {'run' => 'true'} do
306
+ xml.tag! 'authRequestID', request_id
307
+ xml.tag! 'authRequestToken', request_token
308
+ end
309
+ end
310
+
311
+ def add_purchase_service(xml, options)
312
+ xml.tag! 'ccAuthService', {'run' => 'true'}
313
+ xml.tag! 'ccCaptureService', {'run' => 'true'}
314
+ end
315
+
316
+ def add_void_service(xml, request_id, request_token)
317
+ xml.tag! 'voidService', {'run' => 'true'} do
318
+ xml.tag! 'voidRequestID', request_id
319
+ xml.tag! 'voidRequestToken', request_token
320
+ end
321
+ end
322
+
323
+ def add_credit_service(xml, request_id, request_token)
324
+ xml.tag! 'ccCreditService', {'run' => 'true'} do
325
+ xml.tag! 'captureRequestID', request_id
326
+ xml.tag! 'captureRequestToken', request_token
327
+ end
328
+ end
329
+
330
+
331
+ # Where we actually build the full SOAP request using builder
332
+ def build_request(body, options)
333
+ xml = Builder::XmlMarkup.new :indent => 2
334
+ xml.instruct!
335
+ xml.tag! 's:Envelope', {'xmlns:s' => 'http://schemas.xmlsoap.org/soap/envelope/'} do
336
+ xml.tag! 's:Header' do
337
+ xml.tag! 'wsse:Security', {'s:mustUnderstand' => '1', 'xmlns:wsse' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'} do
338
+ xml.tag! 'wsse:UsernameToken' do
339
+ xml.tag! 'wsse:Username', @options[:login]
340
+ xml.tag! 'wsse:Password', @options[:password], 'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'
341
+ end
342
+ end
343
+ end
344
+ xml.tag! 's:Body', {'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema'} do
345
+ xml.tag! 'requestMessage', {'xmlns' => 'urn:schemas-cybersource-com:transaction-data-1.32'} do
346
+ add_merchant_data(xml, options)
347
+ xml << body
348
+ end
349
+ end
350
+ end
351
+ xml.target!
352
+ end
353
+
354
+ # Contact CyberSource, make the SOAP request, and parse the reply into a Response object
355
+ def commit(request, options)
356
+ response = parse(ssl_post(test? ? TEST_URL : LIVE_URL, build_request(request, options)))
357
+
358
+ success = response[:decision] == "ACCEPT"
359
+ message = @@response_codes[('r' + response[:reasonCode]).to_sym] rescue response[:message]
360
+ authorization = success ? [ options[:order_id], response[:requestID], response[:requestToken] ].compact.join(";") : nil
361
+
362
+ Response.new(success, message, response,
363
+ :test => test?,
364
+ :authorization => authorization,
365
+ :avs_result => { :code => response[:avsCode] },
366
+ :cvv_result => response[:cvCode]
367
+ )
368
+ end
369
+
370
+ # Parse the SOAP response
371
+ # Technique inspired by the Paypal Gateway
372
+ def parse(xml)
373
+ reply = {}
374
+ xml = REXML::Document.new(xml)
375
+ if root = REXML::XPath.first(xml, "//c:replyMessage")
376
+ root.elements.to_a.each do |node|
377
+ case node.name
378
+ when 'c:reasonCode'
379
+ reply[:message] = reply(node.text)
380
+ else
381
+ parse_element(reply, node)
382
+ end
383
+ end
384
+ elsif root = REXML::XPath.first(xml, "//soap:Fault")
385
+ parse_element(reply, root)
386
+ reply[:message] = "#{reply[:faultcode]}: #{reply[:faultstring]}"
387
+ end
388
+ return reply
389
+ end
390
+
391
+ def parse_element(reply, node)
392
+ if node.has_elements?
393
+ node.elements.each{|e| parse_element(reply, e) }
394
+ else
395
+ if node.parent.name =~ /item/
396
+ parent = node.parent.name + (node.parent.attributes["id"] ? "_" + node.parent.attributes["id"] : '')
397
+ reply[(parent + '_' + node.name).to_sym] = node.text
398
+ else
399
+ reply[node.name.to_sym] = node.text
400
+ end
401
+ end
402
+ return reply
403
+ end
404
+ end
405
+ end
406
+ end