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,813 @@
1
+ require 'active_support/core_ext/hash/slice'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ # This gateway uses an older version of the Stripe API.
6
+ # To utilize the updated {Payment Intents API}[https://stripe.com/docs/api/payment_intents], integrate with the StripePaymentIntents gateway
7
+ class StripeGateway < Gateway
8
+ self.live_url = 'https://api.stripe.com/v1/'
9
+
10
+ AVS_CODE_TRANSLATOR = {
11
+ 'line1: pass, zip: pass' => 'Y',
12
+ 'line1: pass, zip: fail' => 'A',
13
+ 'line1: pass, zip: unchecked' => 'B',
14
+ 'line1: fail, zip: pass' => 'Z',
15
+ 'line1: fail, zip: fail' => 'N',
16
+ 'line1: unchecked, zip: pass' => 'P',
17
+ 'line1: unchecked, zip: unchecked' => 'I'
18
+ }
19
+
20
+ CVC_CODE_TRANSLATOR = {
21
+ 'pass' => 'M',
22
+ 'fail' => 'N',
23
+ 'unchecked' => 'P'
24
+ }
25
+
26
+ DEFAULT_API_VERSION = '2015-04-07'
27
+
28
+ self.supported_countries = %w(AE AT AU BE BG BR CA CH CY CZ DE DK EE ES FI FR GB GR HK HU IE IN IT JP LT LU LV MT MX MY NL NO NZ PL PT RO SE SG SI SK US)
29
+ self.default_currency = 'USD'
30
+ self.money_format = :cents
31
+ self.supported_cardtypes = %i[visa master american_express discover jcb diners_club maestro unionpay]
32
+ self.currencies_without_fractions = %w(BIF CLP DJF GNF JPY KMF KRW MGA PYG RWF VND VUV XAF XOF XPF UGX)
33
+
34
+ self.homepage_url = 'https://stripe.com/'
35
+ self.display_name = 'Stripe'
36
+
37
+ STANDARD_ERROR_CODE_MAPPING = {
38
+ 'incorrect_number' => STANDARD_ERROR_CODE[:incorrect_number],
39
+ 'invalid_number' => STANDARD_ERROR_CODE[:invalid_number],
40
+ 'invalid_expiry_month' => STANDARD_ERROR_CODE[:invalid_expiry_date],
41
+ 'invalid_expiry_year' => STANDARD_ERROR_CODE[:invalid_expiry_date],
42
+ 'invalid_cvc' => STANDARD_ERROR_CODE[:invalid_cvc],
43
+ 'expired_card' => STANDARD_ERROR_CODE[:expired_card],
44
+ 'incorrect_cvc' => STANDARD_ERROR_CODE[:incorrect_cvc],
45
+ 'incorrect_zip' => STANDARD_ERROR_CODE[:incorrect_zip],
46
+ 'card_declined' => STANDARD_ERROR_CODE[:card_declined],
47
+ 'call_issuer' => STANDARD_ERROR_CODE[:call_issuer],
48
+ 'processing_error' => STANDARD_ERROR_CODE[:processing_error],
49
+ 'incorrect_pin' => STANDARD_ERROR_CODE[:incorrect_pin],
50
+ 'test_mode_live_card' => STANDARD_ERROR_CODE[:test_mode_live_card],
51
+ 'pickup_card' => STANDARD_ERROR_CODE[:pickup_card]
52
+ }
53
+
54
+ BANK_ACCOUNT_HOLDER_TYPE_MAPPING = {
55
+ 'personal' => 'individual',
56
+ 'business' => 'company'
57
+ }
58
+
59
+ MINIMUM_AUTHORIZE_AMOUNTS = {
60
+ 'USD' => 100,
61
+ 'CAD' => 100,
62
+ 'GBP' => 60,
63
+ 'EUR' => 100,
64
+ 'DKK' => 500,
65
+ 'NOK' => 600,
66
+ 'SEK' => 600,
67
+ 'CHF' => 100,
68
+ 'AUD' => 100,
69
+ 'JPY' => 100,
70
+ 'MXN' => 2000,
71
+ 'SGD' => 100,
72
+ 'HKD' => 800
73
+ }
74
+
75
+ def initialize(options = {})
76
+ requires!(options, :login)
77
+ @api_key = options[:login]
78
+ @fee_refund_api_key = options[:fee_refund_login]
79
+ super
80
+ end
81
+
82
+ def authorize(money, payment, options = {})
83
+ if ach?(payment)
84
+ direct_bank_error = 'Direct bank account transactions are not supported for authorize.'
85
+ return Response.new(false, direct_bank_error)
86
+ end
87
+
88
+ MultiResponse.run do |r|
89
+ if payment.is_a?(ApplePayPaymentToken)
90
+ r.process { tokenize_apple_pay_token(payment) }
91
+ payment = StripePaymentToken.new(r.params['token']) if r.success?
92
+ end
93
+ r.process do
94
+ post = create_post_for_auth_or_purchase(money, payment, options)
95
+ add_application_fee(post, options) if emv_payment?(payment)
96
+ post[:capture] = 'false'
97
+ commit(:post, 'charges', post, options)
98
+ end
99
+ end.responses.last
100
+ end
101
+
102
+ # To create a charge on a card or a token, call
103
+ #
104
+ # purchase(money, card_hash_or_token, { ... })
105
+ #
106
+ # To create a charge on a customer, call
107
+ #
108
+ # purchase(money, nil, { :customer => id, ... })
109
+ def purchase(money, payment, options = {})
110
+ if ach?(payment)
111
+ direct_bank_error = 'Direct bank account transactions are not supported. Bank accounts must be stored and verified before use.'
112
+ return Response.new(false, direct_bank_error)
113
+ end
114
+
115
+ MultiResponse.run do |r|
116
+ if payment.is_a?(ApplePayPaymentToken)
117
+ r.process { tokenize_apple_pay_token(payment) }
118
+ payment = StripePaymentToken.new(r.params['token']) if r.success?
119
+ end
120
+ r.process do
121
+ post = create_post_for_auth_or_purchase(money, payment, options)
122
+ post[:card][:processing_method] = 'quick_chip' if quickchip_payment?(payment)
123
+ commit(:post, 'charges', post, options)
124
+ end
125
+ end.responses.last
126
+ end
127
+
128
+ def capture(money, authorization, options = {})
129
+ post = {}
130
+
131
+ if emv_tc_response = options.delete(:icc_data)
132
+ # update the charge with emv data if card present
133
+ update = {}
134
+ update[:card] = { emv_approval_data: emv_tc_response }
135
+ commit(:post, "charges/#{CGI.escape(authorization)}", update, options)
136
+ else
137
+ add_application_fee(post, options)
138
+ add_amount(post, money, options)
139
+ add_exchange_rate(post, options)
140
+ end
141
+
142
+ commit(:post, "charges/#{CGI.escape(authorization)}/capture", post, options)
143
+ end
144
+
145
+ def void(identification, options = {})
146
+ post = {}
147
+ post[:metadata] = options[:metadata] if options[:metadata]
148
+ post[:reason] = options[:reason] if options[:reason]
149
+ post[:expand] = [:charge]
150
+ commit(:post, "charges/#{CGI.escape(identification)}/refunds", post, options)
151
+ end
152
+
153
+ def refund(money, identification, options = {})
154
+ post = {}
155
+ add_amount(post, money, options)
156
+ post[:refund_application_fee] = true if options[:refund_application_fee]
157
+ post[:reverse_transfer] = options[:reverse_transfer] if options[:reverse_transfer]
158
+ post[:metadata] = options[:metadata] if options[:metadata]
159
+ post[:reason] = options[:reason] if options[:reason]
160
+ post[:expand] = [:charge]
161
+
162
+ response = commit(:post, "charges/#{CGI.escape(identification)}/refunds", post, options)
163
+
164
+ if response.success? && options[:refund_fee_amount] && options[:refund_fee_amount].to_s != '0'
165
+ charge = api_request(:get, "charges/#{CGI.escape(identification)}", nil, options)
166
+
167
+ if application_fee = charge['application_fee']
168
+ fee_refund_options = {
169
+ currency: options[:currency], # currency isn't used by Stripe here, but we need it for #add_amount
170
+ key: @fee_refund_api_key
171
+ }
172
+ refund_application_fee(options[:refund_fee_amount].to_i, application_fee, fee_refund_options)
173
+ end
174
+ end
175
+
176
+ response
177
+ end
178
+
179
+ def verify(payment, options = {})
180
+ MultiResponse.run(:use_first_response) do |r|
181
+ r.process { authorize(auth_minimum_amount(options), payment, options) }
182
+ options[:idempotency_key] = nil
183
+ r.process(:ignore_result) { void(r.authorization, options) }
184
+ end
185
+ end
186
+
187
+ def refund_application_fee(money, identification, options = {})
188
+ post = {}
189
+ add_amount(post, money, options)
190
+ commit(:post, "application_fees/#{CGI.escape(identification)}/refunds", post, options)
191
+ end
192
+
193
+ # Note: creating a new credit card will not change the customer's existing default credit card (use :set_default => true)
194
+ def store(payment, options = {})
195
+ params = {}
196
+ post = {}
197
+
198
+ if payment.is_a?(ApplePayPaymentToken)
199
+ token_exchange_response = tokenize_apple_pay_token(payment)
200
+ params = { card: token_exchange_response.params['token']['id'] } if token_exchange_response.success?
201
+ elsif payment.is_a?(StripePaymentToken)
202
+ add_payment_token(params, payment, options)
203
+ elsif payment.is_a?(Check)
204
+ bank_token_response = tokenize_bank_account(payment)
205
+ return bank_token_response unless bank_token_response.success?
206
+
207
+ params = { source: bank_token_response.params['token']['id'] }
208
+ else
209
+ add_creditcard(params, payment, options)
210
+ end
211
+
212
+ post[:validate] = options[:validate] unless options[:validate].nil?
213
+ post[:description] = options[:description] if options[:description]
214
+ post[:email] = options[:email] if options[:email]
215
+
216
+ if options[:account]
217
+ add_external_account(post, params, payment)
218
+ commit(:post, "accounts/#{CGI.escape(options[:account])}/external_accounts", post, options)
219
+ elsif options[:customer]
220
+ MultiResponse.run(:first) do |r|
221
+ # The /cards endpoint does not update other customer parameters.
222
+ r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", params, options) }
223
+
224
+ post[:default_card] = r.params['id'] if options[:set_default] && r.success? && !r.params['id'].blank?
225
+
226
+ r.process { update_customer(options[:customer], post) } if post.count > 0
227
+ end
228
+ else
229
+ commit(:post, 'customers', post.merge(params), options)
230
+ end
231
+ end
232
+
233
+ def update(customer_id, card_id, options = {})
234
+ commit(:post, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", options, options)
235
+ end
236
+
237
+ def update_customer(customer_id, options = {})
238
+ commit(:post, "customers/#{CGI.escape(customer_id)}", options, options)
239
+ end
240
+
241
+ def unstore(identification, options = {}, deprecated_options = {})
242
+ customer_id, card_id = identification.split('|')
243
+
244
+ if options.kind_of?(String)
245
+ ActiveMerchant.deprecated 'Passing the card_id as the 2nd parameter is deprecated. The response authorization includes both the customer_id and the card_id.'
246
+ card_id ||= options
247
+ options = deprecated_options
248
+ end
249
+
250
+ commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, options)
251
+ end
252
+
253
+ def tokenize_apple_pay_token(apple_pay_payment_token, options = {})
254
+ token_response = api_request(:post, "tokens?pk_token=#{CGI.escape(apple_pay_payment_token.payment_data.to_json)}")
255
+ success = !token_response.key?('error')
256
+
257
+ if success && token_response.key?('id')
258
+ Response.new(success, nil, token: token_response)
259
+ else
260
+ Response.new(success, token_response['error']['message'])
261
+ end
262
+ end
263
+
264
+ def verify_credentials
265
+ begin
266
+ ssl_get(live_url + 'charges/nonexistent', headers)
267
+ rescue ResponseError => e
268
+ return false if e.response.code.to_i == 401
269
+ end
270
+
271
+ true
272
+ end
273
+
274
+ def supports_scrubbing?
275
+ true
276
+ end
277
+
278
+ def scrub(transcript)
279
+ transcript.
280
+ gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
281
+ gsub(%r((&?three_d_secure\[cryptogram\]=)[\w=]*(&?)), '\1[FILTERED]\2').
282
+ gsub(%r(((\[card\]|card)\[cryptogram\]=)[^&]+(&?)), '\1[FILTERED]\3').
283
+ gsub(%r(((\[card\]|card)\[cvc\]=)\d+), '\1[FILTERED]').
284
+ gsub(%r(((\[card\]|card)\[emv_approval_data\]=)[^&]+(&?)), '\1[FILTERED]\3').
285
+ gsub(%r(((\[card\]|card)\[emv_auth_data\]=)[^&]+(&?)), '\1[FILTERED]\3').
286
+ gsub(%r(((\[card\]|card)\[encrypted_pin\]=)[^&]+(&?)), '\1[FILTERED]\3').
287
+ gsub(%r(((\[card\]|card)\[encrypted_pin_key_id\]=)[\w=]+(&?)), '\1[FILTERED]\3').
288
+ gsub(%r(((\[card\]|card)\[number\]=)\d+), '\1[FILTERED]').
289
+ gsub(%r(((\[card\]|card)\[swipe_data\]=)[^&]+(&?)), '\1[FILTERED]\3').
290
+ gsub(%r(((\[bank_account\]|bank_account)\[account_number\]=)\d+), '\1[FILTERED]')
291
+ end
292
+
293
+ def supports_network_tokenization?
294
+ true
295
+ end
296
+
297
+ private
298
+
299
+ class StripePaymentToken < PaymentToken
300
+ def type
301
+ 'stripe'
302
+ end
303
+ end
304
+
305
+ def create_source(money, payment, type, options = {})
306
+ post = {}
307
+ add_amount(post, money, options, true)
308
+ post[:type] = type
309
+ if type == 'card'
310
+ add_creditcard(post, payment, options, true)
311
+ add_source_owner(post, payment, options)
312
+ elsif type == 'three_d_secure'
313
+ post[:three_d_secure] = { card: payment }
314
+ post[:redirect] = { return_url: options[:redirect_url] }
315
+ end
316
+ commit(:post, 'sources', post, options)
317
+ end
318
+
319
+ def show_source(source_id, options)
320
+ commit(:get, "sources/#{source_id}", nil, options)
321
+ end
322
+
323
+ def create_webhook_endpoint(options, events)
324
+ post = {}
325
+ post[:url] = options[:callback_url]
326
+ post[:enabled_events] = events
327
+ post[:connect] = true if options[:stripe_account]
328
+ options.delete(:stripe_account)
329
+ commit(:post, 'webhook_endpoints', post, options)
330
+ end
331
+
332
+ def delete_webhook_endpoint(options)
333
+ commit(:delete, "webhook_endpoints/#{options[:webhook_id]}", {}, options)
334
+ end
335
+
336
+ def show_webhook_endpoint(options)
337
+ options.delete(:stripe_account)
338
+ commit(:get, "webhook_endpoints/#{options[:webhook_id]}", nil, options)
339
+ end
340
+
341
+ def list_webhook_endpoints(options)
342
+ params = {}
343
+ params[:limit] = options[:limit] if options[:limit]
344
+ options.delete(:stripe_account)
345
+ commit(:get, "webhook_endpoints?#{post_data(params)}", nil, options)
346
+ end
347
+
348
+ def create_post_for_auth_or_purchase(money, payment, options)
349
+ post = {}
350
+
351
+ if payment.is_a?(StripePaymentToken)
352
+ add_payment_token(post, payment, options)
353
+ else
354
+ add_creditcard(post, payment, options)
355
+ end
356
+
357
+ add_charge_details(post, money, payment, options)
358
+ post
359
+ end
360
+
361
+ # Used internally by Spreedly to populate the charge object for 3DS 1.0 transactions
362
+ def add_charge_details(post, money, payment, options)
363
+ if emv_payment?(payment)
364
+ add_statement_address(post, options)
365
+ add_emv_metadata(post, payment)
366
+ else
367
+ add_amount(post, money, options, true)
368
+ add_customer_data(post, options)
369
+ post[:description] = options[:description]
370
+ post[:statement_descriptor] = options[:statement_description]
371
+ post[:statement_descriptor_suffix] = options[:statement_descriptor_suffix] if options[:statement_descriptor_suffix]
372
+ post[:receipt_email] = options[:receipt_email] if options[:receipt_email]
373
+ add_customer(post, payment, options)
374
+ add_flags(post, options)
375
+ end
376
+
377
+ add_metadata(post, options)
378
+ add_application_fee(post, options)
379
+ add_exchange_rate(post, options)
380
+ add_destination(post, options)
381
+ add_level_three(post, options)
382
+ add_connected_account(post, options)
383
+ add_radar_data(post, options)
384
+ post
385
+ end
386
+
387
+ def add_amount(post, money, options, include_currency = false)
388
+ currency = options[:currency] || currency(money)
389
+ post[:amount] = localized_amount(money, currency)
390
+ post[:currency] = currency.downcase if include_currency
391
+ end
392
+
393
+ def add_application_fee(post, options)
394
+ post[:application_fee] = options[:application_fee] if options[:application_fee]
395
+ end
396
+
397
+ def add_exchange_rate(post, options)
398
+ post[:exchange_rate] = options[:exchange_rate] if options[:exchange_rate]
399
+ end
400
+
401
+ def add_destination(post, options)
402
+ if options[:destination]
403
+ post[:destination] = {}
404
+ post[:destination][:account] = options[:destination]
405
+ post[:destination][:amount] = options[:destination_amount] if options[:destination_amount]
406
+ end
407
+ end
408
+
409
+ def add_level_three(post, options)
410
+ level_three = {}
411
+
412
+ copy_when_present(level_three, [:merchant_reference], options)
413
+ copy_when_present(level_three, [:customer_reference], options)
414
+ copy_when_present(level_three, [:shipping_address_zip], options)
415
+ copy_when_present(level_three, [:shipping_from_zip], options)
416
+ copy_when_present(level_three, [:shipping_amount], options)
417
+ copy_when_present(level_three, [:line_items], options)
418
+
419
+ post[:level3] = level_three unless level_three.empty?
420
+ end
421
+
422
+ def add_expand_parameters(post, options)
423
+ post[:expand] ||= []
424
+ post[:expand].concat(Array.wrap(options[:expand]).map(&:to_sym)).uniq!
425
+ end
426
+
427
+ def add_external_account(post, card_params, payment)
428
+ external_account = {}
429
+ external_account[:object] = 'card'
430
+ external_account[:currency] = (options[:currency] || currency(payment)).downcase
431
+ post[:external_account] = external_account.merge(card_params[:card])
432
+ end
433
+
434
+ def add_customer_data(post, options)
435
+ metadata_options = %i[description ip user_agent referrer]
436
+ post.update(options.slice(*metadata_options))
437
+
438
+ post[:external_id] = options[:order_id]
439
+ post[:payment_user_agent] = "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}"
440
+ end
441
+
442
+ def add_address(post, options)
443
+ return unless post[:card]&.kind_of?(Hash)
444
+
445
+ if address = options[:billing_address] || options[:address]
446
+ post[:card][:address_line1] = address[:address1] if address[:address1]
447
+ post[:card][:address_line2] = address[:address2] if address[:address2]
448
+ post[:card][:address_country] = address[:country] if address[:country]
449
+ post[:card][:address_zip] = address[:zip] if address[:zip]
450
+ post[:card][:address_state] = address[:state] if address[:state]
451
+ post[:card][:address_city] = address[:city] if address[:city]
452
+ end
453
+ end
454
+
455
+ def add_statement_address(post, options)
456
+ return unless statement_address = options[:statement_address]
457
+ return unless %i[address1 city zip state].all? { |key| statement_address[key].present? }
458
+
459
+ post[:statement_address] = {}
460
+ post[:statement_address][:line1] = statement_address[:address1]
461
+ post[:statement_address][:line2] = statement_address[:address2] if statement_address[:address2].present?
462
+ post[:statement_address][:city] = statement_address[:city]
463
+ post[:statement_address][:postal_code] = statement_address[:zip]
464
+ post[:statement_address][:state] = statement_address[:state]
465
+ end
466
+
467
+ def add_creditcard(post, creditcard, options, use_sources = false)
468
+ card = {}
469
+ if emv_payment?(creditcard)
470
+ add_emv_creditcard(post, creditcard.icc_data)
471
+ post[:card][:read_method] = 'contactless' if creditcard.read_method == 'contactless'
472
+ post[:card][:read_method] = 'contactless_magstripe_mode' if creditcard.read_method == 'contactless_magstripe'
473
+ if creditcard.encrypted_pin_cryptogram.present? && creditcard.encrypted_pin_ksn.present?
474
+ post[:card][:encrypted_pin] = creditcard.encrypted_pin_cryptogram
475
+ post[:card][:encrypted_pin_key_id] = creditcard.encrypted_pin_ksn
476
+ end
477
+ elsif creditcard.respond_to?(:number)
478
+ if creditcard.respond_to?(:track_data) && creditcard.track_data.present?
479
+ card[:swipe_data] = creditcard.track_data
480
+ if creditcard.respond_to?(:read_method)
481
+ card[:fallback_reason] = 'no_chip' if creditcard.read_method == 'fallback_no_chip'
482
+ card[:fallback_reason] = 'chip_error' if creditcard.read_method == 'fallback_chip_error'
483
+ card[:read_method] = 'contactless_magstripe_mode' if creditcard.read_method == 'contactless_magstripe'
484
+ end
485
+ else
486
+ card[:number] = creditcard.number
487
+ card[:exp_month] = creditcard.month
488
+ card[:exp_year] = creditcard.year
489
+ card[:cvc] = creditcard.verification_value if creditcard.verification_value?
490
+ card[:name] = creditcard.name if creditcard.name && !use_sources
491
+ end
492
+
493
+ if creditcard.is_a?(NetworkTokenizationCreditCard)
494
+ card[:cryptogram] = creditcard.payment_cryptogram
495
+ card[:eci] = creditcard.eci.rjust(2, '0') if creditcard.eci =~ /^[0-9]+$/
496
+ card[:tokenization_method] = creditcard.source.to_s
497
+ end
498
+ post[:card] = card
499
+
500
+ add_address(post, options) unless use_sources
501
+ elsif creditcard.kind_of?(String)
502
+ if options[:track_data]
503
+ card[:swipe_data] = options[:track_data]
504
+ elsif creditcard.include?('|')
505
+ customer_id, card_id = creditcard.split('|')
506
+ card = card_id
507
+ post[:customer] = customer_id
508
+ else
509
+ card = creditcard
510
+ end
511
+ post[:card] = card
512
+ end
513
+ end
514
+
515
+ def add_emv_creditcard(post, icc_data, options = {})
516
+ post[:card] = { emv_auth_data: icc_data }
517
+ end
518
+
519
+ def add_payment_token(post, token, options = {})
520
+ post[:card] = token.payment_data['id']
521
+ end
522
+
523
+ def add_customer(post, payment, options)
524
+ post[:customer] = options[:customer] if options[:customer] && !payment.respond_to?(:number)
525
+ end
526
+
527
+ def add_flags(post, options)
528
+ post[:uncaptured] = true if options[:uncaptured]
529
+ post[:recurring] = true if options[:eci] == 'recurring' || options[:recurring]
530
+ end
531
+
532
+ def add_metadata(post, options = {})
533
+ post[:metadata] ||= {}
534
+ post[:metadata].merge!(options[:metadata]) if options[:metadata]
535
+ post[:metadata][:email] = options[:email] if options[:email]
536
+ post[:metadata][:order_id] = options[:order_id] if options[:order_id]
537
+ end
538
+
539
+ def add_emv_metadata(post, creditcard)
540
+ post[:metadata] ||= {}
541
+ post[:metadata][:card_read_method] = creditcard.read_method if creditcard.respond_to?(:read_method)
542
+ end
543
+
544
+ def add_source_owner(post, creditcard, options)
545
+ post[:owner] = {}
546
+ post[:owner][:name] = creditcard.name if creditcard.name
547
+ post[:owner][:email] = options[:email] if options[:email]
548
+
549
+ if address = options[:billing_address] || options[:address]
550
+ owner_address = {}
551
+ owner_address[:line1] = address[:address1] if address[:address1]
552
+ owner_address[:line2] = address[:address2] if address[:address2]
553
+ owner_address[:country] = address[:country] if address[:country]
554
+ owner_address[:postal_code] = address[:zip] if address[:zip]
555
+ owner_address[:state] = address[:state] if address[:state]
556
+ owner_address[:city] = address[:city] if address[:city]
557
+
558
+ post[:owner][:phone] = address[:phone] if address[:phone]
559
+ post[:owner][:address] = owner_address
560
+ end
561
+ end
562
+
563
+ def add_connected_account(post, options = {})
564
+ post[:on_behalf_of] = options[:on_behalf_of] if options[:on_behalf_of]
565
+
566
+ return unless options[:transfer_destination]
567
+
568
+ post[:transfer_data] = { destination: options[:transfer_destination] }
569
+ post[:transfer_data][:amount] = options[:transfer_amount] if options[:transfer_amount]
570
+ post[:transfer_group] = options[:transfer_group] if options[:transfer_group]
571
+ post[:application_fee_amount] = options[:application_fee_amount] if options[:application_fee_amount]
572
+ end
573
+
574
+ def add_radar_data(post, options = {})
575
+ return unless options[:radar_session_id]
576
+
577
+ post[:radar_options] = {
578
+ session: options[:radar_session_id]
579
+ }
580
+ end
581
+
582
+ def parse(body)
583
+ JSON.parse(body)
584
+ end
585
+
586
+ def post_data(params)
587
+ return nil unless params
588
+
589
+ flatten_params([], params).join('&')
590
+ end
591
+
592
+ def flatten_params(flattened, params, prefix = nil)
593
+ params.each do |key, value|
594
+ next if value != false && value.blank?
595
+
596
+ flattened_key = prefix.nil? ? key : "#{prefix}[#{key}]"
597
+ if value.is_a?(Hash)
598
+ flatten_params(flattened, value, flattened_key)
599
+ elsif value.is_a?(Array)
600
+ flatten_array(flattened, value, flattened_key)
601
+ else
602
+ flattened << "#{flattened_key}=#{CGI.escape(value.to_s)}"
603
+ end
604
+ end
605
+ flattened
606
+ end
607
+
608
+ def flatten_array(flattened, array, prefix)
609
+ array.each_with_index do |item, idx|
610
+ key = "#{prefix}[#{idx}]"
611
+ if item.is_a?(Hash)
612
+ flatten_params(flattened, item, key)
613
+ elsif item.is_a?(Array)
614
+ flatten_array(flattened, item, key)
615
+ else
616
+ flattened << "#{key}=#{CGI.escape(item.to_s)}"
617
+ end
618
+ end
619
+ end
620
+
621
+ def headers(options = {})
622
+ key = options[:key] || @api_key
623
+ idempotency_key = options[:idempotency_key]
624
+
625
+ headers = {
626
+ 'Authorization' => 'Basic ' + Base64.strict_encode64(key.to_s + ':').strip,
627
+ 'User-Agent' => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
628
+ 'Stripe-Version' => api_version(options),
629
+ 'X-Stripe-Client-User-Agent' => stripe_client_user_agent(options),
630
+ 'X-Stripe-Client-User-Metadata' => { ip: options[:ip] }.to_json
631
+ }
632
+ headers['Idempotency-Key'] = idempotency_key if idempotency_key
633
+ headers['Stripe-Account'] = options[:stripe_account] if options[:stripe_account]
634
+ headers
635
+ end
636
+
637
+ def stripe_client_user_agent(options)
638
+ return user_agent unless options[:application]
639
+
640
+ JSON.dump(JSON.parse(user_agent).merge!({ application: options[:application] }))
641
+ end
642
+
643
+ def api_version(options)
644
+ options[:version] || @options[:version] || self.class::DEFAULT_API_VERSION
645
+ end
646
+
647
+ def api_request(method, endpoint, parameters = nil, options = {})
648
+ raw_response = response = nil
649
+ begin
650
+ raw_response = ssl_request(method, self.live_url + endpoint, post_data(parameters), headers(options))
651
+ response = parse(raw_response)
652
+ rescue ResponseError => e
653
+ raw_response = e.response.body
654
+ response = response_error(raw_response)
655
+ rescue JSON::ParserError
656
+ response = json_error(raw_response)
657
+ end
658
+ response
659
+ end
660
+
661
+ def commit(method, url, parameters = nil, options = {})
662
+ add_expand_parameters(parameters, options) if parameters
663
+ response = api_request(method, url, parameters, options)
664
+ response['webhook_id'] = options[:webhook_id] if options[:webhook_id]
665
+ success = success_from(response, options)
666
+
667
+ card = card_from_response(response)
668
+ avs_code = AVS_CODE_TRANSLATOR["line1: #{card['address_line1_check']}, zip: #{card['address_zip_check']}"]
669
+ cvc_code = CVC_CODE_TRANSLATOR[card['cvc_check']]
670
+ Response.new(success,
671
+ message_from(success, response),
672
+ response,
673
+ test: response_is_test?(response),
674
+ authorization: authorization_from(success, url, method, response),
675
+ avs_result: { code: avs_code },
676
+ cvv_result: cvc_code,
677
+ emv_authorization: emv_authorization_from_response(response),
678
+ error_code: success ? nil : error_code_from(response))
679
+ end
680
+
681
+ def authorization_from(success, url, method, response)
682
+ return response.fetch('error', {})['charge'] unless success
683
+
684
+ if url == 'customers'
685
+ [response['id'], response.dig('sources', 'data').first&.dig('id')].join('|')
686
+ elsif method == :post && (url.match(/customers\/.*\/cards/) || url.match(/payment_methods\/.*\/attach/))
687
+ [response['customer'], response['id']].join('|')
688
+ else
689
+ response['id']
690
+ end
691
+ end
692
+
693
+ def message_from(success, response)
694
+ success ? 'Transaction approved' : response.fetch('error', { 'message' => 'No error details' })['message']
695
+ end
696
+
697
+ def success_from(response, options)
698
+ !response.key?('error') && response['status'] != 'failed'
699
+ end
700
+
701
+ def response_error(raw_response)
702
+ parse(raw_response)
703
+ rescue JSON::ParserError
704
+ json_error(raw_response)
705
+ end
706
+
707
+ def json_error(raw_response)
708
+ msg = 'Invalid response received from the Stripe API. Please contact support@stripe.com if you continue to receive this message.'
709
+ msg += " (The raw response returned by the API was #{raw_response.inspect})"
710
+ {
711
+ 'error' => {
712
+ 'message' => msg
713
+ }
714
+ }
715
+ end
716
+
717
+ def response_is_test?(response)
718
+ if response.has_key?('livemode')
719
+ !response['livemode']
720
+ elsif response['charge'].is_a?(Hash) && response['charge'].has_key?('livemode')
721
+ !response['charge']['livemode']
722
+ else
723
+ false
724
+ end
725
+ end
726
+
727
+ def emv_payment?(payment)
728
+ payment.respond_to?(:emv?) && payment.emv?
729
+ end
730
+
731
+ def quickchip_payment?(payment)
732
+ payment.respond_to?(:read_method) && payment.read_method == 'contact_quickchip'
733
+ end
734
+
735
+ def card_from_response(response)
736
+ response['card'] || response['active_card'] || response['source'] || {}
737
+ end
738
+
739
+ def emv_authorization_from_response(response)
740
+ return response['error']['emv_auth_data'] if response['error']
741
+
742
+ card_from_response(response)['emv_auth_data']
743
+ end
744
+
745
+ def error_code_from(response)
746
+ return STANDARD_ERROR_CODE_MAPPING['processing_error'] unless response['error']
747
+
748
+ code = response['error']['code']
749
+ decline_code = response['error']['decline_code'] if code == 'card_declined'
750
+
751
+ error_code = STANDARD_ERROR_CODE_MAPPING[decline_code]
752
+ error_code ||= STANDARD_ERROR_CODE_MAPPING[code]
753
+ error_code
754
+ end
755
+
756
+ def tokenize_bank_account(bank_account, options = {})
757
+ account_holder_type = BANK_ACCOUNT_HOLDER_TYPE_MAPPING[bank_account.account_holder_type]
758
+
759
+ post = {
760
+ bank_account: {
761
+ account_number: bank_account.account_number,
762
+ country: 'US',
763
+ currency: 'usd',
764
+ routing_number: bank_account.routing_number,
765
+ name: bank_account.name,
766
+ account_holder_type: account_holder_type
767
+ }
768
+ }
769
+
770
+ token_response = api_request(:post, "tokens?#{post_data(post)}")
771
+ success = token_response['error'].nil?
772
+
773
+ if success && token_response['id']
774
+ Response.new(success, nil, token: token_response)
775
+ else
776
+ Response.new(success, token_response['error']['message'])
777
+ end
778
+ end
779
+
780
+ def ach?(payment_method)
781
+ case payment_method
782
+ when String, nil
783
+ false
784
+ else
785
+ card_brand(payment_method) == 'check'
786
+ end
787
+ end
788
+
789
+ def auth_minimum_amount(options)
790
+ return 100 unless options[:currency]
791
+
792
+ return MINIMUM_AUTHORIZE_AMOUNTS[options[:currency].upcase] || 100
793
+ end
794
+
795
+ def copy_when_present(dest, dest_path, source, source_path = nil)
796
+ source_path ||= dest_path
797
+ source_path.each do |key|
798
+ return nil unless source[key]
799
+
800
+ source = source[key]
801
+ end
802
+
803
+ if source
804
+ dest_path.first(dest_path.size - 1).each do |key|
805
+ dest[key] ||= {}
806
+ dest = dest[key]
807
+ end
808
+ dest[dest_path.last] = source
809
+ end
810
+ end
811
+ end
812
+ end
813
+ end