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,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