archetype2142_activemerchant 1.124.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (271) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +3640 -0
  3. data/CONTRIBUTORS +568 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +249 -0
  6. data/lib/active_merchant/billing/apple_pay_payment_token.rb +22 -0
  7. data/lib/active_merchant/billing/avs_result.rb +95 -0
  8. data/lib/active_merchant/billing/base.rb +48 -0
  9. data/lib/active_merchant/billing/check.rb +80 -0
  10. data/lib/active_merchant/billing/compatibility.rb +118 -0
  11. data/lib/active_merchant/billing/credit_card.rb +414 -0
  12. data/lib/active_merchant/billing/credit_card_formatting.rb +24 -0
  13. data/lib/active_merchant/billing/credit_card_methods.rb +444 -0
  14. data/lib/active_merchant/billing/cvv_result.rb +37 -0
  15. data/lib/active_merchant/billing/gateway.rb +331 -0
  16. data/lib/active_merchant/billing/gateways/adyen.rb +712 -0
  17. data/lib/active_merchant/billing/gateways/allied_wallet.rb +205 -0
  18. data/lib/active_merchant/billing/gateways/authorize_net.rb +1107 -0
  19. data/lib/active_merchant/billing/gateways/authorize_net_arb.rb +424 -0
  20. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +977 -0
  21. data/lib/active_merchant/billing/gateways/axcessms.rb +179 -0
  22. data/lib/active_merchant/billing/gateways/balanced.rb +263 -0
  23. data/lib/active_merchant/billing/gateways/bambora_apac.rb +222 -0
  24. data/lib/active_merchant/billing/gateways/bank_frick.rb +225 -0
  25. data/lib/active_merchant/billing/gateways/banwire.rb +116 -0
  26. data/lib/active_merchant/billing/gateways/barclaycard_smartpay.rb +396 -0
  27. data/lib/active_merchant/billing/gateways/barclays_epdq_extra_plus.rb +15 -0
  28. data/lib/active_merchant/billing/gateways/be2bill.rb +131 -0
  29. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +474 -0
  30. data/lib/active_merchant/billing/gateways/beanstream.rb +220 -0
  31. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +57 -0
  32. data/lib/active_merchant/billing/gateways/blue_pay.rb +549 -0
  33. data/lib/active_merchant/billing/gateways/blue_snap.rb +613 -0
  34. data/lib/active_merchant/billing/gateways/bogus.rb +186 -0
  35. data/lib/active_merchant/billing/gateways/borgun.rb +231 -0
  36. data/lib/active_merchant/billing/gateways/bpoint.rb +277 -0
  37. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +23 -0
  38. data/lib/active_merchant/billing/gateways/braintree.rb +19 -0
  39. data/lib/active_merchant/billing/gateways/braintree_blue.rb +832 -0
  40. data/lib/active_merchant/billing/gateways/braintree_orange.rb +19 -0
  41. data/lib/active_merchant/billing/gateways/bridge_pay.rb +244 -0
  42. data/lib/active_merchant/billing/gateways/cams.rb +230 -0
  43. data/lib/active_merchant/billing/gateways/card_connect.rb +320 -0
  44. data/lib/active_merchant/billing/gateways/card_save.rb +21 -0
  45. data/lib/active_merchant/billing/gateways/card_stream.rb +371 -0
  46. data/lib/active_merchant/billing/gateways/cardknox.rb +327 -0
  47. data/lib/active_merchant/billing/gateways/cardprocess.rb +256 -0
  48. data/lib/active_merchant/billing/gateways/cashnet.rb +225 -0
  49. data/lib/active_merchant/billing/gateways/cc5.rb +198 -0
  50. data/lib/active_merchant/billing/gateways/cecabank.rb +249 -0
  51. data/lib/active_merchant/billing/gateways/cenpos.rb +328 -0
  52. data/lib/active_merchant/billing/gateways/checkout.rb +212 -0
  53. data/lib/active_merchant/billing/gateways/checkout_v2.rb +311 -0
  54. data/lib/active_merchant/billing/gateways/citrus_pay.rb +21 -0
  55. data/lib/active_merchant/billing/gateways/clearhaus.rb +219 -0
  56. data/lib/active_merchant/billing/gateways/commercegate.rb +142 -0
  57. data/lib/active_merchant/billing/gateways/conekta.rb +230 -0
  58. data/lib/active_merchant/billing/gateways/creditcall.rb +272 -0
  59. data/lib/active_merchant/billing/gateways/credorax.rb +496 -0
  60. data/lib/active_merchant/billing/gateways/ct_payment.rb +269 -0
  61. data/lib/active_merchant/billing/gateways/culqi.rb +279 -0
  62. data/lib/active_merchant/billing/gateways/cyber_source.rb +1074 -0
  63. data/lib/active_merchant/billing/gateways/d_local.rb +253 -0
  64. data/lib/active_merchant/billing/gateways/data_cash.rb +302 -0
  65. data/lib/active_merchant/billing/gateways/decidir.rb +331 -0
  66. data/lib/active_merchant/billing/gateways/dibs.rb +199 -0
  67. data/lib/active_merchant/billing/gateways/digitzs.rb +295 -0
  68. data/lib/active_merchant/billing/gateways/ebanx.rb +333 -0
  69. data/lib/active_merchant/billing/gateways/efsnet.rb +215 -0
  70. data/lib/active_merchant/billing/gateways/elavon.rb +472 -0
  71. data/lib/active_merchant/billing/gateways/element.rb +386 -0
  72. data/lib/active_merchant/billing/gateways/epay.rb +296 -0
  73. data/lib/active_merchant/billing/gateways/evo_ca.rb +307 -0
  74. data/lib/active_merchant/billing/gateways/eway.rb +226 -0
  75. data/lib/active_merchant/billing/gateways/eway_managed.rb +289 -0
  76. data/lib/active_merchant/billing/gateways/eway_rapid.rb +578 -0
  77. data/lib/active_merchant/billing/gateways/exact.rb +219 -0
  78. data/lib/active_merchant/billing/gateways/ezic.rb +195 -0
  79. data/lib/active_merchant/billing/gateways/fat_zebra.rb +223 -0
  80. data/lib/active_merchant/billing/gateways/federated_canada.rb +158 -0
  81. data/lib/active_merchant/billing/gateways/finansbank.rb +22 -0
  82. data/lib/active_merchant/billing/gateways/first_giving.rb +143 -0
  83. data/lib/active_merchant/billing/gateways/first_pay.rb +182 -0
  84. data/lib/active_merchant/billing/gateways/firstdata_e4.rb +452 -0
  85. data/lib/active_merchant/billing/gateways/firstdata_e4_v27.rb +505 -0
  86. data/lib/active_merchant/billing/gateways/flo2cash.rb +215 -0
  87. data/lib/active_merchant/billing/gateways/flo2cash_simple.rb +20 -0
  88. data/lib/active_merchant/billing/gateways/forte.rb +286 -0
  89. data/lib/active_merchant/billing/gateways/garanti.rb +256 -0
  90. data/lib/active_merchant/billing/gateways/global_collect.rb +463 -0
  91. data/lib/active_merchant/billing/gateways/global_transport.rb +193 -0
  92. data/lib/active_merchant/billing/gateways/hdfc.rb +205 -0
  93. data/lib/active_merchant/billing/gateways/hps.rb +472 -0
  94. data/lib/active_merchant/billing/gateways/iats_payments.rb +312 -0
  95. data/lib/active_merchant/billing/gateways/in_context_paypal_express.rb +15 -0
  96. data/lib/active_merchant/billing/gateways/inspire.rb +213 -0
  97. data/lib/active_merchant/billing/gateways/instapay.rb +159 -0
  98. data/lib/active_merchant/billing/gateways/ipg.rb +390 -0
  99. data/lib/active_merchant/billing/gateways/ipp.rb +176 -0
  100. data/lib/active_merchant/billing/gateways/iridium.rb +467 -0
  101. data/lib/active_merchant/billing/gateways/itransact.rb +448 -0
  102. data/lib/active_merchant/billing/gateways/iveri.rb +254 -0
  103. data/lib/active_merchant/billing/gateways/ixopay.rb +320 -0
  104. data/lib/active_merchant/billing/gateways/jetpay.rb +395 -0
  105. data/lib/active_merchant/billing/gateways/jetpay_v2.rb +432 -0
  106. data/lib/active_merchant/billing/gateways/komoju.rb +115 -0
  107. data/lib/active_merchant/billing/gateways/kushki.rb +270 -0
  108. data/lib/active_merchant/billing/gateways/latitude19.rb +412 -0
  109. data/lib/active_merchant/billing/gateways/linkpoint.rb +448 -0
  110. data/lib/active_merchant/billing/gateways/litle.rb +531 -0
  111. data/lib/active_merchant/billing/gateways/mastercard.rb +293 -0
  112. data/lib/active_merchant/billing/gateways/maxipago.rb +220 -0
  113. data/lib/active_merchant/billing/gateways/mercado_pago.rb +329 -0
  114. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +194 -0
  115. data/lib/active_merchant/billing/gateways/merchant_one.rb +110 -0
  116. data/lib/active_merchant/billing/gateways/merchant_partners.rb +245 -0
  117. data/lib/active_merchant/billing/gateways/merchant_ware.rb +313 -0
  118. data/lib/active_merchant/billing/gateways/merchant_ware_version_four.rb +284 -0
  119. data/lib/active_merchant/billing/gateways/merchant_warrior.rb +248 -0
  120. data/lib/active_merchant/billing/gateways/mercury.rb +352 -0
  121. data/lib/active_merchant/billing/gateways/metrics_global.rb +293 -0
  122. data/lib/active_merchant/billing/gateways/micropayment.rb +182 -0
  123. data/lib/active_merchant/billing/gateways/migs/migs_codes.rb +100 -0
  124. data/lib/active_merchant/billing/gateways/migs.rb +329 -0
  125. data/lib/active_merchant/billing/gateways/mit.rb +260 -0
  126. data/lib/active_merchant/billing/gateways/modern_payments.rb +37 -0
  127. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +215 -0
  128. data/lib/active_merchant/billing/gateways/moka.rb +290 -0
  129. data/lib/active_merchant/billing/gateways/monei.rb +422 -0
  130. data/lib/active_merchant/billing/gateways/moneris.rb +446 -0
  131. data/lib/active_merchant/billing/gateways/money_movers.rb +150 -0
  132. data/lib/active_merchant/billing/gateways/mundipagg.rb +363 -0
  133. data/lib/active_merchant/billing/gateways/nab_transact.rb +299 -0
  134. data/lib/active_merchant/billing/gateways/ncr_secure_pay.rb +163 -0
  135. data/lib/active_merchant/billing/gateways/net_registry.rb +198 -0
  136. data/lib/active_merchant/billing/gateways/netaxept.rb +180 -0
  137. data/lib/active_merchant/billing/gateways/netbanx.rb +376 -0
  138. data/lib/active_merchant/billing/gateways/netbilling.rb +229 -0
  139. data/lib/active_merchant/billing/gateways/netpay.rb +223 -0
  140. data/lib/active_merchant/billing/gateways/network_merchants.rb +238 -0
  141. data/lib/active_merchant/billing/gateways/nmi.rb +356 -0
  142. data/lib/active_merchant/billing/gateways/ogone.rb +481 -0
  143. data/lib/active_merchant/billing/gateways/omise.rb +323 -0
  144. data/lib/active_merchant/billing/gateways/openpay.rb +229 -0
  145. data/lib/active_merchant/billing/gateways/opp.rb +394 -0
  146. data/lib/active_merchant/billing/gateways/optimal_payment.rb +331 -0
  147. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +45 -0
  148. data/lib/active_merchant/billing/gateways/orbital.rb +1209 -0
  149. data/lib/active_merchant/billing/gateways/pac_net_raven.rb +206 -0
  150. data/lib/active_merchant/billing/gateways/pagarme.rb +239 -0
  151. data/lib/active_merchant/billing/gateways/pago_facil.rb +120 -0
  152. data/lib/active_merchant/billing/gateways/pay_arc.rb +392 -0
  153. data/lib/active_merchant/billing/gateways/pay_conex.rb +245 -0
  154. data/lib/active_merchant/billing/gateways/pay_gate_xml.rb +277 -0
  155. data/lib/active_merchant/billing/gateways/pay_hub.rb +213 -0
  156. data/lib/active_merchant/billing/gateways/pay_junction.rb +390 -0
  157. data/lib/active_merchant/billing/gateways/pay_junction_v2.rb +206 -0
  158. data/lib/active_merchant/billing/gateways/pay_secure.rb +110 -0
  159. data/lib/active_merchant/billing/gateways/pay_trace.rb +404 -0
  160. data/lib/active_merchant/billing/gateways/paybox_direct.rb +224 -0
  161. data/lib/active_merchant/billing/gateways/payeezy.rb +458 -0
  162. data/lib/active_merchant/billing/gateways/payex.rb +409 -0
  163. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +235 -0
  164. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +42 -0
  165. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  166. data/lib/active_merchant/billing/gateways/payflow.rb +411 -0
  167. data/lib/active_merchant/billing/gateways/payflow_express.rb +220 -0
  168. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +14 -0
  169. data/lib/active_merchant/billing/gateways/payflow_uk.rb +20 -0
  170. data/lib/active_merchant/billing/gateways/payment_express.rb +373 -0
  171. data/lib/active_merchant/billing/gateways/paymentez.rb +327 -0
  172. data/lib/active_merchant/billing/gateways/paymill.rb +369 -0
  173. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +718 -0
  174. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +65 -0
  175. data/lib/active_merchant/billing/gateways/paypal/paypal_recurring_api.rb +262 -0
  176. data/lib/active_merchant/billing/gateways/paypal.rb +136 -0
  177. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  178. data/lib/active_merchant/billing/gateways/paypal_digital_goods.rb +44 -0
  179. data/lib/active_merchant/billing/gateways/paypal_express.rb +270 -0
  180. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +30 -0
  181. data/lib/active_merchant/billing/gateways/paysafe.rb +412 -0
  182. data/lib/active_merchant/billing/gateways/payscout.rb +159 -0
  183. data/lib/active_merchant/billing/gateways/paystation.rb +204 -0
  184. data/lib/active_merchant/billing/gateways/payu_in.rb +249 -0
  185. data/lib/active_merchant/billing/gateways/payu_latam.rb +469 -0
  186. data/lib/active_merchant/billing/gateways/payu_polska.rb +178 -0
  187. data/lib/active_merchant/billing/gateways/payway.rb +207 -0
  188. data/lib/active_merchant/billing/gateways/payway_dot_com.rb +253 -0
  189. data/lib/active_merchant/billing/gateways/pin.rb +246 -0
  190. data/lib/active_merchant/billing/gateways/plugnpay.rb +283 -0
  191. data/lib/active_merchant/billing/gateways/priority.rb +390 -0
  192. data/lib/active_merchant/billing/gateways/pro_pay.rb +325 -0
  193. data/lib/active_merchant/billing/gateways/psigate.rb +227 -0
  194. data/lib/active_merchant/billing/gateways/psl_card.rb +295 -0
  195. data/lib/active_merchant/billing/gateways/qbms.rb +302 -0
  196. data/lib/active_merchant/billing/gateways/quantum.rb +274 -0
  197. data/lib/active_merchant/billing/gateways/quickbooks.rb +377 -0
  198. data/lib/active_merchant/billing/gateways/quickpay/quickpay_common.rb +184 -0
  199. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v10.rb +297 -0
  200. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v4to7.rb +226 -0
  201. data/lib/active_merchant/billing/gateways/quickpay.rb +24 -0
  202. data/lib/active_merchant/billing/gateways/qvalent.rb +305 -0
  203. data/lib/active_merchant/billing/gateways/realex.rb +400 -0
  204. data/lib/active_merchant/billing/gateways/redsys.rb +718 -0
  205. data/lib/active_merchant/billing/gateways/s5.rb +247 -0
  206. data/lib/active_merchant/billing/gateways/safe_charge.rb +301 -0
  207. data/lib/active_merchant/billing/gateways/sage.rb +446 -0
  208. data/lib/active_merchant/billing/gateways/sage_pay.rb +434 -0
  209. data/lib/active_merchant/billing/gateways/sallie_mae.rb +141 -0
  210. data/lib/active_merchant/billing/gateways/secure_net.rb +260 -0
  211. data/lib/active_merchant/billing/gateways/secure_pay.rb +191 -0
  212. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +290 -0
  213. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +103 -0
  214. data/lib/active_merchant/billing/gateways/securion_pay.rb +265 -0
  215. data/lib/active_merchant/billing/gateways/skip_jack.rb +450 -0
  216. data/lib/active_merchant/billing/gateways/smart_ps.rb +274 -0
  217. data/lib/active_merchant/billing/gateways/so_easy_pay.rb +194 -0
  218. data/lib/active_merchant/billing/gateways/spreedly_core.rb +312 -0
  219. data/lib/active_merchant/billing/gateways/stripe.rb +813 -0
  220. data/lib/active_merchant/billing/gateways/stripe_payment_intents.rb +463 -0
  221. data/lib/active_merchant/billing/gateways/swipe_checkout.rb +151 -0
  222. data/lib/active_merchant/billing/gateways/telr.rb +273 -0
  223. data/lib/active_merchant/billing/gateways/tns.rb +26 -0
  224. data/lib/active_merchant/billing/gateways/trans_first.rb +240 -0
  225. data/lib/active_merchant/billing/gateways/trans_first_transaction_express.rb +612 -0
  226. data/lib/active_merchant/billing/gateways/transact_pro.rb +222 -0
  227. data/lib/active_merchant/billing/gateways/transax.rb +21 -0
  228. data/lib/active_merchant/billing/gateways/transnational.rb +9 -0
  229. data/lib/active_merchant/billing/gateways/trexle.rb +221 -0
  230. data/lib/active_merchant/billing/gateways/trust_commerce.rb +489 -0
  231. data/lib/active_merchant/billing/gateways/usa_epay.rb +24 -0
  232. data/lib/active_merchant/billing/gateways/usa_epay_advanced.rb +1612 -0
  233. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +353 -0
  234. data/lib/active_merchant/billing/gateways/vanco.rb +294 -0
  235. data/lib/active_merchant/billing/gateways/verifi.rb +224 -0
  236. data/lib/active_merchant/billing/gateways/viaklix.rb +171 -0
  237. data/lib/active_merchant/billing/gateways/visanet_peru.rb +246 -0
  238. data/lib/active_merchant/billing/gateways/vpos.rb +220 -0
  239. data/lib/active_merchant/billing/gateways/webpay.rb +97 -0
  240. data/lib/active_merchant/billing/gateways/wepay.rb +235 -0
  241. data/lib/active_merchant/billing/gateways/wirecard.rb +430 -0
  242. data/lib/active_merchant/billing/gateways/wompi.rb +153 -0
  243. data/lib/active_merchant/billing/gateways/world_net.rb +345 -0
  244. data/lib/active_merchant/billing/gateways/worldpay.rb +923 -0
  245. data/lib/active_merchant/billing/gateways/worldpay_online_payments.rb +208 -0
  246. data/lib/active_merchant/billing/gateways/worldpay_us.rb +221 -0
  247. data/lib/active_merchant/billing/gateways.rb +14 -0
  248. data/lib/active_merchant/billing/model.rb +30 -0
  249. data/lib/active_merchant/billing/network_tokenization_credit_card.rb +39 -0
  250. data/lib/active_merchant/billing/payment_token.rb +21 -0
  251. data/lib/active_merchant/billing/rails.rb +3 -0
  252. data/lib/active_merchant/billing/response.rb +97 -0
  253. data/lib/active_merchant/billing/three_d_secure_eci_mapper.rb +27 -0
  254. data/lib/active_merchant/billing.rb +16 -0
  255. data/lib/active_merchant/connection.rb +196 -0
  256. data/lib/active_merchant/country.rb +337 -0
  257. data/lib/active_merchant/empty.rb +20 -0
  258. data/lib/active_merchant/errors.rb +35 -0
  259. data/lib/active_merchant/net_http_ssl_connection.rb +11 -0
  260. data/lib/active_merchant/network_connection_retries.rb +78 -0
  261. data/lib/active_merchant/post_data.rb +26 -0
  262. data/lib/active_merchant/posts_data.rb +92 -0
  263. data/lib/active_merchant/version.rb +3 -0
  264. data/lib/active_merchant.rb +63 -0
  265. data/lib/activemerchant.rb +1 -0
  266. data/lib/certs/cacert.pem +3214 -0
  267. data/lib/support/gateway_support.rb +69 -0
  268. data/lib/support/outbound_hosts.rb +28 -0
  269. data/lib/support/ssl_verify.rb +88 -0
  270. data/lib/support/ssl_version.rb +86 -0
  271. metadata +463 -0
@@ -0,0 +1,923 @@
1
+ require 'nokogiri'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ class WorldpayGateway < Gateway
6
+ self.test_url = 'https://secure-test.worldpay.com/jsp/merchant/xml/paymentService.jsp'
7
+ self.live_url = 'https://secure.worldpay.com/jsp/merchant/xml/paymentService.jsp'
8
+
9
+ self.default_currency = 'GBP'
10
+ self.money_format = :cents
11
+ self.supported_countries = %w(AD AE AG AI AL AM AO AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BM BN BO BR BS BT BW
12
+ BY BZ CA CC CF CH CK CL CM CN CO CR CV CX CY CZ DE DJ DK DO DZ EC EE EG EH ES ET FI FJ FK
13
+ FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GT GU GW GY HK HM HN HR HT HU ID IE IL
14
+ IM IN IO IS IT JE JM JO JP KE KG KH KI KM KN KR KW KY KZ LA LC LI LK LS LT LU LV MA MC MD
15
+ ME MG MH MK ML MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ
16
+ OM PA PE PF PH PK PL PN PR PT PW PY QA RE RO RS RU RW SA SB SC SE SG SI SK SL SM SN ST SV
17
+ SZ TC TD TF TG TH TJ TK TM TO TR TT TV TW TZ UA UG US UY UZ VA VC VE VI VN VU WF WS YE YT
18
+ ZA ZM)
19
+ self.supported_cardtypes = %i[visa master american_express discover jcb maestro elo naranja cabal unionpay]
20
+ self.currencies_without_fractions = %w(HUF IDR JPY KRW BEF XOF XAF XPF GRD GNF ITL LUF MGA MGF PYG PTE RWF ESP TRL VND KMF)
21
+ self.currencies_with_three_decimal_places = %w(BHD KWD OMR TND LYD JOD IQD)
22
+ self.homepage_url = 'http://www.worldpay.com/'
23
+ self.display_name = 'Worldpay Global'
24
+
25
+ NETWORK_TOKEN_TYPE = {
26
+ apple_pay: 'APPLEPAY',
27
+ google_pay: 'GOOGLEPAY',
28
+ network_token: 'NETWORKTOKEN'
29
+ }
30
+
31
+ CARD_CODES = {
32
+ 'visa' => 'VISA-SSL',
33
+ 'master' => 'ECMC-SSL',
34
+ 'discover' => 'DISCOVER-SSL',
35
+ 'american_express' => 'AMEX-SSL',
36
+ 'jcb' => 'JCB-SSL',
37
+ 'maestro' => 'MAESTRO-SSL',
38
+ 'diners_club' => 'DINERS-SSL',
39
+ 'elo' => 'ELO-SSL',
40
+ 'naranja' => 'NARANJA-SSL',
41
+ 'cabal' => 'CABAL-SSL',
42
+ 'unionpay' => 'CHINAUNIONPAY-SSL',
43
+ 'unknown' => 'CARD-SSL'
44
+ }
45
+
46
+ AVS_CODE_MAP = {
47
+ 'A' => 'M', # Match
48
+ 'B' => 'P', # Postcode matches, address not verified
49
+ 'C' => 'Z', # Postcode matches, address does not match
50
+ 'D' => 'B', # Address matched; postcode not checked
51
+ 'E' => 'I', # Address and postal code not checked
52
+ 'F' => 'A', # Address matches, postcode does not match
53
+ 'G' => 'C', # Address does not match, postcode not checked
54
+ 'H' => 'I', # Address and postcode not provided
55
+ 'I' => 'C', # Address not checked postcode does not match
56
+ 'J' => 'C', # Address and postcode does not match
57
+ }
58
+
59
+ CVC_CODE_MAP = {
60
+ 'A' => 'M', # CVV matches
61
+ 'B' => 'P', # Not provided
62
+ 'C' => 'P', # Not checked
63
+ 'D' => 'N', # Does not match
64
+ }
65
+
66
+ def initialize(options = {})
67
+ requires!(options, :login, :password)
68
+ super
69
+ end
70
+
71
+ def purchase(money, payment_method, options = {})
72
+ MultiResponse.run do |r|
73
+ r.process { authorize(money, payment_method, options) }
74
+ r.process { capture(money, r.authorization, options.merge(authorization_validated: true)) } unless options[:skip_capture]
75
+ end
76
+ end
77
+
78
+ def authorize(money, payment_method, options = {})
79
+ requires!(options, :order_id)
80
+ payment_details = payment_details(payment_method)
81
+ authorize_request(money, payment_method, payment_details.merge(options))
82
+ end
83
+
84
+ def capture(money, authorization, options = {})
85
+ authorization = order_id_from_authorization(authorization.to_s)
86
+ MultiResponse.run do |r|
87
+ r.process { inquire_request(authorization, options, 'AUTHORISED', 'CAPTURED') } unless options[:authorization_validated]
88
+ if r.params
89
+ authorization_currency = r.params['amount_currency_code']
90
+ options = options.merge(currency: authorization_currency) if authorization_currency.present?
91
+ end
92
+ r.process { capture_request(money, authorization, options) }
93
+ end
94
+ end
95
+
96
+ def void(authorization, options = {})
97
+ authorization = order_id_from_authorization(authorization.to_s)
98
+ MultiResponse.run do |r|
99
+ r.process { inquire_request(authorization, options, 'AUTHORISED') } unless options[:authorization_validated]
100
+ r.process { cancel_request(authorization, options) }
101
+ end
102
+ end
103
+
104
+ def refund(money, authorization, options = {})
105
+ authorization = order_id_from_authorization(authorization.to_s)
106
+ success_criteria = %w(CAPTURED SETTLED SETTLED_BY_MERCHANT SENT_FOR_REFUND)
107
+ success_criteria.push('AUTHORIZED') if options[:cancel_or_refund]
108
+ response = MultiResponse.run do |r|
109
+ r.process { inquire_request(authorization, options, *success_criteria) } unless options[:authorization_validated]
110
+ r.process { refund_request(money, authorization, options) }
111
+ end
112
+
113
+ if !response.success? && options[:force_full_refund_if_unsettled] &&
114
+ response.params['last_event'] == 'AUTHORISED'
115
+ void(authorization, options)
116
+ else
117
+ response
118
+ end
119
+ end
120
+
121
+ # Credits only function on a Merchant ID/login/profile flagged for Payouts
122
+ # aka Credit Fund Transfers (CFT), whereas normal purchases, refunds,
123
+ # and other transactions should be performed on a normal eCom-flagged
124
+ # merchant ID.
125
+ def credit(money, payment_method, options = {})
126
+ payment_details = payment_details(payment_method)
127
+ if options[:fast_fund_credit]
128
+ fast_fund_credit_request(money, payment_method, payment_details.merge(credit: true, **options))
129
+ else
130
+ credit_request(money, payment_method, payment_details.merge(credit: true, **options))
131
+ end
132
+ end
133
+
134
+ def verify(payment_method, options = {})
135
+ amount = (eligible_for_0_auth?(payment_method, options) ? 0 : 100)
136
+ MultiResponse.run(:use_first_response) do |r|
137
+ r.process { authorize(amount, payment_method, options) }
138
+ r.process(:ignore_result) { void(r.authorization, options.merge(authorization_validated: true)) }
139
+ end
140
+ end
141
+
142
+ def store(credit_card, options = {})
143
+ requires!(options, :customer)
144
+ store_request(credit_card, options)
145
+ end
146
+
147
+ def supports_scrubbing
148
+ true
149
+ end
150
+
151
+ def supports_network_tokenization?
152
+ true
153
+ end
154
+
155
+ def scrub(transcript)
156
+ transcript.
157
+ gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
158
+ gsub(%r((<cardNumber>)\d+(</cardNumber>)), '\1[FILTERED]\2').
159
+ gsub(%r((<cvc>)[^<]+(</cvc>)), '\1[FILTERED]\2').
160
+ gsub(%r((<tokenNumber>)\d+(</tokenNumber>)), '\1[FILTERED]\2').
161
+ gsub(%r((<cryptogram>)[^<]+(</cryptogram>)), '\1[FILTERED]\2')
162
+ end
163
+
164
+ private
165
+
166
+ def authorize_request(money, payment_method, options)
167
+ commit('authorize', build_authorization_request(money, payment_method, options), 'AUTHORISED', 'CAPTURED', options)
168
+ end
169
+
170
+ def capture_request(money, authorization, options)
171
+ commit('capture', build_capture_request(money, authorization, options), 'CAPTURED', :ok, options)
172
+ end
173
+
174
+ def cancel_request(authorization, options)
175
+ commit('cancel', build_void_request(authorization, options), :ok, options)
176
+ end
177
+
178
+ def inquire_request(authorization, options, *success_criteria)
179
+ commit('inquiry', build_order_inquiry_request(authorization, options), *success_criteria, options)
180
+ end
181
+
182
+ def refund_request(money, authorization, options)
183
+ commit('refund', build_refund_request(money, authorization, options), :ok, 'SENT_FOR_REFUND', options)
184
+ end
185
+
186
+ def credit_request(money, payment_method, options)
187
+ commit('credit', build_authorization_request(money, payment_method, options), :ok, 'SENT_FOR_REFUND', options)
188
+ end
189
+
190
+ def fast_fund_credit_request(money, payment_method, options)
191
+ commit('fast_credit', build_fast_fund_credit_request(money, payment_method, options), :ok, 'PUSH_APPROVED', options)
192
+ end
193
+
194
+ def store_request(credit_card, options)
195
+ commit('store', build_store_request(credit_card, options), options)
196
+ end
197
+
198
+ def build_request
199
+ xml = Builder::XmlMarkup.new indent: 2
200
+ xml.instruct! :xml, encoding: 'UTF-8'
201
+ xml.declare! :DOCTYPE, :paymentService, :PUBLIC, '-//WorldPay//DTD WorldPay PaymentService v1//EN', 'http://dtd.worldpay.com/paymentService_v1.dtd'
202
+ xml.paymentService 'version' => '1.4', 'merchantCode' => @options[:login] do
203
+ yield xml
204
+ end
205
+ xml.target!
206
+ end
207
+
208
+ def build_order_modify_request(authorization)
209
+ build_request do |xml|
210
+ xml.modify do
211
+ xml.orderModification 'orderCode' => authorization do
212
+ yield xml
213
+ end
214
+ end
215
+ end
216
+ end
217
+
218
+ def build_order_inquiry_request(authorization, options)
219
+ build_request do |xml|
220
+ xml.inquiry do
221
+ xml.orderInquiry 'orderCode' => authorization
222
+ end
223
+ end
224
+ end
225
+
226
+ def build_authorization_request(money, payment_method, options)
227
+ build_request do |xml|
228
+ xml.submit do
229
+ xml.order order_tag_attributes(options) do
230
+ xml.description(options[:description].blank? ? 'Purchase' : options[:description])
231
+ add_amount(xml, money, options)
232
+ add_order_content(xml, options)
233
+ add_payment_method(xml, money, payment_method, options)
234
+ add_shopper(xml, options)
235
+ add_statement_narrative(xml, options)
236
+ add_risk_data(xml, options[:risk_data]) if options[:risk_data]
237
+ add_sub_merchant_data(xml, options[:sub_merchant_data]) if options[:sub_merchant_data]
238
+ add_hcg_additional_data(xml, options) if options[:hcg_additional_data]
239
+ add_instalments_data(xml, options) if options[:instalments]
240
+ add_moto_flag(xml, options) if options.dig(:metadata, :manual_entry)
241
+ add_additional_3ds_data(xml, options) if options[:execute_threed] && options[:three_ds_version] && options[:three_ds_version] =~ /^2/
242
+ add_3ds_exemption(xml, options) if options[:exemption_type]
243
+ end
244
+ end
245
+ end
246
+ end
247
+
248
+ def order_tag_attributes(options)
249
+ { 'orderCode' => clean_order_id(options[:order_id]), 'installationId' => options[:inst_id] || @options[:inst_id] }.reject { |_, v| !v.present? }
250
+ end
251
+
252
+ def clean_order_id(order_id)
253
+ order_id.to_s.gsub(/(\s|\||<|>|'|")/, '')[0..64]
254
+ end
255
+
256
+ def add_order_content(xml, options)
257
+ return unless options[:order_content]
258
+
259
+ xml.orderContent do
260
+ xml.cdata! options[:order_content]
261
+ end
262
+ end
263
+
264
+ def build_capture_request(money, authorization, options)
265
+ build_order_modify_request(authorization) do |xml|
266
+ xml.capture do
267
+ time = Time.now
268
+ xml.date 'dayOfMonth' => time.day, 'month' => time.month, 'year' => time.year
269
+ add_amount(xml, money, options)
270
+ end
271
+ end
272
+ end
273
+
274
+ def build_void_request(authorization, options)
275
+ if options[:cancel_or_refund]
276
+ build_order_modify_request(authorization, &:cancelOrRefund)
277
+ else
278
+ build_order_modify_request(authorization, &:cancel)
279
+ end
280
+ end
281
+
282
+ def build_refund_request(money, authorization, options)
283
+ build_order_modify_request(authorization) do |xml|
284
+ if options[:cancel_or_refund]
285
+ # Worldpay docs claim amount must be passed. This causes an error.
286
+ xml.cancelOrRefund # { add_amount(xml, money, options.merge(debit_credit_indicator: 'credit')) }
287
+ else
288
+ xml.refund do
289
+ add_amount(xml, money, options.merge(debit_credit_indicator: 'credit'))
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ def build_store_request(credit_card, options)
296
+ build_request do |xml|
297
+ xml.submit do
298
+ xml.paymentTokenCreate do
299
+ add_authenticated_shopper_id(xml, options)
300
+ xml.createToken
301
+ xml.paymentInstrument do
302
+ xml.cardDetails do
303
+ add_card(xml, credit_card, options)
304
+ end
305
+ end
306
+ end
307
+ end
308
+ end
309
+ end
310
+
311
+ def build_fast_fund_credit_request(money, payment_method, options)
312
+ build_request do |xml|
313
+ xml.submit do
314
+ xml.order order_tag_attributes(options) do
315
+ xml.description(options[:description].blank? ? 'Fast Fund Credit' : options[:description])
316
+ add_amount(xml, money, options)
317
+ add_order_content(xml, options)
318
+ add_payment_details_for_ff_credit(xml, payment_method, options)
319
+ add_shopper_id(xml, options)
320
+ end
321
+ end
322
+ end
323
+ end
324
+
325
+ def add_payment_details_for_ff_credit(xml, payment_method, options)
326
+ xml.paymentDetails do
327
+ xml.tag! 'FF_DISBURSE-SSL' do
328
+ if payment_method.is_a?(CreditCard)
329
+ add_card_for_ff_credit(xml, payment_method, options)
330
+ else
331
+ add_token_for_ff_credit(xml, payment_method, options)
332
+ end
333
+ end
334
+ end
335
+ end
336
+
337
+ def add_card_for_ff_credit(xml, payment_method, options)
338
+ xml.recipient do
339
+ xml.paymentInstrument do
340
+ xml.cardDetails do
341
+ add_card(xml, payment_method, options)
342
+ end
343
+ end
344
+ end
345
+ end
346
+
347
+ def add_token_for_ff_credit(xml, payment_method, options)
348
+ return unless payment_method.is_a?(String)
349
+
350
+ token_details = token_details_from_authorization(payment_method)
351
+
352
+ xml.tag! 'recipient', 'tokenScope' => token_details[:token_scope] do
353
+ xml.paymentTokenID token_details[:token_id]
354
+ add_authenticated_shopper_id(xml, token_details)
355
+ end
356
+ end
357
+
358
+ def add_additional_3ds_data(xml, options)
359
+ additional_data = { 'dfReferenceId' => options[:session_id] }
360
+ additional_data['challengeWindowSize'] = options[:browser_size] if options[:browser_size]
361
+
362
+ xml.additional3DSData additional_data
363
+ end
364
+
365
+ def add_3ds_exemption(xml, options)
366
+ xml.exemption 'type' => options[:exemption_type], 'placement' => options[:exemption_placement] || 'AUTHORISATION'
367
+ end
368
+
369
+ def add_risk_data(xml, risk_data)
370
+ xml.riskData do
371
+ add_authentication_risk_data(xml, risk_data[:authentication_risk_data])
372
+ add_shopper_account_risk_data(xml, risk_data[:shopper_account_risk_data])
373
+ add_transaction_risk_data(xml, risk_data[:transaction_risk_data])
374
+ end
375
+ end
376
+
377
+ def add_authentication_risk_data(xml, authentication_risk_data)
378
+ return unless authentication_risk_data
379
+
380
+ timestamp = authentication_risk_data.fetch(:authentication_date, {})
381
+
382
+ xml.authenticationRiskData('authenticationMethod' => authentication_risk_data[:authentication_method]) do
383
+ xml.authenticationTimestamp do
384
+ xml.date(
385
+ 'dayOfMonth' => timestamp[:day_of_month],
386
+ 'month' => timestamp[:month],
387
+ 'year' => timestamp[:year],
388
+ 'hour' => timestamp[:hour],
389
+ 'minute' => timestamp[:minute],
390
+ 'second' => timestamp[:second]
391
+ )
392
+ end
393
+ end
394
+ end
395
+
396
+ def add_sub_merchant_data(xml, options)
397
+ xml.subMerchantData do
398
+ xml.pfId options[:pf_id] if options[:pf_id]
399
+ xml.subName options[:sub_name] if options[:sub_name]
400
+ xml.subId options[:sub_id] if options[:sub_id]
401
+ xml.subStreet options[:sub_street] if options[:sub_street]
402
+ xml.subCity options[:sub_city] if options[:sub_city]
403
+ xml.subState options[:sub_state] if options[:sub_state]
404
+ xml.subCountryCode options[:sub_country_code] if options[:sub_country_code]
405
+ xml.subPostalCode options[:sub_postal_code] if options[:sub_postal_code]
406
+ xml.subTaxId options[:sub_tax_id] if options[:sub_tax_id]
407
+ end
408
+ end
409
+
410
+ def add_shopper_account_risk_data(xml, shopper_account_risk_data)
411
+ return unless shopper_account_risk_data
412
+
413
+ data = {
414
+ 'transactionsAttemptedLastDay' => shopper_account_risk_data[:transactions_attempted_last_day],
415
+ 'transactionsAttemptedLastYear' => shopper_account_risk_data[:transactions_attempted_last_year],
416
+ 'purchasesCompletedLastSixMonths' => shopper_account_risk_data[:purchases_completed_last_six_months],
417
+ 'addCardAttemptsLastDay' => shopper_account_risk_data[:add_card_attempts_last_day],
418
+ 'previousSuspiciousActivity' => shopper_account_risk_data[:previous_suspicious_activity],
419
+ 'shippingNameMatchesAccountName' => shopper_account_risk_data[:shipping_name_matches_account_name],
420
+ 'shopperAccountAgeIndicator' => shopper_account_risk_data[:shopper_account_age_indicator],
421
+ 'shopperAccountChangeIndicator' => shopper_account_risk_data[:shopper_account_change_indicator],
422
+ 'shopperAccountPasswordChangeIndicator' => shopper_account_risk_data[:shopper_account_password_change_indicator],
423
+ 'shopperAccountShippingAddressUsageIndicator' => shopper_account_risk_data[:shopper_account_shipping_address_usage_indicator],
424
+ 'shopperAccountPaymentAccountIndicator' => shopper_account_risk_data[:shopper_account_payment_account_indicator]
425
+ }.reject { |_k, v| v.nil? }
426
+
427
+ xml.shopperAccountRiskData(data) do
428
+ add_date_element(xml, 'shopperAccountCreationDate', shopper_account_risk_data[:shopper_account_creation_date])
429
+ add_date_element(xml, 'shopperAccountModificationDate', shopper_account_risk_data[:shopper_account_modification_date])
430
+ add_date_element(xml, 'shopperAccountPasswordChangeDate', shopper_account_risk_data[:shopper_account_password_change_date])
431
+ add_date_element(xml, 'shopperAccountShippingAddressFirstUseDate', shopper_account_risk_data[:shopper_account_shipping_address_first_use_date])
432
+ add_date_element(xml, 'shopperAccountPaymentAccountFirstUseDate', shopper_account_risk_data[:shopper_account_payment_account_first_use_date])
433
+ end
434
+ end
435
+
436
+ def add_transaction_risk_data(xml, transaction_risk_data)
437
+ return unless transaction_risk_data
438
+
439
+ data = {
440
+ 'shippingMethod' => transaction_risk_data[:shipping_method],
441
+ 'deliveryTimeframe' => transaction_risk_data[:delivery_timeframe],
442
+ 'deliveryEmailAddress' => transaction_risk_data[:delivery_email_address],
443
+ 'reorderingPreviousPurchases' => transaction_risk_data[:reordering_previous_purchases],
444
+ 'preOrderPurchase' => transaction_risk_data[:pre_order_purchase],
445
+ 'giftCardCount' => transaction_risk_data[:gift_card_count]
446
+ }.reject { |_k, v| v.nil? }
447
+
448
+ xml.transactionRiskData(data) do
449
+ xml.transactionRiskDataGiftCardAmount do
450
+ amount_hash = {
451
+ 'value' => transaction_risk_data.dig(:transaction_risk_data_gift_card_amount, :value),
452
+ 'currencyCode' => transaction_risk_data.dig(:transaction_risk_data_gift_card_amount, :currency),
453
+ 'exponent' => transaction_risk_data.dig(:transaction_risk_data_gift_card_amount, :exponent)
454
+ }
455
+ debit_credit_indicator = transaction_risk_data.dig(:transaction_risk_data_gift_card_amount, :debit_credit_indicator)
456
+ amount_hash['debitCreditIndicator'] = debit_credit_indicator if debit_credit_indicator
457
+ xml.amount(amount_hash)
458
+ end
459
+ add_date_element(xml, 'transactionRiskDataPreOrderDate', transaction_risk_data[:transaction_risk_data_pre_order_date])
460
+ end
461
+ end
462
+
463
+ def add_date_element(xml, name, date)
464
+ xml.tag! name do
465
+ xml.date('dayOfMonth' => date[:day_of_month], 'month' => date[:month], 'year' => date[:year])
466
+ end
467
+ end
468
+
469
+ def add_amount(xml, money, options)
470
+ currency = options[:currency] || currency(money)
471
+
472
+ amount_hash = {
473
+ :value => localized_amount(money, currency),
474
+ 'currencyCode' => currency,
475
+ 'exponent' => currency_exponent(currency)
476
+ }
477
+
478
+ amount_hash['debitCreditIndicator'] = options[:debit_credit_indicator] if options[:debit_credit_indicator]
479
+
480
+ xml.amount amount_hash
481
+ end
482
+
483
+ def add_payment_method(xml, amount, payment_method, options)
484
+ case options[:payment_type]
485
+ when :pay_as_order
486
+ add_amount_for_pay_as_order(xml, amount, payment_method, options)
487
+ when :network_token
488
+ add_network_tokenization_card(xml, payment_method)
489
+ else
490
+ add_card_or_token(xml, payment_method, options)
491
+ end
492
+ end
493
+
494
+ def add_amount_for_pay_as_order(xml, amount, payment_method, options)
495
+ if options[:merchant_code]
496
+ xml.payAsOrder 'orderCode' => payment_method, 'merchantCode' => options[:merchant_code] do
497
+ add_amount(xml, amount, options)
498
+ end
499
+ else
500
+ xml.payAsOrder 'orderCode' => payment_method do
501
+ add_amount(xml, amount, options)
502
+ end
503
+ end
504
+ end
505
+
506
+ def add_network_tokenization_card(xml, payment_method)
507
+ token_type = NETWORK_TOKEN_TYPE.fetch(payment_method.source, 'NETWORKTOKEN')
508
+
509
+ xml.paymentDetails do
510
+ xml.tag! 'EMVCO_TOKEN-SSL', 'type' => token_type do
511
+ xml.tokenNumber payment_method.number
512
+ xml.expiryDate do
513
+ xml.date(
514
+ 'month' => format(payment_method.month, :two_digits),
515
+ 'year' => format(payment_method.year, :four_digits_year)
516
+ )
517
+ end
518
+ name = card_holder_name(payment_method, options)
519
+ eci = format(payment_method.eci, :two_digits)
520
+ xml.cardHolderName name if name.present?
521
+ xml.cryptogram payment_method.payment_cryptogram
522
+ xml.eciIndicator eci.empty? ? '07' : eci
523
+ end
524
+ end
525
+ end
526
+
527
+ def add_card_or_token(xml, payment_method, options)
528
+ xml.paymentDetails credit_fund_transfer_attribute(options) do
529
+ if options[:payment_type] == :token
530
+ add_token_details(xml, options)
531
+ else
532
+ add_card_details(xml, payment_method, options)
533
+ end
534
+ add_stored_credential_options(xml, options)
535
+ add_shopper_id(xml, options)
536
+ add_three_d_secure(xml, options)
537
+ end
538
+ end
539
+
540
+ def add_token_details(xml, options)
541
+ xml.tag! 'TOKEN-SSL', 'tokenScope' => options[:token_scope] do
542
+ xml.paymentTokenID options[:token_id]
543
+ end
544
+ end
545
+
546
+ def add_card_details(xml, payment_method, options)
547
+ xml.tag! card_code_for(payment_method) do
548
+ add_card(xml, payment_method, options)
549
+ end
550
+ end
551
+
552
+ def add_shopper_id(xml, options)
553
+ if options[:ip] && options[:session_id]
554
+ xml.session 'shopperIPAddress' => options[:ip], 'id' => options[:session_id]
555
+ else
556
+ xml.session 'shopperIPAddress' => options[:ip] if options[:ip]
557
+ xml.session 'id' => options[:session_id] if options[:session_id]
558
+ end
559
+ end
560
+
561
+ def add_three_d_secure(xml, options)
562
+ return unless three_d_secure = options[:three_d_secure]
563
+
564
+ xml.info3DSecure do
565
+ xml.threeDSVersion three_d_secure[:version]
566
+ if three_d_secure[:version] && three_d_secure[:ds_transaction_id]
567
+ xml.dsTransactionId three_d_secure[:ds_transaction_id]
568
+ else
569
+ xml.xid three_d_secure[:xid]
570
+ end
571
+ xml.cavv three_d_secure[:cavv]
572
+ xml.eci three_d_secure[:eci]
573
+ end
574
+ end
575
+
576
+ def add_card(xml, payment_method, options)
577
+ xml.cardNumber payment_method.number
578
+ xml.expiryDate do
579
+ xml.date(
580
+ 'month' => format(payment_method.month, :two_digits),
581
+ 'year' => format(payment_method.year, :four_digits_year)
582
+ )
583
+ end
584
+ xml.cardHolderName card_holder_name(payment_method, options)
585
+ xml.cvc payment_method.verification_value
586
+
587
+ add_address(xml, (options[:billing_address] || options[:address]), options)
588
+ end
589
+
590
+ def add_stored_credential_options(xml, options = {})
591
+ if options[:stored_credential]
592
+ add_stored_credential_using_normalized_fields(xml, options)
593
+ else
594
+ add_stored_credential_using_gateway_specific_fields(xml, options)
595
+ end
596
+ end
597
+
598
+ def add_stored_credential_using_normalized_fields(xml, options)
599
+ if options[:stored_credential][:initial_transaction]
600
+ xml.storedCredentials 'usage' => 'FIRST'
601
+ else
602
+ reason = case options[:stored_credential][:reason_type]
603
+ when 'installment' then 'INSTALMENT'
604
+ when 'recurring' then 'RECURRING'
605
+ when 'unscheduled' then 'UNSCHEDULED'
606
+ end
607
+
608
+ xml.storedCredentials 'usage' => 'USED', 'merchantInitiatedReason' => reason do
609
+ xml.schemeTransactionIdentifier options[:stored_credential][:network_transaction_id] if options[:stored_credential][:network_transaction_id]
610
+ end
611
+ end
612
+ end
613
+
614
+ def add_stored_credential_using_gateway_specific_fields(xml, options)
615
+ return unless options[:stored_credential_usage]
616
+
617
+ if options[:stored_credential_initiated_reason]
618
+ xml.storedCredentials 'usage' => options[:stored_credential_usage], 'merchantInitiatedReason' => options[:stored_credential_initiated_reason] do
619
+ xml.schemeTransactionIdentifier options[:stored_credential_transaction_id] if options[:stored_credential_transaction_id]
620
+ end
621
+ else
622
+ xml.storedCredentials 'usage' => options[:stored_credential_usage]
623
+ end
624
+ end
625
+
626
+ def add_shopper(xml, options)
627
+ return unless options[:execute_threed] || options[:email] || options[:customer]
628
+
629
+ xml.shopper do
630
+ xml.shopperEmailAddress options[:email] if options[:email]
631
+ add_authenticated_shopper_id(xml, options)
632
+ xml.browser do
633
+ xml.acceptHeader options[:accept_header]
634
+ xml.userAgentHeader options[:user_agent]
635
+ end
636
+ end
637
+ end
638
+
639
+ def add_statement_narrative(xml, options)
640
+ xml.statementNarrative truncate(options[:statement_narrative], 50) if options[:statement_narrative]
641
+ end
642
+
643
+ def add_authenticated_shopper_id(xml, options)
644
+ xml.authenticatedShopperID options[:customer] if options[:customer]
645
+ end
646
+
647
+ def add_address(xml, address, options)
648
+ return unless address
649
+
650
+ address = address_with_defaults(address)
651
+
652
+ xml.cardAddress do
653
+ xml.address do
654
+ if m = /^\s*([^\s]+)\s+(.+)$/.match(address[:name])
655
+ xml.firstName m[1]
656
+ xml.lastName m[2]
657
+ end
658
+ xml.address1 address[:address1]
659
+ xml.address2 address[:address2] if address[:address2]
660
+ xml.postalCode address[:zip]
661
+ xml.city address[:city]
662
+ xml.state address[:state] unless address[:country] != 'US' && options[:execute_threed]
663
+ xml.countryCode address[:country]
664
+ xml.telephoneNumber address[:phone] if address[:phone]
665
+ end
666
+ end
667
+ end
668
+
669
+ def add_hcg_additional_data(xml, options)
670
+ xml.hcgAdditionalData do
671
+ options[:hcg_additional_data].each do |k, v|
672
+ xml.param({ name: k.to_s }, v)
673
+ end
674
+ end
675
+ end
676
+
677
+ def add_instalments_data(xml, options)
678
+ xml.thirdPartyData do
679
+ xml.instalments options[:instalments]
680
+ xml.cpf options[:cpf] if options[:cpf]
681
+ end
682
+ end
683
+
684
+ def add_moto_flag(xml, options)
685
+ xml.dynamicInteractionType 'type' => 'MOTO'
686
+ end
687
+
688
+ def address_with_defaults(address)
689
+ address ||= {}
690
+ address.delete_if { |_, v| v.blank? }
691
+ address.reverse_merge!(default_address)
692
+ end
693
+
694
+ def default_address
695
+ {
696
+ zip: '0000',
697
+ country: 'US',
698
+ city: 'N/A',
699
+ address1: 'N/A'
700
+ }
701
+ end
702
+
703
+ def parse(action, xml)
704
+ xml = xml.strip.gsub(/\&/, '&amp;')
705
+ doc = Nokogiri::XML(xml, &:strict)
706
+ doc.remove_namespaces!
707
+ resp_params = { action: action }
708
+
709
+ parse_elements(doc.root, resp_params)
710
+ resp_params
711
+ end
712
+
713
+ def parse_elements(node, response)
714
+ node_name = node.name.underscore
715
+ node.attributes.each do |k, v|
716
+ response["#{node_name}_#{k.underscore}".to_sym] = v.value
717
+ end
718
+ if node.elements.empty?
719
+ response[node_name.to_sym] = node.text unless node.text.blank?
720
+ else
721
+ response[node_name.to_sym] = true unless node.name.blank?
722
+ node.elements.each do |childnode|
723
+ parse_elements(childnode, response)
724
+ end
725
+ end
726
+ end
727
+
728
+ def headers(options)
729
+ idempotency_key = options[:idempotency_key]
730
+
731
+ headers = {
732
+ 'Content-Type' => 'text/xml',
733
+ 'Authorization' => encoded_credentials
734
+ }
735
+ if options[:cookie]
736
+ headers['Cookie'] = options[:cookie] if options[:cookie]
737
+ end
738
+
739
+ headers['Idempotency-Key'] = idempotency_key if idempotency_key
740
+ headers
741
+ end
742
+
743
+ def commit(action, request, *success_criteria, options)
744
+ xml = ssl_post(url, request, headers(options))
745
+ raw = parse(action, xml)
746
+ if options[:execute_threed]
747
+ raw[:cookie] = @cookie if defined?(@cookie)
748
+ raw[:session_id] = options[:session_id]
749
+ raw[:is3DSOrder] = true
750
+ end
751
+ success = success_from(action, raw, success_criteria)
752
+ message = message_from(success, raw, success_criteria)
753
+
754
+ Response.new(
755
+ success,
756
+ message,
757
+ raw,
758
+ authorization: authorization_from(action, raw, options),
759
+ error_code: error_code_from(success, raw),
760
+ test: test?,
761
+ avs_result: AVSResult.new(code: AVS_CODE_MAP[raw[:avs_result_code_description]]),
762
+ cvv_result: CVVResult.new(CVC_CODE_MAP[raw[:cvc_result_code_description]])
763
+ )
764
+ rescue Nokogiri::SyntaxError
765
+ unparsable_response(xml)
766
+ rescue ActiveMerchant::ResponseError => e
767
+ if e.response.code.to_s == '401'
768
+ return Response.new(false, 'Invalid credentials', {}, test: test?)
769
+ else
770
+ raise e
771
+ end
772
+ end
773
+
774
+ def url
775
+ test? ? self.test_url : self.live_url
776
+ end
777
+
778
+ def unparsable_response(raw_response)
779
+ message = 'Unparsable response received from Worldpay. Please contact Worldpay if you continue to receive this message.'
780
+ message += " (The raw response returned by the API was: #{raw_response.inspect})"
781
+ return Response.new(false, message)
782
+ end
783
+
784
+ # Override the regular handle response so we can access the headers
785
+ # Set-Cookie value is needed for 3DS transactions
786
+ def handle_response(response)
787
+ case response.code.to_i
788
+ when 200...300
789
+ @cookie = response['Set-Cookie']
790
+ response.body
791
+ else
792
+ raise ResponseError.new(response)
793
+ end
794
+ end
795
+
796
+ def success_from(action, raw, success_criteria)
797
+ success_criteria_success?(raw, success_criteria) || action_success?(action, raw)
798
+ end
799
+
800
+ def message_from(success, raw, success_criteria)
801
+ return 'SUCCESS' if success
802
+
803
+ raw[:iso8583_return_code_description] || raw[:error] || required_status_message(raw, success_criteria)
804
+ end
805
+
806
+ # success_criteria can be:
807
+ # - a string or an array of strings (if one of many responses)
808
+ # - An array of strings if one of many responses could be considered a
809
+ # success.
810
+ def success_criteria_success?(raw, success_criteria)
811
+ return if raw[:error]
812
+
813
+ raw[:ok].present? || (success_criteria.include?(raw[:last_event]) if raw[:last_event])
814
+ end
815
+
816
+ def action_success?(action, raw)
817
+ case action
818
+ when 'store'
819
+ raw[:token].present?
820
+ else
821
+ false
822
+ end
823
+ end
824
+
825
+ def error_code_from(success, raw)
826
+ raw[:iso8583_return_code_code] || raw[:error_code] || nil unless success == 'SUCCESS'
827
+ end
828
+
829
+ def required_status_message(raw, success_criteria)
830
+ "A transaction status of #{success_criteria.collect { |c| "'#{c}'" }.join(' or ')} is required." if !success_criteria.include?(raw[:last_event])
831
+ end
832
+
833
+ def authorization_from(action, raw, options)
834
+ order_id = order_id_from(raw)
835
+
836
+ case action
837
+ when 'store'
838
+ authorization_from_token_details(
839
+ order_id: order_id,
840
+ token_id: raw[:payment_token_id],
841
+ token_scope: 'shopper',
842
+ customer: options[:customer]
843
+ )
844
+ else
845
+ order_id
846
+ end
847
+ end
848
+
849
+ def order_id_from(raw)
850
+ pair = raw.detect { |k, _v| k.to_s =~ /_order_code$/ }
851
+ (pair ? pair.last : nil)
852
+ end
853
+
854
+ def authorization_from_token_details(options = {})
855
+ [options[:order_id], options[:token_id], options[:token_scope], options[:customer]].join('|')
856
+ end
857
+
858
+ def order_id_from_authorization(authorization)
859
+ token_details_from_authorization(authorization)[:order_id]
860
+ end
861
+
862
+ def token_details_from_authorization(authorization)
863
+ order_id, token_id, token_scope, customer = authorization.split('|')
864
+
865
+ token_details = {}
866
+ token_details[:order_id] = order_id if order_id.present?
867
+ token_details[:token_id] = token_id if token_id.present?
868
+ token_details[:token_scope] = token_scope if token_scope.present?
869
+ token_details[:customer] = customer if customer.present?
870
+
871
+ token_details
872
+ end
873
+
874
+ def payment_details(payment_method)
875
+ case payment_method
876
+ when String
877
+ token_type_and_details(payment_method)
878
+ when NetworkTokenizationCreditCard
879
+ { payment_type: :network_token }
880
+ else
881
+ { payment_type: :credit }
882
+ end
883
+ end
884
+
885
+ def token_type_and_details(token)
886
+ token_details = token_details_from_authorization(token)
887
+ token_details[:payment_type] = token_details.has_key?(:token_id) ? :token : :pay_as_order
888
+
889
+ token_details
890
+ end
891
+
892
+ def credit_fund_transfer_attribute(options)
893
+ return unless options[:credit]
894
+
895
+ { 'action' => 'REFUND' }
896
+ end
897
+
898
+ def encoded_credentials
899
+ credentials = "#{@options[:login]}:#{@options[:password]}"
900
+ "Basic #{[credentials].pack('m').strip}"
901
+ end
902
+
903
+ def currency_exponent(currency)
904
+ return 0 if non_fractional_currency?(currency)
905
+ return 3 if three_decimal_currency?(currency)
906
+
907
+ return 2
908
+ end
909
+
910
+ def card_code_for(payment_method)
911
+ CARD_CODES[card_brand(payment_method)] || CARD_CODES['unknown']
912
+ end
913
+
914
+ def eligible_for_0_auth?(payment_method, options = {})
915
+ payment_method.is_a?(CreditCard) && %w(visa master).include?(payment_method.brand) && options[:zero_dollar_auth]
916
+ end
917
+
918
+ def card_holder_name(payment_method, options)
919
+ test? && options[:execute_threed] && !options[:three_ds_version]&.start_with?('2') ? '3D' : payment_method.name
920
+ end
921
+ end
922
+ end
923
+ end