active_accountability_merchant 1.97.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +2948 -0
  3. data/CONTRIBUTORS +568 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +246 -0
  6. data/lib/active_merchant.rb +63 -0
  7. data/lib/active_merchant/billing.rb +15 -0
  8. data/lib/active_merchant/billing/apple_pay_payment_token.rb +22 -0
  9. data/lib/active_merchant/billing/avs_result.rb +96 -0
  10. data/lib/active_merchant/billing/base.rb +61 -0
  11. data/lib/active_merchant/billing/check.rb +80 -0
  12. data/lib/active_merchant/billing/compatibility.rb +117 -0
  13. data/lib/active_merchant/billing/credit_card.rb +406 -0
  14. data/lib/active_merchant/billing/credit_card_formatting.rb +23 -0
  15. data/lib/active_merchant/billing/credit_card_methods.rb +343 -0
  16. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  17. data/lib/active_merchant/billing/gateway.rb +330 -0
  18. data/lib/active_merchant/billing/gateways.rb +14 -0
  19. data/lib/active_merchant/billing/gateways/adyen.rb +503 -0
  20. data/lib/active_merchant/billing/gateways/allied_wallet.rb +205 -0
  21. data/lib/active_merchant/billing/gateways/authorize_net.rb +1055 -0
  22. data/lib/active_merchant/billing/gateways/authorize_net_arb.rb +417 -0
  23. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +980 -0
  24. data/lib/active_merchant/billing/gateways/axcessms.rb +181 -0
  25. data/lib/active_merchant/billing/gateways/balanced.rb +256 -0
  26. data/lib/active_merchant/billing/gateways/bambora_apac.rb +226 -0
  27. data/lib/active_merchant/billing/gateways/bank_frick.rb +225 -0
  28. data/lib/active_merchant/billing/gateways/banwire.rb +116 -0
  29. data/lib/active_merchant/billing/gateways/barclaycard_smartpay.rb +384 -0
  30. data/lib/active_merchant/billing/gateways/barclays_epdq_extra_plus.rb +15 -0
  31. data/lib/active_merchant/billing/gateways/be2bill.rb +131 -0
  32. data/lib/active_merchant/billing/gateways/beanstream.rb +217 -0
  33. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +473 -0
  34. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +57 -0
  35. data/lib/active_merchant/billing/gateways/blue_pay.rb +522 -0
  36. data/lib/active_merchant/billing/gateways/blue_snap.rb +522 -0
  37. data/lib/active_merchant/billing/gateways/bogus.rb +186 -0
  38. data/lib/active_merchant/billing/gateways/borgun.rb +220 -0
  39. data/lib/active_merchant/billing/gateways/bpoint.rb +277 -0
  40. data/lib/active_merchant/billing/gateways/braintree.rb +19 -0
  41. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +22 -0
  42. data/lib/active_merchant/billing/gateways/braintree_blue.rb +740 -0
  43. data/lib/active_merchant/billing/gateways/braintree_orange.rb +19 -0
  44. data/lib/active_merchant/billing/gateways/bridge_pay.rb +244 -0
  45. data/lib/active_merchant/billing/gateways/cams.rb +230 -0
  46. data/lib/active_merchant/billing/gateways/card_connect.rb +316 -0
  47. data/lib/active_merchant/billing/gateways/card_save.rb +22 -0
  48. data/lib/active_merchant/billing/gateways/card_stream.rb +367 -0
  49. data/lib/active_merchant/billing/gateways/cardknox.rb +327 -0
  50. data/lib/active_merchant/billing/gateways/cardprocess.rb +254 -0
  51. data/lib/active_merchant/billing/gateways/cashnet.rb +219 -0
  52. data/lib/active_merchant/billing/gateways/cc5.rb +198 -0
  53. data/lib/active_merchant/billing/gateways/cecabank.rb +249 -0
  54. data/lib/active_merchant/billing/gateways/cenpos.rb +327 -0
  55. data/lib/active_merchant/billing/gateways/checkout.rb +214 -0
  56. data/lib/active_merchant/billing/gateways/checkout_v2.rb +270 -0
  57. data/lib/active_merchant/billing/gateways/citrus_pay.rb +22 -0
  58. data/lib/active_merchant/billing/gateways/clearhaus.rb +220 -0
  59. data/lib/active_merchant/billing/gateways/commercegate.rb +142 -0
  60. data/lib/active_merchant/billing/gateways/conekta.rb +228 -0
  61. data/lib/active_merchant/billing/gateways/creditcall.rb +271 -0
  62. data/lib/active_merchant/billing/gateways/credorax.rb +382 -0
  63. data/lib/active_merchant/billing/gateways/ct_payment.rb +268 -0
  64. data/lib/active_merchant/billing/gateways/culqi.rb +277 -0
  65. data/lib/active_merchant/billing/gateways/cyber_source.rb +828 -0
  66. data/lib/active_merchant/billing/gateways/d_local.rb +226 -0
  67. data/lib/active_merchant/billing/gateways/data_cash.rb +305 -0
  68. data/lib/active_merchant/billing/gateways/decidir.rb +233 -0
  69. data/lib/active_merchant/billing/gateways/dibs.rb +199 -0
  70. data/lib/active_merchant/billing/gateways/digitzs.rb +292 -0
  71. data/lib/active_merchant/billing/gateways/ebanx.rb +296 -0
  72. data/lib/active_merchant/billing/gateways/efsnet.rb +215 -0
  73. data/lib/active_merchant/billing/gateways/elavon.rb +320 -0
  74. data/lib/active_merchant/billing/gateways/element.rb +356 -0
  75. data/lib/active_merchant/billing/gateways/epay.rb +285 -0
  76. data/lib/active_merchant/billing/gateways/evo_ca.rb +308 -0
  77. data/lib/active_merchant/billing/gateways/eway.rb +226 -0
  78. data/lib/active_merchant/billing/gateways/eway_managed.rb +290 -0
  79. data/lib/active_merchant/billing/gateways/eway_rapid.rb +563 -0
  80. data/lib/active_merchant/billing/gateways/exact.rb +224 -0
  81. data/lib/active_merchant/billing/gateways/ezic.rb +195 -0
  82. data/lib/active_merchant/billing/gateways/fat_zebra.rb +218 -0
  83. data/lib/active_merchant/billing/gateways/federated_canada.rb +159 -0
  84. data/lib/active_merchant/billing/gateways/finansbank.rb +22 -0
  85. data/lib/active_merchant/billing/gateways/first_giving.rb +142 -0
  86. data/lib/active_merchant/billing/gateways/first_pay.rb +182 -0
  87. data/lib/active_merchant/billing/gateways/firstdata_e4.rb +451 -0
  88. data/lib/active_merchant/billing/gateways/firstdata_e4_v27.rb +485 -0
  89. data/lib/active_merchant/billing/gateways/flo2cash.rb +215 -0
  90. data/lib/active_merchant/billing/gateways/flo2cash_simple.rb +20 -0
  91. data/lib/active_merchant/billing/gateways/forte.rb +270 -0
  92. data/lib/active_merchant/billing/gateways/garanti.rb +259 -0
  93. data/lib/active_merchant/billing/gateways/global_collect.rb +336 -0
  94. data/lib/active_merchant/billing/gateways/global_transport.rb +194 -0
  95. data/lib/active_merchant/billing/gateways/hdfc.rb +206 -0
  96. data/lib/active_merchant/billing/gateways/hps.rb +350 -0
  97. data/lib/active_merchant/billing/gateways/iats_payments.rb +290 -0
  98. data/lib/active_merchant/billing/gateways/in_context_paypal_express.rb +15 -0
  99. data/lib/active_merchant/billing/gateways/inspire.rb +218 -0
  100. data/lib/active_merchant/billing/gateways/instapay.rb +162 -0
  101. data/lib/active_merchant/billing/gateways/ipp.rb +176 -0
  102. data/lib/active_merchant/billing/gateways/iridium.rb +468 -0
  103. data/lib/active_merchant/billing/gateways/itransact.rb +447 -0
  104. data/lib/active_merchant/billing/gateways/iveri.rb +251 -0
  105. data/lib/active_merchant/billing/gateways/jetpay.rb +402 -0
  106. data/lib/active_merchant/billing/gateways/jetpay_v2.rb +437 -0
  107. data/lib/active_merchant/billing/gateways/komoju.rb +115 -0
  108. data/lib/active_merchant/billing/gateways/kushki.rb +219 -0
  109. data/lib/active_merchant/billing/gateways/latitude19.rb +411 -0
  110. data/lib/active_merchant/billing/gateways/linkpoint.rb +449 -0
  111. data/lib/active_merchant/billing/gateways/litle.rb +507 -0
  112. data/lib/active_merchant/billing/gateways/mastercard.rb +267 -0
  113. data/lib/active_merchant/billing/gateways/maxipago.rb +220 -0
  114. data/lib/active_merchant/billing/gateways/mercado_pago.rb +277 -0
  115. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +194 -0
  116. data/lib/active_merchant/billing/gateways/merchant_one.rb +113 -0
  117. data/lib/active_merchant/billing/gateways/merchant_partners.rb +245 -0
  118. data/lib/active_merchant/billing/gateways/merchant_ware.rb +319 -0
  119. data/lib/active_merchant/billing/gateways/merchant_ware_version_four.rb +286 -0
  120. data/lib/active_merchant/billing/gateways/merchant_warrior.rb +210 -0
  121. data/lib/active_merchant/billing/gateways/mercury.rb +356 -0
  122. data/lib/active_merchant/billing/gateways/metrics_global.rb +303 -0
  123. data/lib/active_merchant/billing/gateways/micropayment.rb +183 -0
  124. data/lib/active_merchant/billing/gateways/migs.rb +332 -0
  125. data/lib/active_merchant/billing/gateways/migs/migs_codes.rb +100 -0
  126. data/lib/active_merchant/billing/gateways/modern_payments.rb +37 -0
  127. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +217 -0
  128. data/lib/active_merchant/billing/gateways/monei.rb +338 -0
  129. data/lib/active_merchant/billing/gateways/moneris.rb +377 -0
  130. data/lib/active_merchant/billing/gateways/moneris_us.rb +352 -0
  131. data/lib/active_merchant/billing/gateways/money_movers.rb +151 -0
  132. data/lib/active_merchant/billing/gateways/mundipagg.rb +293 -0
  133. data/lib/active_merchant/billing/gateways/nab_transact.rb +301 -0
  134. data/lib/active_merchant/billing/gateways/ncr_secure_pay.rb +165 -0
  135. data/lib/active_merchant/billing/gateways/net_registry.rb +199 -0
  136. data/lib/active_merchant/billing/gateways/netaxept.rb +180 -0
  137. data/lib/active_merchant/billing/gateways/netbanx.rb +294 -0
  138. data/lib/active_merchant/billing/gateways/netbilling.rb +232 -0
  139. data/lib/active_merchant/billing/gateways/netpay.rb +222 -0
  140. data/lib/active_merchant/billing/gateways/network_merchants.rb +241 -0
  141. data/lib/active_merchant/billing/gateways/nmi.rb +324 -0
  142. data/lib/active_merchant/billing/gateways/ogone.rb +478 -0
  143. data/lib/active_merchant/billing/gateways/omise.rb +324 -0
  144. data/lib/active_merchant/billing/gateways/openpay.rb +228 -0
  145. data/lib/active_merchant/billing/gateways/opp.rb +388 -0
  146. data/lib/active_merchant/billing/gateways/optimal_payment.rb +332 -0
  147. data/lib/active_merchant/billing/gateways/orbital.rb +1006 -0
  148. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +47 -0
  149. data/lib/active_merchant/billing/gateways/pac_net_raven.rb +206 -0
  150. data/lib/active_merchant/billing/gateways/pagarme.rb +246 -0
  151. data/lib/active_merchant/billing/gateways/pago_facil.rb +122 -0
  152. data/lib/active_merchant/billing/gateways/pay_conex.rb +245 -0
  153. data/lib/active_merchant/billing/gateways/pay_gate_xml.rb +280 -0
  154. data/lib/active_merchant/billing/gateways/pay_hub.rb +213 -0
  155. data/lib/active_merchant/billing/gateways/pay_junction.rb +390 -0
  156. data/lib/active_merchant/billing/gateways/pay_junction_v2.rb +188 -0
  157. data/lib/active_merchant/billing/gateways/pay_secure.rb +111 -0
  158. data/lib/active_merchant/billing/gateways/paybox_direct.rb +200 -0
  159. data/lib/active_merchant/billing/gateways/payeezy.rb +410 -0
  160. data/lib/active_merchant/billing/gateways/payex.rb +410 -0
  161. data/lib/active_merchant/billing/gateways/payflow.rb +383 -0
  162. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +235 -0
  163. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +43 -0
  164. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  165. data/lib/active_merchant/billing/gateways/payflow_express.rb +220 -0
  166. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +14 -0
  167. data/lib/active_merchant/billing/gateways/payflow_uk.rb +20 -0
  168. data/lib/active_merchant/billing/gateways/payment_express.rb +369 -0
  169. data/lib/active_merchant/billing/gateways/paymentez.rb +300 -0
  170. data/lib/active_merchant/billing/gateways/paymill.rb +371 -0
  171. data/lib/active_merchant/billing/gateways/paypal.rb +128 -0
  172. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +717 -0
  173. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +65 -0
  174. data/lib/active_merchant/billing/gateways/paypal/paypal_recurring_api.rb +262 -0
  175. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  176. data/lib/active_merchant/billing/gateways/paypal_digital_goods.rb +44 -0
  177. data/lib/active_merchant/billing/gateways/paypal_express.rb +269 -0
  178. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +30 -0
  179. data/lib/active_merchant/billing/gateways/payscout.rb +160 -0
  180. data/lib/active_merchant/billing/gateways/paystation.rb +207 -0
  181. data/lib/active_merchant/billing/gateways/payu_in.rb +248 -0
  182. data/lib/active_merchant/billing/gateways/payu_latam.rb +449 -0
  183. data/lib/active_merchant/billing/gateways/payway.rb +207 -0
  184. data/lib/active_merchant/billing/gateways/pin.rb +234 -0
  185. data/lib/active_merchant/billing/gateways/plugnpay.rb +284 -0
  186. data/lib/active_merchant/billing/gateways/pro_pay.rb +326 -0
  187. data/lib/active_merchant/billing/gateways/psigate.rb +227 -0
  188. data/lib/active_merchant/billing/gateways/psl_card.rb +296 -0
  189. data/lib/active_merchant/billing/gateways/qbms.rb +303 -0
  190. data/lib/active_merchant/billing/gateways/quantum.rb +276 -0
  191. data/lib/active_merchant/billing/gateways/quickbooks.rb +290 -0
  192. data/lib/active_merchant/billing/gateways/quickpay.rb +25 -0
  193. data/lib/active_merchant/billing/gateways/quickpay/quickpay_common.rb +184 -0
  194. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v10.rb +301 -0
  195. data/lib/active_merchant/billing/gateways/quickpay/quickpay_v4to7.rb +226 -0
  196. data/lib/active_merchant/billing/gateways/qvalent.rb +289 -0
  197. data/lib/active_merchant/billing/gateways/realex.rb +374 -0
  198. data/lib/active_merchant/billing/gateways/redsys.rb +534 -0
  199. data/lib/active_merchant/billing/gateways/s5.rb +246 -0
  200. data/lib/active_merchant/billing/gateways/safe_charge.rb +262 -0
  201. data/lib/active_merchant/billing/gateways/sage.rb +448 -0
  202. data/lib/active_merchant/billing/gateways/sage_pay.rb +434 -0
  203. data/lib/active_merchant/billing/gateways/sallie_mae.rb +142 -0
  204. data/lib/active_merchant/billing/gateways/secure_net.rb +267 -0
  205. data/lib/active_merchant/billing/gateways/secure_pay.rb +200 -0
  206. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +292 -0
  207. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +104 -0
  208. data/lib/active_merchant/billing/gateways/securion_pay.rb +264 -0
  209. data/lib/active_merchant/billing/gateways/skip_jack.rb +450 -0
  210. data/lib/active_merchant/billing/gateways/smart_ps.rb +280 -0
  211. data/lib/active_merchant/billing/gateways/so_easy_pay.rb +194 -0
  212. data/lib/active_merchant/billing/gateways/spreedly_core.rb +306 -0
  213. data/lib/active_merchant/billing/gateways/stripe.rb +790 -0
  214. data/lib/active_merchant/billing/gateways/stripe_payment_intents.rb +267 -0
  215. data/lib/active_merchant/billing/gateways/swipe_checkout.rb +152 -0
  216. data/lib/active_merchant/billing/gateways/telr.rb +274 -0
  217. data/lib/active_merchant/billing/gateways/tns.rb +22 -0
  218. data/lib/active_merchant/billing/gateways/trans_first.rb +239 -0
  219. data/lib/active_merchant/billing/gateways/trans_first_transaction_express.rb +608 -0
  220. data/lib/active_merchant/billing/gateways/transact_pro.rb +224 -0
  221. data/lib/active_merchant/billing/gateways/transax.rb +22 -0
  222. data/lib/active_merchant/billing/gateways/transnational.rb +9 -0
  223. data/lib/active_merchant/billing/gateways/trexle.rb +218 -0
  224. data/lib/active_merchant/billing/gateways/trust_commerce.rb +490 -0
  225. data/lib/active_merchant/billing/gateways/usa_epay.rb +25 -0
  226. data/lib/active_merchant/billing/gateways/usa_epay_advanced.rb +1620 -0
  227. data/lib/active_merchant/billing/gateways/usa_epay_transaction.rb +355 -0
  228. data/lib/active_merchant/billing/gateways/vanco.rb +294 -0
  229. data/lib/active_merchant/billing/gateways/verifi.rb +225 -0
  230. data/lib/active_merchant/billing/gateways/viaklix.rb +176 -0
  231. data/lib/active_merchant/billing/gateways/visanet_peru.rb +245 -0
  232. data/lib/active_merchant/billing/gateways/webpay.rb +97 -0
  233. data/lib/active_merchant/billing/gateways/wepay.rb +237 -0
  234. data/lib/active_merchant/billing/gateways/wirecard.rb +432 -0
  235. data/lib/active_merchant/billing/gateways/world_net.rb +344 -0
  236. data/lib/active_merchant/billing/gateways/worldpay.rb +634 -0
  237. data/lib/active_merchant/billing/gateways/worldpay_online_payments.rb +215 -0
  238. data/lib/active_merchant/billing/gateways/worldpay_us.rb +221 -0
  239. data/lib/active_merchant/billing/model.rb +30 -0
  240. data/lib/active_merchant/billing/network_tokenization_credit_card.rb +39 -0
  241. data/lib/active_merchant/billing/payment_token.rb +21 -0
  242. data/lib/active_merchant/billing/rails.rb +3 -0
  243. data/lib/active_merchant/billing/response.rb +92 -0
  244. data/lib/active_merchant/connection.rb +195 -0
  245. data/lib/active_merchant/country.rb +336 -0
  246. data/lib/active_merchant/empty.rb +20 -0
  247. data/lib/active_merchant/errors.rb +35 -0
  248. data/lib/active_merchant/net_http_ssl_connection.rb +10 -0
  249. data/lib/active_merchant/network_connection_retries.rb +80 -0
  250. data/lib/active_merchant/post_data.rb +25 -0
  251. data/lib/active_merchant/posts_data.rb +92 -0
  252. data/lib/active_merchant/version.rb +3 -0
  253. data/lib/activemerchant.rb +1 -0
  254. data/lib/certs/cacert.pem +3988 -0
  255. data/lib/support/gateway_support.rb +69 -0
  256. data/lib/support/outbound_hosts.rb +28 -0
  257. data/lib/support/ssl_verify.rb +92 -0
  258. data/lib/support/ssl_version.rb +87 -0
  259. metadata +435 -0
@@ -0,0 +1,790 @@
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(AT AU BE BR CA CH DE DK ES FI FR GB HK IE IT JP LU MX NL NO NZ PT SE SG US)
29
+ self.default_currency = 'USD'
30
+ self.money_format = :cents
31
+ self.supported_cardtypes = [:visa, :master, :american_express, :discover, :jcb, :diners_club, :maestro]
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
+ MultiResponse.run do |r|
84
+ if payment.is_a?(ApplePayPaymentToken)
85
+ r.process { tokenize_apple_pay_token(payment) }
86
+ payment = StripePaymentToken.new(r.params['token']) if r.success?
87
+ end
88
+ r.process do
89
+ post = create_post_for_auth_or_purchase(money, payment, options)
90
+ if emv_payment?(payment)
91
+ add_application_fee(post, options)
92
+ else
93
+ post[:capture] = 'false'
94
+ end
95
+ commit(:post, 'charges', post, options)
96
+ end
97
+ end.responses.last
98
+ end
99
+
100
+ # To create a charge on a card or a token, call
101
+ #
102
+ # purchase(money, card_hash_or_token, { ... })
103
+ #
104
+ # To create a charge on a customer, call
105
+ #
106
+ # purchase(money, nil, { :customer => id, ... })
107
+ def purchase(money, payment, options = {})
108
+ if ach?(payment)
109
+ direct_bank_error = 'Direct bank account transactions are not supported. Bank accounts must be stored and verified before use.'
110
+ return Response.new(false, direct_bank_error)
111
+ end
112
+
113
+ MultiResponse.run do |r|
114
+ if payment.is_a?(ApplePayPaymentToken)
115
+ r.process { tokenize_apple_pay_token(payment) }
116
+ payment = StripePaymentToken.new(r.params['token']) if r.success?
117
+ end
118
+ r.process do
119
+ post = create_post_for_auth_or_purchase(money, payment, options)
120
+ post[:card][:processing_method] = 'quick_chip' if quickchip_payment?(payment)
121
+ commit(:post, 'charges', post, options)
122
+ end
123
+ end.responses.last
124
+ end
125
+
126
+ def capture(money, authorization, options = {})
127
+ post = {}
128
+
129
+ if emv_tc_response = options.delete(:icc_data)
130
+ post[:card] = { emv_approval_data: emv_tc_response }
131
+ commit(:post, "charges/#{CGI.escape(authorization)}", post, options)
132
+ else
133
+ add_application_fee(post, options)
134
+ add_amount(post, money, options)
135
+ add_exchange_rate(post, options)
136
+ commit(:post, "charges/#{CGI.escape(authorization)}/capture", post, options)
137
+ end
138
+ end
139
+
140
+ def void(identification, options = {})
141
+ post = {}
142
+ post[:metadata] = options[:metadata] if options[:metadata]
143
+ post[:reason] = options[:reason] if options[:reason]
144
+ post[:expand] = [:charge]
145
+ commit(:post, "charges/#{CGI.escape(identification)}/refunds", post, options)
146
+ end
147
+
148
+ def refund(money, identification, options = {})
149
+ post = {}
150
+ add_amount(post, money, options)
151
+ post[:refund_application_fee] = true if options[:refund_application_fee]
152
+ post[:reverse_transfer] = options[:reverse_transfer] if options[:reverse_transfer]
153
+ post[:metadata] = options[:metadata] if options[:metadata]
154
+ post[:reason] = options[:reason] if options[:reason]
155
+ post[:expand] = [:charge]
156
+
157
+ response = commit(:post, "charges/#{CGI.escape(identification)}/refunds", post, options)
158
+
159
+ if response.success? && options[:refund_fee_amount] && options[:refund_fee_amount].to_s != '0'
160
+ charge = api_request(:get, "charges/#{CGI.escape(identification)}", nil, options)
161
+
162
+ if application_fee = charge['application_fee']
163
+ fee_refund_options = {
164
+ currency: options[:currency], # currency isn't used by Stripe here, but we need it for #add_amount
165
+ key: @fee_refund_api_key
166
+ }
167
+ refund_application_fee(options[:refund_fee_amount].to_i, application_fee, fee_refund_options)
168
+ end
169
+ end
170
+
171
+ response
172
+ end
173
+
174
+ def verify(payment, options = {})
175
+ MultiResponse.run(:use_first_response) do |r|
176
+ r.process { authorize(auth_minimum_amount(options), payment, options) }
177
+ options[:idempotency_key] = nil
178
+ r.process(:ignore_result) { void(r.authorization, options) }
179
+ end
180
+ end
181
+
182
+ def refund_application_fee(money, identification, options = {})
183
+ post = {}
184
+ add_amount(post, money, options)
185
+ commit(:post, "application_fees/#{CGI.escape(identification)}/refunds", post, options)
186
+ end
187
+
188
+ # Note: creating a new credit card will not change the customer's existing default credit card (use :set_default => true)
189
+ def store(payment, options = {})
190
+ params = {}
191
+ post = {}
192
+
193
+ if payment.is_a?(ApplePayPaymentToken)
194
+ token_exchange_response = tokenize_apple_pay_token(payment)
195
+ params = { card: token_exchange_response.params['token']['id'] } if token_exchange_response.success?
196
+ elsif payment.is_a?(StripePaymentToken)
197
+ add_payment_token(params, payment, options)
198
+ elsif payment.is_a?(Check)
199
+ bank_token_response = tokenize_bank_account(payment)
200
+ return bank_token_response unless bank_token_response.success?
201
+ params = { source: bank_token_response.params['token']['id'] }
202
+ else
203
+ add_creditcard(params, payment, options)
204
+ end
205
+
206
+ post[:validate] = options[:validate] unless options[:validate].nil?
207
+ post[:description] = options[:description] if options[:description]
208
+ post[:email] = options[:email] if options[:email]
209
+
210
+ if options[:account]
211
+ add_external_account(post, params, payment)
212
+ commit(:post, "accounts/#{CGI.escape(options[:account])}/external_accounts", post, options)
213
+ elsif options[:customer]
214
+ MultiResponse.run(:first) do |r|
215
+ # The /cards endpoint does not update other customer parameters.
216
+ r.process { commit(:post, "customers/#{CGI.escape(options[:customer])}/cards", params, options) }
217
+
218
+ if options[:set_default] and r.success? and !r.params['id'].blank?
219
+ post[:default_card] = r.params['id']
220
+ end
221
+
222
+ if post.count > 0
223
+ r.process { update_customer(options[:customer], post) }
224
+ end
225
+ end
226
+ else
227
+ commit(:post, 'customers', post.merge(params), options)
228
+ end
229
+ end
230
+
231
+ def update(customer_id, card_id, options = {})
232
+ commit(:post, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", options, options)
233
+ end
234
+
235
+ def update_customer(customer_id, options = {})
236
+ commit(:post, "customers/#{CGI.escape(customer_id)}", options, options)
237
+ end
238
+
239
+ def unstore(identification, options = {}, deprecated_options = {})
240
+ customer_id, card_id = identification.split('|')
241
+
242
+ if options.kind_of?(String)
243
+ ActiveMerchant.deprecated 'Passing the card_id as the 2nd parameter is deprecated. The response authorization includes both the customer_id and the card_id.'
244
+ card_id ||= options
245
+ options = deprecated_options
246
+ end
247
+
248
+ commit(:delete, "customers/#{CGI.escape(customer_id)}/cards/#{CGI.escape(card_id)}", nil, options)
249
+ end
250
+
251
+ def tokenize_apple_pay_token(apple_pay_payment_token, options = {})
252
+ token_response = api_request(:post, "tokens?pk_token=#{CGI.escape(apple_pay_payment_token.payment_data.to_json)}")
253
+ success = !token_response.key?('error')
254
+
255
+ if success && token_response.key?('id')
256
+ Response.new(success, nil, token: token_response)
257
+ else
258
+ Response.new(success, token_response['error']['message'])
259
+ end
260
+ end
261
+
262
+ def verify_credentials
263
+ begin
264
+ ssl_get(live_url + 'charges/nonexistent', headers)
265
+ rescue ResponseError => e
266
+ return false if e.response.code.to_i == 401
267
+ end
268
+
269
+ true
270
+ end
271
+
272
+ def supports_scrubbing?
273
+ true
274
+ end
275
+
276
+ def scrub(transcript)
277
+ transcript.
278
+ gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
279
+ gsub(%r((&?three_d_secure\[cryptogram\]=)[\w=]*(&?)), '\1[FILTERED]\2').
280
+ gsub(%r((card\[cryptogram\]=)[^&]+(&?)), '\1[FILTERED]\2').
281
+ gsub(%r((card\[cvc\]=)\d+), '\1[FILTERED]').
282
+ gsub(%r((card\[emv_approval_data\]=)[^&]+(&?)), '\1[FILTERED]\2').
283
+ gsub(%r((card\[emv_auth_data\]=)[^&]+(&?)), '\1[FILTERED]\2').
284
+ gsub(%r((card\[encrypted_pin\]=)[^&]+(&?)), '\1[FILTERED]\2').
285
+ gsub(%r((card\[encrypted_pin_key_id\]=)[\w=]+(&?)), '\1[FILTERED]\2').
286
+ gsub(%r((card\[number\]=)\d+), '\1[FILTERED]').
287
+ gsub(%r((card\[swipe_data\]=)[^&]+(&?)), '\1[FILTERED]\2')
288
+ end
289
+
290
+ def supports_network_tokenization?
291
+ true
292
+ end
293
+
294
+ private
295
+
296
+ class StripePaymentToken < PaymentToken
297
+ def type
298
+ 'stripe'
299
+ end
300
+ end
301
+
302
+ def create_source(money, payment, type, options = {})
303
+ post = {}
304
+ add_amount(post, money, options, true)
305
+ post[:type] = type
306
+ if type == 'card'
307
+ add_creditcard(post, payment, options, true)
308
+ add_source_owner(post, payment, options)
309
+ elsif type == 'three_d_secure'
310
+ post[:three_d_secure] = {card: payment}
311
+ post[:redirect] = {return_url: options[:redirect_url]}
312
+ end
313
+ commit(:post, 'sources', post, options)
314
+ end
315
+
316
+ def show_source(source_id, options)
317
+ commit(:get, "sources/#{source_id}", nil, options)
318
+ end
319
+
320
+ def create_webhook_endpoint(options, events)
321
+ post = {}
322
+ post[:url] = options[:callback_url]
323
+ post[:enabled_events] = events
324
+ post[:connect] = true if options[:stripe_account]
325
+ options.delete(:stripe_account)
326
+ commit(:post, 'webhook_endpoints', post, options)
327
+ end
328
+
329
+ def delete_webhook_endpoint(options)
330
+ commit(:delete, "webhook_endpoints/#{options[:webhook_id]}", {}, options)
331
+ end
332
+
333
+ def show_webhook_endpoint(options)
334
+ options.delete(:stripe_account)
335
+ commit(:get, "webhook_endpoints/#{options[:webhook_id]}", nil, options)
336
+ end
337
+
338
+ def list_webhook_endpoints(options)
339
+ params = {}
340
+ params[:limit] = options[:limit] if options[:limit]
341
+ options.delete(:stripe_account)
342
+ commit(:get, "webhook_endpoints?#{post_data(params)}", nil, options)
343
+ end
344
+
345
+ def create_post_for_auth_or_purchase(money, payment, options)
346
+ post = {}
347
+
348
+ if payment.is_a?(StripePaymentToken)
349
+ add_payment_token(post, payment, options)
350
+ else
351
+ add_creditcard(post, payment, options)
352
+ end
353
+
354
+ add_charge_details(post, money, payment, options)
355
+ post
356
+ end
357
+
358
+ # Used internally by Spreedly to populate the charge object for 3DS 1.0 transactions
359
+ def add_charge_details(post, money, payment, options)
360
+ if emv_payment?(payment)
361
+ add_statement_address(post, options)
362
+ add_emv_metadata(post, payment)
363
+ else
364
+ add_amount(post, money, options, true)
365
+ add_customer_data(post, options)
366
+ post[:description] = options[:description]
367
+ post[:statement_descriptor] = options[:statement_description]
368
+ post[:receipt_email] = options[:receipt_email] if options[:receipt_email]
369
+ add_customer(post, payment, options)
370
+ add_flags(post, options)
371
+ end
372
+
373
+ add_metadata(post, options)
374
+ add_application_fee(post, options)
375
+ add_exchange_rate(post, options)
376
+ add_destination(post, options)
377
+ add_level_three(post, options)
378
+ post
379
+ end
380
+
381
+ def add_amount(post, money, options, include_currency = false)
382
+ currency = options[:currency] || currency(money)
383
+ post[:amount] = localized_amount(money, currency)
384
+ post[:currency] = currency.downcase if include_currency
385
+ end
386
+
387
+ def add_application_fee(post, options)
388
+ post[:application_fee] = options[:application_fee] if options[:application_fee]
389
+ end
390
+
391
+ def add_exchange_rate(post, options)
392
+ post[:exchange_rate] = options[:exchange_rate] if options[:exchange_rate]
393
+ end
394
+
395
+ def add_destination(post, options)
396
+ if options[:destination]
397
+ post[:destination] = {}
398
+ post[:destination][:account] = options[:destination]
399
+ post[:destination][:amount] = options[:destination_amount] if options[:destination_amount]
400
+ end
401
+ end
402
+
403
+ def add_level_three(post, options)
404
+ level_three = {}
405
+
406
+ copy_when_present(level_three, [:merchant_reference], options)
407
+ copy_when_present(level_three, [:customer_reference], options)
408
+ copy_when_present(level_three, [:shipping_address_zip], options)
409
+ copy_when_present(level_three, [:shipping_from_zip], options)
410
+ copy_when_present(level_three, [:shipping_amount], options)
411
+ copy_when_present(level_three, [:line_items], options)
412
+
413
+ unless level_three.empty?
414
+ post[:level3] = level_three
415
+ end
416
+ end
417
+
418
+ def add_expand_parameters(post, options)
419
+ post[:expand] ||= []
420
+ post[:expand].concat(Array.wrap(options[:expand]).map(&:to_sym)).uniq!
421
+ end
422
+
423
+ def add_external_account(post, card_params, payment)
424
+ external_account = {}
425
+ external_account[:object] ='card'
426
+ external_account[:currency] = (options[:currency] || currency(payment)).downcase
427
+ post[:external_account] = external_account.merge(card_params[:card])
428
+ end
429
+
430
+ def add_customer_data(post, options)
431
+ metadata_options = [:description, :ip, :user_agent, :referrer]
432
+ post.update(options.slice(*metadata_options))
433
+
434
+ post[:external_id] = options[:order_id]
435
+ post[:payment_user_agent] = "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}"
436
+ end
437
+
438
+ def add_address(post, options)
439
+ return unless post[:card]&.kind_of?(Hash)
440
+ if address = options[:billing_address] || options[:address]
441
+ post[:card][:address_line1] = address[:address1] if address[:address1]
442
+ post[:card][:address_line2] = address[:address2] if address[:address2]
443
+ post[:card][:address_country] = address[:country] if address[:country]
444
+ post[:card][:address_zip] = address[:zip] if address[:zip]
445
+ post[:card][:address_state] = address[:state] if address[:state]
446
+ post[:card][:address_city] = address[:city] if address[:city]
447
+ end
448
+ end
449
+
450
+ def add_statement_address(post, options)
451
+ return unless statement_address = options[:statement_address]
452
+ return unless [:address1, :city, :zip, :state].all? { |key| statement_address[key].present? }
453
+
454
+ post[:statement_address] = {}
455
+ post[:statement_address][:line1] = statement_address[:address1]
456
+ post[:statement_address][:line2] = statement_address[:address2] if statement_address[:address2].present?
457
+ post[:statement_address][:city] = statement_address[:city]
458
+ post[:statement_address][:postal_code] = statement_address[:zip]
459
+ post[:statement_address][:state] = statement_address[:state]
460
+ end
461
+
462
+ def add_creditcard(post, creditcard, options, use_sources = false)
463
+ card = {}
464
+ if emv_payment?(creditcard)
465
+ add_emv_creditcard(post, creditcard.icc_data)
466
+ post[:card][:read_method] = 'contactless' if creditcard.read_method == 'contactless'
467
+ post[:card][:read_method] = 'contactless_magstripe_mode' if creditcard.read_method == 'contactless_magstripe'
468
+ if creditcard.encrypted_pin_cryptogram.present? && creditcard.encrypted_pin_ksn.present?
469
+ post[:card][:encrypted_pin] = creditcard.encrypted_pin_cryptogram
470
+ post[:card][:encrypted_pin_key_id] = creditcard.encrypted_pin_ksn
471
+ end
472
+ elsif creditcard.respond_to?(:number)
473
+ if creditcard.respond_to?(:track_data) && creditcard.track_data.present?
474
+ card[:swipe_data] = creditcard.track_data
475
+ if creditcard.respond_to?(:read_method)
476
+ card[:fallback_reason] = 'no_chip' if creditcard.read_method == 'fallback_no_chip'
477
+ card[:fallback_reason] = 'chip_error' if creditcard.read_method == 'fallback_chip_error'
478
+ card[:read_method] = 'contactless_magstripe_mode' if creditcard.read_method == 'contactless_magstripe'
479
+ end
480
+ else
481
+ card[:number] = creditcard.number
482
+ card[:exp_month] = creditcard.month
483
+ card[:exp_year] = creditcard.year
484
+ card[:cvc] = creditcard.verification_value if creditcard.verification_value?
485
+ card[:name] = creditcard.name if creditcard.name && !use_sources
486
+ end
487
+
488
+ if creditcard.is_a?(NetworkTokenizationCreditCard)
489
+ card[:cryptogram] = creditcard.payment_cryptogram
490
+ card[:eci] = creditcard.eci.rjust(2, '0') if creditcard.eci =~ /^[0-9]+$/
491
+ card[:tokenization_method] = creditcard.source.to_s
492
+ end
493
+ post[:card] = card
494
+
495
+ add_address(post, options) unless use_sources
496
+ elsif creditcard.kind_of?(String)
497
+ if options[:track_data]
498
+ card[:swipe_data] = options[:track_data]
499
+ elsif creditcard.include?('|')
500
+ customer_id, card_id = creditcard.split('|')
501
+ card = card_id
502
+ post[:customer] = customer_id
503
+ else
504
+ card = creditcard
505
+ end
506
+ post[:card] = card
507
+ end
508
+ end
509
+
510
+ def add_emv_creditcard(post, icc_data, options = {})
511
+ post[:card] = { emv_auth_data: icc_data }
512
+ end
513
+
514
+ def add_payment_token(post, token, options = {})
515
+ post[:card] = token.payment_data['id']
516
+ end
517
+
518
+ def add_customer(post, payment, options)
519
+ if options[:customer] && !payment.respond_to?(:number)
520
+ ActiveMerchant.deprecated 'Passing the customer in the options is deprecated. Just use the response.authorization instead.'
521
+ post[:customer] = options[:customer]
522
+ end
523
+ end
524
+
525
+ def add_flags(post, options)
526
+ post[:uncaptured] = true if options[:uncaptured]
527
+ post[:recurring] = true if options[:eci] == 'recurring' || options[:recurring]
528
+ end
529
+
530
+ def add_metadata(post, options = {})
531
+ post[:metadata] ||= {}
532
+ post[:metadata].merge!(options[:metadata]) if options[:metadata]
533
+ post[:metadata][:email] = options[:email] if options[:email]
534
+ post[:metadata][:order_id] = options[:order_id] if options[:order_id]
535
+ post.delete(:metadata) if post[:metadata].empty?
536
+ end
537
+
538
+ def add_emv_metadata(post, creditcard)
539
+ post[:metadata] ||= {}
540
+ post[:metadata][:card_read_method] = creditcard.read_method if creditcard.respond_to?(:read_method)
541
+ end
542
+
543
+ def add_source_owner(post, creditcard, options)
544
+ post[:owner] = {}
545
+ post[:owner][:name] = creditcard.name if creditcard.name
546
+ post[:owner][:email] = options[:email] if options[:email]
547
+
548
+ if address = options[:billing_address] || options[:address]
549
+ owner_address = {}
550
+ owner_address[:line1] = address[:address1] if address[:address1]
551
+ owner_address[:line2] = address[:address2] if address[:address2]
552
+ owner_address[:country] = address[:country] if address[:country]
553
+ owner_address[:postal_code] = address[:zip] if address[:zip]
554
+ owner_address[:state] = address[:state] if address[:state]
555
+ owner_address[:city] = address[:city] if address[:city]
556
+
557
+ post[:owner][:phone] = address[:phone] if address[:phone]
558
+ post[:owner][:address] = owner_address
559
+ end
560
+ end
561
+
562
+ def parse(body)
563
+ JSON.parse(body)
564
+ end
565
+
566
+ def post_data(params)
567
+ return nil unless params
568
+ flatten_params([], params).join('&')
569
+ end
570
+
571
+ def flatten_params(flattened, params, prefix = nil)
572
+ params.each do |key, value|
573
+ next if value != false && value.blank?
574
+ flattened_key = prefix.nil? ? key : "#{prefix}[#{key}]"
575
+ if value.is_a?(Hash)
576
+ flatten_params(flattened, value, flattened_key)
577
+ elsif value.is_a?(Array)
578
+ flatten_array(flattened, value, flattened_key)
579
+ else
580
+ flattened << "#{flattened_key}=#{CGI.escape(value.to_s)}"
581
+ end
582
+ end
583
+ flattened
584
+ end
585
+
586
+ def flatten_array(flattened, array, prefix)
587
+ array.each_with_index do |item, idx|
588
+ key = "#{prefix}[#{idx}]"
589
+ if item.is_a?(Hash)
590
+ flatten_params(flattened, item, key)
591
+ elsif item.is_a?(Array)
592
+ flatten_array(flattened, item, key)
593
+ else
594
+ flattened << "#{key}=#{CGI.escape(item.to_s)}"
595
+ end
596
+ end
597
+ end
598
+
599
+ def headers(options = {})
600
+ key = options[:key] || @api_key
601
+ idempotency_key = options[:idempotency_key]
602
+
603
+ headers = {
604
+ 'Authorization' => 'Basic ' + Base64.encode64(key.to_s + ':').strip,
605
+ 'User-Agent' => "Stripe/v1 ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
606
+ 'Stripe-Version' => api_version(options),
607
+ 'X-Stripe-Client-User-Agent' => stripe_client_user_agent(options),
608
+ 'X-Stripe-Client-User-Metadata' => {:ip => options[:ip]}.to_json
609
+ }
610
+ headers['Idempotency-Key'] = idempotency_key if idempotency_key
611
+ headers['Stripe-Account'] = options[:stripe_account] if options[:stripe_account]
612
+ headers
613
+ end
614
+
615
+ def stripe_client_user_agent(options)
616
+ return user_agent unless options[:application]
617
+ JSON.dump(JSON.parse(user_agent).merge!({application: options[:application]}))
618
+ end
619
+
620
+ def api_version(options)
621
+ options[:version] || @options[:version] || self.class::DEFAULT_API_VERSION
622
+ end
623
+
624
+ def api_request(method, endpoint, parameters = nil, options = {})
625
+ raw_response = response = nil
626
+ begin
627
+ raw_response = ssl_request(method, self.live_url + endpoint, post_data(parameters), headers(options))
628
+ response = parse(raw_response)
629
+ rescue ResponseError => e
630
+ raw_response = e.response.body
631
+ response = response_error(raw_response)
632
+ rescue JSON::ParserError
633
+ response = json_error(raw_response)
634
+ end
635
+ response
636
+ end
637
+
638
+ def commit(method, url, parameters = nil, options = {})
639
+ add_expand_parameters(parameters, options) if parameters
640
+ response = api_request(method, url, parameters, options)
641
+ response['webhook_id'] = options[:webhook_id] if options[:webhook_id]
642
+ success = success_from(response)
643
+
644
+ card = card_from_response(response)
645
+ avs_code = AVS_CODE_TRANSLATOR["line1: #{card["address_line1_check"]}, zip: #{card["address_zip_check"]}"]
646
+ cvc_code = CVC_CODE_TRANSLATOR[card['cvc_check']]
647
+
648
+ Response.new(success,
649
+ message_from(success, response),
650
+ response,
651
+ :test => response_is_test?(response),
652
+ :authorization => authorization_from(success, url, method, response),
653
+ :avs_result => { :code => avs_code },
654
+ :cvv_result => cvc_code,
655
+ :emv_authorization => emv_authorization_from_response(response),
656
+ :error_code => success ? nil : error_code_from(response)
657
+ )
658
+ end
659
+
660
+ def authorization_from(success, url, method, response)
661
+ return response.fetch('error', {})['charge'] unless success
662
+
663
+ if url == 'customers'
664
+ [response['id'], response.dig('sources', 'data').first&.dig('id')].join('|')
665
+ elsif method == :post && (url.match(/customers\/.*\/cards/) || url.match(/payment_methods\/.*\/attach/))
666
+ [response['customer'], response['id']].join('|')
667
+ else
668
+ response['id']
669
+ end
670
+ end
671
+
672
+ def message_from(success, response)
673
+ success ? 'Transaction approved' : response.fetch('error', {'message' => 'No error details'})['message']
674
+ end
675
+
676
+ def success_from(response)
677
+ !response.key?('error') && response['status'] != 'failed'
678
+ end
679
+
680
+ def response_error(raw_response)
681
+ parse(raw_response)
682
+ rescue JSON::ParserError
683
+ json_error(raw_response)
684
+ end
685
+
686
+ def json_error(raw_response)
687
+ msg = 'Invalid response received from the Stripe API. Please contact support@stripe.com if you continue to receive this message.'
688
+ msg += " (The raw response returned by the API was #{raw_response.inspect})"
689
+ {
690
+ 'error' => {
691
+ 'message' => msg
692
+ }
693
+ }
694
+ end
695
+
696
+ def response_is_test?(response)
697
+ if response.has_key?('livemode')
698
+ !response['livemode']
699
+ elsif response['charge'].is_a?(Hash) && response['charge'].has_key?('livemode')
700
+ !response['charge']['livemode']
701
+ else
702
+ false
703
+ end
704
+ end
705
+
706
+ def emv_payment?(payment)
707
+ payment.respond_to?(:emv?) && payment.emv?
708
+ end
709
+
710
+ def quickchip_payment?(payment)
711
+ payment.respond_to?(:read_method) && payment.read_method == 'contact_quickchip'
712
+ end
713
+
714
+ def card_from_response(response)
715
+ response['card'] || response['active_card'] || response['source'] || {}
716
+ end
717
+
718
+ def emv_authorization_from_response(response)
719
+ return response['error']['emv_auth_data'] if response['error']
720
+
721
+ card_from_response(response)['emv_auth_data']
722
+ end
723
+
724
+ def error_code_from(response)
725
+ return STANDARD_ERROR_CODE_MAPPING['processing_error'] unless response['error']
726
+
727
+ code = response['error']['code']
728
+ decline_code = response['error']['decline_code'] if code == 'card_declined'
729
+
730
+ error_code = STANDARD_ERROR_CODE_MAPPING[decline_code]
731
+ error_code ||= STANDARD_ERROR_CODE_MAPPING[code]
732
+ error_code
733
+ end
734
+
735
+ def tokenize_bank_account(bank_account, options = {})
736
+ account_holder_type = BANK_ACCOUNT_HOLDER_TYPE_MAPPING[bank_account.account_holder_type]
737
+
738
+ post = {
739
+ bank_account: {
740
+ account_number: bank_account.account_number,
741
+ country: 'US',
742
+ currency: 'usd',
743
+ routing_number: bank_account.routing_number,
744
+ name: bank_account.name,
745
+ account_holder_type: account_holder_type,
746
+ }
747
+ }
748
+
749
+ token_response = api_request(:post, "tokens?#{post_data(post)}")
750
+ success = token_response['error'].nil?
751
+
752
+ if success && token_response['id']
753
+ Response.new(success, nil, token: token_response)
754
+ else
755
+ Response.new(success, token_response['error']['message'])
756
+ end
757
+ end
758
+
759
+ def ach?(payment_method)
760
+ case payment_method
761
+ when String, nil
762
+ false
763
+ else
764
+ card_brand(payment_method) == 'check'
765
+ end
766
+ end
767
+
768
+ def auth_minimum_amount(options)
769
+ return 100 unless options[:currency]
770
+ return MINIMUM_AUTHORIZE_AMOUNTS[options[:currency].upcase] || 100
771
+ end
772
+
773
+ def copy_when_present(dest, dest_path, source, source_path = nil)
774
+ source_path ||= dest_path
775
+ source_path.each do |key|
776
+ return nil unless source[key]
777
+ source = source[key]
778
+ end
779
+
780
+ if source
781
+ dest_path.first(dest_path.size - 1).each do |key|
782
+ dest[key] ||= {}
783
+ dest = dest[key]
784
+ end
785
+ dest[dest_path.last] = source
786
+ end
787
+ end
788
+ end
789
+ end
790
+ end