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,374 @@
1
+ require 'nokogiri'
2
+ require 'digest/sha1'
3
+
4
+ module ActiveMerchant
5
+ module Billing
6
+ # Realex is the leading CC gateway in Ireland
7
+ # see http://www.realexpayments.com
8
+ # Contributed by John Ward (john@ward.name)
9
+ # see http://thinedgeofthewedge.blogspot.com
10
+ #
11
+ # Realex works using the following
12
+ # login - The unique id of the merchant
13
+ # password - The secret is used to digitally sign the request
14
+ # account - This is an optional third part of the authentication process
15
+ # and is used if the merchant wishes do distinguish cc traffic from the different sources
16
+ # by using a different account. This must be created in advance
17
+ #
18
+ # the Realex team decided to make the orderid unique per request,
19
+ # so if validation fails you can not correct and resend using the
20
+ # same order id
21
+ class RealexGateway < Gateway
22
+ self.live_url = self.test_url = 'https://epage.payandshop.com/epage-remote.cgi'
23
+
24
+ CARD_MAPPING = {
25
+ 'master' => 'MC',
26
+ 'visa' => 'VISA',
27
+ 'american_express' => 'AMEX',
28
+ 'diners_club' => 'DINERS',
29
+ 'maestro' => 'MC'
30
+ }
31
+
32
+ self.money_format = :cents
33
+ self.default_currency = 'EUR'
34
+ self.supported_cardtypes = [ :visa, :master, :american_express, :diners_club ]
35
+ self.supported_countries = %w(IE GB FR BE NL LU IT US CA ES)
36
+ self.homepage_url = 'http://www.realexpayments.com/'
37
+ self.display_name = 'Realex'
38
+
39
+ SUCCESS, DECLINED = 'Successful', 'Declined'
40
+ BANK_ERROR = REALEX_ERROR = 'Gateway is in maintenance. Please try again later.'
41
+ ERROR = CLIENT_DEACTIVATED = 'Gateway Error'
42
+
43
+ def initialize(options = {})
44
+ requires!(options, :login, :password)
45
+ options[:refund_hash] = Digest::SHA1.hexdigest(options[:rebate_secret]) if options[:rebate_secret].present?
46
+ options[:credit_hash] = Digest::SHA1.hexdigest(options[:refund_secret]) if options[:refund_secret].present?
47
+ super
48
+ end
49
+
50
+ def purchase(money, credit_card, options = {})
51
+ requires!(options, :order_id)
52
+
53
+ request = build_purchase_or_authorization_request(:purchase, money, credit_card, options)
54
+ commit(request)
55
+ end
56
+
57
+ def authorize(money, creditcard, options = {})
58
+ requires!(options, :order_id)
59
+
60
+ request = build_purchase_or_authorization_request(:authorization, money, creditcard, options)
61
+ commit(request)
62
+ end
63
+
64
+ def capture(money, authorization, options = {})
65
+ request = build_capture_request(money, authorization, options)
66
+ commit(request)
67
+ end
68
+
69
+ def refund(money, authorization, options = {})
70
+ request = build_refund_request(money, authorization, options)
71
+ commit(request)
72
+ end
73
+
74
+ def credit(money, creditcard, options = {})
75
+ request = build_credit_request(money, creditcard, options)
76
+ commit(request)
77
+ end
78
+
79
+ def void(authorization, options = {})
80
+ request = build_void_request(authorization, options)
81
+ commit(request)
82
+ end
83
+
84
+ def verify(credit_card, options = {})
85
+ requires!(options, :order_id)
86
+
87
+ request = build_verify_request(credit_card, options)
88
+ commit(request)
89
+ end
90
+
91
+ def supports_scrubbing
92
+ true
93
+ end
94
+
95
+ def scrub(transcript)
96
+ transcript.
97
+ gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
98
+ gsub(%r((<number>)\d+(</number>))i, '\1[FILTERED]\2')
99
+ end
100
+
101
+ private
102
+
103
+ def commit(request)
104
+ response = parse(ssl_post(self.live_url, request))
105
+
106
+ Response.new(
107
+ (response[:result] == '00'),
108
+ message_from(response),
109
+ response,
110
+ :test => (response[:message] =~ %r{\[ test system \]}),
111
+ :authorization => authorization_from(response),
112
+ avs_result: AVSResult.new(code: response[:avspostcoderesponse]),
113
+ cvv_result: CVVResult.new(response[:cvnresult])
114
+ )
115
+ end
116
+
117
+ def parse(xml)
118
+ response = {}
119
+
120
+ doc = Nokogiri::XML(xml)
121
+ doc.xpath('//response/*').each do |node|
122
+ if node.elements.size == 0
123
+ response[node.name.downcase.to_sym] = normalize(node.text)
124
+ else
125
+ node.elements.each do |childnode|
126
+ name = "#{node.name.downcase}_#{childnode.name.downcase}"
127
+ response[name.to_sym] = normalize(childnode.text)
128
+ end
129
+ end
130
+ end unless doc.root.nil?
131
+
132
+ response
133
+ end
134
+
135
+ def authorization_from(parsed)
136
+ [parsed[:orderid], parsed[:pasref], parsed[:authcode]].join(';')
137
+ end
138
+
139
+ def build_purchase_or_authorization_request(action, money, credit_card, options)
140
+ timestamp = new_timestamp
141
+ xml = Builder::XmlMarkup.new :indent => 2
142
+ xml.tag! 'request', 'timestamp' => timestamp, 'type' => 'auth' do
143
+ add_merchant_details(xml, options)
144
+ xml.tag! 'orderid', sanitize_order_id(options[:order_id])
145
+ add_amount(xml, money, options)
146
+ add_card(xml, credit_card)
147
+ xml.tag! 'autosettle', 'flag' => auto_settle_flag(action)
148
+ add_signed_digest(xml, timestamp, @options[:login], sanitize_order_id(options[:order_id]), amount(money), (options[:currency] || currency(money)), credit_card.number)
149
+ if credit_card.is_a?(NetworkTokenizationCreditCard)
150
+ add_network_tokenization_card(xml, credit_card)
151
+ else
152
+ add_three_d_secure(xml, options)
153
+ end
154
+ add_comments(xml, options)
155
+ add_address_and_customer_info(xml, options)
156
+ end
157
+ xml.target!
158
+ end
159
+
160
+ def build_capture_request(money, authorization, options)
161
+ timestamp = new_timestamp
162
+ xml = Builder::XmlMarkup.new :indent => 2
163
+ xml.tag! 'request', 'timestamp' => timestamp, 'type' => 'settle' do
164
+ add_merchant_details(xml, options)
165
+ add_amount(xml, money, options)
166
+ add_transaction_identifiers(xml, authorization, options)
167
+ add_comments(xml, options)
168
+ add_signed_digest(xml, timestamp, @options[:login], sanitize_order_id(options[:order_id]), amount(money), (options[:currency] || currency(money)), nil)
169
+ end
170
+ xml.target!
171
+ end
172
+
173
+ def build_refund_request(money, authorization, options)
174
+ timestamp = new_timestamp
175
+ xml = Builder::XmlMarkup.new :indent => 2
176
+ xml.tag! 'request', 'timestamp' => timestamp, 'type' => 'rebate' do
177
+ add_merchant_details(xml, options)
178
+ add_transaction_identifiers(xml, authorization, options)
179
+ xml.tag! 'amount', amount(money), 'currency' => options[:currency] || currency(money)
180
+ xml.tag! 'refundhash', @options[:refund_hash] if @options[:refund_hash]
181
+ xml.tag! 'autosettle', 'flag' => 1
182
+ add_comments(xml, options)
183
+ add_signed_digest(xml, timestamp, @options[:login], sanitize_order_id(options[:order_id]), amount(money), (options[:currency] || currency(money)), nil)
184
+ end
185
+ xml.target!
186
+ end
187
+
188
+ def build_credit_request(money, credit_card, options)
189
+ timestamp = new_timestamp
190
+ xml = Builder::XmlMarkup.new :indent => 2
191
+ xml.tag! 'request', 'timestamp' => timestamp, 'type' => 'credit' do
192
+ add_merchant_details(xml, options)
193
+ xml.tag! 'orderid', sanitize_order_id(options[:order_id])
194
+ add_amount(xml, money, options)
195
+ add_card(xml, credit_card)
196
+ xml.tag! 'refundhash', @options[:credit_hash] if @options[:credit_hash]
197
+ xml.tag! 'autosettle', 'flag' => 1
198
+ add_comments(xml, options)
199
+ add_signed_digest(xml, timestamp, @options[:login], sanitize_order_id(options[:order_id]), amount(money), (options[:currency] || currency(money)), credit_card.number)
200
+ end
201
+ xml.target!
202
+ end
203
+
204
+ def build_void_request(authorization, options)
205
+ timestamp = new_timestamp
206
+ xml = Builder::XmlMarkup.new :indent => 2
207
+ xml.tag! 'request', 'timestamp' => timestamp, 'type' => 'void' do
208
+ add_merchant_details(xml, options)
209
+ add_transaction_identifiers(xml, authorization, options)
210
+ add_comments(xml, options)
211
+ add_signed_digest(xml, timestamp, @options[:login], sanitize_order_id(options[:order_id]), nil, nil, nil)
212
+ end
213
+ xml.target!
214
+ end
215
+
216
+ # Verify initiates an OTB (Open To Buy) request
217
+ def build_verify_request(credit_card, options)
218
+ timestamp = new_timestamp
219
+ xml = Builder::XmlMarkup.new :indent => 2
220
+ xml.tag! 'request', 'timestamp' => timestamp, 'type' => 'otb' do
221
+ add_merchant_details(xml, options)
222
+ xml.tag! 'orderid', sanitize_order_id(options[:order_id])
223
+ add_card(xml, credit_card)
224
+ add_comments(xml, options)
225
+ add_signed_digest(xml, timestamp, @options[:login], sanitize_order_id(options[:order_id]), credit_card.number)
226
+ end
227
+ xml.target!
228
+ end
229
+
230
+ def add_address_and_customer_info(xml, options)
231
+ billing_address = options[:billing_address] || options[:address]
232
+ shipping_address = options[:shipping_address]
233
+
234
+ return unless billing_address || shipping_address || options[:customer] || options[:invoice] || options[:ip]
235
+
236
+ xml.tag! 'tssinfo' do
237
+ xml.tag! 'custnum', options[:customer] if options[:customer]
238
+ xml.tag! 'prodid', options[:invoice] if options[:invoice]
239
+ xml.tag! 'custipaddress', options[:ip] if options[:ip]
240
+
241
+ if billing_address
242
+ xml.tag! 'address', 'type' => 'billing' do
243
+ xml.tag! 'code', format_address_code(billing_address)
244
+ xml.tag! 'country', billing_address[:country]
245
+ end
246
+ end
247
+
248
+ if shipping_address
249
+ xml.tag! 'address', 'type' => 'shipping' do
250
+ xml.tag! 'code', format_address_code(shipping_address)
251
+ xml.tag! 'country', shipping_address[:country]
252
+ end
253
+ end
254
+ end
255
+ end
256
+
257
+ def add_merchant_details(xml, options)
258
+ xml.tag! 'merchantid', @options[:login]
259
+ if options[:account] || @options[:account]
260
+ xml.tag! 'account', (options[:account] || @options[:account])
261
+ end
262
+ end
263
+
264
+ def add_transaction_identifiers(xml, authorization, options)
265
+ options[:order_id], pasref, authcode = authorization.split(';')
266
+ xml.tag! 'orderid', sanitize_order_id(options[:order_id])
267
+ xml.tag! 'pasref', pasref
268
+ xml.tag! 'authcode', authcode
269
+ end
270
+
271
+ def add_comments(xml, options)
272
+ return unless options[:description]
273
+ xml.tag! 'comments' do
274
+ xml.tag! 'comment', options[:description], 'id' => 1
275
+ end
276
+ end
277
+
278
+ def add_amount(xml, money, options)
279
+ xml.tag! 'amount', amount(money), 'currency' => options[:currency] || currency(money)
280
+ end
281
+
282
+ def add_card(xml, credit_card)
283
+ xml.tag! 'card' do
284
+ xml.tag! 'number', credit_card.number
285
+ xml.tag! 'expdate', expiry_date(credit_card)
286
+ xml.tag! 'chname', credit_card.name
287
+ xml.tag! 'type', CARD_MAPPING[card_brand(credit_card).to_s]
288
+ xml.tag! 'issueno', ''
289
+ xml.tag! 'cvn' do
290
+ xml.tag! 'number', credit_card.verification_value
291
+ xml.tag! 'presind', (options['presind'] || (credit_card.verification_value? ? 1 : nil))
292
+ end
293
+ end
294
+ end
295
+
296
+ def add_network_tokenization_card(xml, payment)
297
+ xml.tag! 'mpi' do
298
+ xml.tag! 'cavv', payment.payment_cryptogram
299
+ xml.tag! 'eci', payment.eci
300
+ end
301
+ xml.tag! 'supplementarydata' do
302
+ xml.tag! 'item', 'type' => 'mobile' do
303
+ xml.tag! 'field01', payment.source.to_s.gsub('_', '-')
304
+ end
305
+ end
306
+ end
307
+
308
+ def add_three_d_secure(xml, options)
309
+ return unless three_d_secure = options[:three_d_secure]
310
+ version = three_d_secure.fetch(:version, '')
311
+ xml.tag! 'mpi' do
312
+ if version =~ /^2/
313
+ xml.tag! 'authentication_value', three_d_secure[:cavv]
314
+ xml.tag! 'ds_trans_id', three_d_secure[:ds_transaction_id]
315
+ else
316
+ xml.tag! 'cavv', three_d_secure[:cavv]
317
+ xml.tag! 'xid', three_d_secure[:xid]
318
+ end
319
+ xml.tag! 'eci', three_d_secure[:eci]
320
+ xml.tag! 'message_version', version
321
+ end
322
+ end
323
+
324
+ def format_address_code(address)
325
+ code = [address[:zip].to_s, address[:address1].to_s + address[:address2].to_s]
326
+ code.collect { |e| e.gsub(/\D/, '') }.reject(&:empty?).join('|')
327
+ end
328
+
329
+ def new_timestamp
330
+ Time.now.strftime('%Y%m%d%H%M%S')
331
+ end
332
+
333
+ def add_signed_digest(xml, *values)
334
+ string = Digest::SHA1.hexdigest(values.join('.'))
335
+ xml.tag! 'sha1hash', Digest::SHA1.hexdigest([string, @options[:password]].join('.'))
336
+ end
337
+
338
+ def auto_settle_flag(action)
339
+ action == :authorization ? '0' : '1'
340
+ end
341
+
342
+ def expiry_date(credit_card)
343
+ "#{format(credit_card.month, :two_digits)}#{format(credit_card.year, :two_digits)}"
344
+ end
345
+
346
+ def message_from(response)
347
+ case response[:result]
348
+ when '00'
349
+ SUCCESS
350
+ when '101'
351
+ response[:message]
352
+ when '102', '103'
353
+ DECLINED
354
+ when /^2[0-9][0-9]/
355
+ BANK_ERROR
356
+ when /^3[0-9][0-9]/
357
+ REALEX_ERROR
358
+ when /^5[0-9][0-9]/
359
+ response[:message]
360
+ when '600', '601', '603'
361
+ ERROR
362
+ when '666'
363
+ CLIENT_DEACTIVATED
364
+ else
365
+ DECLINED
366
+ end
367
+ end
368
+
369
+ def sanitize_order_id(order_id)
370
+ order_id.to_s.gsub(/[^a-zA-Z0-9\-_]/, '')
371
+ end
372
+ end
373
+ end
374
+ end
@@ -0,0 +1,534 @@
1
+ # coding: utf-8
2
+
3
+ require 'nokogiri'
4
+
5
+ module ActiveMerchant #:nodoc:
6
+ module Billing #:nodoc:
7
+ # = Redsys Merchant Gateway
8
+ #
9
+ # Gateway support for the Spanish "Redsys" payment gateway system. This is
10
+ # used by many banks in Spain and is particularly well supported by
11
+ # Catalunya Caixa's ecommerce department.
12
+ #
13
+ # Redsys requires an order_id be provided with each transaction and it must
14
+ # follow a specific format. The rules are as follows:
15
+ #
16
+ # * First 4 digits must be numerical
17
+ # * Remaining 8 digits may be alphanumeric
18
+ # * Max length: 12
19
+ #
20
+ # If an invalid order_id is provided, we do our best to clean it up.
21
+ #
22
+ # Much of the code for this library is based on the active_merchant_sermepa
23
+ # integration gateway which uses essentially the same API but with the
24
+ # banks own payment screen.
25
+ #
26
+ # Written by Samuel Lown for Cabify. For implementation questions, or
27
+ # test access details please get in touch: sam@cabify.com.
28
+ #
29
+ # *** SHA256 Authentication Update ***
30
+ #
31
+ # Redsys is dropping support for the SHA1 authentication method. This
32
+ # adapter has been updated to work with the new SHA256 authentication
33
+ # method, however in your initialization options hash you will need to
34
+ # specify the key/value :signature_algorithm => "sha256" to use the
35
+ # SHA256 method. Otherwise it will default to using the SHA1.
36
+ #
37
+ #
38
+ class RedsysGateway < Gateway
39
+ self.live_url = 'https://sis.sermepa.es/sis/operaciones'
40
+ self.test_url = 'https://sis-t.redsys.es:25443/sis/operaciones'
41
+
42
+ self.supported_countries = ['ES']
43
+ self.default_currency = 'EUR'
44
+ self.money_format = :cents
45
+
46
+ # Not all card types may be activated by the bank!
47
+ self.supported_cardtypes = [:visa, :master, :american_express, :jcb, :diners_club]
48
+ self.homepage_url = 'http://www.redsys.es/'
49
+ self.display_name = 'Redsys'
50
+
51
+ CURRENCY_CODES = {
52
+ 'AED' => '784',
53
+ 'ARS' => '32',
54
+ 'AUD' => '36',
55
+ 'BRL' => '986',
56
+ 'BOB' => '68',
57
+ 'CAD' => '124',
58
+ 'CHF' => '756',
59
+ 'CLP' => '152',
60
+ 'CNY' => '156',
61
+ 'COP' => '170',
62
+ 'CRC' => '188',
63
+ 'CZK' => '203',
64
+ 'DKK' => '208',
65
+ 'DOP' => '214',
66
+ 'EUR' => '978',
67
+ 'GBP' => '826',
68
+ 'GTQ' => '320',
69
+ 'HUF' => '348',
70
+ 'IDR' => '360',
71
+ 'INR' => '356',
72
+ 'JPY' => '392',
73
+ 'KRW' => '410',
74
+ 'MYR' => '458',
75
+ 'MXN' => '484',
76
+ 'NOK' => '578',
77
+ 'NZD' => '554',
78
+ 'PEN' => '604',
79
+ 'PLN' => '985',
80
+ 'RUB' => '643',
81
+ 'SAR' => '682',
82
+ 'SEK' => '752',
83
+ 'SGD' => '702',
84
+ 'THB' => '764',
85
+ 'TWD' => '901',
86
+ 'USD' => '840',
87
+ 'UYU' => '858'
88
+ }
89
+
90
+ # The set of supported transactions for this gateway.
91
+ # More operations are supported by the gateway itself, but
92
+ # are not supported in this library.
93
+ SUPPORTED_TRANSACTIONS = {
94
+ :purchase => 'A',
95
+ :authorize => '1',
96
+ :capture => '2',
97
+ :refund => '3',
98
+ :cancel => '9'
99
+ }
100
+
101
+ # These are the text meanings sent back by the acquirer when
102
+ # a card has been rejected. Syntax or general request errors
103
+ # are not covered here.
104
+ RESPONSE_TEXTS = {
105
+ 0 => 'Transaction Approved',
106
+ 400 => 'Cancellation Accepted',
107
+ 481 => 'Cancellation Accepted',
108
+ 500 => 'Reconciliation Accepted',
109
+ 900 => 'Refund / Confirmation approved',
110
+
111
+ 101 => 'Card expired',
112
+ 102 => 'Card blocked temporarily or under susciption of fraud',
113
+ 104 => 'Transaction not permitted',
114
+ 107 => 'Contact the card issuer',
115
+ 109 => 'Invalid identification by merchant or POS terminal',
116
+ 110 => 'Invalid amount',
117
+ 114 => 'Card cannot be used to the requested transaction',
118
+ 116 => 'Insufficient credit',
119
+ 118 => 'Non-registered card',
120
+ 125 => 'Card not effective',
121
+ 129 => 'CVV2/CVC2 Error',
122
+ 167 => 'Contact the card issuer: suspected fraud',
123
+ 180 => 'Card out of service',
124
+ 181 => 'Card with credit or debit restrictions',
125
+ 182 => 'Card with credit or debit restrictions',
126
+ 184 => 'Authentication error',
127
+ 190 => 'Refusal with no specific reason',
128
+ 191 => 'Expiry date incorrect',
129
+
130
+ 201 => 'Card expired',
131
+ 202 => 'Card blocked temporarily or under suspicion of fraud',
132
+ 204 => 'Transaction not permitted',
133
+ 207 => 'Contact the card issuer',
134
+ 208 => 'Lost or stolen card',
135
+ 209 => 'Lost or stolen card',
136
+ 280 => 'CVV2/CVC2 Error',
137
+ 290 => 'Declined with no specific reason',
138
+
139
+ 480 => 'Original transaction not located, or time-out exceeded',
140
+ 501 => 'Original transaction not located, or time-out exceeded',
141
+ 502 => 'Original transaction not located, or time-out exceeded',
142
+ 503 => 'Original transaction not located, or time-out exceeded',
143
+
144
+ 904 => 'Merchant not registered at FUC',
145
+ 909 => 'System error',
146
+ 912 => 'Issuer not available',
147
+ 913 => 'Duplicate transmission',
148
+ 916 => 'Amount too low',
149
+ 928 => 'Time-out exceeded',
150
+ 940 => 'Transaction cancelled previously',
151
+ 941 => 'Authorization operation already cancelled',
152
+ 942 => 'Original authorization declined',
153
+ 943 => 'Different details from origin transaction',
154
+ 944 => 'Session error',
155
+ 945 => 'Duplicate transmission',
156
+ 946 => 'Cancellation of transaction while in progress',
157
+ 947 => 'Duplicate tranmission while in progress',
158
+ 949 => 'POS Inoperative',
159
+ 950 => 'Refund not possible',
160
+ 9064 => 'Card number incorrect',
161
+ 9078 => 'No payment method available',
162
+ 9093 => 'Non-existent card',
163
+ 9218 => 'Recursive transaction in bad gateway',
164
+ 9253 => 'Check-digit incorrect',
165
+ 9256 => 'Preauth not allowed for merchant',
166
+ 9257 => 'Preauth not allowed for card',
167
+ 9261 => 'Operating limit exceeded',
168
+ 9912 => 'Issuer not available',
169
+ 9913 => 'Confirmation error',
170
+ 9914 => 'KO Confirmation'
171
+ }
172
+
173
+ # Creates a new instance
174
+ #
175
+ # Redsys requires a login and secret_key, and optionally also accepts a
176
+ # non-default terminal.
177
+ #
178
+ # ==== Options
179
+ #
180
+ # * <tt>:login</tt> -- The Redsys Merchant ID (REQUIRED)
181
+ # * <tt>:secret_key</tt> -- The Redsys Secret Key. (REQUIRED)
182
+ # * <tt>:terminal</tt> -- The Redsys Terminal. Defaults to 1. (OPTIONAL)
183
+ # * <tt>:test</tt> -- +true+ or +false+. Defaults to +false+. (OPTIONAL)
184
+ # * <tt>:signature_algorithm</tt> -- +"sha256"+ Defaults to +"sha1"+. (OPTIONAL)
185
+ def initialize(options = {})
186
+ requires!(options, :login, :secret_key)
187
+ options[:terminal] ||= 1
188
+ options[:signature_algorithm] ||= 'sha1'
189
+ super
190
+ end
191
+
192
+ def purchase(money, payment, options = {})
193
+ requires!(options, :order_id)
194
+
195
+ data = {}
196
+ add_action(data, :purchase)
197
+ add_amount(data, money, options)
198
+ add_order(data, options[:order_id])
199
+ add_payment(data, payment)
200
+ data[:description] = options[:description]
201
+ data[:store_in_vault] = options[:store]
202
+
203
+ commit data
204
+ end
205
+
206
+ def authorize(money, payment, options = {})
207
+ requires!(options, :order_id)
208
+
209
+ data = {}
210
+ add_action(data, :authorize)
211
+ add_amount(data, money, options)
212
+ add_order(data, options[:order_id])
213
+ add_payment(data, payment)
214
+ data[:description] = options[:description]
215
+ data[:store_in_vault] = options[:store]
216
+
217
+ commit data
218
+ end
219
+
220
+ def capture(money, authorization, options = {})
221
+ data = {}
222
+ add_action(data, :capture)
223
+ add_amount(data, money, options)
224
+ order_id, _, _ = split_authorization(authorization)
225
+ add_order(data, order_id)
226
+ data[:description] = options[:description]
227
+
228
+ commit data
229
+ end
230
+
231
+ def void(authorization, options = {})
232
+ data = {}
233
+ add_action(data, :cancel)
234
+ order_id, amount, currency = split_authorization(authorization)
235
+ add_amount(data, amount, :currency => currency)
236
+ add_order(data, order_id)
237
+ data[:description] = options[:description]
238
+
239
+ commit data
240
+ end
241
+
242
+ def refund(money, authorization, options = {})
243
+ data = {}
244
+ add_action(data, :refund)
245
+ add_amount(data, money, options)
246
+ order_id, _, _ = split_authorization(authorization)
247
+ add_order(data, order_id)
248
+ data[:description] = options[:description]
249
+
250
+ commit data
251
+ end
252
+
253
+ def verify(creditcard, options = {})
254
+ MultiResponse.run(:use_first_response) do |r|
255
+ r.process { authorize(100, creditcard, options) }
256
+ r.process(:ignore_result) { void(r.authorization, options) }
257
+ end
258
+ end
259
+
260
+ def supports_scrubbing
261
+ true
262
+ end
263
+
264
+ def scrub(transcript)
265
+ transcript.
266
+ gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
267
+ gsub(%r((%3CDS_MERCHANT_PAN%3E)\d+(%3C%2FDS_MERCHANT_PAN%3E))i, '\1[FILTERED]\2').
268
+ gsub(%r((%3CDS_MERCHANT_CVV2%3E)\d+(%3C%2FDS_MERCHANT_CVV2%3E))i, '\1[FILTERED]\2').
269
+ gsub(%r((<DS_MERCHANT_PAN>)\d+(</DS_MERCHANT_PAN>))i, '\1[FILTERED]\2').
270
+ gsub(%r((<DS_MERCHANT_CVV2>)\d+(</DS_MERCHANT_CVV2>))i, '\1[FILTERED]\2').
271
+ gsub(%r((DS_MERCHANT_CVV2)%2F%3E%0A%3C%2F)i, '\1[BLANK]').
272
+ gsub(%r((DS_MERCHANT_CVV2)%2F%3E%3C)i, '\1[BLANK]').
273
+ gsub(%r((DS_MERCHANT_CVV2%3E)(%3C%2FDS_MERCHANT_CVV2))i, '\1[BLANK]\2').
274
+ gsub(%r((<DS_MERCHANT_CVV2>)(</DS_MERCHANT_CVV2>))i, '\1[BLANK]\2').
275
+ gsub(%r((DS_MERCHANT_CVV2%3E)\++(%3C%2FDS_MERCHANT_CVV2))i, '\1[BLANK]\2').
276
+ gsub(%r((<DS_MERCHANT_CVV2>)\s+(</DS_MERCHANT_CVV2>))i, '\1[BLANK]\2')
277
+ end
278
+
279
+ private
280
+
281
+ def add_action(data, action)
282
+ data[:action] = transaction_code(action)
283
+ end
284
+
285
+ def add_amount(data, money, options)
286
+ data[:amount] = amount(money).to_s
287
+ data[:currency] = currency_code(options[:currency] || currency(money))
288
+ end
289
+
290
+ def add_order(data, order_id)
291
+ data[:order_id] = clean_order_id(order_id)
292
+ end
293
+
294
+ def url
295
+ test? ? test_url : live_url
296
+ end
297
+
298
+ def add_payment(data, card)
299
+ if card.is_a?(String)
300
+ data[:credit_card_token] = card
301
+ else
302
+ name = [card.first_name, card.last_name].join(' ').slice(0, 60)
303
+ year = sprintf('%.4i', card.year)
304
+ month = sprintf('%.2i', card.month)
305
+ data[:card] = {
306
+ :name => name,
307
+ :pan => card.number,
308
+ :date => "#{year[2..3]}#{month}",
309
+ :cvv => card.verification_value
310
+ }
311
+ end
312
+ end
313
+
314
+ def commit(data)
315
+ parse(ssl_post(url, "entrada=#{CGI.escape(xml_request_from(data))}", headers))
316
+ end
317
+
318
+ def headers
319
+ {
320
+ 'Content-Type' => 'application/x-www-form-urlencoded'
321
+ }
322
+ end
323
+
324
+ def xml_request_from(data)
325
+ if sha256_authentication?
326
+ build_sha256_xml_request(data)
327
+ else
328
+ build_sha1_xml_request(data)
329
+ end
330
+ end
331
+
332
+ def build_signature(data)
333
+ str = data[:amount] +
334
+ data[:order_id].to_s +
335
+ @options[:login].to_s +
336
+ data[:currency]
337
+
338
+ if card = data[:card]
339
+ str << card[:pan]
340
+ str << card[:cvv] if card[:cvv]
341
+ end
342
+
343
+ str << data[:action]
344
+ if data[:store_in_vault]
345
+ str << 'REQUIRED'
346
+ elsif data[:credit_card_token]
347
+ str << data[:credit_card_token]
348
+ end
349
+ str << @options[:secret_key]
350
+
351
+ Digest::SHA1.hexdigest(str)
352
+ end
353
+
354
+ def build_sha256_xml_request(data)
355
+ xml = Builder::XmlMarkup.new
356
+ xml.instruct!
357
+ xml.REQUEST do
358
+ build_merchant_data(xml, data)
359
+ xml.DS_SIGNATUREVERSION 'HMAC_SHA256_V1'
360
+ xml.DS_SIGNATURE sign_request(merchant_data_xml(data), data[:order_id])
361
+ end
362
+ xml.target!
363
+ end
364
+
365
+ def build_sha1_xml_request(data)
366
+ xml = Builder::XmlMarkup.new :indent => 2
367
+ build_merchant_data(xml, data)
368
+ xml.target!
369
+ end
370
+
371
+ def merchant_data_xml(data)
372
+ xml = Builder::XmlMarkup.new
373
+ build_merchant_data(xml, data)
374
+ xml.target!
375
+ end
376
+
377
+ def build_merchant_data(xml, data)
378
+ xml.DATOSENTRADA do
379
+ # Basic elements
380
+ xml.DS_Version 0.1
381
+ xml.DS_MERCHANT_CURRENCY data[:currency]
382
+ xml.DS_MERCHANT_AMOUNT data[:amount]
383
+ xml.DS_MERCHANT_ORDER data[:order_id]
384
+ xml.DS_MERCHANT_TRANSACTIONTYPE data[:action]
385
+ xml.DS_MERCHANT_PRODUCTDESCRIPTION data[:description]
386
+ xml.DS_MERCHANT_TERMINAL @options[:terminal]
387
+ xml.DS_MERCHANT_MERCHANTCODE @options[:login]
388
+ xml.DS_MERCHANT_MERCHANTSIGNATURE build_signature(data) unless sha256_authentication?
389
+
390
+ # Only when card is present
391
+ if data[:card]
392
+ xml.DS_MERCHANT_TITULAR data[:card][:name]
393
+ xml.DS_MERCHANT_PAN data[:card][:pan]
394
+ xml.DS_MERCHANT_EXPIRYDATE data[:card][:date]
395
+ xml.DS_MERCHANT_CVV2 data[:card][:cvv]
396
+ xml.DS_MERCHANT_IDENTIFIER 'REQUIRED' if data[:store_in_vault]
397
+ elsif data[:credit_card_token]
398
+ xml.DS_MERCHANT_IDENTIFIER data[:credit_card_token]
399
+ xml.DS_MERCHANT_DIRECTPAYMENT 'true'
400
+ end
401
+ end
402
+ end
403
+
404
+ def parse(data)
405
+ params = {}
406
+ success = false
407
+ message = ''
408
+ options = @options.merge(:test => test?)
409
+ xml = Nokogiri::XML(data)
410
+ code = xml.xpath('//RETORNOXML/CODIGO').text
411
+ if code == '0'
412
+ op = xml.xpath('//RETORNOXML/OPERACION')
413
+ op.children.each do |element|
414
+ params[element.name.downcase.to_sym] = element.text
415
+ end
416
+
417
+ if validate_signature(params)
418
+ message = response_text(params[:ds_response])
419
+ options[:authorization] = build_authorization(params)
420
+ success = is_success_response?(params[:ds_response])
421
+ else
422
+ message = 'Response failed validation check'
423
+ end
424
+ else
425
+ # Some kind of programmer error with the request!
426
+ message = "#{code} ERROR"
427
+ end
428
+
429
+ Response.new(success, message, params, options)
430
+ end
431
+
432
+ def validate_signature(data)
433
+ if sha256_authentication?
434
+ sig = Base64.strict_encode64(mac256(get_key(data[:ds_order].to_s), xml_signed_fields(data)))
435
+ sig.casecmp(data[:ds_signature].to_s).zero?
436
+ else
437
+ str = data[:ds_amount] +
438
+ data[:ds_order].to_s +
439
+ data[:ds_merchantcode] +
440
+ data[:ds_currency] +
441
+ data[:ds_response] +
442
+ data[:ds_cardnumber].to_s +
443
+ data[:ds_transactiontype].to_s +
444
+ data[:ds_securepayment].to_s +
445
+ @options[:secret_key]
446
+
447
+ sig = Digest::SHA1.hexdigest(str)
448
+ data[:ds_signature].to_s.downcase == sig
449
+ end
450
+ end
451
+
452
+ def build_authorization(params)
453
+ [params[:ds_order], params[:ds_amount], params[:ds_currency]].join('|')
454
+ end
455
+
456
+ def split_authorization(authorization)
457
+ order_id, amount, currency = authorization.split('|')
458
+ [order_id, amount.to_i, currency]
459
+ end
460
+
461
+ def currency_code(currency)
462
+ return currency if currency =~ /^\d+$/
463
+ raise ArgumentError, "Unknown currency #{currency}" unless CURRENCY_CODES[currency]
464
+ CURRENCY_CODES[currency]
465
+ end
466
+
467
+ def transaction_code(type)
468
+ SUPPORTED_TRANSACTIONS[type]
469
+ end
470
+
471
+ def response_text(code)
472
+ code = code.to_i
473
+ code = 0 if code < 100
474
+ RESPONSE_TEXTS[code] || 'Unkown code, please check in manual'
475
+ end
476
+
477
+ def is_success_response?(code)
478
+ (code.to_i < 100) || [400, 481, 500, 900].include?(code.to_i)
479
+ end
480
+
481
+ def clean_order_id(order_id)
482
+ cleansed = order_id.gsub(/[^\da-zA-Z]/, '')
483
+ if cleansed =~ /^\d{4}/
484
+ cleansed[0..11]
485
+ else
486
+ '%04d%s' % [rand(0..9999), cleansed[0...8]]
487
+ end
488
+ end
489
+
490
+ def sha256_authentication?
491
+ @options[:signature_algorithm] == 'sha256'
492
+ end
493
+
494
+ def sign_request(xml_request_string, order_id)
495
+ key = encrypt(@options[:secret_key], order_id)
496
+ Base64.strict_encode64(mac256(key, xml_request_string))
497
+ end
498
+
499
+ def encrypt(key, order_id)
500
+ block_length = 8
501
+ cipher = OpenSSL::Cipher.new('DES3')
502
+ cipher.encrypt
503
+
504
+ cipher.key = Base64.strict_decode64(key)
505
+ # The OpenSSL default of an all-zeroes ("\\0") IV is used.
506
+ cipher.padding = 0
507
+
508
+ order_id += "\0" until order_id.bytesize % block_length == 0 # Pad with zeros
509
+
510
+ output = cipher.update(order_id) + cipher.final
511
+ output
512
+ end
513
+
514
+ def mac256(key, data)
515
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), key, data)
516
+ end
517
+
518
+ def xml_signed_fields(data)
519
+ xml_signed_fields = data[:ds_amount] + data[:ds_order] + data[:ds_merchantcode] +
520
+ data[:ds_currency] + data[:ds_response]
521
+
522
+ if data[:ds_cardnumber]
523
+ xml_signed_fields += data[:ds_cardnumber]
524
+ end
525
+
526
+ xml_signed_fields + data[:ds_transactiontype] + data[:ds_securepayment]
527
+ end
528
+
529
+ def get_key(order_id)
530
+ encrypt(@options[:secret_key], order_id)
531
+ end
532
+ end
533
+ end
534
+ end