active_accountability_merchant 1.97.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +2948 -0
  3. data/CONTRIBUTORS +568 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +246 -0
  6. data/lib/active_merchant.rb +63 -0
  7. data/lib/active_merchant/billing.rb +15 -0
  8. data/lib/active_merchant/billing/apple_pay_payment_token.rb +22 -0
  9. data/lib/active_merchant/billing/avs_result.rb +96 -0
  10. data/lib/active_merchant/billing/base.rb +61 -0
  11. data/lib/active_merchant/billing/check.rb +80 -0
  12. data/lib/active_merchant/billing/compatibility.rb +117 -0
  13. data/lib/active_merchant/billing/credit_card.rb +406 -0
  14. data/lib/active_merchant/billing/credit_card_formatting.rb +23 -0
  15. data/lib/active_merchant/billing/credit_card_methods.rb +343 -0
  16. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  17. data/lib/active_merchant/billing/gateway.rb +330 -0
  18. data/lib/active_merchant/billing/gateways.rb +14 -0
  19. data/lib/active_merchant/billing/gateways/adyen.rb +503 -0
  20. data/lib/active_merchant/billing/gateways/allied_wallet.rb +205 -0
  21. data/lib/active_merchant/billing/gateways/authorize_net.rb +1055 -0
  22. data/lib/active_merchant/billing/gateways/authorize_net_arb.rb +417 -0
  23. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +980 -0
  24. data/lib/active_merchant/billing/gateways/axcessms.rb +181 -0
  25. data/lib/active_merchant/billing/gateways/balanced.rb +256 -0
  26. data/lib/active_merchant/billing/gateways/bambora_apac.rb +226 -0
  27. data/lib/active_merchant/billing/gateways/bank_frick.rb +225 -0
  28. data/lib/active_merchant/billing/gateways/banwire.rb +116 -0
  29. data/lib/active_merchant/billing/gateways/barclaycard_smartpay.rb +384 -0
  30. data/lib/active_merchant/billing/gateways/barclays_epdq_extra_plus.rb +15 -0
  31. data/lib/active_merchant/billing/gateways/be2bill.rb +131 -0
  32. data/lib/active_merchant/billing/gateways/beanstream.rb +217 -0
  33. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +473 -0
  34. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +57 -0
  35. data/lib/active_merchant/billing/gateways/blue_pay.rb +522 -0
  36. data/lib/active_merchant/billing/gateways/blue_snap.rb +522 -0
  37. data/lib/active_merchant/billing/gateways/bogus.rb +186 -0
  38. data/lib/active_merchant/billing/gateways/borgun.rb +220 -0
  39. data/lib/active_merchant/billing/gateways/bpoint.rb +277 -0
  40. data/lib/active_merchant/billing/gateways/braintree.rb +19 -0
  41. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +22 -0
  42. data/lib/active_merchant/billing/gateways/braintree_blue.rb +740 -0
  43. data/lib/active_merchant/billing/gateways/braintree_orange.rb +19 -0
  44. data/lib/active_merchant/billing/gateways/bridge_pay.rb +244 -0
  45. data/lib/active_merchant/billing/gateways/cams.rb +230 -0
  46. data/lib/active_merchant/billing/gateways/card_connect.rb +316 -0
  47. data/lib/active_merchant/billing/gateways/card_save.rb +22 -0
  48. data/lib/active_merchant/billing/gateways/card_stream.rb +367 -0
  49. data/lib/active_merchant/billing/gateways/cardknox.rb +327 -0
  50. data/lib/active_merchant/billing/gateways/cardprocess.rb +254 -0
  51. data/lib/active_merchant/billing/gateways/cashnet.rb +219 -0
  52. data/lib/active_merchant/billing/gateways/cc5.rb +198 -0
  53. data/lib/active_merchant/billing/gateways/cecabank.rb +249 -0
  54. data/lib/active_merchant/billing/gateways/cenpos.rb +327 -0
  55. data/lib/active_merchant/billing/gateways/checkout.rb +214 -0
  56. data/lib/active_merchant/billing/gateways/checkout_v2.rb +270 -0
  57. data/lib/active_merchant/billing/gateways/citrus_pay.rb +22 -0
  58. data/lib/active_merchant/billing/gateways/clearhaus.rb +220 -0
  59. data/lib/active_merchant/billing/gateways/commercegate.rb +142 -0
  60. data/lib/active_merchant/billing/gateways/conekta.rb +228 -0
  61. data/lib/active_merchant/billing/gateways/creditcall.rb +271 -0
  62. data/lib/active_merchant/billing/gateways/credorax.rb +382 -0
  63. data/lib/active_merchant/billing/gateways/ct_payment.rb +268 -0
  64. data/lib/active_merchant/billing/gateways/culqi.rb +277 -0
  65. data/lib/active_merchant/billing/gateways/cyber_source.rb +828 -0
  66. data/lib/active_merchant/billing/gateways/d_local.rb +226 -0
  67. data/lib/active_merchant/billing/gateways/data_cash.rb +305 -0
  68. data/lib/active_merchant/billing/gateways/decidir.rb +233 -0
  69. data/lib/active_merchant/billing/gateways/dibs.rb +199 -0
  70. data/lib/active_merchant/billing/gateways/digitzs.rb +292 -0
  71. data/lib/active_merchant/billing/gateways/ebanx.rb +296 -0
  72. data/lib/active_merchant/billing/gateways/efsnet.rb +215 -0
  73. data/lib/active_merchant/billing/gateways/elavon.rb +320 -0
  74. data/lib/active_merchant/billing/gateways/element.rb +356 -0
  75. data/lib/active_merchant/billing/gateways/epay.rb +285 -0
  76. data/lib/active_merchant/billing/gateways/evo_ca.rb +308 -0
  77. data/lib/active_merchant/billing/gateways/eway.rb +226 -0
  78. data/lib/active_merchant/billing/gateways/eway_managed.rb +290 -0
  79. data/lib/active_merchant/billing/gateways/eway_rapid.rb +563 -0
  80. data/lib/active_merchant/billing/gateways/exact.rb +224 -0
  81. data/lib/active_merchant/billing/gateways/ezic.rb +195 -0
  82. data/lib/active_merchant/billing/gateways/fat_zebra.rb +218 -0
  83. data/lib/active_merchant/billing/gateways/federated_canada.rb +159 -0
  84. data/lib/active_merchant/billing/gateways/finansbank.rb +22 -0
  85. data/lib/active_merchant/billing/gateways/first_giving.rb +142 -0
  86. data/lib/active_merchant/billing/gateways/first_pay.rb +182 -0
  87. data/lib/active_merchant/billing/gateways/firstdata_e4.rb +451 -0
  88. data/lib/active_merchant/billing/gateways/firstdata_e4_v27.rb +485 -0
  89. data/lib/active_merchant/billing/gateways/flo2cash.rb +215 -0
  90. data/lib/active_merchant/billing/gateways/flo2cash_simple.rb +20 -0
  91. data/lib/active_merchant/billing/gateways/forte.rb +270 -0
  92. data/lib/active_merchant/billing/gateways/garanti.rb +259 -0
  93. data/lib/active_merchant/billing/gateways/global_collect.rb +336 -0
  94. data/lib/active_merchant/billing/gateways/global_transport.rb +194 -0
  95. data/lib/active_merchant/billing/gateways/hdfc.rb +206 -0
  96. data/lib/active_merchant/billing/gateways/hps.rb +350 -0
  97. data/lib/active_merchant/billing/gateways/iats_payments.rb +290 -0
  98. data/lib/active_merchant/billing/gateways/in_context_paypal_express.rb +15 -0
  99. data/lib/active_merchant/billing/gateways/inspire.rb +218 -0
  100. data/lib/active_merchant/billing/gateways/instapay.rb +162 -0
  101. data/lib/active_merchant/billing/gateways/ipp.rb +176 -0
  102. data/lib/active_merchant/billing/gateways/iridium.rb +468 -0
  103. data/lib/active_merchant/billing/gateways/itransact.rb +447 -0
  104. data/lib/active_merchant/billing/gateways/iveri.rb +251 -0
  105. data/lib/active_merchant/billing/gateways/jetpay.rb +402 -0
  106. data/lib/active_merchant/billing/gateways/jetpay_v2.rb +437 -0
  107. data/lib/active_merchant/billing/gateways/komoju.rb +115 -0
  108. data/lib/active_merchant/billing/gateways/kushki.rb +219 -0
  109. data/lib/active_merchant/billing/gateways/latitude19.rb +411 -0
  110. data/lib/active_merchant/billing/gateways/linkpoint.rb +449 -0
  111. data/lib/active_merchant/billing/gateways/litle.rb +507 -0
  112. data/lib/active_merchant/billing/gateways/mastercard.rb +267 -0
  113. data/lib/active_merchant/billing/gateways/maxipago.rb +220 -0
  114. data/lib/active_merchant/billing/gateways/mercado_pago.rb +277 -0
  115. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +194 -0
  116. data/lib/active_merchant/billing/gateways/merchant_one.rb +113 -0
  117. data/lib/active_merchant/billing/gateways/merchant_partners.rb +245 -0
  118. data/lib/active_merchant/billing/gateways/merchant_ware.rb +319 -0
  119. data/lib/active_merchant/billing/gateways/merchant_ware_version_four.rb +286 -0
  120. data/lib/active_merchant/billing/gateways/merchant_warrior.rb +210 -0
  121. data/lib/active_merchant/billing/gateways/mercury.rb +356 -0
  122. data/lib/active_merchant/billing/gateways/metrics_global.rb +303 -0
  123. data/lib/active_merchant/billing/gateways/micropayment.rb +183 -0
  124. data/lib/active_merchant/billing/gateways/migs.rb +332 -0
  125. data/lib/active_merchant/billing/gateways/migs/migs_codes.rb +100 -0
  126. data/lib/active_merchant/billing/gateways/modern_payments.rb +37 -0
  127. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +217 -0
  128. data/lib/active_merchant/billing/gateways/monei.rb +338 -0
  129. data/lib/active_merchant/billing/gateways/moneris.rb +377 -0
  130. data/lib/active_merchant/billing/gateways/moneris_us.rb +352 -0
  131. data/lib/active_merchant/billing/gateways/money_movers.rb +151 -0
  132. data/lib/active_merchant/billing/gateways/mundipagg.rb +293 -0
  133. data/lib/active_merchant/billing/gateways/nab_transact.rb +301 -0
  134. data/lib/active_merchant/billing/gateways/ncr_secure_pay.rb +165 -0
  135. data/lib/active_merchant/billing/gateways/net_registry.rb +199 -0
  136. data/lib/active_merchant/billing/gateways/netaxept.rb +180 -0
  137. data/lib/active_merchant/billing/gateways/netbanx.rb +294 -0
  138. data/lib/active_merchant/billing/gateways/netbilling.rb +232 -0
  139. data/lib/active_merchant/billing/gateways/netpay.rb +222 -0
  140. data/lib/active_merchant/billing/gateways/network_merchants.rb +241 -0
  141. data/lib/active_merchant/billing/gateways/nmi.rb +324 -0
  142. data/lib/active_merchant/billing/gateways/ogone.rb +478 -0
  143. data/lib/active_merchant/billing/gateways/omise.rb +324 -0
  144. data/lib/active_merchant/billing/gateways/openpay.rb +228 -0
  145. data/lib/active_merchant/billing/gateways/opp.rb +388 -0
  146. data/lib/active_merchant/billing/gateways/optimal_payment.rb +332 -0
  147. data/lib/active_merchant/billing/gateways/orbital.rb +1006 -0
  148. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +47 -0
  149. data/lib/active_merchant/billing/gateways/pac_net_raven.rb +206 -0
  150. data/lib/active_merchant/billing/gateways/pagarme.rb +246 -0
  151. data/lib/active_merchant/billing/gateways/pago_facil.rb +122 -0
  152. data/lib/active_merchant/billing/gateways/pay_conex.rb +245 -0
  153. data/lib/active_merchant/billing/gateways/pay_gate_xml.rb +280 -0
  154. data/lib/active_merchant/billing/gateways/pay_hub.rb +213 -0
  155. data/lib/active_merchant/billing/gateways/pay_junction.rb +390 -0
  156. data/lib/active_merchant/billing/gateways/pay_junction_v2.rb +188 -0
  157. data/lib/active_merchant/billing/gateways/pay_secure.rb +111 -0
  158. data/lib/active_merchant/billing/gateways/paybox_direct.rb +200 -0
  159. data/lib/active_merchant/billing/gateways/payeezy.rb +410 -0
  160. data/lib/active_merchant/billing/gateways/payex.rb +410 -0
  161. data/lib/active_merchant/billing/gateways/payflow.rb +383 -0
  162. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +235 -0
  163. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +43 -0
  164. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  165. data/lib/active_merchant/billing/gateways/payflow_express.rb +220 -0
  166. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +14 -0
  167. data/lib/active_merchant/billing/gateways/payflow_uk.rb +20 -0
  168. data/lib/active_merchant/billing/gateways/payment_express.rb +369 -0
  169. data/lib/active_merchant/billing/gateways/paymentez.rb +300 -0
  170. data/lib/active_merchant/billing/gateways/paymill.rb +371 -0
  171. data/lib/active_merchant/billing/gateways/paypal.rb +128 -0
  172. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +717 -0
  173. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +65 -0
  174. data/lib/active_merchant/billing/gateways/paypal/paypal_recurring_api.rb +262 -0
  175. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  176. data/lib/active_merchant/billing/gateways/paypal_digital_goods.rb +44 -0
  177. data/lib/active_merchant/billing/gateways/paypal_express.rb +269 -0
  178. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +30 -0
  179. data/lib/active_merchant/billing/gateways/payscout.rb +160 -0
  180. data/lib/active_merchant/billing/gateways/paystation.rb +207 -0
  181. data/lib/active_merchant/billing/gateways/payu_in.rb +248 -0
  182. data/lib/active_merchant/billing/gateways/payu_latam.rb +449 -0
  183. data/lib/active_merchant/billing/gateways/payway.rb +207 -0
  184. data/lib/active_merchant/billing/gateways/pin.rb +234 -0
  185. data/lib/active_merchant/billing/gateways/plugnpay.rb +284 -0
  186. data/lib/active_merchant/billing/gateways/pro_pay.rb +326 -0
  187. data/lib/active_merchant/billing/gateways/psigate.rb +227 -0
  188. data/lib/active_merchant/billing/gateways/psl_card.rb +296 -0
  189. data/lib/active_merchant/billing/gateways/qbms.rb +303 -0
  190. data/lib/active_merchant/billing/gateways/quantum.rb +276 -0
  191. data/lib/active_merchant/billing/gateways/quickbooks.rb +290 -0
  192. data/lib/active_merchant/billing/gateways/quickpay.rb +25 -0
  193. data/lib/active_merchant/billing/gateways/quickpay/quickpay_common.rb +184 -0
  194. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v10.rb +301 -0
  195. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v4to7.rb +226 -0
  196. data/lib/active_merchant/billing/gateways/qvalent.rb +289 -0
  197. data/lib/active_merchant/billing/gateways/realex.rb +374 -0
  198. data/lib/active_merchant/billing/gateways/redsys.rb +534 -0
  199. data/lib/active_merchant/billing/gateways/s5.rb +246 -0
  200. data/lib/active_merchant/billing/gateways/safe_charge.rb +262 -0
  201. data/lib/active_merchant/billing/gateways/sage.rb +448 -0
  202. data/lib/active_merchant/billing/gateways/sage_pay.rb +434 -0
  203. data/lib/active_merchant/billing/gateways/sallie_mae.rb +142 -0
  204. data/lib/active_merchant/billing/gateways/secure_net.rb +267 -0
  205. data/lib/active_merchant/billing/gateways/secure_pay.rb +200 -0
  206. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +292 -0
  207. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +104 -0
  208. data/lib/active_merchant/billing/gateways/securion_pay.rb +264 -0
  209. data/lib/active_merchant/billing/gateways/skip_jack.rb +450 -0
  210. data/lib/active_merchant/billing/gateways/smart_ps.rb +280 -0
  211. data/lib/active_merchant/billing/gateways/so_easy_pay.rb +194 -0
  212. data/lib/active_merchant/billing/gateways/spreedly_core.rb +306 -0
  213. data/lib/active_merchant/billing/gateways/stripe.rb +790 -0
  214. data/lib/active_merchant/billing/gateways/stripe_payment_intents.rb +267 -0
  215. data/lib/active_merchant/billing/gateways/swipe_checkout.rb +152 -0
  216. data/lib/active_merchant/billing/gateways/telr.rb +274 -0
  217. data/lib/active_merchant/billing/gateways/tns.rb +22 -0
  218. data/lib/active_merchant/billing/gateways/trans_first.rb +239 -0
  219. data/lib/active_merchant/billing/gateways/trans_first_transaction_express.rb +608 -0
  220. data/lib/active_merchant/billing/gateways/transact_pro.rb +224 -0
  221. data/lib/active_merchant/billing/gateways/transax.rb +22 -0
  222. data/lib/active_merchant/billing/gateways/transnational.rb +9 -0
  223. data/lib/active_merchant/billing/gateways/trexle.rb +218 -0
  224. data/lib/active_merchant/billing/gateways/trust_commerce.rb +490 -0
  225. data/lib/active_merchant/billing/gateways/usa_epay.rb +25 -0
  226. data/lib/active_merchant/billing/gateways/usa_epay_advanced.rb +1620 -0
  227. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +355 -0
  228. data/lib/active_merchant/billing/gateways/vanco.rb +294 -0
  229. data/lib/active_merchant/billing/gateways/verifi.rb +225 -0
  230. data/lib/active_merchant/billing/gateways/viaklix.rb +176 -0
  231. data/lib/active_merchant/billing/gateways/visanet_peru.rb +245 -0
  232. data/lib/active_merchant/billing/gateways/webpay.rb +97 -0
  233. data/lib/active_merchant/billing/gateways/wepay.rb +237 -0
  234. data/lib/active_merchant/billing/gateways/wirecard.rb +432 -0
  235. data/lib/active_merchant/billing/gateways/world_net.rb +344 -0
  236. data/lib/active_merchant/billing/gateways/worldpay.rb +634 -0
  237. data/lib/active_merchant/billing/gateways/worldpay_online_payments.rb +215 -0
  238. data/lib/active_merchant/billing/gateways/worldpay_us.rb +221 -0
  239. data/lib/active_merchant/billing/model.rb +30 -0
  240. data/lib/active_merchant/billing/network_tokenization_credit_card.rb +39 -0
  241. data/lib/active_merchant/billing/payment_token.rb +21 -0
  242. data/lib/active_merchant/billing/rails.rb +3 -0
  243. data/lib/active_merchant/billing/response.rb +92 -0
  244. data/lib/active_merchant/connection.rb +195 -0
  245. data/lib/active_merchant/country.rb +336 -0
  246. data/lib/active_merchant/empty.rb +20 -0
  247. data/lib/active_merchant/errors.rb +35 -0
  248. data/lib/active_merchant/net_http_ssl_connection.rb +10 -0
  249. data/lib/active_merchant/network_connection_retries.rb +80 -0
  250. data/lib/active_merchant/post_data.rb +25 -0
  251. data/lib/active_merchant/posts_data.rb +92 -0
  252. data/lib/active_merchant/version.rb +3 -0
  253. data/lib/activemerchant.rb +1 -0
  254. data/lib/certs/cacert.pem +3988 -0
  255. data/lib/support/gateway_support.rb +69 -0
  256. data/lib/support/outbound_hosts.rb +28 -0
  257. data/lib/support/ssl_verify.rb +92 -0
  258. data/lib/support/ssl_version.rb +87 -0
  259. metadata +435 -0
@@ -0,0 +1,448 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class SageGateway < Gateway
4
+ include Empty
5
+
6
+ self.display_name = 'http://www.sagepayments.com'
7
+ self.homepage_url = 'Sage Payment Solutions'
8
+ self.live_url = 'https://www.sagepayments.net/cgi-bin'
9
+
10
+ self.supported_countries = ['US', 'CA']
11
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :jcb, :diners_club]
12
+
13
+ TRANSACTIONS = {
14
+ :purchase => '01',
15
+ :authorization => '02',
16
+ :capture => '11',
17
+ :void => '04',
18
+ :credit => '06',
19
+ :refund => '10'
20
+ }
21
+
22
+ SOURCE_CARD = 'bankcard'
23
+ SOURCE_ECHECK = 'virtual_check'
24
+
25
+ def initialize(options = {})
26
+ requires!(options, :login, :password)
27
+ super
28
+ end
29
+
30
+ def authorize(money, credit_card, options = {})
31
+ post = {}
32
+ add_credit_card(post, credit_card)
33
+ add_transaction_data(post, money, options)
34
+ commit(:authorization, post, SOURCE_CARD)
35
+ end
36
+
37
+ def purchase(money, payment_method, options = {})
38
+ post = {}
39
+ if card_brand(payment_method) == 'check'
40
+ source = SOURCE_ECHECK
41
+ add_check(post, payment_method)
42
+ add_check_customer_data(post, options)
43
+ else
44
+ source = SOURCE_CARD
45
+ add_credit_card(post, payment_method)
46
+ end
47
+ add_transaction_data(post, money, options)
48
+ commit(:purchase, post, source)
49
+ end
50
+
51
+ # The +money+ amount is not used. The entire amount of the
52
+ # initial authorization will be captured.
53
+ def capture(money, reference, options = {})
54
+ post = {}
55
+ add_reference(post, reference)
56
+ commit(:capture, post, SOURCE_CARD)
57
+ end
58
+
59
+ def void(reference, options = {})
60
+ post = {}
61
+ add_reference(post, reference)
62
+ source = reference.split(';').last
63
+ commit(:void, post, source)
64
+ end
65
+
66
+ def credit(money, payment_method, options = {})
67
+ post = {}
68
+ if card_brand(payment_method) == 'check'
69
+ source = SOURCE_ECHECK
70
+ add_check(post, payment_method)
71
+ add_check_customer_data(post, options)
72
+ else
73
+ source = SOURCE_CARD
74
+ add_credit_card(post, payment_method)
75
+ end
76
+ add_transaction_data(post, money, options)
77
+ commit(:credit, post, source)
78
+ end
79
+
80
+ def refund(money, reference, options={})
81
+ post = {}
82
+ add_reference(post, reference)
83
+ add_transaction_data(post, money, options)
84
+ commit(:refund, post, SOURCE_CARD)
85
+ end
86
+
87
+ def store(credit_card, options = {})
88
+ vault.store(credit_card, options)
89
+ end
90
+
91
+ def unstore(identification, options = {})
92
+ vault.unstore(identification, options)
93
+ end
94
+
95
+ def supports_scrubbing?
96
+ true
97
+ end
98
+
99
+ def scrub(transcript)
100
+ force_utf8(transcript).
101
+ gsub(%r((M_id=)[^&]*), '\1[FILTERED]').
102
+ gsub(%r((M_key=)[^&]*), '\1[FILTERED]').
103
+ gsub(%r((C_cardnumber=)[^&]*), '\1[FILTERED]').
104
+ gsub(%r((C_cvv=)[^&]*), '\1[FILTERED]').
105
+ gsub(%r((C_rte=)[^&]*), '\1[FILTERED]').
106
+ gsub(%r((C_acct=)[^&]*), '\1[FILTERED]').
107
+ gsub(%r((C_ssn=)[^&]*), '\1[FILTERED]').
108
+ gsub(%r((<ns1:CARDNUMBER>).+(</ns1:CARDNUMBER>)), '\1[FILTERED]\2').
109
+ gsub(%r((<ns1:M_ID>).+(</ns1:M_ID>)), '\1[FILTERED]\2').
110
+ gsub(%r((<ns1:M_KEY>).+(</ns1:M_KEY>)), '\1[FILTERED]\2')
111
+ end
112
+
113
+ private
114
+
115
+ # use the same method as in pay_conex
116
+ def force_utf8(string)
117
+ return nil unless string
118
+ binary = string.encode('BINARY', invalid: :replace, undef: :replace, replace: '?') # Needed for Ruby 2.0 since #encode is a no-op if the string is already UTF-8. It's not needed for Ruby 2.1 and up since it's not a no-op there.
119
+ binary.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
120
+ end
121
+
122
+ def add_credit_card(post, credit_card)
123
+ post[:C_name] = credit_card.name
124
+ post[:C_cardnumber] = credit_card.number
125
+ post[:C_exp] = expdate(credit_card)
126
+ post[:C_cvv] = credit_card.verification_value if credit_card.verification_value?
127
+ end
128
+
129
+ def add_check(post, check)
130
+ post[:C_first_name] = check.first_name
131
+ post[:C_last_name] = check.last_name
132
+ post[:C_rte] = check.routing_number
133
+ post[:C_acct] = check.account_number
134
+ post[:C_check_number] = check.number
135
+ post[:C_acct_type] = account_type(check)
136
+ end
137
+
138
+ def add_check_customer_data(post, options)
139
+ # Required  Customer Type – (NACHA Transaction Class)
140
+ # CCD for Commercial, Merchant Initiated
141
+ # PPD for Personal, Merchant Initiated
142
+ # WEB for Internet, Consumer Initiated
143
+ # RCK for Returned Checks
144
+ # ARC for Account Receivable Entry
145
+ # TEL for TelephoneInitiated
146
+ post[:C_customer_type] = 'WEB'
147
+
148
+ # Optional  10  Digit Originator  ID – Assigned  By for  each transaction  class  or  business  purpose. If  not provided, the default Originator ID for the specific  Customer Type will be applied. 
149
+ post[:C_originator_id] = options[:originator_id]
150
+
151
+ # Optional  Transaction Addenda
152
+ post[:T_addenda] = options[:addenda]
153
+
154
+ # Required  Check  Writer  Social  Security  Number  (  Numbers Only, No Dashes ) 
155
+ post[:C_ssn] = options[:ssn].to_s.gsub(/[^\d]/, '')
156
+
157
+ post[:C_dl_state_code] = options[:drivers_license_state]
158
+ post[:C_dl_number] = options[:drivers_license_number]
159
+ post[:C_dob] = format_birth_date(options[:date_of_birth])
160
+ end
161
+
162
+ def format_birth_date(date)
163
+ date.respond_to?(:strftime) ? date.strftime('%m/%d/%Y') : date
164
+ end
165
+
166
+ # DDA for Checking
167
+ # SAV for Savings 
168
+ def account_type(check)
169
+ case check.account_type
170
+ when 'checking' then 'DDA'
171
+ when 'savings' then 'SAV'
172
+ else raise ArgumentError, "Unknown account type #{check.account_type}"
173
+ end
174
+ end
175
+
176
+ def parse(data, source)
177
+ source == SOURCE_ECHECK ? parse_check(data) : parse_credit_card(data)
178
+ end
179
+
180
+ def parse_check(data)
181
+ response = {}
182
+ response[:success] = data[1, 1]
183
+ response[:code] = data[2, 6].strip
184
+ response[:message] = data[8, 32].strip
185
+ response[:risk] = data[40, 2]
186
+ response[:reference] = data[42, 10]
187
+
188
+ extra_data = data[53...-1].split("\034")
189
+ response[:order_number] = extra_data[0]
190
+ response[:authentication_indicator] = extra_data[1]
191
+ response[:authentication_disclosure] = extra_data[2]
192
+ response
193
+ end
194
+
195
+ def parse_credit_card(data)
196
+ response = {}
197
+ response[:success] = data[1, 1]
198
+ response[:code] = data[2, 6]
199
+ response[:message] = data[8, 32].strip
200
+ response[:front_end] = data[40, 2]
201
+ response[:cvv_result] = data[42, 1]
202
+ response[:avs_result] = data[43, 1].strip
203
+ response[:risk] = data[44, 2]
204
+ response[:reference] = data[46, 10]
205
+
206
+ response[:order_number], response[:recurring] = data[57...-1].split("\034")
207
+ response
208
+ end
209
+
210
+ def add_invoice(post, options)
211
+ post[:T_ordernum] = (options[:order_id] || generate_unique_id).slice(0, 20)
212
+ post[:T_tax] = amount(options[:tax]) unless empty?(options[:tax])
213
+ post[:T_shipping] = amount(options[:shipping]) unless empty?(options[:shipping])
214
+ end
215
+
216
+ def add_reference(post, reference)
217
+ ref, _ = reference.to_s.split(';')
218
+ post[:T_reference] = ref
219
+ end
220
+
221
+ def add_amount(post, money)
222
+ post[:T_amt] = amount(money)
223
+ end
224
+
225
+ def add_customer_data(post, options)
226
+ post[:T_customer_number] = options[:customer] if Float(options[:customer]) rescue nil
227
+ end
228
+
229
+ def add_addresses(post, options)
230
+ billing_address = options[:billing_address] || options[:address] || {}
231
+
232
+ post[:C_address] = billing_address[:address1]
233
+ post[:C_city] = billing_address[:city]
234
+ post[:C_state] = empty?(billing_address[:state]) ? 'Outside of US' : billing_address[:state]
235
+ post[:C_zip] = billing_address[:zip]
236
+ post[:C_country] = billing_address[:country]
237
+ post[:C_telephone] = billing_address[:phone]
238
+ post[:C_fax] = billing_address[:fax]
239
+ post[:C_email] = options[:email]
240
+
241
+ if shipping_address = options[:shipping_address]
242
+ post[:C_ship_name] = shipping_address[:name]
243
+ post[:C_ship_address] = shipping_address[:address1]
244
+ post[:C_ship_city] = shipping_address[:city]
245
+ post[:C_ship_state] = shipping_address[:state]
246
+ post[:C_ship_zip] = shipping_address[:zip]
247
+ post[:C_ship_country] = shipping_address[:country]
248
+ end
249
+ end
250
+
251
+ def add_transaction_data(post, money, options)
252
+ add_amount(post, money)
253
+ add_invoice(post, options)
254
+ add_addresses(post, options)
255
+ add_customer_data(post, options)
256
+ end
257
+
258
+ def commit(action, params, source)
259
+ url = url(params, source)
260
+ response = parse(ssl_post(url, post_data(action, params)), source)
261
+
262
+ Response.new(success?(response), response[:message], response,
263
+ :test => test?,
264
+ :authorization => authorization_from(response, source),
265
+ :avs_result => { :code => response[:avs_result] },
266
+ :cvv_result => response[:cvv_result]
267
+ )
268
+ end
269
+
270
+ def url(params, source)
271
+ if source == SOURCE_ECHECK
272
+ "#{live_url}/eftVirtualCheck.dll?transaction"
273
+ else
274
+ "#{live_url}/eftBankcard.dll?transaction"
275
+ end
276
+ end
277
+
278
+ def authorization_from(response, source)
279
+ "#{response[:reference]};#{source}"
280
+ end
281
+
282
+ def success?(response)
283
+ response[:success] == 'A'
284
+ end
285
+
286
+ def post_data(action, params = {})
287
+ params[:M_id] = @options[:login]
288
+ params[:M_key] = @options[:password]
289
+ params[:T_code] = TRANSACTIONS[action]
290
+
291
+ params.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
292
+ end
293
+
294
+ def vault
295
+ @vault ||= SageVault.new(@options, self)
296
+ end
297
+
298
+ class SageVault
299
+
300
+ def initialize(options, gateway)
301
+ @live_url = 'https://www.sagepayments.net/web_services/wsVault/wsVault.asmx'
302
+ @options = options
303
+ @gateway = gateway
304
+ end
305
+
306
+ def store(credit_card, options = {})
307
+ request = build_store_request(credit_card, options)
308
+ commit(:store, request)
309
+ end
310
+
311
+ def unstore(identification, options = {})
312
+ request = build_unstore_request(identification, options)
313
+ commit(:unstore, request)
314
+ end
315
+
316
+ private
317
+
318
+ # A valid request example, since the Sage docs have none:
319
+ #
320
+ # <?xml version="1.0" encoding="UTF-8" ?>
321
+ # <SOAP-ENV:Envelope
322
+ # xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
323
+ # xmlns:ns1="https://www.sagepayments.net/web_services/wsVault/wsVault">
324
+ # <SOAP-ENV:Body>
325
+ # <ns1:INSERT_CREDIT_CARD_DATA>
326
+ # <ns1:M_ID>279277516172</ns1:M_ID>
327
+ # <ns1:M_KEY>O3I8G2H8V6A3</ns1:M_KEY>
328
+ # <ns1:CARDNUMBER>4111111111111111</ns1:CARDNUMBER>
329
+ # <ns1:EXPIRATION_DATE>0915</ns1:EXPIRATION_DATE>
330
+ # </ns1:INSERT_CREDIT_CARD_DATA>
331
+ # </SOAP-ENV:Body>
332
+ # </SOAP-ENV:Envelope>
333
+ def build_store_request(credit_card, options)
334
+ xml = Builder::XmlMarkup.new
335
+ add_credit_card(xml, credit_card, options)
336
+ xml.target!
337
+ end
338
+
339
+ def build_unstore_request(identification, options)
340
+ xml = Builder::XmlMarkup.new
341
+ add_identification(xml, identification, options)
342
+ xml.target!
343
+ end
344
+
345
+ def add_customer_data(xml)
346
+ xml.tag! 'ns1:M_ID', @options[:login]
347
+ xml.tag! 'ns1:M_KEY', @options[:password]
348
+ end
349
+
350
+ def add_credit_card(xml, credit_card, options)
351
+ xml.tag! 'ns1:CARDNUMBER', credit_card.number
352
+ xml.tag! 'ns1:EXPIRATION_DATE', exp_date(credit_card)
353
+ end
354
+
355
+ def add_identification(xml, identification, options)
356
+ xml.tag! 'ns1:GUID', identification
357
+ end
358
+
359
+ def exp_date(credit_card)
360
+ year = sprintf('%.4i', credit_card.year)
361
+ month = sprintf('%.2i', credit_card.month)
362
+
363
+ "#{month}#{year[-2..-1]}"
364
+ end
365
+
366
+ def commit(action, request)
367
+ response = parse(
368
+ @gateway.ssl_post(
369
+ @live_url,
370
+ build_soap_request(action, request),
371
+ build_headers(action)
372
+ )
373
+ )
374
+
375
+ case action
376
+ when :store
377
+ success = response[:success] == 'true'
378
+ message = response[:message].downcase.capitalize if response[:message]
379
+ when :unstore
380
+ success = response[:delete_data_result] == 'true'
381
+ message = success ? 'Succeeded' : 'Failed'
382
+ end
383
+
384
+ Response.new(success, message, response,
385
+ authorization: response[:guid]
386
+ )
387
+ end
388
+
389
+ ENVELOPE_NAMESPACES = {
390
+ 'xmlns:SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/',
391
+ 'xmlns:ns1' => 'https://www.sagepayments.net/web_services/wsVault/wsVault'
392
+ }
393
+
394
+ ACTION_ELEMENTS = {
395
+ store: 'INSERT_CREDIT_CARD_DATA',
396
+ unstore: 'DELETE_DATA'
397
+ }
398
+
399
+ def build_soap_request(action, body)
400
+ xml = Builder::XmlMarkup.new
401
+
402
+ xml.instruct!
403
+ xml.tag! 'SOAP-ENV:Envelope', ENVELOPE_NAMESPACES do
404
+ xml.tag! 'SOAP-ENV:Body' do
405
+ xml.tag! "ns1:#{ACTION_ELEMENTS[action]}" do
406
+ add_customer_data(xml)
407
+ xml << body
408
+ end
409
+ end
410
+ end
411
+ xml.target!
412
+ end
413
+
414
+ SOAP_ACTIONS = {
415
+ store: 'https://www.sagepayments.net/web_services/wsVault/wsVault/INSERT_CREDIT_CARD_DATA',
416
+ unstore: 'https://www.sagepayments.net/web_services/wsVault/wsVault/DELETE_DATA'
417
+ }
418
+
419
+ def build_headers(action)
420
+ {
421
+ 'SOAPAction' => SOAP_ACTIONS[action],
422
+ 'Content-Type' => 'text/xml; charset=utf-8'
423
+ }
424
+ end
425
+
426
+ def parse(body)
427
+ response = {}
428
+ hashify_xml!(body, response)
429
+ response
430
+ end
431
+
432
+ def hashify_xml!(xml, response)
433
+ xml = REXML::Document.new(xml)
434
+
435
+ # Store
436
+ xml.elements.each('//Table1/*') do |node|
437
+ response[node.name.underscore.to_sym] = node.text
438
+ end
439
+
440
+ # Unstore
441
+ xml.elements.each('//DELETE_DATAResponse/*') do |node|
442
+ response[node.name.underscore.to_sym] = node.text
443
+ end
444
+ end
445
+ end
446
+ end
447
+ end
448
+ end
@@ -0,0 +1,434 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class SagePayGateway < Gateway
4
+ cattr_accessor :simulate
5
+ self.simulate = false
6
+
7
+ class_attribute :simulator_url
8
+
9
+ self.test_url = 'https://test.sagepay.com/gateway/service'
10
+ self.live_url = 'https://live.sagepay.com/gateway/service'
11
+ self.simulator_url = 'https://test.sagepay.com/Simulator'
12
+
13
+ APPROVED = 'OK'
14
+
15
+ TRANSACTIONS = {
16
+ :purchase => 'PAYMENT',
17
+ :credit => 'REFUND',
18
+ :authorization => 'DEFERRED',
19
+ :capture => 'RELEASE',
20
+ :void => 'VOID',
21
+ :abort => 'ABORT',
22
+ :store => 'TOKEN',
23
+ :unstore => 'REMOVETOKEN',
24
+ :repeat => 'REPEAT'
25
+ }
26
+
27
+ CREDIT_CARDS = {
28
+ :visa => 'VISA',
29
+ :master => 'MC',
30
+ :delta => 'DELTA',
31
+ :maestro => 'MAESTRO',
32
+ :american_express => 'AMEX',
33
+ :electron => 'UKE',
34
+ :diners_club => 'DC',
35
+ :jcb => 'JCB'
36
+ }
37
+
38
+ AVS_CODE = {
39
+ 'NOTPROVIDED' => nil,
40
+ 'NOTCHECKED' => 'X',
41
+ 'MATCHED' => 'Y',
42
+ 'NOTMATCHED' => 'N'
43
+ }
44
+
45
+ CVV_CODE = {
46
+ 'NOTPROVIDED' => 'S',
47
+ 'NOTCHECKED' => 'X',
48
+ 'MATCHED' => 'M',
49
+ 'NOTMATCHED' => 'N'
50
+ }
51
+
52
+ OPTIONAL_REQUEST_FIELDS = {
53
+ paypal_callback_url: :PayPalCallbackURL,
54
+ basket: :Basket,
55
+ gift_aid_payment: :GiftAidPayment,
56
+ apply_avscv2: :ApplyAVSCV2,
57
+ apply_3d_secure: :Apply3DSecure,
58
+ account_type: :AccountType,
59
+ billing_agreement: :BillingAgreement,
60
+ basket_xml: :BasketXML,
61
+ customer_xml: :CustomerXML,
62
+ surcharge_xml: :SurchargeXML,
63
+ vendor_data: :VendorData,
64
+ language: :Language,
65
+ website: :Website,
66
+ recipient_account_number: :FIRecipientAcctNumber,
67
+ recipient_surname: :FIRecipientSurname,
68
+ recipient_postcode: :FIRecipientPostcode,
69
+ recipient_dob: :FIRecipientDoB
70
+ }
71
+
72
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :jcb, :maestro, :diners_club]
73
+ self.supported_countries = ['GB', 'IE']
74
+ self.default_currency = 'GBP'
75
+
76
+ self.homepage_url = 'http://www.sagepay.com'
77
+ self.display_name = 'SagePay'
78
+
79
+ def initialize(options = {})
80
+ requires!(options, :login)
81
+ super
82
+ end
83
+
84
+ def purchase(money, payment_method, options = {})
85
+ requires!(options, :order_id)
86
+
87
+ post = {}
88
+
89
+ add_amount(post, money, options)
90
+ add_invoice(post, options)
91
+ add_payment_method(post, payment_method, options)
92
+ add_address(post, options)
93
+ add_customer_data(post, options)
94
+ add_optional_data(post, options)
95
+
96
+ commit((past_purchase_reference?(payment_method) ? :repeat : :purchase), post)
97
+ end
98
+
99
+ def authorize(money, payment_method, options = {})
100
+ requires!(options, :order_id)
101
+
102
+ post = {}
103
+
104
+ add_amount(post, money, options)
105
+ add_invoice(post, options)
106
+ add_payment_method(post, payment_method, options)
107
+ add_address(post, options)
108
+ add_customer_data(post, options)
109
+ add_optional_data(post, options)
110
+
111
+ commit(:authorization, post)
112
+ end
113
+
114
+ # You can only capture a transaction once, even if you didn't capture the full amount the first time.
115
+ def capture(money, identification, options = {})
116
+ post = {}
117
+
118
+ add_reference(post, identification)
119
+ add_release_amount(post, money, options)
120
+
121
+ commit(:capture, post)
122
+ end
123
+
124
+ def void(identification, options = {})
125
+ post = {}
126
+
127
+ add_reference(post, identification)
128
+ action = abort_or_void_from(identification)
129
+
130
+ commit(action, post)
131
+ end
132
+
133
+ # Refunding requires a new order_id to passed in, as well as a description
134
+ def refund(money, identification, options = {})
135
+ requires!(options, :order_id, :description)
136
+
137
+ post = {}
138
+
139
+ add_related_reference(post, identification)
140
+ add_amount(post, money, options)
141
+ add_invoice(post, options)
142
+
143
+ commit(:credit, post)
144
+ end
145
+
146
+ def credit(money, identification, options = {})
147
+ ActiveMerchant.deprecated CREDIT_DEPRECATION_MESSAGE
148
+ refund(money, identification, options)
149
+ end
150
+
151
+ def store(credit_card, options = {})
152
+ post = {}
153
+ add_credit_card(post, credit_card)
154
+ add_currency(post, 0, options)
155
+
156
+ commit(:store, post)
157
+ end
158
+
159
+ def unstore(token, options = {})
160
+ post = {}
161
+ add_token(post, token)
162
+ commit(:unstore, post)
163
+ end
164
+
165
+ def verify(credit_card, options={})
166
+ MultiResponse.run(:use_first_response) do |r|
167
+ r.process { authorize(100, credit_card, options) }
168
+ r.process(:ignore_result) { void(r.authorization, options) }
169
+ end
170
+ end
171
+
172
+ def supports_scrubbing
173
+ true
174
+ end
175
+
176
+ def scrub(transcript)
177
+ transcript.
178
+ gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
179
+ gsub(%r((&?CardNumber=)\d+(&?)), '\1[FILTERED]\2').
180
+ gsub(%r((&?CV2=)\d+(&?)), '\1[FILTERED]\2')
181
+ end
182
+
183
+ private
184
+
185
+ def truncate(value, max_size)
186
+ return nil unless value
187
+ return value.to_s if CGI.escape(value.to_s).length <= max_size
188
+
189
+ if value.size > max_size
190
+ truncate(super(value, max_size), max_size)
191
+ else
192
+ truncate(value.to_s.chop, max_size)
193
+ end
194
+ end
195
+
196
+ def add_reference(post, identification)
197
+ order_id, transaction_id, authorization, security_key = identification.split(';')
198
+
199
+ add_pair(post, :VendorTxCode, order_id)
200
+ add_pair(post, :VPSTxId, transaction_id)
201
+ add_pair(post, :TxAuthNo, authorization)
202
+ add_pair(post, :SecurityKey, security_key)
203
+ end
204
+
205
+ def add_related_reference(post, identification)
206
+ order_id, transaction_id, authorization, security_key = identification.split(';')
207
+
208
+ add_pair(post, :RelatedVendorTxCode, order_id)
209
+ add_pair(post, :RelatedVPSTxId, transaction_id)
210
+ add_pair(post, :RelatedTxAuthNo, authorization)
211
+ add_pair(post, :RelatedSecurityKey, security_key)
212
+ end
213
+
214
+ def add_amount(post, money, options)
215
+ currency = options[:currency] || currency(money)
216
+ add_pair(post, :Amount, localized_amount(money, currency), :required => true)
217
+ add_pair(post, :Currency, currency, :required => true)
218
+ end
219
+
220
+ def add_currency(post, money, options)
221
+ currency = options[:currency] || currency(money)
222
+ add_pair(post, :Currency, currency, :required => true)
223
+ end
224
+
225
+ # doesn't actually use the currency -- dodgy!
226
+ def add_release_amount(post, money, options)
227
+ add_pair(post, :ReleaseAmount, amount(money), :required => true)
228
+ end
229
+
230
+ def add_customer_data(post, options)
231
+ add_pair(post, :CustomerEMail, truncate(options[:email], 255)) unless options[:email].blank?
232
+ add_pair(post, :ClientIPAddress, options[:ip])
233
+ end
234
+
235
+ def add_optional_data(post, options)
236
+ add_pair(post, :CreateToken, 1) unless options[:store].blank?
237
+
238
+ OPTIONAL_REQUEST_FIELDS.each do |gateway_option, sagepay_field|
239
+ add_pair(post, sagepay_field, options[gateway_option])
240
+ end
241
+ end
242
+
243
+ def add_address(post, options)
244
+ if billing_address = options[:billing_address] || options[:address]
245
+ first_name, last_name = split_names(billing_address[:name])
246
+ add_pair(post, :BillingSurname, truncate(last_name, 20))
247
+ add_pair(post, :BillingFirstnames, truncate(first_name, 20))
248
+ add_pair(post, :BillingAddress1, truncate(billing_address[:address1], 100))
249
+ add_pair(post, :BillingAddress2, truncate(billing_address[:address2], 100))
250
+ add_pair(post, :BillingCity, truncate(billing_address[:city], 40))
251
+ add_pair(post, :BillingState, truncate(billing_address[:state], 2)) if is_usa(billing_address[:country])
252
+ add_pair(post, :BillingCountry, truncate(billing_address[:country], 2))
253
+ add_pair(post, :BillingPhone, sanitize_phone(billing_address[:phone]))
254
+ add_pair(post, :BillingPostCode, truncate(billing_address[:zip], 10))
255
+ end
256
+
257
+ if shipping_address = options[:shipping_address] || billing_address
258
+ first_name, last_name = split_names(shipping_address[:name])
259
+ add_pair(post, :DeliverySurname, truncate(last_name, 20))
260
+ add_pair(post, :DeliveryFirstnames, truncate(first_name, 20))
261
+ add_pair(post, :DeliveryAddress1, truncate(shipping_address[:address1], 100))
262
+ add_pair(post, :DeliveryAddress2, truncate(shipping_address[:address2], 100))
263
+ add_pair(post, :DeliveryCity, truncate(shipping_address[:city], 40))
264
+ add_pair(post, :DeliveryState, truncate(shipping_address[:state], 2)) if is_usa(shipping_address[:country])
265
+ add_pair(post, :DeliveryCountry, truncate(shipping_address[:country], 2))
266
+ add_pair(post, :DeliveryPhone, sanitize_phone(shipping_address[:phone]))
267
+ add_pair(post, :DeliveryPostCode, truncate(shipping_address[:zip], 10))
268
+ end
269
+ end
270
+
271
+ def add_invoice(post, options)
272
+ add_pair(post, :VendorTxCode, sanitize_order_id(options[:order_id]), :required => true)
273
+ add_pair(post, :Description, truncate(options[:description] || options[:order_id], 100))
274
+ end
275
+
276
+ def add_payment_method(post, payment_method, options)
277
+ if payment_method.is_a?(String)
278
+ if past_purchase_reference?(payment_method)
279
+ add_related_reference(post, payment_method)
280
+ else
281
+ add_token_details(post, payment_method, options)
282
+ end
283
+ else
284
+ add_credit_card(post, payment_method)
285
+ end
286
+ end
287
+
288
+ def add_credit_card(post, credit_card)
289
+ add_pair(post, :CardHolder, truncate(credit_card.name, 50), :required => true)
290
+ add_pair(post, :CardNumber, credit_card.number, :required => true)
291
+
292
+ add_pair(post, :ExpiryDate, format_date(credit_card.month, credit_card.year), :required => true)
293
+ add_pair(post, :CardType, map_card_type(credit_card))
294
+
295
+ add_pair(post, :CV2, credit_card.verification_value)
296
+ end
297
+
298
+ def add_token_details(post, token, options)
299
+ add_token(post, token)
300
+ add_pair(post, :StoreToken, options[:customer])
301
+ add_pair(post, :CV2, options[:verification_value])
302
+ end
303
+
304
+ def add_token(post, token)
305
+ add_pair(post, :Token, token)
306
+ end
307
+
308
+ def sanitize_order_id(order_id)
309
+ cleansed = order_id.to_s.gsub(/[^-a-zA-Z0-9._]/, '')
310
+ truncate(cleansed, 40)
311
+ end
312
+
313
+ def sanitize_phone(phone)
314
+ return nil unless phone
315
+ cleansed = phone.to_s.gsub(/[^0-9+]/, '')
316
+ truncate(cleansed, 20)
317
+ end
318
+
319
+ def is_usa(country)
320
+ truncate(country, 2) == 'US'
321
+ end
322
+
323
+ def map_card_type(credit_card)
324
+ raise ArgumentError, 'The credit card type must be provided' if card_brand(credit_card).blank?
325
+
326
+ card_type = card_brand(credit_card).to_sym
327
+
328
+ if card_type == :visa && credit_card.electron?
329
+ CREDIT_CARDS[:electron]
330
+ else
331
+ CREDIT_CARDS[card_type]
332
+ end
333
+ end
334
+
335
+ # MMYY format
336
+ def format_date(month, year)
337
+ return nil if year.blank? || month.blank?
338
+
339
+ year = sprintf('%.4i', year)
340
+ month = sprintf('%.2i', month)
341
+
342
+ "#{month}#{year[-2..-1]}"
343
+ end
344
+
345
+ def commit(action, parameters)
346
+ response = parse(ssl_post(url_for(action), post_data(action, parameters)))
347
+
348
+ Response.new(response['Status'] == APPROVED, message_from(response), response,
349
+ :test => test?,
350
+ :authorization => authorization_from(response, parameters, action),
351
+ :avs_result => {
352
+ :street_match => AVS_CODE[response['AddressResult']],
353
+ :postal_match => AVS_CODE[response['PostCodeResult']],
354
+ },
355
+ :cvv_result => CVV_CODE[response['CV2Result']]
356
+ )
357
+ end
358
+
359
+ def authorization_from(response, params, action)
360
+ case action
361
+ when :store
362
+ response['Token']
363
+ else
364
+ [ params[:VendorTxCode],
365
+ response['VPSTxId'] || params[:VPSTxId],
366
+ response['TxAuthNo'],
367
+ response['SecurityKey'] || params[:SecurityKey],
368
+ action ].join(';')
369
+ end
370
+ end
371
+
372
+ def abort_or_void_from(identification)
373
+ original_transaction = identification.split(';').last
374
+ original_transaction == 'authorization' ? :abort : :void
375
+ end
376
+
377
+ def url_for(action)
378
+ simulate ? build_simulator_url(action) : build_url(action)
379
+ end
380
+
381
+ def build_url(action)
382
+ endpoint = case action
383
+ when :purchase, :authorization then 'vspdirect-register'
384
+ when :store then 'directtoken'
385
+ else TRANSACTIONS[action].downcase
386
+ end
387
+ "#{test? ? self.test_url : self.live_url}/#{endpoint}.vsp"
388
+ end
389
+
390
+ def build_simulator_url(action)
391
+ endpoint = [ :purchase, :authorization ].include?(action) ? 'VSPDirectGateway.asp' : "VSPServerGateway.asp?Service=Vendor#{TRANSACTIONS[action].capitalize}Tx"
392
+ "#{self.simulator_url}/#{endpoint}"
393
+ end
394
+
395
+ def message_from(response)
396
+ response['Status'] == APPROVED ? 'Success' : (response['StatusDetail'] || 'Unspecified error') # simonr 20080207 can't actually get non-nil blanks, so this is shorter
397
+ end
398
+
399
+ def post_data(action, parameters = {})
400
+ parameters.update(
401
+ :Vendor => @options[:login],
402
+ :TxType => TRANSACTIONS[action],
403
+ :VPSProtocol => @options.fetch(:protocol_version, '3.00')
404
+ )
405
+
406
+ if(application_id && (application_id != Gateway.application_id))
407
+ parameters.update(:ReferrerID => application_id)
408
+ end
409
+
410
+ parameters.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
411
+ end
412
+
413
+ # SagePay returns data in the following format
414
+ # Key1=value1
415
+ # Key2=value2
416
+ def parse(body)
417
+ result = {}
418
+ body.to_s.each_line do |pair|
419
+ result[$1] = $2 if pair.strip =~ /\A([^=]+)=(.+)\Z/im
420
+ end
421
+ result
422
+ end
423
+
424
+ def add_pair(post, key, value, options = {})
425
+ post[key] = value if !value.blank? || options[:required]
426
+ end
427
+
428
+ def past_purchase_reference?(payment_method)
429
+ return false unless payment_method.is_a?(String)
430
+ payment_method.split(';').last == 'purchase'
431
+ end
432
+ end
433
+ end
434
+ end