stripe 12.5.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (688) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +197 -9
  3. data/Gemfile +4 -0
  4. data/Makefile +2 -0
  5. data/OPENAPI_VERSION +1 -1
  6. data/README.md +48 -13
  7. data/Rakefile +0 -5
  8. data/VERSION +1 -1
  9. data/examples/README.md +16 -0
  10. data/examples/example_template.rb +36 -0
  11. data/examples/meter_event_stream.rb +57 -0
  12. data/examples/thinevent_webhook_handler.rb +39 -0
  13. data/justfile +43 -0
  14. data/lib/stripe/api_operations/nested_resource.rb +1 -21
  15. data/lib/stripe/api_operations/request.rb +20 -69
  16. data/lib/stripe/api_operations/save.rb +5 -3
  17. data/lib/stripe/api_operations/singleton_save.rb +6 -2
  18. data/lib/stripe/api_requestor.rb +1136 -0
  19. data/lib/stripe/api_resource.rb +38 -17
  20. data/lib/stripe/api_version.rb +1 -1
  21. data/lib/stripe/connection_manager.rb +1 -1
  22. data/lib/stripe/errors.rb +8 -9
  23. data/lib/stripe/event_types.rb +14 -0
  24. data/lib/stripe/events/v1_billing_meter_error_report_triggered_event.rb +23 -0
  25. data/lib/stripe/events/v1_billing_meter_no_meter_found_event.rb +13 -0
  26. data/lib/stripe/list_object.rb +2 -3
  27. data/lib/stripe/oauth.rb +8 -15
  28. data/lib/stripe/object_types.rb +26 -3
  29. data/lib/stripe/request_options.rb +129 -0
  30. data/lib/stripe/request_params.rb +24 -0
  31. data/lib/stripe/resources/account.rb +4642 -8
  32. data/lib/stripe/resources/account_link.rb +54 -0
  33. data/lib/stripe/resources/account_session.rb +739 -0
  34. data/lib/stripe/resources/apple_pay_domain.rb +58 -9
  35. data/lib/stripe/resources/application.rb +20 -0
  36. data/lib/stripe/resources/application_fee.rb +89 -2
  37. data/lib/stripe/resources/application_fee_refund.rb +17 -0
  38. data/lib/stripe/resources/apps/secret.rb +143 -2
  39. data/lib/stripe/resources/balance.rb +122 -0
  40. data/lib/stripe/resources/balance_transaction.rb +105 -2
  41. data/lib/stripe/resources/bank_account.rb +85 -3
  42. data/lib/stripe/resources/billing/alert.rb +137 -2
  43. data/lib/stripe/resources/billing/alert_triggered.rb +26 -0
  44. data/lib/stripe/resources/billing/credit_balance_summary.rb +54 -0
  45. data/lib/stripe/resources/billing/credit_balance_transaction.rb +133 -0
  46. data/lib/stripe/resources/billing/credit_grant.rb +307 -0
  47. data/lib/stripe/resources/billing/meter.rb +180 -9
  48. data/lib/stripe/resources/billing/meter_event.rb +37 -3
  49. data/lib/stripe/resources/billing/meter_event_adjustment.rb +44 -1
  50. data/lib/stripe/resources/billing/meter_event_summary.rb +17 -0
  51. data/lib/stripe/resources/billing_portal/configuration.rb +557 -4
  52. data/lib/stripe/resources/billing_portal/session.rb +271 -0
  53. data/lib/stripe/resources/capability.rb +84 -0
  54. data/lib/stripe/resources/card.rb +78 -1
  55. data/lib/stripe/resources/cash_balance.rb +17 -0
  56. data/lib/stripe/resources/charge.rb +1616 -4
  57. data/lib/stripe/resources/checkout/session.rb +3534 -10
  58. data/lib/stripe/resources/climate/order.rb +171 -4
  59. data/lib/stripe/resources/climate/product.rb +49 -2
  60. data/lib/stripe/resources/climate/supplier.rb +47 -2
  61. data/lib/stripe/resources/confirmation_token.rb +1448 -0
  62. data/lib/stripe/resources/connect_collection_transfer.rb +24 -0
  63. data/lib/stripe/resources/country_spec.rb +56 -2
  64. data/lib/stripe/resources/coupon.rb +195 -6
  65. data/lib/stripe/resources/credit_note.rb +641 -2
  66. data/lib/stripe/resources/credit_note_line_item.rb +67 -0
  67. data/lib/stripe/resources/customer.rb +832 -9
  68. data/lib/stripe/resources/customer_balance_transaction.rb +29 -0
  69. data/lib/stripe/resources/customer_cash_balance_transaction.rb +109 -0
  70. data/lib/stripe/resources/customer_session.rb +163 -0
  71. data/lib/stripe/resources/discount.rb +29 -0
  72. data/lib/stripe/resources/dispute.rb +614 -4
  73. data/lib/stripe/resources/entitlements/active_entitlement.rb +39 -2
  74. data/lib/stripe/resources/entitlements/active_entitlement_summary.rb +23 -0
  75. data/lib/stripe/resources/entitlements/feature.rb +83 -2
  76. data/lib/stripe/resources/ephemeral_key.rb +23 -2
  77. data/lib/stripe/resources/event.rb +92 -2
  78. data/lib/stripe/resources/exchange_rate.rb +26 -2
  79. data/lib/stripe/resources/file.rb +123 -15
  80. data/lib/stripe/resources/file_link.rb +107 -4
  81. data/lib/stripe/resources/financial_connections/account.rb +239 -2
  82. data/lib/stripe/resources/financial_connections/account_owner.rb +17 -0
  83. data/lib/stripe/resources/financial_connections/account_ownership.rb +9 -0
  84. data/lib/stripe/resources/financial_connections/session.rb +95 -0
  85. data/lib/stripe/resources/financial_connections/transaction.rb +94 -2
  86. data/lib/stripe/resources/forwarding/request.rb +158 -2
  87. data/lib/stripe/resources/funding_instructions.rb +308 -0
  88. data/lib/stripe/resources/identity/verification_report.rb +259 -2
  89. data/lib/stripe/resources/identity/verification_session.rb +356 -4
  90. data/lib/stripe/resources/invoice.rb +3210 -27
  91. data/lib/stripe/resources/invoice_item.rb +406 -6
  92. data/lib/stripe/resources/invoice_line_item.rb +356 -3
  93. data/lib/stripe/resources/invoice_payment.rb +105 -0
  94. data/lib/stripe/resources/invoice_rendering_template.rb +120 -0
  95. data/lib/stripe/resources/issuing/authorization.rb +1132 -6
  96. data/lib/stripe/resources/issuing/card.rb +682 -5
  97. data/lib/stripe/resources/issuing/cardholder.rb +653 -5
  98. data/lib/stripe/resources/issuing/dispute.rb +785 -4
  99. data/lib/stripe/resources/issuing/personalization_design.rb +280 -4
  100. data/lib/stripe/resources/issuing/physical_bundle.rb +56 -2
  101. data/lib/stripe/resources/issuing/token.rb +170 -4
  102. data/lib/stripe/resources/issuing/transaction.rb +916 -4
  103. data/lib/stripe/resources/line_item.rb +47 -0
  104. data/lib/stripe/resources/login_link.rb +7 -0
  105. data/lib/stripe/resources/mandate.rb +142 -0
  106. data/lib/stripe/resources/payment_intent.rb +8660 -4
  107. data/lib/stripe/resources/payment_link.rb +1636 -4
  108. data/lib/stripe/resources/payment_method.rb +1546 -4
  109. data/lib/stripe/resources/payment_method_configuration.rb +2780 -4
  110. data/lib/stripe/resources/payment_method_domain.rb +154 -10
  111. data/lib/stripe/resources/payout.rb +213 -4
  112. data/lib/stripe/resources/person.rb +264 -1
  113. data/lib/stripe/resources/plan.rb +308 -6
  114. data/lib/stripe/resources/price.rb +609 -5
  115. data/lib/stripe/resources/product.rb +474 -2
  116. data/lib/stripe/resources/product_feature.rb +12 -0
  117. data/lib/stripe/resources/promotion_code.rb +216 -4
  118. data/lib/stripe/resources/quote.rb +927 -10
  119. data/lib/stripe/resources/radar/early_fraud_warning.rb +70 -2
  120. data/lib/stripe/resources/radar/value_list.rb +122 -6
  121. data/lib/stripe/resources/radar/value_list_item.rb +90 -4
  122. data/lib/stripe/resources/refund.rb +396 -4
  123. data/lib/stripe/resources/reporting/report_run.rb +143 -2
  124. data/lib/stripe/resources/reporting/report_type.rb +29 -2
  125. data/lib/stripe/resources/reserve_transaction.rb +22 -0
  126. data/lib/stripe/resources/reversal.rb +21 -0
  127. data/lib/stripe/resources/review.rb +101 -2
  128. data/lib/stripe/resources/setup_attempt.rb +370 -2
  129. data/lib/stripe/resources/setup_intent.rb +3477 -4
  130. data/lib/stripe/resources/shipping_rate.rb +254 -4
  131. data/lib/stripe/resources/sigma/scheduled_query_run.rb +47 -2
  132. data/lib/stripe/resources/source.rb +1203 -4
  133. data/lib/stripe/resources/source_mandate_notification.rb +66 -0
  134. data/lib/stripe/resources/source_transaction.rb +85 -0
  135. data/lib/stripe/resources/subscription.rb +1697 -11
  136. data/lib/stripe/resources/subscription_item.rb +319 -11
  137. data/lib/stripe/resources/subscription_schedule.rb +1285 -4
  138. data/lib/stripe/resources/tax/calculation.rb +378 -0
  139. data/lib/stripe/resources/tax/calculation_line_item.rb +56 -0
  140. data/lib/stripe/resources/tax/registration.rb +2375 -2
  141. data/lib/stripe/resources/tax/settings.rb +117 -0
  142. data/lib/stripe/resources/tax/transaction.rb +258 -0
  143. data/lib/stripe/resources/tax/transaction_line_item.rb +31 -0
  144. data/lib/stripe/resources/tax_code.rb +28 -2
  145. data/lib/stripe/resources/tax_deducted_at_source.rb +22 -0
  146. data/lib/stripe/resources/tax_id.rb +114 -2
  147. data/lib/stripe/resources/tax_rate.rb +197 -6
  148. data/lib/stripe/resources/terminal/configuration.rb +1266 -6
  149. data/lib/stripe/resources/terminal/connection_token.rb +18 -0
  150. data/lib/stripe/resources/terminal/location.rb +172 -6
  151. data/lib/stripe/resources/terminal/reader.rb +433 -6
  152. data/lib/stripe/resources/test_helpers/test_clock.rb +81 -4
  153. data/lib/stripe/resources/token.rb +1160 -1
  154. data/lib/stripe/resources/topup.rb +169 -4
  155. data/lib/stripe/resources/transfer.rb +148 -4
  156. data/lib/stripe/resources/treasury/credit_reversal.rb +85 -2
  157. data/lib/stripe/resources/treasury/debit_reversal.rb +96 -2
  158. data/lib/stripe/resources/treasury/financial_account.rb +749 -5
  159. data/lib/stripe/resources/treasury/financial_account_features.rb +189 -0
  160. data/lib/stripe/resources/treasury/inbound_transfer.rb +231 -3
  161. data/lib/stripe/resources/treasury/outbound_payment.rb +491 -3
  162. data/lib/stripe/resources/treasury/outbound_transfer.rb +346 -3
  163. data/lib/stripe/resources/treasury/received_credit.rb +239 -2
  164. data/lib/stripe/resources/treasury/received_debit.rb +198 -2
  165. data/lib/stripe/resources/treasury/transaction.rb +169 -2
  166. data/lib/stripe/resources/treasury/transaction_entry.rb +150 -2
  167. data/lib/stripe/resources/v2/amount.rb +9 -0
  168. data/lib/stripe/resources/v2/billing/meter_event.rb +34 -0
  169. data/lib/stripe/resources/v2/billing/meter_event_adjustment.rb +36 -0
  170. data/lib/stripe/resources/v2/billing/meter_event_session.rb +29 -0
  171. data/lib/stripe/resources/v2/event.rb +41 -0
  172. data/lib/stripe/resources/v2/event_destination.rb +73 -0
  173. data/lib/stripe/resources/webhook_endpoint.rb +119 -11
  174. data/lib/stripe/resources.rb +19 -2
  175. data/lib/stripe/search_result_object.rb +1 -1
  176. data/lib/stripe/services/account_capability_service.rb +71 -0
  177. data/lib/stripe/services/account_external_account_service.rb +318 -0
  178. data/lib/stripe/services/account_link_service.rb +63 -0
  179. data/lib/stripe/services/account_login_link_service.rb +28 -0
  180. data/lib/stripe/services/account_person_service.rb +936 -0
  181. data/lib/stripe/services/account_service.rb +4100 -0
  182. data/lib/stripe/services/account_session_service.rb +501 -0
  183. data/lib/stripe/services/apple_pay_domain_service.rb +101 -0
  184. data/lib/stripe/services/application_fee_refund_service.rb +114 -0
  185. data/lib/stripe/services/application_fee_service.rb +92 -0
  186. data/lib/stripe/services/apps/secret_service.rb +168 -0
  187. data/lib/stripe/services/apps_service.rb +13 -0
  188. data/lib/stripe/services/balance_service.rb +21 -0
  189. data/lib/stripe/services/balance_transaction_service.rb +101 -0
  190. data/lib/stripe/services/billing/alert_service.rb +187 -0
  191. data/lib/stripe/services/billing/credit_balance_summary_service.rb +67 -0
  192. data/lib/stripe/services/billing/credit_balance_transaction_service.rb +70 -0
  193. data/lib/stripe/services/billing/credit_grant_service.rb +240 -0
  194. data/lib/stripe/services/billing/meter_event_adjustment_service.rb +45 -0
  195. data/lib/stripe/services/billing/meter_event_service.rb +40 -0
  196. data/lib/stripe/services/billing/meter_event_summary_service.rb +58 -0
  197. data/lib/stripe/services/billing/meter_service.rb +211 -0
  198. data/lib/stripe/services/billing_portal/configuration_service.rb +496 -0
  199. data/lib/stripe/services/billing_portal/session_service.rb +192 -0
  200. data/lib/stripe/services/billing_portal_service.rb +14 -0
  201. data/lib/stripe/services/billing_service.rb +20 -0
  202. data/lib/stripe/services/charge_service.rb +471 -0
  203. data/lib/stripe/services/checkout/session_line_item_service.rb +37 -0
  204. data/lib/stripe/services/checkout/session_service.rb +2466 -0
  205. data/lib/stripe/services/checkout_service.rb +13 -0
  206. data/lib/stripe/services/climate/order_service.rb +170 -0
  207. data/lib/stripe/services/climate/product_service.rb +57 -0
  208. data/lib/stripe/services/climate/supplier_service.rb +57 -0
  209. data/lib/stripe/services/climate_service.rb +15 -0
  210. data/lib/stripe/services/confirmation_token_service.rb +26 -0
  211. data/lib/stripe/services/country_spec_service.rb +55 -0
  212. data/lib/stripe/services/coupon_service.rb +203 -0
  213. data/lib/stripe/services/credit_note_line_item_service.rb +35 -0
  214. data/lib/stripe/services/credit_note_preview_lines_service.rb +169 -0
  215. data/lib/stripe/services/credit_note_service.rb +458 -0
  216. data/lib/stripe/services/customer_balance_transaction_service.rb +113 -0
  217. data/lib/stripe/services/customer_cash_balance_service.rb +57 -0
  218. data/lib/stripe/services/customer_cash_balance_transaction_service.rb +55 -0
  219. data/lib/stripe/services/customer_funding_instructions_service.rb +61 -0
  220. data/lib/stripe/services/customer_payment_method_service.rb +68 -0
  221. data/lib/stripe/services/customer_payment_source_service.rb +255 -0
  222. data/lib/stripe/services/customer_service.rb +666 -0
  223. data/lib/stripe/services/customer_session_service.rb +113 -0
  224. data/lib/stripe/services/customer_tax_id_service.rb +95 -0
  225. data/lib/stripe/services/dispute_service.rb +422 -0
  226. data/lib/stripe/services/entitlements/active_entitlement_service.rb +66 -0
  227. data/lib/stripe/services/entitlements/feature_service.rb +128 -0
  228. data/lib/stripe/services/entitlements_service.rb +14 -0
  229. data/lib/stripe/services/ephemeral_key_service.rb +64 -0
  230. data/lib/stripe/services/event_service.rb +87 -0
  231. data/lib/stripe/services/exchange_rate_service.rb +55 -0
  232. data/lib/stripe/services/file_link_service.rb +132 -0
  233. data/lib/stripe/services/file_service.rb +124 -0
  234. data/lib/stripe/services/financial_connections/account_owner_service.rb +46 -0
  235. data/lib/stripe/services/financial_connections/account_service.rb +177 -0
  236. data/lib/stripe/services/financial_connections/session_service.rb +98 -0
  237. data/lib/stripe/services/financial_connections/transaction_service.rb +100 -0
  238. data/lib/stripe/services/financial_connections_service.rb +15 -0
  239. data/lib/stripe/services/forwarding/request_service.rb +147 -0
  240. data/lib/stripe/services/forwarding_service.rb +13 -0
  241. data/lib/stripe/services/identity/verification_report_service.rb +95 -0
  242. data/lib/stripe/services/identity/verification_session_service.rb +337 -0
  243. data/lib/stripe/services/identity_service.rb +14 -0
  244. data/lib/stripe/services/invoice_item_service.rb +395 -0
  245. data/lib/stripe/services/invoice_line_item_service.rb +252 -0
  246. data/lib/stripe/services/invoice_payment_service.rb +83 -0
  247. data/lib/stripe/services/invoice_rendering_template_service.rb +101 -0
  248. data/lib/stripe/services/invoice_service.rb +2831 -0
  249. data/lib/stripe/services/issuing/authorization_service.rb +169 -0
  250. data/lib/stripe/services/issuing/card_service.rb +507 -0
  251. data/lib/stripe/services/issuing/cardholder_service.rb +577 -0
  252. data/lib/stripe/services/issuing/dispute_service.rb +696 -0
  253. data/lib/stripe/services/issuing/personalization_design_service.rb +245 -0
  254. data/lib/stripe/services/issuing/physical_bundle_service.rb +70 -0
  255. data/lib/stripe/services/issuing/token_service.rb +114 -0
  256. data/lib/stripe/services/issuing/transaction_service.rb +118 -0
  257. data/lib/stripe/services/issuing_service.rb +20 -0
  258. data/lib/stripe/services/mandate_service.rb +26 -0
  259. data/lib/stripe/services/oauth_service.rb +63 -0
  260. data/lib/stripe/services/payment_intent_service.rb +7258 -0
  261. data/lib/stripe/services/payment_link_line_item_service.rb +35 -0
  262. data/lib/stripe/services/payment_link_service.rb +1333 -0
  263. data/lib/stripe/services/payment_method_configuration_service.rb +2054 -0
  264. data/lib/stripe/services/payment_method_domain_service.rb +142 -0
  265. data/lib/stripe/services/payment_method_service.rb +894 -0
  266. data/lib/stripe/services/payout_service.rb +225 -0
  267. data/lib/stripe/services/plan_service.rb +298 -0
  268. data/lib/stripe/services/price_service.rb +545 -0
  269. data/lib/stripe/services/product_feature_service.rb +92 -0
  270. data/lib/stripe/services/product_service.rb +495 -0
  271. data/lib/stripe/services/promotion_code_service.rb +228 -0
  272. data/lib/stripe/services/quote_computed_upfront_line_items_service.rb +35 -0
  273. data/lib/stripe/services/quote_line_item_service.rb +35 -0
  274. data/lib/stripe/services/quote_service.rb +703 -0
  275. data/lib/stripe/services/radar/early_fraud_warning_service.rb +93 -0
  276. data/lib/stripe/services/radar/value_list_item_service.rb +131 -0
  277. data/lib/stripe/services/radar/value_list_service.rb +166 -0
  278. data/lib/stripe/services/radar_service.rb +15 -0
  279. data/lib/stripe/services/refund_service.rb +200 -0
  280. data/lib/stripe/services/reporting/report_run_service.rb +147 -0
  281. data/lib/stripe/services/reporting/report_type_service.rb +48 -0
  282. data/lib/stripe/services/reporting_service.rb +14 -0
  283. data/lib/stripe/services/review_service.rb +89 -0
  284. data/lib/stripe/services/setup_attempt_service.rb +68 -0
  285. data/lib/stripe/services/setup_intent_service.rb +3268 -0
  286. data/lib/stripe/services/shipping_rate_service.rb +250 -0
  287. data/lib/stripe/services/sigma/scheduled_query_run_service.rb +57 -0
  288. data/lib/stripe/services/sigma_service.rb +13 -0
  289. data/lib/stripe/services/source_service.rb +665 -0
  290. data/lib/stripe/services/source_transaction_service.rb +35 -0
  291. data/lib/stripe/services/subscription_item_service.rb +346 -0
  292. data/lib/stripe/services/subscription_schedule_service.rb +1134 -0
  293. data/lib/stripe/services/subscription_service.rb +1539 -0
  294. data/lib/stripe/services/tax/calculation_line_item_service.rb +37 -0
  295. data/lib/stripe/services/tax/calculation_service.rb +242 -0
  296. data/lib/stripe/services/tax/registration_service.rb +1618 -0
  297. data/lib/stripe/services/tax/settings_service.rb +104 -0
  298. data/lib/stripe/services/tax/transaction_line_item_service.rb +37 -0
  299. data/lib/stripe/services/tax/transaction_service.rb +159 -0
  300. data/lib/stripe/services/tax_code_service.rb +49 -0
  301. data/lib/stripe/services/tax_id_service.rb +117 -0
  302. data/lib/stripe/services/tax_rate_service.rb +193 -0
  303. data/lib/stripe/services/tax_service.rb +16 -0
  304. data/lib/stripe/services/terminal/configuration_service.rb +1060 -0
  305. data/lib/stripe/services/terminal/connection_token_service.rb +31 -0
  306. data/lib/stripe/services/terminal/location_service.rb +208 -0
  307. data/lib/stripe/services/terminal/reader_service.rb +384 -0
  308. data/lib/stripe/services/terminal_service.rb +16 -0
  309. data/lib/stripe/services/test_helpers/confirmation_token_service.rb +661 -0
  310. data/lib/stripe/services/test_helpers/customer_service.rb +37 -0
  311. data/lib/stripe/services/test_helpers/issuing/authorization_service.rb +832 -0
  312. data/lib/stripe/services/test_helpers/issuing/card_service.rb +110 -0
  313. data/lib/stripe/services/test_helpers/issuing/personalization_design_service.rb +84 -0
  314. data/lib/stripe/services/test_helpers/issuing/transaction_service.rb +675 -0
  315. data/lib/stripe/services/test_helpers/issuing_service.rb +19 -0
  316. data/lib/stripe/services/test_helpers/refund_service.rb +28 -0
  317. data/lib/stripe/services/test_helpers/terminal/reader_service.rb +65 -0
  318. data/lib/stripe/services/test_helpers/terminal_service.rb +15 -0
  319. data/lib/stripe/services/test_helpers/test_clock_service.rb +120 -0
  320. data/lib/stripe/services/test_helpers/treasury/inbound_transfer_service.rb +81 -0
  321. data/lib/stripe/services/test_helpers/treasury/outbound_payment_service.rb +141 -0
  322. data/lib/stripe/services/test_helpers/treasury/outbound_transfer_service.rb +141 -0
  323. data/lib/stripe/services/test_helpers/treasury/received_credit_service.rb +81 -0
  324. data/lib/stripe/services/test_helpers/treasury/received_debit_service.rb +81 -0
  325. data/lib/stripe/services/test_helpers/treasury_service.rb +19 -0
  326. data/lib/stripe/services/test_helpers_service.rb +19 -0
  327. data/lib/stripe/services/token_service.rb +1163 -0
  328. data/lib/stripe/services/topup_service.rb +191 -0
  329. data/lib/stripe/services/transfer_reversal_service.rb +122 -0
  330. data/lib/stripe/services/transfer_service.rb +166 -0
  331. data/lib/stripe/services/treasury/credit_reversal_service.rb +100 -0
  332. data/lib/stripe/services/treasury/debit_reversal_service.rb +104 -0
  333. data/lib/stripe/services/treasury/financial_account_features_service.rb +196 -0
  334. data/lib/stripe/services/treasury/financial_account_service.rb +554 -0
  335. data/lib/stripe/services/treasury/inbound_transfer_service.rb +140 -0
  336. data/lib/stripe/services/treasury/outbound_payment_service.rb +310 -0
  337. data/lib/stripe/services/treasury/outbound_transfer_service.rb +176 -0
  338. data/lib/stripe/services/treasury/received_credit_service.rb +82 -0
  339. data/lib/stripe/services/treasury/received_debit_service.rb +70 -0
  340. data/lib/stripe/services/treasury/transaction_entry_service.rb +117 -0
  341. data/lib/stripe/services/treasury/transaction_service.rb +125 -0
  342. data/lib/stripe/services/treasury_service.rb +22 -0
  343. data/lib/stripe/services/v1_services.rb +90 -0
  344. data/lib/stripe/services/v2/billing/meter_event_adjustment_service.rb +44 -0
  345. data/lib/stripe/services/v2/billing/meter_event_service.rb +46 -0
  346. data/lib/stripe/services/v2/billing/meter_event_session_service.rb +24 -0
  347. data/lib/stripe/services/v2/billing/meter_event_stream_service.rb +56 -0
  348. data/lib/stripe/services/v2/billing_service.rb +18 -0
  349. data/lib/stripe/services/v2/core/event_destination_service.rb +241 -0
  350. data/lib/stripe/services/v2/core/event_service.rb +49 -0
  351. data/lib/stripe/services/v2/core_service.rb +16 -0
  352. data/lib/stripe/services/v2_services.rb +14 -0
  353. data/lib/stripe/services/webhook_endpoint_service.rb +157 -0
  354. data/lib/stripe/services.rb +180 -0
  355. data/lib/stripe/singleton_api_resource.rb +1 -18
  356. data/lib/stripe/stripe_client.rb +53 -1031
  357. data/lib/stripe/stripe_configuration.rb +52 -19
  358. data/lib/stripe/stripe_object.rb +54 -20
  359. data/lib/stripe/stripe_service.rb +32 -0
  360. data/lib/stripe/thin_event.rb +37 -0
  361. data/lib/stripe/util.rb +78 -37
  362. data/lib/stripe/v2_list_object.rb +84 -0
  363. data/lib/stripe/version.rb +1 -1
  364. data/lib/stripe/webhook.rb +1 -1
  365. data/lib/stripe.rb +38 -25
  366. data/rbi/stripe/resources/account.rbi +4840 -0
  367. data/rbi/stripe/resources/account_link.rbi +74 -0
  368. data/rbi/stripe/resources/account_session.rbi +894 -0
  369. data/rbi/stripe/resources/apple_pay_domain.rbi +90 -0
  370. data/rbi/stripe/resources/application.rbi +20 -0
  371. data/rbi/stripe/resources/application_fee.rbi +118 -0
  372. data/rbi/stripe/resources/application_fee_refund.rbi +37 -0
  373. data/rbi/stripe/resources/apps/secret.rbi +195 -0
  374. data/rbi/stripe/resources/balance.rbi +180 -0
  375. data/rbi/stripe/resources/balance_transaction.rbi +144 -0
  376. data/rbi/stripe/resources/bank_account.rbi +127 -0
  377. data/rbi/stripe/resources/billing/alert.rbi +201 -0
  378. data/rbi/stripe/resources/billing/alert_triggered.rbi +28 -0
  379. data/rbi/stripe/resources/billing/credit_balance_summary.rbi +63 -0
  380. data/rbi/stripe/resources/billing/credit_balance_transaction.rbi +147 -0
  381. data/rbi/stripe/resources/billing/credit_grant.rbi +291 -0
  382. data/rbi/stripe/resources/billing/meter.rbi +226 -0
  383. data/rbi/stripe/resources/billing/meter_event.rbi +64 -0
  384. data/rbi/stripe/resources/billing/meter_event_adjustment.rbi +64 -0
  385. data/rbi/stripe/resources/billing/meter_event_summary.rbi +35 -0
  386. data/rbi/stripe/resources/billing_portal/configuration.rbi +645 -0
  387. data/rbi/stripe/resources/billing_portal/session.rbi +345 -0
  388. data/rbi/stripe/resources/capability.rbi +125 -0
  389. data/rbi/stripe/resources/card.rbi +125 -0
  390. data/rbi/stripe/resources/cash_balance.rbi +32 -0
  391. data/rbi/stripe/resources/charge.rbi +2054 -0
  392. data/rbi/stripe/resources/checkout/session.rbi +3951 -0
  393. data/rbi/stripe/resources/climate/order.rbi +245 -0
  394. data/rbi/stripe/resources/climate/product.rbi +75 -0
  395. data/rbi/stripe/resources/climate/supplier.rbi +72 -0
  396. data/rbi/stripe/resources/confirmation_token.rbi +1793 -0
  397. data/rbi/stripe/resources/connect_collection_transfer.rbi +26 -0
  398. data/rbi/stripe/resources/country_spec.rbi +85 -0
  399. data/rbi/stripe/resources/coupon.rbi +252 -0
  400. data/rbi/stripe/resources/credit_note.rbi +747 -0
  401. data/rbi/stripe/resources/credit_note_line_item.rbi +101 -0
  402. data/rbi/stripe/resources/customer.rbi +1009 -0
  403. data/rbi/stripe/resources/customer_balance_transaction.rbi +56 -0
  404. data/rbi/stripe/resources/customer_cash_balance_transaction.rbi +151 -0
  405. data/rbi/stripe/resources/customer_session.rbi +200 -0
  406. data/rbi/stripe/resources/discount.rbi +53 -0
  407. data/rbi/stripe/resources/dispute.rbi +761 -0
  408. data/rbi/stripe/resources/entitlements/active_entitlement.rbi +58 -0
  409. data/rbi/stripe/resources/entitlements/active_entitlement_summary.rbi +23 -0
  410. data/rbi/stripe/resources/entitlements/feature.rbi +117 -0
  411. data/rbi/stripe/resources/ephemeral_key.rbi +44 -0
  412. data/rbi/stripe/resources/event.rbi +146 -0
  413. data/rbi/stripe/resources/exchange_rate.rbi +66 -0
  414. data/rbi/stripe/resources/file.rbi +143 -0
  415. data/rbi/stripe/resources/file_link.rbi +141 -0
  416. data/rbi/stripe/resources/financial_connections/account.rbi +339 -0
  417. data/rbi/stripe/resources/financial_connections/account_owner.rbi +35 -0
  418. data/rbi/stripe/resources/financial_connections/account_ownership.rbi +23 -0
  419. data/rbi/stripe/resources/financial_connections/session.rbi +123 -0
  420. data/rbi/stripe/resources/financial_connections/transaction.rbi +124 -0
  421. data/rbi/stripe/resources/forwarding/request.rbi +216 -0
  422. data/rbi/stripe/resources/funding_instructions.rbi +437 -0
  423. data/rbi/stripe/resources/identity/verification_report.rbi +351 -0
  424. data/rbi/stripe/resources/identity/verification_session.rbi +517 -0
  425. data/rbi/stripe/resources/invoice.rbi +3733 -0
  426. data/rbi/stripe/resources/invoice_item.rbi +475 -0
  427. data/rbi/stripe/resources/invoice_line_item.rbi +432 -0
  428. data/rbi/stripe/resources/invoice_payment.rbi +114 -0
  429. data/rbi/stripe/resources/invoice_rendering_template.rbi +104 -0
  430. data/rbi/stripe/resources/issuing/authorization.rbi +1283 -0
  431. data/rbi/stripe/resources/issuing/card.rbi +741 -0
  432. data/rbi/stripe/resources/issuing/cardholder.rbi +744 -0
  433. data/rbi/stripe/resources/issuing/dispute.rbi +913 -0
  434. data/rbi/stripe/resources/issuing/personalization_design.rbi +338 -0
  435. data/rbi/stripe/resources/issuing/physical_bundle.rbi +79 -0
  436. data/rbi/stripe/resources/issuing/token.rbi +226 -0
  437. data/rbi/stripe/resources/issuing/transaction.rbi +1061 -0
  438. data/rbi/stripe/resources/line_item.rbi +72 -0
  439. data/rbi/stripe/resources/login_link.rbi +18 -0
  440. data/rbi/stripe/resources/mandate.rbi +187 -0
  441. data/rbi/stripe/resources/payment_intent.rbi +9731 -0
  442. data/rbi/stripe/resources/payment_link.rbi +1801 -0
  443. data/rbi/stripe/resources/payment_method.rbi +1822 -0
  444. data/rbi/stripe/resources/payment_method_configuration.rbi +3154 -0
  445. data/rbi/stripe/resources/payment_method_domain.rbi +212 -0
  446. data/rbi/stripe/resources/payout.rbi +300 -0
  447. data/rbi/stripe/resources/person.rbi +371 -0
  448. data/rbi/stripe/resources/plan.rbi +373 -0
  449. data/rbi/stripe/resources/price.rbi +694 -0
  450. data/rbi/stripe/resources/product.rbi +557 -0
  451. data/rbi/stripe/resources/product_feature.rbi +26 -0
  452. data/rbi/stripe/resources/promotion_code.rbi +261 -0
  453. data/rbi/stripe/resources/quote.rbi +1123 -0
  454. data/rbi/stripe/resources/radar/early_fraud_warning.rbi +98 -0
  455. data/rbi/stripe/resources/radar/value_list.rbi +170 -0
  456. data/rbi/stripe/resources/radar/value_list_item.rbi +131 -0
  457. data/rbi/stripe/resources/refund.rbi +516 -0
  458. data/rbi/stripe/resources/reporting/report_run.rbi +191 -0
  459. data/rbi/stripe/resources/reporting/report_type.rbi +57 -0
  460. data/rbi/stripe/resources/reserve_transaction.rbi +23 -0
  461. data/rbi/stripe/resources/reversal.rbi +51 -0
  462. data/rbi/stripe/resources/review.rbi +154 -0
  463. data/rbi/stripe/resources/setup_attempt.rbi +477 -0
  464. data/rbi/stripe/resources/setup_intent.rbi +3860 -0
  465. data/rbi/stripe/resources/shipping_rate.rbi +305 -0
  466. data/rbi/stripe/resources/sigma/scheduled_query_run.rbi +75 -0
  467. data/rbi/stripe/resources/source.rbi +1496 -0
  468. data/rbi/stripe/resources/source_mandate_notification.rbi +77 -0
  469. data/rbi/stripe/resources/source_transaction.rbi +127 -0
  470. data/rbi/stripe/resources/subscription.rbi +1964 -0
  471. data/rbi/stripe/resources/subscription_item.rbi +370 -0
  472. data/rbi/stripe/resources/subscription_schedule.rbi +1495 -0
  473. data/rbi/stripe/resources/tax/calculation.rbi +474 -0
  474. data/rbi/stripe/resources/tax/calculation_line_item.rbi +88 -0
  475. data/rbi/stripe/resources/tax/registration.rbi +2485 -0
  476. data/rbi/stripe/resources/tax/settings.rbi +141 -0
  477. data/rbi/stripe/resources/tax/transaction.rbi +351 -0
  478. data/rbi/stripe/resources/tax/transaction_line_item.rbi +54 -0
  479. data/rbi/stripe/resources/tax_code.rbi +44 -0
  480. data/rbi/stripe/resources/tax_deducted_at_source.rbi +23 -0
  481. data/rbi/stripe/resources/tax_id.rbi +167 -0
  482. data/rbi/stripe/resources/tax_rate.rbi +239 -0
  483. data/rbi/stripe/resources/terminal/configuration.rbi +1388 -0
  484. data/rbi/stripe/resources/terminal/connection_token.rbi +37 -0
  485. data/rbi/stripe/resources/terminal/location.rbi +224 -0
  486. data/rbi/stripe/resources/terminal/reader.rbi +587 -0
  487. data/rbi/stripe/resources/test_helpers/test_clock.rbi +134 -0
  488. data/rbi/stripe/resources/token.rbi +1217 -0
  489. data/rbi/stripe/resources/topup.rbi +222 -0
  490. data/rbi/stripe/resources/transfer.rbi +199 -0
  491. data/rbi/stripe/resources/treasury/credit_reversal.rbi +119 -0
  492. data/rbi/stripe/resources/treasury/debit_reversal.rbi +131 -0
  493. data/rbi/stripe/resources/treasury/financial_account.rbi +865 -0
  494. data/rbi/stripe/resources/treasury/financial_account_features.rbi +260 -0
  495. data/rbi/stripe/resources/treasury/inbound_transfer.rbi +295 -0
  496. data/rbi/stripe/resources/treasury/outbound_payment.rbi +592 -0
  497. data/rbi/stripe/resources/treasury/outbound_transfer.rbi +431 -0
  498. data/rbi/stripe/resources/treasury/received_credit.rbi +302 -0
  499. data/rbi/stripe/resources/treasury/received_debit.rbi +256 -0
  500. data/rbi/stripe/resources/treasury/transaction.rbi +215 -0
  501. data/rbi/stripe/resources/treasury/transaction_entry.rbi +192 -0
  502. data/rbi/stripe/resources/v2/amount.rbi +14 -0
  503. data/rbi/stripe/resources/v2/billing/meter_event.rbi +37 -0
  504. data/rbi/stripe/resources/v2/billing/meter_event_adjustment.rbi +41 -0
  505. data/rbi/stripe/resources/v2/billing/meter_event_session.rbi +31 -0
  506. data/rbi/stripe/resources/v2/event.rbi +48 -0
  507. data/rbi/stripe/resources/v2/event_destination.rbi +91 -0
  508. data/rbi/stripe/resources/webhook_endpoint.rbi +170 -0
  509. data/rbi/stripe/services/account_capability_service.rbi +51 -0
  510. data/rbi/stripe/services/account_external_account_service.rbi +306 -0
  511. data/rbi/stripe/services/account_link_service.rbi +58 -0
  512. data/rbi/stripe/services/account_login_link_service.rbi +22 -0
  513. data/rbi/stripe/services/account_person_service.rbi +923 -0
  514. data/rbi/stripe/services/account_service.rbi +4143 -0
  515. data/rbi/stripe/services/account_session_service.rbi +572 -0
  516. data/rbi/stripe/services/apple_pay_domain_service.rbi +78 -0
  517. data/rbi/stripe/services/application_fee_refund_service.rbi +93 -0
  518. data/rbi/stripe/services/application_fee_service.rbi +78 -0
  519. data/rbi/stripe/services/apps/secret_service.rbi +151 -0
  520. data/rbi/stripe/services/apps_service.rbi +9 -0
  521. data/rbi/stripe/services/balance_service.rbi +21 -0
  522. data/rbi/stripe/services/balance_transaction_service.rbi +93 -0
  523. data/rbi/stripe/services/billing/alert_service.rbi +152 -0
  524. data/rbi/stripe/services/billing/credit_balance_summary_service.rbi +68 -0
  525. data/rbi/stripe/services/billing/credit_balance_transaction_service.rbi +59 -0
  526. data/rbi/stripe/services/billing/credit_grant_service.rbi +211 -0
  527. data/rbi/stripe/services/billing/meter_event_adjustment_service.rbi +42 -0
  528. data/rbi/stripe/services/billing/meter_event_service.rbi +42 -0
  529. data/rbi/stripe/services/billing/meter_event_summary_service.rbi +54 -0
  530. data/rbi/stripe/services/billing/meter_service.rbi +163 -0
  531. data/rbi/stripe/services/billing_portal/configuration_service.rbi +511 -0
  532. data/rbi/stripe/services/billing_portal/session_service.rbi +204 -0
  533. data/rbi/stripe/services/billing_portal_service.rbi +10 -0
  534. data/rbi/stripe/services/billing_service.rbi +15 -0
  535. data/rbi/stripe/services/charge_service.rbi +448 -0
  536. data/rbi/stripe/services/checkout/session_line_item_service.rbi +33 -0
  537. data/rbi/stripe/services/checkout/session_service.rbi +2585 -0
  538. data/rbi/stripe/services/checkout_service.rbi +9 -0
  539. data/rbi/stripe/services/climate/order_service.rbi +142 -0
  540. data/rbi/stripe/services/climate/product_service.rbi +46 -0
  541. data/rbi/stripe/services/climate/supplier_service.rbi +46 -0
  542. data/rbi/stripe/services/climate_service.rbi +11 -0
  543. data/rbi/stripe/services/confirmation_token_service.rbi +20 -0
  544. data/rbi/stripe/services/country_spec_service.rbi +44 -0
  545. data/rbi/stripe/services/coupon_service.rbi +196 -0
  546. data/rbi/stripe/services/credit_note_line_item_service.rbi +31 -0
  547. data/rbi/stripe/services/credit_note_preview_lines_service.rbi +167 -0
  548. data/rbi/stripe/services/credit_note_service.rbi +416 -0
  549. data/rbi/stripe/services/customer_balance_transaction_service.rbi +92 -0
  550. data/rbi/stripe/services/customer_cash_balance_service.rbi +45 -0
  551. data/rbi/stripe/services/customer_cash_balance_transaction_service.rbi +44 -0
  552. data/rbi/stripe/services/customer_funding_instructions_service.rbi +59 -0
  553. data/rbi/stripe/services/customer_payment_method_service.rbi +57 -0
  554. data/rbi/stripe/services/customer_payment_source_service.rbi +232 -0
  555. data/rbi/stripe/services/customer_service.rbi +651 -0
  556. data/rbi/stripe/services/customer_session_service.rbi +116 -0
  557. data/rbi/stripe/services/customer_tax_id_service.rbi +72 -0
  558. data/rbi/stripe/services/dispute_service.rbi +424 -0
  559. data/rbi/stripe/services/entitlements/active_entitlement_service.rbi +55 -0
  560. data/rbi/stripe/services/entitlements/feature_service.rbi +107 -0
  561. data/rbi/stripe/services/entitlements_service.rbi +10 -0
  562. data/rbi/stripe/services/ephemeral_key_service.rbi +53 -0
  563. data/rbi/stripe/services/event_service.rbi +83 -0
  564. data/rbi/stripe/services/exchange_rate_service.rbi +44 -0
  565. data/rbi/stripe/services/file_link_service.rbi +124 -0
  566. data/rbi/stripe/services/file_service.rbi +116 -0
  567. data/rbi/stripe/services/financial_connections/account_owner_service.rbi +42 -0
  568. data/rbi/stripe/services/financial_connections/account_service.rbi +133 -0
  569. data/rbi/stripe/services/financial_connections/session_service.rbi +88 -0
  570. data/rbi/stripe/services/financial_connections/transaction_service.rbi +92 -0
  571. data/rbi/stripe/services/financial_connections_service.rbi +11 -0
  572. data/rbi/stripe/services/forwarding/request_service.rbi +134 -0
  573. data/rbi/stripe/services/forwarding_service.rbi +9 -0
  574. data/rbi/stripe/services/identity/verification_report_service.rbi +87 -0
  575. data/rbi/stripe/services/identity/verification_session_service.rbi +323 -0
  576. data/rbi/stripe/services/identity_service.rbi +10 -0
  577. data/rbi/stripe/services/invoice_item_service.rbi +368 -0
  578. data/rbi/stripe/services/invoice_line_item_service.rbi +259 -0
  579. data/rbi/stripe/services/invoice_payment_service.rbi +71 -0
  580. data/rbi/stripe/services/invoice_rendering_template_service.rbi +82 -0
  581. data/rbi/stripe/services/invoice_service.rbi +2920 -0
  582. data/rbi/stripe/services/issuing/authorization_service.rbi +146 -0
  583. data/rbi/stripe/services/issuing/card_service.rbi +495 -0
  584. data/rbi/stripe/services/issuing/cardholder_service.rbi +587 -0
  585. data/rbi/stripe/services/issuing/dispute_service.rbi +707 -0
  586. data/rbi/stripe/services/issuing/personalization_design_service.rbi +243 -0
  587. data/rbi/stripe/services/issuing/physical_bundle_service.rbi +59 -0
  588. data/rbi/stripe/services/issuing/token_service.rbi +99 -0
  589. data/rbi/stripe/services/issuing/transaction_service.rbi +105 -0
  590. data/rbi/stripe/services/issuing_service.rbi +16 -0
  591. data/rbi/stripe/services/mandate_service.rbi +20 -0
  592. data/rbi/stripe/services/payment_intent_service.rbi +7759 -0
  593. data/rbi/stripe/services/payment_link_line_item_service.rbi +31 -0
  594. data/rbi/stripe/services/payment_link_service.rbi +1352 -0
  595. data/rbi/stripe/services/payment_method_configuration_service.rbi +2211 -0
  596. data/rbi/stripe/services/payment_method_domain_service.rbi +112 -0
  597. data/rbi/stripe/services/payment_method_service.rbi +840 -0
  598. data/rbi/stripe/services/payout_service.rbi +205 -0
  599. data/rbi/stripe/services/plan_service.rbi +284 -0
  600. data/rbi/stripe/services/price_service.rbi +546 -0
  601. data/rbi/stripe/services/product_feature_service.rbi +69 -0
  602. data/rbi/stripe/services/product_service.rbi +479 -0
  603. data/rbi/stripe/services/promotion_code_service.rbi +214 -0
  604. data/rbi/stripe/services/quote_computed_upfront_line_items_service.rbi +31 -0
  605. data/rbi/stripe/services/quote_line_item_service.rbi +31 -0
  606. data/rbi/stripe/services/quote_service.rbi +686 -0
  607. data/rbi/stripe/services/radar/early_fraud_warning_service.rbi +85 -0
  608. data/rbi/stripe/services/radar/value_list_item_service.rbi +111 -0
  609. data/rbi/stripe/services/radar/value_list_service.rbi +143 -0
  610. data/rbi/stripe/services/radar_service.rbi +11 -0
  611. data/rbi/stripe/services/refund_service.rbi +185 -0
  612. data/rbi/stripe/services/reporting/report_run_service.rbi +135 -0
  613. data/rbi/stripe/services/reporting/report_type_service.rbi +35 -0
  614. data/rbi/stripe/services/reporting_service.rbi +10 -0
  615. data/rbi/stripe/services/review_service.rbi +84 -0
  616. data/rbi/stripe/services/setup_attempt_service.rbi +65 -0
  617. data/rbi/stripe/services/setup_intent_service.rbi +3596 -0
  618. data/rbi/stripe/services/shipping_rate_service.rbi +241 -0
  619. data/rbi/stripe/services/sigma/scheduled_query_run_service.rbi +46 -0
  620. data/rbi/stripe/services/sigma_service.rbi +9 -0
  621. data/rbi/stripe/services/source_service.rbi +663 -0
  622. data/rbi/stripe/services/source_transaction_service.rbi +31 -0
  623. data/rbi/stripe/services/subscription_item_service.rbi +324 -0
  624. data/rbi/stripe/services/subscription_schedule_service.rbi +1192 -0
  625. data/rbi/stripe/services/subscription_service.rbi +1596 -0
  626. data/rbi/stripe/services/tax/calculation_line_item_service.rbi +33 -0
  627. data/rbi/stripe/services/tax/calculation_service.rbi +230 -0
  628. data/rbi/stripe/services/tax/registration_service.rbi +1689 -0
  629. data/rbi/stripe/services/tax/settings_service.rbi +93 -0
  630. data/rbi/stripe/services/tax/transaction_line_item_service.rbi +33 -0
  631. data/rbi/stripe/services/tax/transaction_service.rbi +146 -0
  632. data/rbi/stripe/services/tax_code_service.rbi +44 -0
  633. data/rbi/stripe/services/tax_id_service.rbi +112 -0
  634. data/rbi/stripe/services/tax_rate_service.rbi +185 -0
  635. data/rbi/stripe/services/tax_service.rbi +12 -0
  636. data/rbi/stripe/services/terminal/configuration_service.rbi +1120 -0
  637. data/rbi/stripe/services/terminal/connection_token_service.rbi +25 -0
  638. data/rbi/stripe/services/terminal/location_service.rbi +184 -0
  639. data/rbi/stripe/services/terminal/reader_service.rbi +344 -0
  640. data/rbi/stripe/services/terminal_service.rbi +12 -0
  641. data/rbi/stripe/services/test_helpers/confirmation_token_service.rbi +746 -0
  642. data/rbi/stripe/services/test_helpers/customer_service.rbi +33 -0
  643. data/rbi/stripe/services/test_helpers/issuing/authorization_service.rbi +848 -0
  644. data/rbi/stripe/services/test_helpers/issuing/card_service.rbi +76 -0
  645. data/rbi/stripe/services/test_helpers/issuing/personalization_design_service.rbi +69 -0
  646. data/rbi/stripe/services/test_helpers/issuing/transaction_service.rbi +711 -0
  647. data/rbi/stripe/services/test_helpers/issuing_service.rbi +14 -0
  648. data/rbi/stripe/services/test_helpers/refund_service.rbi +22 -0
  649. data/rbi/stripe/services/test_helpers/terminal/reader_service.rbi +62 -0
  650. data/rbi/stripe/services/test_helpers/terminal_service.rbi +11 -0
  651. data/rbi/stripe/services/test_helpers/test_clock_service.rbi +92 -0
  652. data/rbi/stripe/services/test_helpers/treasury/inbound_transfer_service.rbi +64 -0
  653. data/rbi/stripe/services/test_helpers/treasury/outbound_payment_service.rbi +125 -0
  654. data/rbi/stripe/services/test_helpers/treasury/outbound_transfer_service.rbi +125 -0
  655. data/rbi/stripe/services/test_helpers/treasury/received_credit_service.rbi +87 -0
  656. data/rbi/stripe/services/test_helpers/treasury/received_debit_service.rbi +87 -0
  657. data/rbi/stripe/services/test_helpers/treasury_service.rbi +15 -0
  658. data/rbi/stripe/services/test_helpers_service.rbi +15 -0
  659. data/rbi/stripe/services/token_service.rbi +1189 -0
  660. data/rbi/stripe/services/topup_service.rbi +176 -0
  661. data/rbi/stripe/services/transfer_reversal_service.rbi +101 -0
  662. data/rbi/stripe/services/transfer_service.rbi +152 -0
  663. data/rbi/stripe/services/treasury/credit_reversal_service.rbi +84 -0
  664. data/rbi/stripe/services/treasury/debit_reversal_service.rbi +88 -0
  665. data/rbi/stripe/services/treasury/financial_account_features_service.rbi +198 -0
  666. data/rbi/stripe/services/treasury/financial_account_service.rbi +562 -0
  667. data/rbi/stripe/services/treasury/inbound_transfer_service.rbi +117 -0
  668. data/rbi/stripe/services/treasury/outbound_payment_service.rbi +304 -0
  669. data/rbi/stripe/services/treasury/outbound_transfer_service.rbi +157 -0
  670. data/rbi/stripe/services/treasury/received_credit_service.rbi +72 -0
  671. data/rbi/stripe/services/treasury/received_debit_service.rbi +59 -0
  672. data/rbi/stripe/services/treasury/transaction_entry_service.rbi +111 -0
  673. data/rbi/stripe/services/treasury/transaction_service.rbi +122 -0
  674. data/rbi/stripe/services/treasury_service.rbi +18 -0
  675. data/rbi/stripe/services/v1_services.rbi +77 -0
  676. data/rbi/stripe/services/v2/billing/meter_event_adjustment_service.rbi +39 -0
  677. data/rbi/stripe/services/v2/billing/meter_event_service.rbi +42 -0
  678. data/rbi/stripe/services/v2/billing/meter_event_session_service.rbi +20 -0
  679. data/rbi/stripe/services/v2/billing/meter_event_stream_service.rbi +55 -0
  680. data/rbi/stripe/services/v2/billing_service.rbi +14 -0
  681. data/rbi/stripe/services/v2/core/event_destination_service.rbi +200 -0
  682. data/rbi/stripe/services/v2/core/event_service.rbi +38 -0
  683. data/rbi/stripe/services/v2/core_service.rbi +12 -0
  684. data/rbi/stripe/services/v2_services.rbi +10 -0
  685. data/rbi/stripe/services/webhook_endpoint_service.rbi +131 -0
  686. metadata +533 -4
  687. data/lib/stripe/resources/usage_record.rb +0 -17
  688. data/lib/stripe/resources/usage_record_summary.rb +0 -11
@@ -0,0 +1,1136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "stripe/instrumentation"
4
+
5
+ module Stripe
6
+ # APIRequestor executes requests against the Stripe API and allows a user to
7
+ # recover both a resource a call returns as well as a response object that
8
+ # contains information on the HTTP call.
9
+ class APIRequestor
10
+ # A set of all known thread contexts across all threads and a mutex to
11
+ # synchronize global access to them.
12
+ @thread_contexts_with_connection_managers = Set.new
13
+ @thread_contexts_with_connection_managers_mutex = Mutex.new
14
+ @last_connection_manager_gc = Util.monotonic_time
15
+
16
+ # Initializes a new APIRequestor
17
+ def initialize(config_arg = {})
18
+ @system_profiler = SystemProfiler.new
19
+ @last_request_metrics = Queue.new
20
+
21
+ @config = case config_arg
22
+ when Hash
23
+ StripeConfiguration.new.reverse_duplicate_merge(config_arg)
24
+ when Stripe::StripeConfiguration
25
+ config_arg
26
+ when String
27
+ StripeConfiguration.new.reverse_duplicate_merge(
28
+ { api_key: config_arg }
29
+ )
30
+ else
31
+ raise ArgumentError, "Can't handle argument: #{config_arg}"
32
+ end
33
+ end
34
+
35
+ attr_reader :config, :options
36
+
37
+ # Gets a currently active `APIRequestor`. Set for the current thread when
38
+ # `APIRequestor#request` is being run so that API operations being executed
39
+ # inside of that block can find the currently active requestor. It's reset to
40
+ # the original value (hopefully `nil`) after the block ends.
41
+ #
42
+ # For internal use only. Does not provide a stable API and may be broken
43
+ # with future non-major changes.
44
+ def self.active_requestor
45
+ current_thread_context.active_requestor || default_requestor
46
+ end
47
+
48
+ # Finishes any active connections by closing their TCP connection and
49
+ # clears them from internal tracking in all connection managers across all
50
+ # threads.
51
+ #
52
+ # If passed a `config` object, only clear connection managers for that
53
+ # particular configuration.
54
+ #
55
+ # For internal use only. Does not provide a stable API and may be broken
56
+ # with future non-major changes.
57
+ def self.clear_all_connection_managers(config: nil)
58
+ # Just a quick path for when configuration is being set for the first
59
+ # time before any connections have been opened. There is technically some
60
+ # potential for thread raciness here, but not in a practical sense.
61
+ return if @thread_contexts_with_connection_managers.empty?
62
+
63
+ @thread_contexts_with_connection_managers_mutex.synchronize do
64
+ pruned_contexts = Set.new
65
+
66
+ @thread_contexts_with_connection_managers.each do |thread_context|
67
+ # Note that the thread context itself is not destroyed, but we clear
68
+ # its connection manager and remove our reference to it. If it ever
69
+ # makes a new request we'll give it a new connection manager and
70
+ # it'll go back into `@thread_contexts_with_connection_managers`.
71
+ thread_context.default_connection_managers.reject! do |cm_config, cm|
72
+ if config.nil? || config.key == cm_config
73
+ cm.clear
74
+ true
75
+ end
76
+ end
77
+
78
+ pruned_contexts << thread_context if thread_context.default_connection_managers.empty?
79
+ end
80
+
81
+ @thread_contexts_with_connection_managers.subtract(pruned_contexts)
82
+ end
83
+ end
84
+
85
+ # A default requestor for the current thread.
86
+ def self.default_requestor
87
+ current_thread_context.default_requestor ||= APIRequestor.new(Stripe.config)
88
+ end
89
+
90
+ # A default connection manager for the current thread scoped to the
91
+ # configuration object that may be provided.
92
+ def self.default_connection_manager(config = Stripe.config)
93
+ current_thread_context.default_connection_managers[config.key] ||= begin
94
+ connection_manager = ConnectionManager.new(config)
95
+
96
+ @thread_contexts_with_connection_managers_mutex.synchronize do
97
+ maybe_gc_connection_managers
98
+ @thread_contexts_with_connection_managers << current_thread_context
99
+ end
100
+
101
+ connection_manager
102
+ end
103
+ end
104
+
105
+ # Checks if an error is a problem that we should retry on. This includes
106
+ # both socket errors that may represent an intermittent problem and some
107
+ # special HTTP statuses.
108
+ def self.should_retry?(error,
109
+ num_retries:, config: Stripe.config)
110
+ return false if num_retries >= config.max_network_retries
111
+
112
+ case error
113
+ when Net::OpenTimeout, Net::ReadTimeout
114
+ # Retry on timeout-related problems (either on open or read).
115
+ true
116
+ when EOFError, Errno::ECONNREFUSED, Errno::ECONNRESET, # rubocop:todo Lint/DuplicateBranch
117
+ Errno::EHOSTUNREACH, Errno::ETIMEDOUT, SocketError
118
+ # Destination refused the connection, the connection was reset, or a
119
+ # variety of other connection failures. This could occur from a single
120
+ # saturated server, so retry in case it's intermittent.
121
+ true
122
+ when Stripe::StripeError
123
+ # The API may ask us not to retry (e.g. if doing so would be a no-op),
124
+ # or advise us to retry (e.g. in cases of lock timeouts). Defer to
125
+ # those instructions if given.
126
+ return false if error.http_headers["stripe-should-retry"] == "false"
127
+ return true if error.http_headers["stripe-should-retry"] == "true"
128
+
129
+ # 409 Conflict
130
+ return true if error.http_status == 409
131
+
132
+ # 429 Too Many Requests
133
+ #
134
+ # There are a few different problems that can lead to a 429. The most
135
+ # common is rate limiting, on which we *don't* want to retry because
136
+ # that'd likely contribute to more contention problems. However, some
137
+ # 429s are lock timeouts, which is when a request conflicted with
138
+ # another request or an internal process on some particular object.
139
+ # These 429s are safe to retry.
140
+ return true if error.http_status == 429 && error.code == "lock_timeout"
141
+
142
+ # Retry on 500, 503, and other internal errors.
143
+ #
144
+ # Note that we expect the stripe-should-retry header to be false
145
+ # in most cases when a 500 is returned, since our idempotency framework
146
+ # would typically replay it anyway.
147
+ true if error.http_status >= 500
148
+ else
149
+ false
150
+ end
151
+ end
152
+
153
+ def self.sleep_time(num_retries, config: Stripe.config)
154
+ # Apply exponential backoff with initial_network_retry_delay on the
155
+ # number of num_retries so far as inputs. Do not allow the number to
156
+ # exceed max_network_retry_delay.
157
+ sleep_seconds = [
158
+ config.initial_network_retry_delay * (2**(num_retries - 1)),
159
+ config.max_network_retry_delay,
160
+ ].min
161
+
162
+ # Apply some jitter by randomizing the value in the range of
163
+ # (sleep_seconds / 2) to (sleep_seconds).
164
+ sleep_seconds *= (0.5 * (1 + rand))
165
+
166
+ # But never sleep less than the base sleep seconds.
167
+ [config.initial_network_retry_delay, sleep_seconds].max
168
+ end
169
+
170
+ # Executes the API call within the given block. Usage looks like:
171
+ #
172
+ # client = APIRequestor.new
173
+ # charge, resp = client.request { Charge.create }
174
+ #
175
+ def request
176
+ old_api_requestor = self.class.current_thread_context.active_requestor
177
+ self.class.current_thread_context.active_requestor = self
178
+
179
+ if self.class.current_thread_context.last_responses&.key?(object_id)
180
+ raise "calls to APIRequestor#request cannot be nested within a thread"
181
+ end
182
+
183
+ self.class.current_thread_context.last_responses ||= {}
184
+ self.class.current_thread_context.last_responses[object_id] = nil
185
+
186
+ begin
187
+ res = yield
188
+ [res, self.class.current_thread_context.last_responses[object_id]]
189
+ ensure
190
+ self.class.current_thread_context.active_requestor = old_api_requestor
191
+ self.class.current_thread_context.last_responses.delete(object_id)
192
+ end
193
+ end
194
+ extend Gem::Deprecate
195
+ deprecate :request, "StripeClient#raw_request", 2024, 9
196
+
197
+ def execute_request(method, path, base_address,
198
+ params: {}, opts: {}, usage: [])
199
+ params = params.to_h if params.is_a?(RequestParams)
200
+ http_resp, req_opts = execute_request_internal(
201
+ method, path, base_address, params, opts, usage
202
+ )
203
+ req_opts = RequestOptions.extract_opts_from_hash(req_opts)
204
+
205
+ resp = interpret_response(http_resp)
206
+
207
+ # If being called from `APIRequestor#request`, put the last response in
208
+ # thread-local memory so that it can be returned to the user. Don't store
209
+ # anything otherwise so that we don't leak memory.
210
+ store_last_response(object_id, resp)
211
+
212
+ api_mode = Util.get_api_mode(path)
213
+ Util.convert_to_stripe_object_with_params(resp.data, params, RequestOptions.persistable(req_opts), resp,
214
+ api_mode: api_mode, requestor: self)
215
+ end
216
+
217
+ # Execute request without instantiating a new object if the relevant object's name matches the class
218
+ #
219
+ # For internal use only. Does not provide a stable API and may be broken
220
+ # with future non-major changes.
221
+ def execute_request_initialize_from(method, path, base_address, object,
222
+ params: {}, opts: {}, usage: [])
223
+ opts = RequestOptions.combine_opts(object.instance_variable_get(:@opts) || {}, opts)
224
+ opts = Util.normalize_opts(opts)
225
+
226
+ params = params.to_h if params.is_a?(RequestParams)
227
+
228
+ http_resp, req_opts = execute_request_internal(
229
+ method, path, base_address, params, opts, usage
230
+ )
231
+ req_opts = RequestOptions.extract_opts_from_hash(req_opts)
232
+
233
+ resp = interpret_response(http_resp)
234
+
235
+ # If being called from `APIRequestor#request`, put the last response in
236
+ # thread-local memory so that it can be returned to the user. Don't store
237
+ # anything otherwise so that we don't leak memory.
238
+ store_last_response(object_id, resp)
239
+
240
+ if Util.object_name_matches_class?(resp.data[:object], object.class)
241
+ object.send(:initialize_from,
242
+ resp.data, RequestOptions.persistable(req_opts), resp,
243
+ api_mode: :v1, requestor: self)
244
+ else
245
+ Util.convert_to_stripe_object_with_params(resp.data, params,
246
+ RequestOptions.persistable(req_opts),
247
+ resp, api_mode: :v1, requestor: self)
248
+ end
249
+ end
250
+
251
+ def interpret_response(http_resp)
252
+ StripeResponse.from_net_http(http_resp)
253
+ rescue JSON::ParserError
254
+ raise general_api_error(http_resp.code.to_i, http_resp.body)
255
+ end
256
+
257
+ # Executes a request and returns the body as a stream instead of converting
258
+ # it to a StripeObject. This should be used for any request where we expect
259
+ # an arbitrary binary response.
260
+ #
261
+ # A `read_body_chunk` block can be passed, which will be called repeatedly
262
+ # with the body chunks read from the socket.
263
+ #
264
+ # If a block is passed, a StripeHeadersOnlyResponse is returned as the
265
+ # block is expected to do all the necessary body processing. If no block is
266
+ # passed, then a StripeStreamResponse is returned containing an IO stream
267
+ # with the response body.
268
+ def execute_request_stream(method, path,
269
+ base_address,
270
+ params: {}, opts: {}, usage: [],
271
+ &read_body_chunk_block)
272
+ unless block_given?
273
+ raise ArgumentError,
274
+ "execute_request_stream requires a read_body_chunk_block"
275
+ end
276
+
277
+ params = params.to_h if params.is_a?(RequestParams)
278
+ http_resp, api_key = execute_request_internal(
279
+ method, path, base_address, params, opts, usage, &read_body_chunk_block
280
+ )
281
+
282
+ # When the read_body_chunk_block is given, we no longer have access to the
283
+ # response body at this point and so return a response object containing
284
+ # only the headers. This is because the body was consumed by the block.
285
+ resp = StripeHeadersOnlyResponse.from_net_http(http_resp)
286
+
287
+ [resp, api_key]
288
+ end
289
+
290
+ def store_last_response(object_id, resp)
291
+ return unless last_response_has_key?(object_id)
292
+
293
+ self.class.current_thread_context.last_responses[object_id] = resp
294
+ end
295
+
296
+ def last_response_has_key?(object_id)
297
+ self.class.current_thread_context.last_responses&.key?(object_id)
298
+ end
299
+
300
+ #
301
+ # private
302
+ #
303
+
304
+ # Time (in seconds) that a connection manager has not been used before it's
305
+ # eligible for garbage collection.
306
+ CONNECTION_MANAGER_GC_LAST_USED_EXPIRY = 120
307
+
308
+ # How often to check (in seconds) for connection managers that haven't been
309
+ # used in a long time and which should be garbage collected.
310
+ CONNECTION_MANAGER_GC_PERIOD = 60
311
+
312
+ ERROR_MESSAGE_CONNECTION =
313
+ "Unexpected error communicating when trying to connect to " \
314
+ "Stripe (%s). You may be seeing this message because your DNS is not " \
315
+ "working or you don't have an internet connection. To check, try " \
316
+ "running `host stripe.com` from the command line."
317
+ ERROR_MESSAGE_SSL =
318
+ "Could not establish a secure connection to Stripe (%s), you " \
319
+ "may need to upgrade your OpenSSL version. To check, try running " \
320
+ "`openssl s_client -connect api.stripe.com:443` from the command " \
321
+ "line."
322
+
323
+ # Common error suffix sared by both connect and read timeout messages.
324
+ ERROR_MESSAGE_TIMEOUT_SUFFIX =
325
+ "Please check your internet connection and try again. " \
326
+ "If this problem persists, you should check Stripe's service " \
327
+ "status at https://status.stripe.com, or let us know at " \
328
+ "support@stripe.com."
329
+
330
+ ERROR_MESSAGE_TIMEOUT_CONNECT = (
331
+ "Timed out connecting to Stripe (%s). " +
332
+ ERROR_MESSAGE_TIMEOUT_SUFFIX
333
+ ).freeze
334
+
335
+ ERROR_MESSAGE_TIMEOUT_READ = (
336
+ "Timed out communicating with Stripe (%s). " +
337
+ ERROR_MESSAGE_TIMEOUT_SUFFIX
338
+ ).freeze
339
+
340
+ # Maps types of exceptions that we're likely to see during a network
341
+ # request to more user-friendly messages that we put in front of people.
342
+ # The original error message is also appended onto the final exception for
343
+ # full transparency.
344
+ NETWORK_ERROR_MESSAGES_MAP = {
345
+ EOFError => ERROR_MESSAGE_CONNECTION,
346
+ Errno::ECONNREFUSED => ERROR_MESSAGE_CONNECTION,
347
+ Errno::ECONNRESET => ERROR_MESSAGE_CONNECTION,
348
+ Errno::EHOSTUNREACH => ERROR_MESSAGE_CONNECTION,
349
+ Errno::ETIMEDOUT => ERROR_MESSAGE_TIMEOUT_CONNECT,
350
+ SocketError => ERROR_MESSAGE_CONNECTION,
351
+
352
+ Net::OpenTimeout => ERROR_MESSAGE_TIMEOUT_CONNECT,
353
+ Net::ReadTimeout => ERROR_MESSAGE_TIMEOUT_READ,
354
+
355
+ OpenSSL::SSL::SSLError => ERROR_MESSAGE_SSL,
356
+ }.freeze
357
+ private_constant :NETWORK_ERROR_MESSAGES_MAP
358
+
359
+ # A record representing any data that `APIRequestor` puts into
360
+ # `Thread.current`. Making it a class likes this gives us a little extra
361
+ # type safety and lets us document what each field does.
362
+ #
363
+ # For internal use only. Does not provide a stable API and may be broken
364
+ # with future non-major changes.
365
+ class ThreadContext
366
+ # A `APIRequestor` that's been flagged as currently active within a
367
+ # thread by `APIRequestor#request`. A requestor stays active until the
368
+ # completion of the request block.
369
+ attr_accessor :active_requestor
370
+
371
+ # A default `APIRequestor` object for the thread. Used in all cases where
372
+ # the user hasn't specified their own.
373
+ attr_accessor :default_requestor
374
+
375
+ # A temporary map of object IDs to responses from last executed API
376
+ # calls. Used to return a responses from calls to `APIRequestor#request`.
377
+ #
378
+ # Stored in the thread data to make the use of a single `APIRequestor`
379
+ # object safe across multiple threads. Stored as a map so that multiple
380
+ # `APIRequestor` objects can run concurrently on the same thread.
381
+ #
382
+ # Responses are only left in as long as they're needed, which means
383
+ # they're removed as soon as a call leaves `APIRequestor#request`, and
384
+ # because that's wrapped in an `ensure` block, they should never leave
385
+ # garbage in `Thread.current`.
386
+ attr_accessor :last_responses
387
+
388
+ # A map of connection mangers for the thread. Normally shared between
389
+ # all `APIRequestor` objects on a particular thread, and created so as to
390
+ # minimize the number of open connections that an application needs.
391
+ def default_connection_managers
392
+ @default_connection_managers ||= {}
393
+ end
394
+
395
+ def reset_connection_managers
396
+ @default_connection_managers = {}
397
+ end
398
+ end
399
+
400
+ # Access data stored for `APIRequestor` within the thread's current
401
+ # context. Returns `ThreadContext`.
402
+ #
403
+ # For internal use only. Does not provide a stable API and may be broken
404
+ # with future non-major changes.
405
+ def self.current_thread_context
406
+ Thread.current[:api_requestor__internal_use_only] ||= ThreadContext.new
407
+ end
408
+
409
+ # Garbage collects connection managers that haven't been used in some time,
410
+ # with the idea being that we want to remove old connection managers that
411
+ # belong to dead threads and the like.
412
+ #
413
+ # Prefixed with `maybe_` because garbage collection will only run
414
+ # periodically so that we're not constantly engaged in busy work. If
415
+ # connection managers live a little passed their useful age it's not
416
+ # harmful, so it's not necessary to get them right away.
417
+ #
418
+ # For testability, returns `nil` if it didn't run and the number of
419
+ # connection managers that were garbage collected otherwise.
420
+ #
421
+ # IMPORTANT: This method is not thread-safe and expects to be called inside
422
+ # a lock on `@thread_contexts_with_connection_managers_mutex`.
423
+ #
424
+ # For internal use only. Does not provide a stable API and may be broken
425
+ # with future non-major changes.
426
+ def self.maybe_gc_connection_managers
427
+ next_gc_time = @last_connection_manager_gc + CONNECTION_MANAGER_GC_PERIOD
428
+ return nil if next_gc_time > Util.monotonic_time
429
+
430
+ last_used_threshold =
431
+ Util.monotonic_time - CONNECTION_MANAGER_GC_LAST_USED_EXPIRY
432
+
433
+ pruned_contexts = []
434
+ @thread_contexts_with_connection_managers.each do |thread_context|
435
+ thread_context
436
+ .default_connection_managers
437
+ .each do |config_key, connection_manager|
438
+ next if connection_manager.last_used > last_used_threshold
439
+
440
+ connection_manager.clear
441
+ thread_context.default_connection_managers.delete(config_key)
442
+ end
443
+ end
444
+
445
+ @thread_contexts_with_connection_managers.each do |thread_context|
446
+ next unless thread_context.default_connection_managers.empty?
447
+
448
+ pruned_contexts << thread_context
449
+ end
450
+
451
+ @thread_contexts_with_connection_managers -= pruned_contexts
452
+ @last_connection_manager_gc = Util.monotonic_time
453
+
454
+ pruned_contexts.count
455
+ end
456
+
457
+ private def execute_request_internal(method, path,
458
+ base_address, params, opts, usage,
459
+ &read_body_chunk_block)
460
+ api_mode = Util.get_api_mode(path)
461
+ opts = RequestOptions.merge_config_and_opts(config, opts)
462
+
463
+ raise ArgumentError, "method should be a symbol" \
464
+ unless method.is_a?(Symbol)
465
+ raise ArgumentError, "path should be a string" \
466
+ unless path.is_a?(String)
467
+
468
+ base_url ||= config.base_addresses[base_address]
469
+
470
+ raise ArgumentError, "api_base cannot be empty" if base_url.nil? || base_url.empty?
471
+
472
+ api_key ||= opts[:api_key]
473
+ params = Util.objects_to_ids(params)
474
+
475
+ check_api_key!(api_key)
476
+
477
+ body_params = nil
478
+ query_params = nil
479
+ case method
480
+ when :get, :head, :delete
481
+ query_params = params
482
+ else
483
+ body_params = params
484
+ end
485
+
486
+ query_params, path = merge_query_params(query_params, path)
487
+
488
+ headers = request_headers(method, api_mode, opts)
489
+ url = api_url(path, base_url)
490
+
491
+ # Merge given query parameters with any already encoded in the path.
492
+ query = query_params ? Util.encode_parameters(query_params, api_mode) : nil
493
+
494
+ # Encoding body parameters is a little more complex because we may have
495
+ # to send a multipart-encoded body. `body_log` is produced separately as
496
+ # a log-friendly variant of the encoded form. File objects are displayed
497
+ # as such instead of as their file contents.
498
+ body, body_log =
499
+ body_params ? encode_body(body_params, headers, api_mode) : [nil, nil]
500
+
501
+ # stores information on the request we're about to make so that we don't
502
+ # have to pass as many parameters around for logging.
503
+ context = RequestLogContext.new
504
+ context.account = headers["Stripe-Account"]
505
+ context.api_key = api_key
506
+ context.api_version = headers["Stripe-Version"]
507
+ context.body = body_log
508
+ context.idempotency_key = headers["Idempotency-Key"]
509
+ context.method = method
510
+ context.path = path
511
+ context.query = query
512
+
513
+ # A block can be passed in to read the content directly from the response.
514
+ # We want to execute this block only when the response was actually
515
+ # successful. When it wasn't, we defer to the standard error handling as
516
+ # we have to read the body and parse the error JSON.
517
+ response_block =
518
+ if block_given?
519
+ lambda do |response|
520
+ response.read_body(&read_body_chunk_block) unless should_handle_as_error(response.code.to_i)
521
+ end
522
+ end
523
+
524
+ http_resp =
525
+ execute_request_with_rescues(base_url, headers, api_mode, usage, context) do
526
+ self.class
527
+ .default_connection_manager(config)
528
+ .execute_request(method, url,
529
+ body: body,
530
+ headers: headers,
531
+ query: query,
532
+ &response_block)
533
+ end
534
+
535
+ [http_resp, opts]
536
+ end
537
+
538
+ private def api_url(url, base_url)
539
+ base_url + url
540
+ end
541
+
542
+ private def check_api_key!(api_key)
543
+ unless api_key
544
+ raise AuthenticationError, "No API key provided. " \
545
+ 'Set your API key using "Stripe.api_key = <API-KEY>". ' \
546
+ "You can generate API keys from the Stripe web interface. " \
547
+ "See https://stripe.com/api for details, or email " \
548
+ "support@stripe.com if you have any questions."
549
+ end
550
+
551
+ return unless api_key =~ /\s/
552
+
553
+ raise AuthenticationError, "Your API key is invalid, as it contains " \
554
+ "whitespace. (HINT: You can double-check your API key from the " \
555
+ "Stripe web interface. See https://stripe.com/api for details, or " \
556
+ "email support@stripe.com if you have any questions.)"
557
+ end
558
+
559
+ # Encodes a set of body parameters using multipart if `Content-Type` is set
560
+ # for that, or standard form-encoding otherwise. Returns the encoded body
561
+ # and a version of the encoded body that's safe to be logged.
562
+ private def encode_body(body_params, headers, api_mode)
563
+ body = nil
564
+ flattened_params = Util.flatten_params(body_params, api_mode)
565
+
566
+ if headers["Content-Type"] == MultipartEncoder::MULTIPART_FORM_DATA
567
+ body, content_type = MultipartEncoder.encode(flattened_params)
568
+
569
+ # Set a new content type that also includes the multipart boundary.
570
+ # See `MultipartEncoder` for details.
571
+ headers["Content-Type"] = content_type
572
+
573
+ # `#to_s` any complex objects like files and the like to build output
574
+ # that's more condusive to logging.
575
+ flattened_params =
576
+ flattened_params.map { |k, v| [k, v.is_a?(String) ? v : v.to_s] }.to_h
577
+
578
+ elsif api_mode == :v2
579
+ body = JSON.generate(body_params)
580
+ headers["Content-Type"] = "application/json"
581
+ else
582
+ body = Util.encode_parameters(body_params, api_mode)
583
+ headers["Content-Type"] = "application/x-www-form-urlencoded"
584
+ end
585
+
586
+ body_log = if api_mode == :v2
587
+ body
588
+ else
589
+ # We don't use `Util.encode_parameters` partly as an optimization (to
590
+ # not redo work we've already done), and partly because the encoded
591
+ # forms of certain characters introduce a lot of visual noise and it's
592
+ # nice to have a clearer format for logs.
593
+ flattened_params.map { |k, v| "#{k}=#{v}" }.join("&")
594
+ end
595
+
596
+ [body, body_log]
597
+ end
598
+
599
+ private def should_handle_as_error(http_status)
600
+ http_status >= 400
601
+ end
602
+
603
+ private def execute_request_with_rescues(base_url, headers, api_mode, usage, context)
604
+ num_retries = 0
605
+
606
+ begin
607
+ request_start = nil
608
+ user_data = nil
609
+
610
+ log_request(context, num_retries)
611
+ user_data = notify_request_begin(context)
612
+
613
+ request_start = Util.monotonic_time
614
+ resp = yield
615
+ request_duration = Util.monotonic_time - request_start
616
+
617
+ http_status = resp.code.to_i
618
+ context = context.dup_from_response_headers(resp)
619
+
620
+ handle_error_response(resp, context, api_mode) if should_handle_as_error(http_status)
621
+
622
+ log_response(context, request_start, http_status, resp.body, resp)
623
+ notify_request_end(context, request_duration, http_status,
624
+ num_retries, user_data, resp, headers)
625
+
626
+ if config.enable_telemetry? && context.request_id
627
+ request_duration_ms = (request_duration * 1000).to_i
628
+ @last_request_metrics << StripeRequestMetrics.new(context.request_id, request_duration_ms, usage: usage)
629
+ end
630
+
631
+ # We rescue all exceptions from a request so that we have an easy spot to
632
+ # implement our retry logic across the board. We'll re-raise if it's a
633
+ # type of exception that we didn't expect to handle.
634
+ rescue StandardError => e
635
+ # If we modify context we copy it into a new variable so as not to
636
+ # taint the original on a retry.
637
+ error_context = context
638
+ http_status = nil
639
+ request_duration = Util.monotonic_time - request_start if request_start
640
+
641
+ if e.is_a?(Stripe::StripeError)
642
+ error_context = context.dup_from_response_headers(e.http_headers)
643
+ http_status = resp.code.to_i
644
+ log_response(error_context, request_start,
645
+ e.http_status, e.http_body, resp)
646
+ else
647
+ log_response_error(error_context, request_start, e)
648
+ end
649
+ notify_request_end(context, request_duration, http_status, num_retries,
650
+ user_data, resp, headers)
651
+
652
+ if self.class.should_retry?(e,
653
+ num_retries: num_retries,
654
+ config: config)
655
+ num_retries += 1
656
+ sleep self.class.sleep_time(num_retries, config: config)
657
+ retry
658
+ end
659
+
660
+ case e
661
+ when Stripe::StripeError
662
+ raise
663
+ when *NETWORK_ERROR_MESSAGES_MAP.keys
664
+ handle_network_error(e, error_context, num_retries, base_url)
665
+
666
+ # Only handle errors when we know we can do so, and re-raise otherwise.
667
+ # This should be pretty infrequent.
668
+ else # rubocop:todo Lint/DuplicateBranch
669
+ raise
670
+ end
671
+ end
672
+
673
+ resp
674
+ end
675
+
676
+ private def notify_request_begin(context)
677
+ return unless Instrumentation.any_subscribers?(:request_begin)
678
+
679
+ event = Instrumentation::RequestBeginEvent.new(
680
+ method: context.method,
681
+ path: context.path,
682
+ user_data: {}
683
+ )
684
+ Stripe::Instrumentation.notify(:request_begin, event)
685
+
686
+ # This field may be set in the `request_begin` callback. If so, we'll
687
+ # forward it onto `request_end`.
688
+ event.user_data
689
+ end
690
+
691
+ private def notify_request_end(context, duration, http_status, num_retries,
692
+ user_data, resp, headers)
693
+ return if !Instrumentation.any_subscribers?(:request_end) &&
694
+ !Instrumentation.any_subscribers?(:request)
695
+
696
+ request_context = Stripe::Instrumentation::RequestContext.new(
697
+ duration: duration,
698
+ context: context,
699
+ header: headers
700
+ )
701
+ response_context = Stripe::Instrumentation::ResponseContext.new(
702
+ http_status: http_status,
703
+ response: resp
704
+ )
705
+
706
+ event = Instrumentation::RequestEndEvent.new(
707
+ request_context: request_context,
708
+ response_context: response_context,
709
+ num_retries: num_retries,
710
+ user_data: user_data || {}
711
+ )
712
+ Stripe::Instrumentation.notify(:request_end, event)
713
+
714
+ # The name before `request_begin` was also added. Provided for backwards
715
+ # compatibility.
716
+ Stripe::Instrumentation.notify(:request, event)
717
+ end
718
+
719
+ private def general_api_error(status, body)
720
+ APIError.new("Invalid response object from API: #{body.inspect} " \
721
+ "(HTTP response code was #{status})",
722
+ http_status: status, http_body: body)
723
+ end
724
+
725
+ # Formats a plugin "app info" hash into a string that we can tack onto the
726
+ # end of a User-Agent string where it'll be fairly prominent in places like
727
+ # the Dashboard. Note that this formatting has been implemented to match
728
+ # other libraries, and shouldn't be changed without universal consensus.
729
+ private def format_app_info(info)
730
+ str = info[:name]
731
+ str = "#{str}/#{info[:version]}" unless info[:version].nil?
732
+ str = "#{str} (#{info[:url]})" unless info[:url].nil?
733
+ str
734
+ end
735
+
736
+ private def handle_error_response(http_resp, context, api_mode)
737
+ begin
738
+ resp = StripeResponse.from_net_http(http_resp)
739
+ error_data = resp.data[:error]
740
+
741
+ raise StripeError, "Indeterminate error" unless error_data
742
+ rescue JSON::ParserError, StripeError
743
+ raise general_api_error(http_resp.code.to_i, http_resp.body)
744
+ end
745
+
746
+ error = if error_data.is_a?(String)
747
+ specific_oauth_error(resp, error_data, context)
748
+ elsif api_mode == :v2
749
+ specific_v2_api_error(resp, error_data, context)
750
+ else
751
+ specific_api_error(resp, error_data, context)
752
+ end
753
+
754
+ error.response = resp
755
+ raise(error)
756
+ end
757
+
758
+ # Works around an edge case where we end up with both query parameters from
759
+ # parameteers and query parameters that were appended onto the end of the
760
+ # given path.
761
+ #
762
+ # Decode any parameters that were added onto the end of a path and add them
763
+ # to a unified query parameter hash so that all parameters end up in one
764
+ # place and all of them are correctly included in the final request.
765
+ private def merge_query_params(query_params, path)
766
+ u = URI.parse(path)
767
+
768
+ # Return original results if there was nothing to be found.
769
+ return query_params, path if u.query.nil?
770
+
771
+ query_params ||= {}
772
+ query_params = Hash[URI.decode_www_form(u.query)].merge(query_params)
773
+
774
+ # Reset the path minus any query parameters that were specified.
775
+ path = u.path
776
+
777
+ [query_params, path]
778
+ end
779
+
780
+ private def specific_api_error(resp, error_data, context)
781
+ Util.log_error("Stripe API error",
782
+ status: resp.http_status,
783
+ error_code: error_data[:code],
784
+ error_message: error_data[:message],
785
+ error_param: error_data[:param],
786
+ error_type: error_data[:type],
787
+ idempotency_key: context.idempotency_key,
788
+ request_id: context.request_id,
789
+ config: config)
790
+
791
+ # The standard set of arguments that can be used to initialize most of
792
+ # the exceptions.
793
+ opts = {
794
+ http_body: resp.http_body,
795
+ http_headers: resp.http_headers,
796
+ http_status: resp.http_status,
797
+ json_body: resp.data,
798
+ code: error_data[:code],
799
+ }
800
+
801
+ case resp.http_status
802
+ when 400, 404
803
+ case error_data[:type]
804
+ when "idempotency_error"
805
+ IdempotencyError.new(error_data[:message], **opts)
806
+ else
807
+ InvalidRequestError.new(
808
+ error_data[:message], error_data[:param],
809
+ **opts
810
+ )
811
+ end
812
+ when 401
813
+ AuthenticationError.new(error_data[:message], **opts)
814
+ when 402
815
+ CardError.new(
816
+ error_data[:message], error_data[:param],
817
+ **opts
818
+ )
819
+ when 403
820
+ PermissionError.new(error_data[:message], **opts)
821
+ when 429
822
+ RateLimitError.new(error_data[:message], **opts)
823
+ else
824
+ APIError.new(error_data[:message], **opts)
825
+ end
826
+ end
827
+
828
+ private def specific_v2_api_error(resp, error_data, context)
829
+ Util.log_error("Stripe v2 API error",
830
+ status: resp.http_status,
831
+ error_code: error_data[:code],
832
+ error_message: error_data[:message],
833
+ error_param: error_data[:param],
834
+ error_type: error_data[:type],
835
+ idempotency_key: context.idempotency_key,
836
+ request_id: context.request_id,
837
+ config: config)
838
+
839
+ # The standard set of arguments that can be used to initialize most of
840
+ # the exceptions.
841
+ opts = {
842
+ http_body: resp.http_body,
843
+ http_headers: resp.http_headers,
844
+ http_status: resp.http_status,
845
+ json_body: resp.data,
846
+ code: error_data[:code],
847
+ }
848
+
849
+ case error_data[:type]
850
+ when "idempotency_error"
851
+ IdempotencyError.new(error_data[:message], **opts)
852
+ # switch cases: The beginning of the section generated from our OpenAPI spec
853
+ when "temporary_session_expired"
854
+ TemporarySessionExpiredError.new(error_data[:message], **opts)
855
+ # switch cases: The end of the section generated from our OpenAPI spec
856
+ else
857
+ specific_api_error(resp, error_data, context)
858
+ end
859
+ end
860
+
861
+ # Attempts to look at a response's error code and return an OAuth error if
862
+ # one matches. Will return `nil` if the code isn't recognized.
863
+ private def specific_oauth_error(resp, error_code, context)
864
+ description = resp.data[:error_description] || error_code
865
+
866
+ Util.log_error("Stripe OAuth error",
867
+ status: resp.http_status,
868
+ error_code: error_code,
869
+ error_description: description,
870
+ idempotency_key: context.idempotency_key,
871
+ request_id: context.request_id,
872
+ config: config)
873
+
874
+ args = {
875
+ http_status: resp.http_status, http_body: resp.http_body,
876
+ json_body: resp.data, http_headers: resp.http_headers,
877
+ }
878
+
879
+ case error_code
880
+ when "invalid_client"
881
+ OAuth::InvalidClientError.new(error_code, description, **args)
882
+ when "invalid_grant"
883
+ OAuth::InvalidGrantError.new(error_code, description, **args)
884
+ when "invalid_request"
885
+ OAuth::InvalidRequestError.new(error_code, description, **args)
886
+ when "invalid_scope"
887
+ OAuth::InvalidScopeError.new(error_code, description, **args)
888
+ when "unsupported_grant_type"
889
+ OAuth::UnsupportedGrantTypeError.new(error_code, description, **args)
890
+ when "unsupported_response_type"
891
+ OAuth::UnsupportedResponseTypeError.new(error_code, description, **args)
892
+ else
893
+ # We'd prefer that all errors are typed, but we create a generic
894
+ # OAuthError in case we run into a code that we don't recognize.
895
+ OAuth::OAuthError.new(error_code, description, **args)
896
+ end
897
+ end
898
+
899
+ private def handle_network_error(error, context, num_retries,
900
+ base_url)
901
+ Util.log_error("Stripe network error",
902
+ error_message: error.message,
903
+ idempotency_key: context.idempotency_key,
904
+ request_id: context.request_id,
905
+ config: config)
906
+
907
+ errors, message = NETWORK_ERROR_MESSAGES_MAP.detect do |(e, _)|
908
+ error.is_a?(e)
909
+ end
910
+
911
+ if errors.nil?
912
+ message = "Unexpected error #{error.class.name} communicating " \
913
+ "with Stripe. Please let us know at support@stripe.com."
914
+ end
915
+
916
+ message = message % base_url
917
+
918
+ message += " Request was retried #{num_retries} times." if num_retries > 0
919
+
920
+ raise APIConnectionError,
921
+ message + "\n\n(Network error: #{error.message})"
922
+ end
923
+
924
+ private def request_headers(method, api_mode, req_opts)
925
+ user_agent = "Stripe/#{api_mode} RubyBindings/#{Stripe::VERSION}"
926
+ user_agent += " " + format_app_info(Stripe.app_info) unless Stripe.app_info.nil?
927
+
928
+ headers = {
929
+ "User-Agent" => user_agent,
930
+ "Authorization" => "Bearer #{req_opts[:api_key]}",
931
+ }
932
+
933
+ if config.enable_telemetry?
934
+ begin
935
+ headers["X-Stripe-Client-Telemetry"] = JSON.generate(
936
+ last_request_metrics: @last_request_metrics.pop(true)&.payload
937
+ )
938
+ rescue ThreadError
939
+ # last_request_metrics is best effort, ignore failures when queue
940
+ # is empty. should fail if this is the first request ever sent
941
+ # on a requestor
942
+ end
943
+ end
944
+
945
+ headers["Idempotency-Key"] = req_opts[:idempotency_key] if req_opts[:idempotency_key]
946
+ # It is only safe to retry network failures on post and delete
947
+ # requests if we add an Idempotency-Key header
948
+ if %i[post delete].include?(method) && (api_mode == :v2 || config.max_network_retries > 0)
949
+ headers["Idempotency-Key"] ||= SecureRandom.uuid
950
+ end
951
+
952
+ headers["Stripe-Version"] = req_opts[:stripe_version] if req_opts[:stripe_version]
953
+ headers["Stripe-Account"] = req_opts[:stripe_account] if req_opts[:stripe_account]
954
+ headers["Stripe-Context"] = req_opts[:stripe_context] if req_opts[:stripe_context]
955
+
956
+ user_agent = @system_profiler.user_agent
957
+ begin
958
+ headers.update(
959
+ "X-Stripe-Client-User-Agent" => JSON.generate(user_agent)
960
+ )
961
+ rescue StandardError => e
962
+ headers.update(
963
+ "X-Stripe-Client-Raw-User-Agent" => user_agent.inspect,
964
+ :error => "#{e} (#{e.class})"
965
+ )
966
+ end
967
+
968
+ headers.update(req_opts[:headers])
969
+ end
970
+
971
+ private def log_request(context, num_retries)
972
+ Util.log_info("Request to Stripe API",
973
+ account: context.account,
974
+ api_version: context.api_version,
975
+ idempotency_key: context.idempotency_key,
976
+ method: context.method,
977
+ num_retries: num_retries,
978
+ path: context.path,
979
+ config: config)
980
+ Util.log_debug("Request details",
981
+ body: context.body,
982
+ idempotency_key: context.idempotency_key,
983
+ query: context.query,
984
+ config: config,
985
+ process_id: Process.pid,
986
+ thread_object_id: Thread.current.object_id,
987
+ log_timestamp: Util.monotonic_time)
988
+ end
989
+
990
+ private def log_response(context, request_start, status, body, resp)
991
+ Util.log_info("Response from Stripe API",
992
+ account: context.account,
993
+ api_version: context.api_version,
994
+ elapsed: Util.monotonic_time - request_start,
995
+ idempotency_key: context.idempotency_key,
996
+ method: context.method,
997
+ path: context.path,
998
+ request_id: context.request_id,
999
+ status: status,
1000
+ config: config)
1001
+ Util.log_debug("Response details",
1002
+ body: body,
1003
+ idempotency_key: context.idempotency_key,
1004
+ request_id: context.request_id,
1005
+ config: config,
1006
+ process_id: Process.pid,
1007
+ thread_object_id: Thread.current.object_id,
1008
+ response_object_id: resp.object_id,
1009
+ log_timestamp: Util.monotonic_time)
1010
+
1011
+ return unless context.request_id
1012
+
1013
+ Util.log_debug("Dashboard link for request",
1014
+ idempotency_key: context.idempotency_key,
1015
+ request_id: context.request_id,
1016
+ url: Util.request_id_dashboard_url(context.request_id,
1017
+ context.api_key),
1018
+ config: config)
1019
+ end
1020
+
1021
+ private def log_response_error(context, request_start, error)
1022
+ elapsed = request_start ? Util.monotonic_time - request_start : nil
1023
+ Util.log_error("Request error",
1024
+ elapsed: elapsed,
1025
+ error_message: error.message,
1026
+ idempotency_key: context.idempotency_key,
1027
+ method: context.method,
1028
+ path: context.path,
1029
+ config: config)
1030
+ end
1031
+
1032
+ # RequestLogContext stores information about a request that's begin made so
1033
+ # that we can log certain information. It's useful because it means that we
1034
+ # don't have to pass around as many parameters.
1035
+ class RequestLogContext
1036
+ attr_accessor :body, :account, :api_key, :api_version, :idempotency_key, :method, :path, :query, :request_id
1037
+
1038
+ # The idea with this method is that we might want to update some of
1039
+ # context information because a response that we've received from the API
1040
+ # contains information that's more authoritative than what we started
1041
+ # with for a request. For example, we should trust whatever came back in
1042
+ # a `Stripe-Version` header beyond what configuration information that we
1043
+ # might have had available.
1044
+ def dup_from_response_headers(headers)
1045
+ context = dup
1046
+ context.account = headers["Stripe-Account"]
1047
+ context.api_version = headers["Stripe-Version"]
1048
+ context.idempotency_key = headers["Idempotency-Key"]
1049
+ context.request_id = headers["Request-Id"]
1050
+ context
1051
+ end
1052
+ end
1053
+
1054
+ # SystemProfiler extracts information about the system that we're running
1055
+ # in so that we can generate a rich user agent header to help debug
1056
+ # integrations.
1057
+ class SystemProfiler
1058
+ def self.uname
1059
+ if ::File.exist?("/proc/version")
1060
+ ::File.read("/proc/version").strip
1061
+ else
1062
+ case RbConfig::CONFIG["host_os"]
1063
+ when /linux|darwin|bsd|sunos|solaris|cygwin/i
1064
+ uname_from_system
1065
+ when /mswin|mingw/i
1066
+ uname_from_system_ver
1067
+ else
1068
+ "unknown platform"
1069
+ end
1070
+ end
1071
+ end
1072
+
1073
+ def self.uname_from_system
1074
+ (`uname -a 2>/dev/null` || "").strip
1075
+ rescue Errno::ENOENT
1076
+ "uname executable not found"
1077
+ rescue Errno::ENOMEM # couldn't create subprocess
1078
+ "uname lookup failed"
1079
+ end
1080
+
1081
+ def self.uname_from_system_ver
1082
+ (`ver` || "").strip
1083
+ rescue Errno::ENOENT
1084
+ "ver executable not found"
1085
+ rescue Errno::ENOMEM # couldn't create subprocess
1086
+ "uname lookup failed"
1087
+ end
1088
+
1089
+ def initialize
1090
+ @uname = self.class.uname
1091
+ end
1092
+
1093
+ def user_agent
1094
+ lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} " \
1095
+ "(#{RUBY_RELEASE_DATE})"
1096
+
1097
+ {
1098
+ application: Stripe.app_info,
1099
+ bindings_version: Stripe::VERSION,
1100
+ lang: "ruby",
1101
+ lang_version: lang_version,
1102
+ platform: RUBY_PLATFORM,
1103
+ engine: defined?(RUBY_ENGINE) ? RUBY_ENGINE : "",
1104
+ publisher: "stripe",
1105
+ uname: @uname,
1106
+ hostname: Socket.gethostname,
1107
+ }.delete_if { |_k, v| v.nil? }
1108
+ end
1109
+ end
1110
+
1111
+ # StripeRequestMetrics tracks metadata to be reported to stripe for metrics
1112
+ # collection
1113
+ class StripeRequestMetrics
1114
+ # The Stripe request ID of the response.
1115
+ attr_accessor :request_id
1116
+
1117
+ # Request duration in milliseconds
1118
+ attr_accessor :request_duration_ms
1119
+
1120
+ # list of names of tracked behaviors associated with this request
1121
+ attr_accessor :usage
1122
+
1123
+ def initialize(request_id, request_duration_ms, usage: [])
1124
+ self.request_id = request_id
1125
+ self.request_duration_ms = request_duration_ms
1126
+ self.usage = usage
1127
+ end
1128
+
1129
+ def payload
1130
+ ret = { request_id: request_id, request_duration_ms: request_duration_ms }
1131
+ ret[:usage] = usage if !usage.nil? && !usage.empty?
1132
+ ret
1133
+ end
1134
+ end
1135
+ end
1136
+ end