n8_activemerchant 1.9.3

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 (158) hide show
  1. data/CHANGELOG +574 -0
  2. data/CONTRIBUTORS +175 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +151 -0
  5. data/gem-public_cert.pem +20 -0
  6. data/lib/active_merchant.rb +49 -0
  7. data/lib/active_merchant/billing.rb +9 -0
  8. data/lib/active_merchant/billing/avs_result.rb +98 -0
  9. data/lib/active_merchant/billing/base.rb +57 -0
  10. data/lib/active_merchant/billing/check.rb +68 -0
  11. data/lib/active_merchant/billing/credit_card.rb +161 -0
  12. data/lib/active_merchant/billing/credit_card_formatting.rb +21 -0
  13. data/lib/active_merchant/billing/credit_card_methods.rb +125 -0
  14. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  15. data/lib/active_merchant/billing/expiry_date.rb +34 -0
  16. data/lib/active_merchant/billing/gateway.rb +169 -0
  17. data/lib/active_merchant/billing/gateways.rb +18 -0
  18. data/lib/active_merchant/billing/gateways/authorize_net.rb +654 -0
  19. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +736 -0
  20. data/lib/active_merchant/billing/gateways/beanstream.rb +102 -0
  21. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +244 -0
  22. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  23. data/lib/active_merchant/billing/gateways/bogus.rb +102 -0
  24. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  25. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  26. data/lib/active_merchant/billing/gateways/braintree_blue.rb +210 -0
  27. data/lib/active_merchant/billing/gateways/braintree_orange.rb +17 -0
  28. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  29. data/lib/active_merchant/billing/gateways/cyber_source.rb +406 -0
  30. data/lib/active_merchant/billing/gateways/data_cash.rb +593 -0
  31. data/lib/active_merchant/billing/gateways/efsnet.rb +229 -0
  32. data/lib/active_merchant/billing/gateways/elavon.rb +134 -0
  33. data/lib/active_merchant/billing/gateways/epay.rb +263 -0
  34. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  35. data/lib/active_merchant/billing/gateways/exact.rb +222 -0
  36. data/lib/active_merchant/billing/gateways/first_pay.rb +172 -0
  37. data/lib/active_merchant/billing/gateways/garanti.rb +222 -0
  38. data/lib/active_merchant/billing/gateways/inspire.rb +221 -0
  39. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  40. data/lib/active_merchant/billing/gateways/iridium.rb +253 -0
  41. data/lib/active_merchant/billing/gateways/jetpay.rb +270 -0
  42. data/lib/active_merchant/billing/gateways/linkpoint.rb +449 -0
  43. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +154 -0
  44. data/lib/active_merchant/billing/gateways/merchant_ware.rb +283 -0
  45. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  46. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  47. data/lib/active_merchant/billing/gateways/moneris.rb +205 -0
  48. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  49. data/lib/active_merchant/billing/gateways/netaxept.rb +234 -0
  50. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  51. data/lib/active_merchant/billing/gateways/ogone.rb +279 -0
  52. data/lib/active_merchant/billing/gateways/pay_junction.rb +392 -0
  53. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  54. data/lib/active_merchant/billing/gateways/paybox_direct.rb +203 -0
  55. data/lib/active_merchant/billing/gateways/payflow.rb +236 -0
  56. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +207 -0
  57. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  58. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  59. data/lib/active_merchant/billing/gateways/payflow_express.rb +138 -0
  60. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  61. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  62. data/lib/active_merchant/billing/gateways/payment_express.rb +230 -0
  63. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  64. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +326 -0
  65. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +38 -0
  66. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  67. data/lib/active_merchant/billing/gateways/paypal_express.rb +145 -0
  68. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +20 -0
  69. data/lib/active_merchant/billing/gateways/paysimple/paysimple.rb +333 -0
  70. data/lib/active_merchant/billing/gateways/paysimple/usaepay.wsdl +1596 -0
  71. data/lib/active_merchant/billing/gateways/plugnpay.rb +292 -0
  72. data/lib/active_merchant/billing/gateways/psigate.rb +214 -0
  73. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  74. data/lib/active_merchant/billing/gateways/quickpay.rb +213 -0
  75. data/lib/active_merchant/billing/gateways/realex.rb +200 -0
  76. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  77. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  78. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  79. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  80. data/lib/active_merchant/billing/gateways/sage_pay.rb +315 -0
  81. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  82. data/lib/active_merchant/billing/gateways/secure_net.rb +330 -0
  83. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  84. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +157 -0
  85. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  86. data/lib/active_merchant/billing/gateways/skip_jack.rb +453 -0
  87. data/lib/active_merchant/billing/gateways/smart_ps.rb +265 -0
  88. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  89. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  90. data/lib/active_merchant/billing/gateways/trust_commerce.rb +418 -0
  91. data/lib/active_merchant/billing/gateways/usa_epay.rb +194 -0
  92. data/lib/active_merchant/billing/gateways/usa_epay_soap.rb +154 -0
  93. data/lib/active_merchant/billing/gateways/verifi.rb +228 -0
  94. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  95. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  96. data/lib/active_merchant/billing/integrations.rb +17 -0
  97. data/lib/active_merchant/billing/integrations/action_view_helper.rb +68 -0
  98. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  99. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  100. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  101. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  102. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  103. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  104. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  105. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  106. data/lib/active_merchant/billing/integrations/direc_pay.rb +41 -0
  107. data/lib/active_merchant/billing/integrations/direc_pay/helper.rb +188 -0
  108. data/lib/active_merchant/billing/integrations/direc_pay/notification.rb +76 -0
  109. data/lib/active_merchant/billing/integrations/direc_pay/return.rb +32 -0
  110. data/lib/active_merchant/billing/integrations/direc_pay/status.rb +37 -0
  111. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  112. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  113. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  114. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  115. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  116. data/lib/active_merchant/billing/integrations/helper.rb +96 -0
  117. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  118. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  119. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  120. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  121. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  122. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  123. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  124. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  125. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  126. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  127. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  128. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  129. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  130. data/lib/active_merchant/billing/integrations/quickpay.rb +17 -0
  131. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  132. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  133. data/lib/active_merchant/billing/integrations/return.rb +37 -0
  134. data/lib/active_merchant/billing/integrations/sage_pay_form.rb +37 -0
  135. data/lib/active_merchant/billing/integrations/sage_pay_form/encryption.rb +33 -0
  136. data/lib/active_merchant/billing/integrations/sage_pay_form/helper.rb +109 -0
  137. data/lib/active_merchant/billing/integrations/sage_pay_form/notification.rb +204 -0
  138. data/lib/active_merchant/billing/integrations/sage_pay_form/return.rb +27 -0
  139. data/lib/active_merchant/billing/integrations/two_checkout.rb +23 -0
  140. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  141. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  142. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  143. data/lib/active_merchant/billing/response.rb +32 -0
  144. data/lib/active_merchant/common.rb +14 -0
  145. data/lib/active_merchant/common/connection.rb +162 -0
  146. data/lib/active_merchant/common/country.rb +328 -0
  147. data/lib/active_merchant/common/error.rb +26 -0
  148. data/lib/active_merchant/common/post_data.rb +24 -0
  149. data/lib/active_merchant/common/posts_data.rb +61 -0
  150. data/lib/active_merchant/common/requires_parameters.rb +16 -0
  151. data/lib/active_merchant/common/utils.rb +18 -0
  152. data/lib/active_merchant/common/validateable.rb +76 -0
  153. data/lib/active_merchant/version.rb +3 -0
  154. data/lib/activemerchant.rb +1 -0
  155. data/lib/certs/cacert.pem +7815 -0
  156. data/lib/support/gateway_support.rb +58 -0
  157. data/lib/support/outbound_hosts.rb +25 -0
  158. metadata +270 -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
@@ -0,0 +1,593 @@
1
+ module ActiveMerchant
2
+ module Billing
3
+ class DataCashGateway < Gateway
4
+ self.default_currency = 'GBP'
5
+ self.supported_countries = ['GB']
6
+
7
+ # From the DataCash docs; Page 13, the following cards are
8
+ # usable:
9
+ # American Express, ATM, Carte Blanche, Diners Club, Discover,
10
+ # EnRoute, GE Capital, JCB, Laser, Maestro, Mastercard, Solo,
11
+ # Switch, Visa, Visa Delta, VISA Electron, Visa Purchasing
12
+ #
13
+ # Note continuous authority is only supported for :visa, :master and :american_express card types
14
+ self.supported_cardtypes = [ :visa, :master, :american_express, :discover, :diners_club, :jcb, :maestro, :switch, :solo, :laser ]
15
+
16
+ self.homepage_url = 'http://www.datacash.com/'
17
+ self.display_name = 'DataCash'
18
+
19
+ # Datacash server URLs
20
+ TEST_URL = 'https://testserver.datacash.com/Transaction'
21
+ LIVE_URL = 'https://mars.transaction.datacash.com/Transaction'
22
+
23
+ # Different Card Transaction Types
24
+ AUTH_TYPE = 'auth'
25
+ CANCEL_TYPE = 'cancel'
26
+ FULFILL_TYPE = 'fulfill'
27
+ PRE_TYPE = 'pre'
28
+ REFUND_TYPE = 'refund'
29
+ TRANSACTION_REFUND_TYPE = 'txn_refund'
30
+
31
+ # Constant strings for use in the ExtendedPolicy complex element for
32
+ # CV2 checks
33
+ POLICY_ACCEPT = 'accept'
34
+ POLICY_REJECT = 'reject'
35
+
36
+ # Datacash success code
37
+ DATACASH_SUCCESS = '1'
38
+
39
+ # Creates a new DataCashGateway
40
+ #
41
+ # The gateway requires that a valid login and password be passed
42
+ # in the +options+ hash.
43
+ #
44
+ # ==== Options
45
+ #
46
+ # * <tt>:login</tt> -- The Datacash account login.
47
+ # * <tt>:password</tt> -- The Datacash account password.
48
+ # * <tt>:test => +true+ or +false+</tt> -- Use the test or live Datacash url.
49
+ #
50
+ def initialize(options = {})
51
+ requires!(options, :login, :password)
52
+ @options = options
53
+ super
54
+ end
55
+
56
+ # Perform a purchase, which is essentially an authorization and capture in a single operation.
57
+ #
58
+ # ==== Parameters
59
+ # * <tt>money</tt> The amount to be authorized as an Integer value in cents.
60
+ # * <tt>authorization_or_credit_card</tt>:: The continuous authority reference or CreditCard details for the transaction.
61
+ # * <tt>options</tt> A hash of optional parameters.
62
+ # * <tt>:order_id</tt> A unique reference for this order (corresponds to merchantreference in datacash documentation)
63
+ # * <tt>:set_up_continuous_authority</tt>
64
+ # Set to true to set up a recurring historic transaction account be set up.
65
+ # Only supported for :visa, :master and :american_express card types
66
+ # See http://www.datacash.com/services/recurring/historic.php for more details of historic transactions.
67
+ # * <tt>:address</tt>:: billing address for card
68
+ #
69
+ # The continuous authority reference will be available in response#params['ca_referece'] if you have requested one
70
+ def purchase(money, authorization_or_credit_card, options = {})
71
+ requires!(options, :order_id)
72
+
73
+ if authorization_or_credit_card.is_a?(String)
74
+ request = build_purchase_or_authorization_request_with_continuous_authority_reference_request(AUTH_TYPE, money, authorization_or_credit_card, options)
75
+ else
76
+ request = build_purchase_or_authorization_request_with_credit_card_request(AUTH_TYPE, money, authorization_or_credit_card, options)
77
+ end
78
+
79
+ commit(request)
80
+ end
81
+
82
+ # Performs an authorization, which reserves the funds on the customer's credit card, but does not
83
+ # charge the card.
84
+ #
85
+ # ==== Parameters
86
+ #
87
+ # * <tt>money</tt> The amount to be authorized as an Integer value in cents.
88
+ # * <tt>authorization_or_credit_card</tt>:: The continuous authority reference or CreditCard details for the transaction.
89
+ # * <tt>options</tt> A hash of optional parameters.
90
+ # * <tt>:order_id</tt> A unique reference for this order (corresponds to merchantreference in datacash documentation)
91
+ # * <tt>:set_up_continuous_authority</tt>::
92
+ # Set to true to set up a recurring historic transaction account be set up.
93
+ # Only supported for :visa, :master and :american_express card types
94
+ # See http://www.datacash.com/services/recurring/historic.php for more details of historic transactions.
95
+ # * <tt>:address</tt>:: billing address for card
96
+ #
97
+ # The continuous authority reference will be available in response#params['ca_referece'] if you have requested one
98
+ def authorize(money, authorization_or_credit_card, options = {})
99
+ requires!(options, :order_id)
100
+
101
+ if authorization_or_credit_card.is_a?(String)
102
+ request = build_purchase_or_authorization_request_with_continuous_authority_reference_request(AUTH_TYPE, money, authorization_or_credit_card, options)
103
+ else
104
+ request = build_purchase_or_authorization_request_with_credit_card_request(PRE_TYPE, money, authorization_or_credit_card, options)
105
+ end
106
+
107
+ commit(request)
108
+ end
109
+
110
+ # Captures the funds from an authorized transaction.
111
+ #
112
+ # ==== Parameters
113
+ #
114
+ # * <tt>money</tt> -- The amount to be captured as anInteger value in cents.
115
+ # * <tt>authorization</tt> -- The authorization returned from the previous authorize request.
116
+ def capture(money, authorization, options = {})
117
+ commit(build_void_or_capture_request(FULFILL_TYPE, money, authorization, options))
118
+ end
119
+
120
+ # Void a previous transaction
121
+ #
122
+ # ==== Parameters
123
+ #
124
+ # * <tt>authorization</tt> - The authorization returned from the previous authorize request.
125
+ def void(authorization, options = {})
126
+ request = build_void_or_capture_request(CANCEL_TYPE, nil, authorization, options)
127
+
128
+ commit(request)
129
+ end
130
+
131
+ # Refund to a card
132
+ #
133
+ # ==== Parameters
134
+ #
135
+ # * <tt>money</tt> The amount to be refunded as an Integer value in cents. Set to nil for a full refund on existing transaction.
136
+ # * <tt>reference_or_credit_card</tt> The credit card you want to refund OR the datacash_reference for the existing transaction you are refunding
137
+ # * <tt>options</tt> Are ignored when refunding via reference to an existing transaction, otherwise
138
+ # * <tt>:order_id</tt> A unique reference for this order (corresponds to merchantreference in datacash documentation)
139
+ # * <tt>:address</tt>:: billing address for card
140
+ def credit(money, reference_or_credit_card, options = {})
141
+ if reference_or_credit_card.is_a?(String)
142
+ request = build_transaction_refund_request(money, reference_or_credit_card)
143
+ else
144
+ request = build_refund_request(money, reference_or_credit_card, options)
145
+ end
146
+
147
+ commit(request)
148
+ end
149
+
150
+ # Is the gateway running in test mode?
151
+ def test?
152
+ @options[:test] || super
153
+ end
154
+
155
+ private
156
+ # Create the xml document for a 'cancel' or 'fulfill' transaction.
157
+ #
158
+ # Final XML should look like:
159
+ # <Request>
160
+ # <Authentication>
161
+ # <client>99000001</client>
162
+ # <password>******</password>
163
+ # </Authentication>
164
+ # <Transaction>
165
+ # <TxnDetails>
166
+ # <amount>25.00</amount>
167
+ # </TxnDetails>
168
+ # <HistoricTxn>
169
+ # <reference>4900200000000001</reference>
170
+ # <authcode>A6</authcode>
171
+ # <method>fulfill</method>
172
+ # </HistoricTxn>
173
+ # </Transaction>
174
+ # </Request>
175
+ #
176
+ # Parameters:
177
+ # * <tt>type</tt> must be FULFILL_TYPE or CANCEL_TYPE
178
+ # * <tt>money</tt> - optional - Integer value in cents
179
+ # * <tt>authorization</tt> - the Datacash authorization from a previous succesful authorize transaction
180
+ # * <tt>options</tt>
181
+ # * <tt>order_id</tt> - A unique reference for the transaction
182
+ #
183
+ # Returns:
184
+ # -Builder xml document
185
+ #
186
+ def build_void_or_capture_request(type, money, authorization, options)
187
+ reference, auth_code, ca_reference = authorization.to_s.split(';')
188
+
189
+ xml = Builder::XmlMarkup.new :indent => 2
190
+ xml.instruct!
191
+ xml.tag! :Request do
192
+ add_authentication(xml)
193
+
194
+ xml.tag! :Transaction do
195
+ xml.tag! :HistoricTxn do
196
+ xml.tag! :reference, reference
197
+ xml.tag! :authcode, auth_code
198
+ xml.tag! :method, type
199
+ end
200
+
201
+ if money
202
+ xml.tag! :TxnDetails do
203
+ xml.tag! :merchantreference, format_reference_number(options[:order_id])
204
+ xml.tag! :amount, amount(money), :currency => options[:currency] || currency(money)
205
+ end
206
+ end
207
+ end
208
+ end
209
+ xml.target!
210
+ end
211
+
212
+ # Create the xml document for an 'auth' or 'pre' transaction with a credit card
213
+ #
214
+ # Final XML should look like:
215
+ #
216
+ # <Request>
217
+ # <Authentication>
218
+ # <client>99000000</client>
219
+ # <password>*******</password>
220
+ # </Authentication>
221
+ # <Transaction>
222
+ # <TxnDetails>
223
+ # <merchantreference>123456</merchantreference>
224
+ # <amount currency="EUR">10.00</amount>
225
+ # </TxnDetails>
226
+ # <CardTxn>
227
+ # <Card>
228
+ # <pan>4444********1111</pan>
229
+ # <expirydate>03/04</expirydate>
230
+ # <Cv2Avs>
231
+ # <street_address1>Flat 7</street_address1>
232
+ # <street_address2>89 Jumble
233
+ # Street</street_address2>
234
+ # <street_address3>Mytown</street_address3>
235
+ # <postcode>AV12FR</postcode>
236
+ # <cv2>123</cv2>
237
+ # <ExtendedPolicy>
238
+ # <cv2_policy notprovided="reject"
239
+ # notchecked="accept"
240
+ # matched="accept"
241
+ # notmatched="reject"
242
+ # partialmatch="reject"/>
243
+ # <postcode_policy notprovided="reject"
244
+ # notchecked="accept"
245
+ # matched="accept"
246
+ # notmatched="reject"
247
+ # partialmatch="accept"/>
248
+ # <address_policy notprovided="reject"
249
+ # notchecked="accept"
250
+ # matched="accept"
251
+ # notmatched="reject"
252
+ # partialmatch="accept"/>
253
+ # </ExtendedPolicy>
254
+ # </Cv2Avs>
255
+ # </Card>
256
+ # <method>auth</method>
257
+ # </CardTxn>
258
+ # </Transaction>
259
+ # </Request>
260
+ #
261
+ # Parameters:
262
+ # -type must be 'auth' or 'pre'
263
+ # -money - A money object with the price and currency
264
+ # -credit_card - The credit_card details to use
265
+ # -options:
266
+ # :order_id is the merchant reference number
267
+ # :billing_address is the billing address for the cc
268
+ # :address is the delivery address
269
+ #
270
+ # Returns:
271
+ # -xml: Builder document containing the markup
272
+ #
273
+ def build_purchase_or_authorization_request_with_credit_card_request(type, money, credit_card, options)
274
+ xml = Builder::XmlMarkup.new :indent => 2
275
+ xml.instruct!
276
+ xml.tag! :Request do
277
+ add_authentication(xml)
278
+
279
+ xml.tag! :Transaction do
280
+ if options[:set_up_continuous_authority]
281
+ xml.tag! :ContAuthTxn, :type => 'setup'
282
+ end
283
+ xml.tag! :CardTxn do
284
+ xml.tag! :method, type
285
+ add_credit_card(xml, credit_card, options[:billing_address])
286
+ end
287
+ xml.tag! :TxnDetails do
288
+ xml.tag! :merchantreference, format_reference_number(options[:order_id])
289
+ xml.tag! :amount, amount(money), :currency => options[:currency] || currency(money)
290
+ end
291
+ end
292
+ end
293
+ xml.target!
294
+ end
295
+
296
+ # Create the xml document for an 'auth' or 'pre' transaction with
297
+ # continuous authorization
298
+ #
299
+ # Final XML should look like:
300
+ #
301
+ # <Request>
302
+ # <Transaction>
303
+ # <ContAuthTxn type="historic" />
304
+ # <TxnDetails>
305
+ # <merchantreference>3851231</merchantreference>
306
+ # <capturemethod>cont_auth</capturemethod>
307
+ # <amount currency="GBP">18.50</amount>
308
+ # </TxnDetails>
309
+ # <HistoricTxn>
310
+ # <reference>4500200040925092</reference>
311
+ # <method>auth</method>
312
+ # </HistoricTxn>
313
+ # </Transaction>
314
+ # <Authentication>
315
+ # <client>99000001</client>
316
+ # <password>mypasswd</password>
317
+ # </Authentication>
318
+ # </Request>
319
+ #
320
+ # Parameters:
321
+ # -type must be 'auth' or 'pre'
322
+ # -money - A money object with the price and currency
323
+ # -authorization - The authorization containing a continuous authority reference previously set up on a credit card
324
+ # -options:
325
+ # :order_id is the merchant reference number
326
+ #
327
+ # Returns:
328
+ # -xml: Builder document containing the markup
329
+ #
330
+ def build_purchase_or_authorization_request_with_continuous_authority_reference_request(type, money, authorization, options)
331
+ reference, auth_code, ca_reference = authorization.to_s.split(';')
332
+ raise ArgumentError, "The continuous authority reference is required for continuous authority transactions" if ca_reference.blank?
333
+
334
+ xml = Builder::XmlMarkup.new :indent => 2
335
+ xml.instruct!
336
+ xml.tag! :Request do
337
+ add_authentication(xml)
338
+ xml.tag! :Transaction do
339
+ xml.tag! :ContAuthTxn, :type => 'historic'
340
+ xml.tag! :HistoricTxn do
341
+ xml.tag! :reference, ca_reference
342
+ xml.tag! :method, type
343
+ end
344
+ xml.tag! :TxnDetails do
345
+ xml.tag! :merchantreference, format_reference_number(options[:order_id])
346
+ xml.tag! :amount, amount(money), :currency => options[:currency] || currency(money)
347
+ xml.tag! :capturemethod, 'cont_auth'
348
+ end
349
+ end
350
+ end
351
+ xml.target!
352
+ end
353
+
354
+ # Create the xml document for a full or partial refund transaction with
355
+ #
356
+ # Final XML should look like:
357
+ #
358
+ # <Request>
359
+ # <Authentication>
360
+ # <client>99000001</client>
361
+ # <password>*******</password>
362
+ # </Authentication>
363
+ # <Transaction>
364
+ # <HistoricTxn>
365
+ # <method>txn_refund</method>
366
+ # <reference>12345678</reference>
367
+ # </HistoricTxn>
368
+ # <TxnDetails>
369
+ # <amount>10.00</amount>
370
+ # </TxnDetails>
371
+ # </Transaction>
372
+ # </Request>
373
+ #
374
+ def build_transaction_refund_request(money, reference)
375
+ xml = Builder::XmlMarkup.new :indent => 2
376
+ xml.instruct!
377
+ xml.tag! :Request do
378
+ add_authentication(xml)
379
+ xml.tag! :Transaction do
380
+ xml.tag! :HistoricTxn do
381
+ xml.tag! :reference, reference
382
+ xml.tag! :method, TRANSACTION_REFUND_TYPE
383
+ end
384
+ unless money.nil?
385
+ xml.tag! :TxnDetails do
386
+ xml.tag! :amount, amount(money)
387
+ end
388
+ end
389
+ end
390
+ end
391
+ xml.target!
392
+ end
393
+
394
+ # Create the xml document for a full or partial refund with
395
+ #
396
+ # Final XML should look like:
397
+ #
398
+ # <Request>
399
+ # <Authentication>
400
+ # <client>99000001</client>
401
+ # <password>*****</password>
402
+ # </Authentication>
403
+ # <Transaction>
404
+ # <CardTxn>
405
+ # <Card>
406
+ # <pan>633300*********1</pan>
407
+ # <expirydate>04/06</expirydate>
408
+ # <startdate>01/04</startdate>
409
+ # </Card>
410
+ # <method>refund</method>
411
+ # </CardTxn>
412
+ # <TxnDetails>
413
+ # <merchantreference>1000001</merchantreference>
414
+ # <amount currency="GBP">95.99</amount>
415
+ # </TxnDetails>
416
+ # </Transaction>
417
+ # </Request>
418
+ def build_refund_request(money, credit_card, options)
419
+ xml = Builder::XmlMarkup.new :indent => 2
420
+ xml.instruct!
421
+ xml.tag! :Request do
422
+ add_authentication(xml)
423
+ xml.tag! :Transaction do
424
+ xml.tag! :CardTxn do
425
+ xml.tag! :method, REFUND_TYPE
426
+ add_credit_card(xml, credit_card, options[:billing_address])
427
+ end
428
+ xml.tag! :TxnDetails do
429
+ xml.tag! :merchantreference, format_reference_number(options[:order_id])
430
+ xml.tag! :amount, amount(money)
431
+ end
432
+ end
433
+ end
434
+ xml.target!
435
+ end
436
+
437
+
438
+ # Adds the authentication element to the passed builder xml doc
439
+ #
440
+ # Parameters:
441
+ # -xml: Builder document that is being built up
442
+ #
443
+ # Returns:
444
+ # -none: The results is stored in the passed xml document
445
+ #
446
+ def add_authentication(xml)
447
+ xml.tag! :Authentication do
448
+ xml.tag! :client, @options[:login]
449
+ xml.tag! :password, @options[:password]
450
+ end
451
+ end
452
+
453
+ # Add credit_card detals to the passed XML Builder doc
454
+ #
455
+ # Parameters:
456
+ # -xml: Builder document that is being built up
457
+ # -credit_card: ActiveMerchant::Billing::CreditCard object
458
+ # -billing_address: Hash containing all of the billing address details
459
+ #
460
+ # Returns:
461
+ # -none: The results is stored in the passed xml document
462
+ #
463
+ def add_credit_card(xml, credit_card, address)
464
+
465
+ xml.tag! :Card do
466
+
467
+ # DataCash calls the CC number 'pan'
468
+ xml.tag! :pan, credit_card.number
469
+ xml.tag! :expirydate, format_date(credit_card.month, credit_card.year)
470
+
471
+ # optional values - for Solo etc
472
+ if [ 'switch', 'solo' ].include?(card_brand(credit_card).to_s)
473
+
474
+ xml.tag! :issuenumber, credit_card.issue_number unless credit_card.issue_number.blank?
475
+
476
+ if !credit_card.start_month.blank? && !credit_card.start_year.blank?
477
+ xml.tag! :startdate, format_date(credit_card.start_month, credit_card.start_year)
478
+ end
479
+ end
480
+
481
+ xml.tag! :Cv2Avs do
482
+ xml.tag! :cv2, credit_card.verification_value if credit_card.verification_value?
483
+ if address
484
+ xml.tag! :street_address1, address[:address1] unless address[:address1].blank?
485
+ xml.tag! :street_address2, address[:address2] unless address[:address2].blank?
486
+ xml.tag! :street_address3, address[:address3] unless address[:address3].blank?
487
+ xml.tag! :street_address4, address[:address4] unless address[:address4].blank?
488
+ xml.tag! :postcode, address[:zip] unless address[:zip].blank?
489
+ end
490
+
491
+ # The ExtendedPolicy defines what to do when the passed data
492
+ # matches, or not...
493
+ #
494
+ # All of the following elements MUST be present for the
495
+ # xml to be valid (or can drop the ExtendedPolicy and use
496
+ # a predefined one
497
+ xml.tag! :ExtendedPolicy do
498
+ xml.tag! :cv2_policy,
499
+ :notprovided => POLICY_REJECT,
500
+ :notchecked => POLICY_REJECT,
501
+ :matched => POLICY_ACCEPT,
502
+ :notmatched => POLICY_REJECT,
503
+ :partialmatch => POLICY_REJECT
504
+ xml.tag! :postcode_policy,
505
+ :notprovided => POLICY_ACCEPT,
506
+ :notchecked => POLICY_ACCEPT,
507
+ :matched => POLICY_ACCEPT,
508
+ :notmatched => POLICY_REJECT,
509
+ :partialmatch => POLICY_ACCEPT
510
+ xml.tag! :address_policy,
511
+ :notprovided => POLICY_ACCEPT,
512
+ :notchecked => POLICY_ACCEPT,
513
+ :matched => POLICY_ACCEPT,
514
+ :notmatched => POLICY_REJECT,
515
+ :partialmatch => POLICY_ACCEPT
516
+ end
517
+ end
518
+ end
519
+ end
520
+
521
+ # Send the passed data to DataCash for processing
522
+ #
523
+ # Parameters:
524
+ # -request: The XML data that is to be sent to Datacash
525
+ #
526
+ # Returns:
527
+ # - ActiveMerchant::Billing::Response object
528
+ #
529
+ def commit(request)
530
+ response = parse(ssl_post(test? ? TEST_URL : LIVE_URL, request))
531
+
532
+ Response.new(response[:status] == DATACASH_SUCCESS, response[:reason], response,
533
+ :test => test?,
534
+ :authorization => "#{response[:datacash_reference]};#{response[:authcode]};#{response[:ca_reference]}"
535
+ )
536
+ end
537
+
538
+ # Returns a date string in the format Datacash expects
539
+ #
540
+ # Parameters:
541
+ # -month: integer, the month
542
+ # -year: integer, the year
543
+ #
544
+ # Returns:
545
+ # -String: date in MM/YY format
546
+ #
547
+ def format_date(month, year)
548
+ "#{format(month,:two_digits)}/#{format(year, :two_digits)}"
549
+ end
550
+
551
+ # Parse the datacash response and create a Response object
552
+ #
553
+ # Parameters:
554
+ # -body: The XML returned from Datacash
555
+ #
556
+ # Returns:
557
+ # -a hash with all of the values returned in the Datacash XML response
558
+ #
559
+ def parse(body)
560
+
561
+ response = {}
562
+ xml = REXML::Document.new(body)
563
+ root = REXML::XPath.first(xml, "//Response")
564
+
565
+ root.elements.to_a.each do |node|
566
+ parse_element(response, node)
567
+ end
568
+
569
+ response
570
+ end
571
+
572
+ # Parse an xml element
573
+ #
574
+ # Parameters:
575
+ # -response: The hash that the values are being returned in
576
+ # -node: The node that is currently being read
577
+ #
578
+ # Returns:
579
+ # - none (results are stored in the passed hash)
580
+ def parse_element(response, node)
581
+ if node.has_elements?
582
+ node.elements.each{|e| parse_element(response, e) }
583
+ else
584
+ response[node.name.underscore.to_sym] = node.text
585
+ end
586
+ end
587
+
588
+ def format_reference_number(number)
589
+ number.to_s.gsub(/[^A-Za-z0-9]/, '').rjust(6, "0").first(30)
590
+ end
591
+ end
592
+ end
593
+ end