johnreitano-activemerchant 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. data/CHANGELOG +508 -0
  2. data/CONTRIBUTORS +134 -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 +98 -0
  21. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  22. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  23. data/lib/active_merchant/billing/gateways/cyber_source.rb +594 -0
  24. data/lib/active_merchant/billing/gateways/data_cash.rb +593 -0
  25. data/lib/active_merchant/billing/gateways/efsnet.rb +229 -0
  26. data/lib/active_merchant/billing/gateways/elavon.rb +134 -0
  27. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  28. data/lib/active_merchant/billing/gateways/exact.rb +222 -0
  29. data/lib/active_merchant/billing/gateways/first_pay.rb +172 -0
  30. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  31. data/lib/active_merchant/billing/gateways/jetpay.rb +270 -0
  32. data/lib/active_merchant/billing/gateways/linkpoint.rb +449 -0
  33. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +154 -0
  34. data/lib/active_merchant/billing/gateways/merchant_ware.rb +283 -0
  35. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  36. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  37. data/lib/active_merchant/billing/gateways/moneris.rb +205 -0
  38. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  39. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  40. data/lib/active_merchant/billing/gateways/ogone.rb +279 -0
  41. data/lib/active_merchant/billing/gateways/pay_junction.rb +392 -0
  42. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  43. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +207 -0
  44. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  45. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  46. data/lib/active_merchant/billing/gateways/payflow.rb +236 -0
  47. data/lib/active_merchant/billing/gateways/payflow_express.rb +138 -0
  48. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  49. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  50. data/lib/active_merchant/billing/gateways/payment_express.rb +230 -0
  51. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +326 -0
  52. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +38 -0
  53. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  54. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  55. data/lib/active_merchant/billing/gateways/paypal_express.rb +130 -0
  56. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +20 -0
  57. data/lib/active_merchant/billing/gateways/plugnpay.rb +292 -0
  58. data/lib/active_merchant/billing/gateways/psigate.rb +214 -0
  59. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  60. data/lib/active_merchant/billing/gateways/quickpay.rb +213 -0
  61. data/lib/active_merchant/billing/gateways/realex.rb +200 -0
  62. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  63. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  64. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  65. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  66. data/lib/active_merchant/billing/gateways/sage_pay.rb +309 -0
  67. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  68. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  69. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +157 -0
  70. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  71. data/lib/active_merchant/billing/gateways/skip_jack.rb +453 -0
  72. data/lib/active_merchant/billing/gateways/smart_ps.rb +265 -0
  73. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  74. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  75. data/lib/active_merchant/billing/gateways/trust_commerce.rb +418 -0
  76. data/lib/active_merchant/billing/gateways/usa_epay.rb +194 -0
  77. data/lib/active_merchant/billing/gateways/verifi.rb +228 -0
  78. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  79. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  80. data/lib/active_merchant/billing/gateways.rb +18 -0
  81. data/lib/active_merchant/billing/integrations/action_view_helper.rb +79 -0
  82. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  83. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  84. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  85. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  86. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  87. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  88. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  89. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  90. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  91. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  92. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  93. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  94. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  95. data/lib/active_merchant/billing/integrations/helper.rb +93 -0
  96. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  97. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  98. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  99. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  100. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  101. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  102. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  103. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  104. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  105. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  106. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  107. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  108. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  109. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  110. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  111. data/lib/active_merchant/billing/integrations/quickpay.rb +17 -0
  112. data/lib/active_merchant/billing/integrations/return.rb +35 -0
  113. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  114. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  115. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  116. data/lib/active_merchant/billing/integrations/two_checkout.rb +23 -0
  117. data/lib/active_merchant/billing/integrations.rb +29 -0
  118. data/lib/active_merchant/billing/response.rb +32 -0
  119. data/lib/active_merchant/billing.rb +9 -0
  120. data/lib/active_merchant/lib/connection.rb +170 -0
  121. data/lib/active_merchant/lib/country.rb +319 -0
  122. data/lib/active_merchant/lib/error.rb +4 -0
  123. data/lib/active_merchant/lib/post_data.rb +22 -0
  124. data/lib/active_merchant/lib/posts_data.rb +47 -0
  125. data/lib/active_merchant/lib/requires_parameters.rb +16 -0
  126. data/lib/active_merchant/lib/utils.rb +18 -0
  127. data/lib/active_merchant/lib/validateable.rb +76 -0
  128. data/lib/active_merchant.rb +46 -0
  129. data/lib/certs/cacert.pem +7815 -0
  130. data/lib/support/gateway_support.rb +58 -0
  131. metadata +218 -0
@@ -0,0 +1,594 @@
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, payment_source, options = {})
108
+ requires!(options, :order_id, :email)
109
+ setup_address_hash(options)
110
+ if payment_source.is_a?(String)
111
+ requires!(options, [:type, :credit_card, :check])
112
+ commit(build_subscription_auth_request(money, payment_source, options), options)
113
+ else
114
+ commit(build_auth_request(money, payment_source, options), options)
115
+ end
116
+ end
117
+
118
+ # Capture an authorization that has previously been requested
119
+ def capture(money, authorization, options = {})
120
+ setup_address_hash(options)
121
+ commit(build_capture_request(money, authorization, options), options)
122
+ end
123
+
124
+ # Purchase is an auth followed by a capture
125
+ # You must supply an order_id in the options hash
126
+ def purchase(money, payment_source, options = {})
127
+ requires!(options, :order_id, :email)
128
+ setup_address_hash(options)
129
+ if payment_source.is_a?(String)
130
+ requires!(options, [:type, :credit_card, :check])
131
+ commit(build_subscription_purchase_request(money, payment_source, options), options)
132
+ else
133
+ commit(build_purchase_request(money, payment_source, options), options)
134
+ end
135
+ end
136
+
137
+ def void(identification, options = {})
138
+ commit(build_void_request(identification, options), options)
139
+ end
140
+
141
+ def credit(money, identification, options = {})
142
+ commit(build_credit_request(money, identification, options), options)
143
+ end
144
+
145
+ def create_subscription(payment_source, options = {})
146
+ requires!(options, :subscription, :billing_address, :order_id, :email)
147
+ requires!(options[:subscription], [:frequency, "on-demand", "weekly", "bi-weekly", "semi-monthly", "quarterly", "quad-weekly", "semi-annually", "annually"])
148
+ requires!(options[:billing_address], :first_name, :last_name)
149
+ setup_address_hash(options)
150
+ commit(build_create_subscription_request(payment_source, options), options)
151
+ end
152
+
153
+ def update_subscription(identification, options = {})
154
+ requires!(options, :order_id)
155
+ setup_address_hash(options)
156
+ commit(build_update_subscription_request(identification, options), options)
157
+ end
158
+
159
+ # CyberSource requires that you provide line item information for tax calculations
160
+ # 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
161
+ #
162
+ # The line_item hash goes in the options hash and should look like
163
+ #
164
+ # :line_items => [
165
+ # {
166
+ # :declared_value => '1',
167
+ # :quantity => '2',
168
+ # :code => 'default',
169
+ # :description => 'Giant Walrus',
170
+ # :sku => 'WA323232323232323'
171
+ # },
172
+ # {
173
+ # :declared_value => '6',
174
+ # :quantity => '1',
175
+ # :code => 'default',
176
+ # :description => 'Marble Snowcone',
177
+ # :sku => 'FAKE1232132113123'
178
+ # }
179
+ # ]
180
+ #
181
+ # This functionality is only supported by this particular gateway may
182
+ # be changed at any time
183
+ def calculate_tax(creditcard, options)
184
+ requires!(options, :line_items)
185
+ setup_address_hash(options)
186
+ commit(build_tax_calculation_request(creditcard, options), options)
187
+ end
188
+
189
+ private
190
+ # Create all address hash key value pairs so that we still function if we were only provided with one or two of them
191
+ def setup_address_hash(options)
192
+ options[:billing_address] = options[:billing_address] || options[:address] || {}
193
+ options[:shipping_address] = options[:shipping_address] || {}
194
+ end
195
+
196
+ def build_auth_request(money, payment_source, options)
197
+ xml = Builder::XmlMarkup.new :indent => 2
198
+ add_address(xml, options[:billing_address], options)
199
+ add_purchase_data(xml, money, true, options)
200
+ add_payment_source(xml, payment_source)
201
+ add_auth_service(xml)
202
+ add_business_rules_data(xml)
203
+ xml.target!
204
+ end
205
+
206
+ def build_tax_calculation_request(creditcard, options)
207
+ xml = Builder::XmlMarkup.new :indent => 2
208
+ add_address(xml, options[:billing_address], options, false)
209
+ add_address(xml, options[:shipping_address], options, true)
210
+ add_line_item_data(xml, options)
211
+ add_purchase_data(xml, 0, false, options)
212
+ add_tax_service(xml)
213
+ add_business_rules_data(xml)
214
+ xml.target!
215
+ end
216
+
217
+ def build_capture_request(money, authorization, options)
218
+ order_id, request_id, request_token = authorization.split(";")
219
+ options[:order_id] = order_id
220
+ xml = Builder::XmlMarkup.new :indent => 2
221
+ add_purchase_data(xml, money, true, options)
222
+ add_capture_service(xml, request_id, request_token)
223
+ add_business_rules_data(xml)
224
+ xml.target!
225
+ end
226
+
227
+ def build_purchase_request(money, payment_source, options)
228
+ xml = Builder::XmlMarkup.new :indent => 2
229
+ add_address(xml, options[:billing_address], options)
230
+ add_purchase_data(xml, money, true, options)
231
+ add_payment_source(xml, payment_source)
232
+ add_purchase_service(xml, payment_source, options)
233
+ add_business_rules_data(xml)
234
+ xml.target!
235
+ end
236
+
237
+ def build_void_request(identification, options)
238
+ order_id, request_id, request_token = identification.split(";")
239
+ options[:order_id] = order_id
240
+
241
+ xml = Builder::XmlMarkup.new :indent => 2
242
+ add_void_service(xml, request_id, request_token)
243
+ xml.target!
244
+ end
245
+
246
+ def build_credit_request(money, identification, options)
247
+ order_id, request_id, request_token = identification.split(";")
248
+ options[:order_id] = order_id
249
+
250
+ xml = Builder::XmlMarkup.new :indent => 2
251
+ add_purchase_data(xml, money, true, options)
252
+ add_credit_service(xml, request_id, request_token)
253
+
254
+ xml.target!
255
+ end
256
+
257
+ def build_create_subscription_request(payment_source, options)
258
+ xml = Builder::XmlMarkup.new :indent => 2
259
+ add_address(xml, options[:billing_address], options)
260
+ add_purchase_data(xml, options[:setup_fee], true, options)
261
+
262
+
263
+ case determine_funding_source(payment_source)
264
+ when :credit_card then add_creditcard(xml, payment_source)
265
+ when :check then add_check(xml, payment_source)
266
+ else raise ArgumentError, "Unsupported funding source provided"
267
+ end
268
+
269
+ add_subscription(xml, options, payment_source)
270
+ add_subscription_create_service(xml, options)
271
+ add_business_rules_data(xml)
272
+ xml.target!
273
+ end
274
+
275
+ def build_update_subscription_request(identification, options)
276
+ reference_code, subscription_id, request_token = identification.split(";")
277
+ options[:subscription] ||= {}
278
+ options[:subscription][:subscription_id] = subscription_id
279
+
280
+ xml = Builder::XmlMarkup.new :indent => 2
281
+ add_address(xml, options[:billing_address], options) unless options[:billing_address].blank?
282
+ add_purchase_data(xml, options[:setup_fee], true, options) unless options[:setup_fee].blank?
283
+ add_creditcard(xml, options[:credit_card]) if options[:credit_card]
284
+ add_subscription(xml, options)
285
+ add_subscription_update_service(xml, options)
286
+ add_business_rules_data(xml)
287
+ xml.target!
288
+ end
289
+
290
+ def build_subscription_purchase_request(money, identification, options)
291
+ reference_code, subscription_id, request_token = identification.split(";")
292
+ options[:subscription] ||= {}
293
+ options[:subscription][:subscription_id] = subscription_id
294
+
295
+ xml = Builder::XmlMarkup.new :indent => 2
296
+ add_purchase_data(xml, money, true, options)
297
+ add_subscription(xml, options)
298
+
299
+ case options[:type]
300
+ when :credit_card then add_cc_purchase_service(xml, options)
301
+ when :check then add_check_service(xml)
302
+ end
303
+
304
+ add_business_rules_data(xml)
305
+ xml.target!
306
+ end
307
+
308
+ def build_subscription_auth_request(money, identification, options)
309
+ reference_code, subscription_id, request_token = identification.split(";")
310
+ options[:subscription] ||= {}
311
+ options[:subscription][:subscription_id] = subscription_id
312
+
313
+ xml = Builder::XmlMarkup.new :indent => 2
314
+ add_purchase_data(xml, money, true, options)
315
+ add_subscription(xml, options)
316
+ add_auth_service(xml)
317
+ add_business_rules_data(xml)
318
+ xml.target!
319
+ end
320
+
321
+ def add_business_rules_data(xml)
322
+ xml.tag! 'businessRules' do
323
+ xml.tag!('ignoreAVSResult', 'true') if @options[:ignore_avs]
324
+ xml.tag!('ignoreCVResult', 'true') if @options[:ignore_cvv]
325
+ end
326
+ end
327
+
328
+ def add_line_item_data(xml, options)
329
+ options[:line_items].each_with_index do |value, index|
330
+ xml.tag! 'item', {'id' => index} do
331
+ xml.tag! 'unitPrice', amount(value[:declared_value])
332
+ xml.tag! 'quantity', value[:quantity]
333
+ xml.tag! 'productCode', value[:code] || 'shipping_only'
334
+ xml.tag! 'productName', value[:description]
335
+ xml.tag! 'productSKU', value[:sku]
336
+ end
337
+ end
338
+ end
339
+
340
+ def add_merchant_data(xml, options)
341
+ xml.tag! 'merchantID', @options[:login]
342
+ xml.tag! 'merchantReferenceCode', options[:order_id]
343
+ xml.tag! 'clientLibrary' ,'Ruby Active Merchant'
344
+ xml.tag! 'clientLibraryVersion', '1.0'
345
+ xml.tag! 'clientEnvironment' , 'Linux'
346
+ end
347
+
348
+ def add_payment_source(xml, source, options={})
349
+ case determine_funding_source(source)
350
+ #when :subscription then add_customer_vault_id(params, source)
351
+ when :credit_card then add_creditcard(xml, source)
352
+ when :check then add_check(xml, source)
353
+ end
354
+ end
355
+
356
+ def add_purchase_data(xml, money, include_grand_total = false, options={})
357
+ money ||= 0
358
+ xml.tag! 'purchaseTotals' do
359
+ xml.tag! 'currency', options[:currency] || currency(money)
360
+ xml.tag!('grandTotalAmount', amount(money)) if include_grand_total
361
+ end
362
+ end
363
+
364
+ def add_address(xml, address, options, shipTo = false)
365
+ xml.tag! shipTo ? 'shipTo' : 'billTo' do
366
+ xml.tag! 'firstName', address[:first_name]
367
+ xml.tag! 'lastName', address[:last_name]
368
+ xml.tag! 'street1', address[:address1]
369
+ xml.tag! 'street2', address[:address2]
370
+ xml.tag! 'city', address[:city]
371
+ xml.tag! 'state', address[:state]
372
+ xml.tag! 'postalCode', address[:zip]
373
+ xml.tag! 'country', address[:country]
374
+ xml.tag! 'company', address[:company] unless address[:company].blank?
375
+ xml.tag! 'companyTaxID', address[:companyTaxID] unless address[:company_tax_id].blank?
376
+ xml.tag! 'phoneNumber', address[:phone_number] unless address[:phone_number].blank?
377
+ xml.tag! 'email', options[:email] unless options[:email].blank?
378
+ xml.tag! 'driversLicenseNumber', options[:drivers_license_number] unless options[:drivers_license_number].blank?
379
+ xml.tag! 'driversLicenseState', options[:drivers_license_state] unless options[:drivers_license_state].blank?
380
+ end
381
+ end
382
+
383
+ def add_creditcard(xml, creditcard)
384
+ xml.tag! 'card' do
385
+ xml.tag! 'accountNumber', creditcard.number
386
+ xml.tag! 'expirationMonth', format(creditcard.month, :two_digits)
387
+ xml.tag! 'expirationYear', format(creditcard.year, :four_digits)
388
+ xml.tag!('cvNumber', creditcard.verification_value) unless (@options[:ignore_cvv] || creditcard.verification_value.blank? )
389
+ xml.tag! 'cardType', @@credit_card_codes[card_brand(creditcard).to_sym]
390
+ end
391
+ end
392
+
393
+ def add_check(xml, check)
394
+ #convert check object account type into cybs account type code
395
+ if check.account_type == "checking"
396
+ accountType = check.account_holder_type == "business" ? 'X' : 'C'
397
+ else
398
+ accountType = 'S'
399
+ end
400
+
401
+ xml.tag! 'check' do
402
+ xml.tag! 'accountNumber', check.account_number
403
+ xml.tag! 'accountType', accountType
404
+ xml.tag! 'bankTransitNumber', check.routing_number
405
+ xml.tag! 'checkNumber', check.number if check.number
406
+ end
407
+ end
408
+
409
+ def add_check_service(xml)
410
+ xml.tag! 'ecDebitService', {'run' => 'true'}
411
+ end
412
+
413
+ def add_tax_service(xml)
414
+ xml.tag! 'taxService', {'run' => 'true'} do
415
+ xml.tag!('nexus', @options[:nexus]) unless @options[:nexus].blank?
416
+ xml.tag!('sellerRegistration', @options[:vat_reg_number]) unless @options[:vat_reg_number].blank?
417
+ end
418
+ end
419
+
420
+ def add_auth_service(xml)
421
+ xml.tag! 'ccAuthService', {'run' => 'true'}
422
+ end
423
+
424
+ def add_capture_service(xml, request_id, request_token)
425
+ xml.tag! 'ccCaptureService', {'run' => 'true'} do
426
+ xml.tag! 'authRequestID', request_id
427
+ xml.tag! 'authRequestToken', request_token
428
+ end
429
+ end
430
+
431
+ def add_purchase_service(xml, source, options)
432
+ case determine_funding_source(source)
433
+ when :credit_card then add_cc_purchase_service(xml, options)
434
+ when :check then add_check_service(xml)
435
+ end
436
+ end
437
+
438
+ def add_cc_purchase_service(xml, options)
439
+ xml.tag! 'ccAuthService', {'run' => 'true'}
440
+ xml.tag! 'ccCaptureService', {'run' => 'true'}
441
+ end
442
+
443
+ def add_void_service(xml, request_id, request_token)
444
+ xml.tag! 'voidService', {'run' => 'true'} do
445
+ xml.tag! 'voidRequestID', request_id
446
+ xml.tag! 'voidRequestToken', request_token
447
+ end
448
+ end
449
+
450
+ def add_credit_service(xml, request_id, request_token)
451
+ xml.tag! 'ccCreditService', {'run' => 'true'} do
452
+ xml.tag! 'captureRequestID', request_id
453
+ xml.tag! 'captureRequestToken', request_token
454
+ end
455
+ end
456
+
457
+ def add_subscription_create_service(xml, options)
458
+ add_cc_purchase_service(xml, options) if options[:setup_fee]
459
+ xml.tag! 'paySubscriptionCreateService', {'run' => 'true'}
460
+ end
461
+
462
+ def add_subscription_update_service(xml, options)
463
+ add_cc_purchase_service(xml, options) if options[:setup_fee]
464
+ xml.tag! 'paySubscriptionUpdateService', {'run' => 'true'}
465
+ end
466
+
467
+ def add_subscription(xml, options, payment_source=nil)
468
+ if payment_source
469
+ xml.tag! 'subscription' do
470
+ xml.tag! 'paymentMethod', determine_funding_source(payment_source).to_s.gsub(/_/, " ")
471
+ end
472
+ end
473
+
474
+ xml.tag! 'recurringSubscriptionInfo' do
475
+ xml.tag! 'subscriptionID', options[:subscription][:subscription_id]
476
+ xml.tag! 'status', options[:subscription][:status] if options[:subscription][:status]
477
+ xml.tag! 'amount', options[:subscription][:amount] if options[:subscription][:amount]
478
+ xml.tag! 'numberOfPayments', options[:subscription][:occurrences] if options[:subscription][:occurrences]
479
+ xml.tag! 'automaticRenew', options[:subscription][:auto_renew] if options[:subscription][:auto_renew]
480
+ xml.tag! 'frequency', options[:subscription][:frequency] if options[:subscription][:frequency]
481
+ xml.tag! 'startDate', options[:subscription][:start_date].strftime("%Y%m%d") if options[:subscription][:start_date]
482
+ xml.tag! 'endDate', options[:subscription][:end_date].strftime("%Y%m%d") if options[:subscription][:end_date]
483
+ xml.tag! 'approvalRequired', options[:subscription][:approval_required] || false
484
+ xml.tag! 'event', options[:subscription][:event] if options[:subscription][:event]
485
+ xml.tag! 'billPayment', options[:subscription][:bill_payment] if options[:subscription][:bill_payment]
486
+ end
487
+ end
488
+
489
+ # Where we actually build the full SOAP request using builder
490
+ def build_request(body, options)
491
+ xml = Builder::XmlMarkup.new :indent => 2
492
+ xml.instruct!
493
+ xml.tag! 's:Envelope', {'xmlns:s' => 'http://schemas.xmlsoap.org/soap/envelope/'} do
494
+ xml.tag! 's:Header' do
495
+ 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
496
+ xml.tag! 'wsse:UsernameToken' do
497
+ xml.tag! 'wsse:Username', @options[:login]
498
+ xml.tag! 'wsse:Password', @options[:password], 'Type' => 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'
499
+ end
500
+ end
501
+ end
502
+ xml.tag! 's:Body', {'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema'} do
503
+ xml.tag! 'requestMessage', {'xmlns' => 'urn:schemas-cybersource-com:transaction-data-1.32'} do
504
+ add_merchant_data(xml, options)
505
+ xml << body
506
+ end
507
+ end
508
+ end
509
+ xml.target!
510
+ end
511
+
512
+ # Contact CyberSource, make the SOAP request, and parse the reply into a Response object
513
+ def commit(request, options)
514
+ response = parse(ssl_post(test? ? TEST_URL : LIVE_URL, build_request(request, options)))
515
+ success = response[:decision] == "ACCEPT"
516
+ message = @@response_codes[('r' + response[:reasonCode]).to_sym] rescue response[:message]
517
+ authorization = success ? [ options[:order_id], response[:requestID], response[:requestToken] ].compact.join(";") : nil
518
+
519
+ Response.new(success, message, response,
520
+ :test => test?,
521
+ :authorization => authorization,
522
+ :avs_result => { :code => response[:avsCode] },
523
+ :cvv_result => response[:cvCode]
524
+ )
525
+ end
526
+
527
+ def commit(request, options)
528
+ request = build_request(request, options)
529
+ puts "REQUEST: "
530
+ puts request
531
+ puts "RESPONSE: "
532
+ response = ssl_post(test? ? TEST_URL : LIVE_URL, request)
533
+ puts response
534
+ response = parse(response)
535
+
536
+ success = response[:decision] == "ACCEPT"
537
+ message = @@response_codes[('r' + response[:reasonCode]).to_sym] rescue response[:message]
538
+ authorization = success ? [ options[:order_id], response[:requestID], response[:requestToken] ].compact.join(";") : nil
539
+
540
+ Response.new(success, message, response,
541
+ :test => test?,
542
+ :authorization => authorization,
543
+ :avs_result => { :code => response[:avsCode] },
544
+ :cvv_result => response[:cvCode]
545
+ )
546
+ end
547
+
548
+ # Parse the SOAP response
549
+ # Technique inspired by the Paypal Gateway
550
+ def parse(xml)
551
+ reply = {}
552
+ xml = REXML::Document.new(xml)
553
+ if root = REXML::XPath.first(xml, "//c:replyMessage")
554
+ root.elements.to_a.each do |node|
555
+ case node.name
556
+ when 'c:reasonCode'
557
+ reply[:message] = reply(node.text)
558
+ else
559
+ parse_element(reply, node)
560
+ end
561
+ end
562
+ elsif root = REXML::XPath.first(xml, "//soap:Fault")
563
+ parse_element(reply, root)
564
+ reply[:message] = "#{reply[:faultcode]}: #{reply[:faultstring]}"
565
+ end
566
+ return reply
567
+ end
568
+
569
+ def parse_element(reply, node)
570
+ if node.has_elements?
571
+ node.elements.each{|e| parse_element(reply, e) }
572
+ else
573
+ if node.parent.name =~ /item/
574
+ parent = node.parent.name + (node.parent.attributes["id"] ? "_" + node.parent.attributes["id"] : '')
575
+ reply[(parent + '_' + node.name).to_sym] = node.text
576
+ else
577
+ reply[node.name.to_sym] = node.text
578
+ end
579
+ end
580
+ return reply
581
+ end
582
+
583
+ def determine_funding_source(source)
584
+ case
585
+ when source.is_a?(String) then :subscription
586
+ when CreditCard.card_companies.keys.include?(card_brand(source)) then :credit_card
587
+ when card_brand(source) == 'check' then :check
588
+ else raise ArgumentError, "Unsupported funding source provided"
589
+ end
590
+ end
591
+
592
+ end
593
+ end
594
+ end