stripe 4.17.0 → 5.32.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -1
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +10 -14
  5. data/README.md +167 -63
  6. data/Rakefile +8 -7
  7. data/VERSION +1 -1
  8. data/lib/stripe/api_operations/create.rb +1 -1
  9. data/lib/stripe/api_operations/delete.rb +7 -3
  10. data/lib/stripe/api_operations/list.rb +1 -12
  11. data/lib/stripe/api_operations/nested_resource.rb +36 -27
  12. data/lib/stripe/api_operations/request.rb +47 -8
  13. data/lib/stripe/api_operations/save.rb +8 -7
  14. data/lib/stripe/api_resource.rb +30 -5
  15. data/lib/stripe/connection_manager.rb +164 -0
  16. data/lib/stripe/error_object.rb +94 -0
  17. data/lib/stripe/errors.rb +31 -10
  18. data/lib/stripe/instrumentation.rb +82 -0
  19. data/lib/stripe/list_object.rb +41 -7
  20. data/lib/stripe/multipart_encoder.rb +131 -0
  21. data/lib/stripe/oauth.rb +8 -6
  22. data/lib/stripe/object_types.rb +102 -0
  23. data/lib/stripe/{account.rb → resources/account.rb} +44 -33
  24. data/lib/stripe/{account_link.rb → resources/account_link.rb} +2 -1
  25. data/lib/stripe/resources/alipay_account.rb +34 -0
  26. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +2 -1
  27. data/lib/stripe/resources/application_fee.rb +14 -0
  28. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  29. data/lib/stripe/{balance.rb → resources/balance.rb} +2 -1
  30. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +2 -5
  31. data/lib/stripe/resources/bank_account.rb +43 -0
  32. data/lib/stripe/{issuing/dispute.rb → resources/billing_portal/configuration.rb} +4 -3
  33. data/lib/stripe/{checkout → resources/billing_portal}/session.rb +3 -2
  34. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +4 -3
  35. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  36. data/lib/stripe/{capability.rb → resources/capability.rb} +11 -4
  37. data/lib/stripe/{card.rb → resources/card.rb} +12 -3
  38. data/lib/stripe/resources/charge.rb +23 -0
  39. data/lib/stripe/resources/checkout/session.rb +16 -0
  40. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +2 -1
  41. data/lib/stripe/{coupon.rb → resources/coupon.rb} +2 -1
  42. data/lib/stripe/resources/credit_note.rb +33 -0
  43. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  44. data/lib/stripe/resources/customer.rb +41 -0
  45. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  46. data/lib/stripe/{discount.rb → resources/discount.rb} +1 -1
  47. data/lib/stripe/resources/dispute.rb +22 -0
  48. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +6 -2
  49. data/lib/stripe/{event.rb → resources/event.rb} +2 -1
  50. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +2 -1
  51. data/lib/stripe/{file.rb → resources/file.rb} +7 -12
  52. data/lib/stripe/{file_link.rb → resources/file_link.rb} +2 -1
  53. data/lib/stripe/resources/invoice.rb +74 -0
  54. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +2 -1
  55. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  56. data/lib/stripe/{issuing → resources/issuing}/authorization.rb +14 -5
  57. data/lib/stripe/{issuing → resources/issuing}/card.rb +8 -3
  58. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  59. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +2 -1
  60. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  61. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +2 -1
  62. data/lib/stripe/resources/line_item.rb +7 -0
  63. data/lib/stripe/resources/login_link.rb +14 -0
  64. data/lib/stripe/resources/mandate.rb +8 -0
  65. data/lib/stripe/resources/order.rb +33 -0
  66. data/lib/stripe/{order_return.rb → resources/order_return.rb} +2 -1
  67. data/lib/stripe/resources/payment_intent.rb +43 -0
  68. data/lib/stripe/{payment_method.rb → resources/payment_method.rb} +14 -5
  69. data/lib/stripe/resources/payout.rb +33 -0
  70. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  71. data/lib/stripe/{plan.rb → resources/plan.rb} +2 -1
  72. data/lib/stripe/resources/price.rb +12 -0
  73. data/lib/stripe/{product.rb → resources/product.rb} +2 -1
  74. data/lib/stripe/resources/promotion_code.rb +12 -0
  75. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  76. data/lib/stripe/{radar → resources/radar}/value_list.rb +2 -1
  77. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +2 -1
  78. data/lib/stripe/{recipient.rb → resources/recipient.rb} +2 -5
  79. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  80. data/lib/stripe/{refund.rb → resources/refund.rb} +2 -1
  81. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +2 -1
  82. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +2 -1
  83. data/lib/stripe/resources/reversal.rb +29 -0
  84. data/lib/stripe/{review.rb → resources/review.rb} +8 -3
  85. data/lib/stripe/resources/setup_attempt.rb +10 -0
  86. data/lib/stripe/resources/setup_intent.rb +33 -0
  87. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +2 -1
  88. data/lib/stripe/{sku.rb → resources/sku.rb} +2 -1
  89. data/lib/stripe/{source.rb → resources/source.rb} +22 -15
  90. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  91. data/lib/stripe/{subscription.rb → resources/subscription.rb} +12 -13
  92. data/lib/stripe/resources/subscription_item.rb +26 -0
  93. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  94. data/lib/stripe/resources/tax_id.rb +26 -0
  95. data/lib/stripe/{tax_rate.rb → resources/tax_rate.rb} +2 -1
  96. data/lib/stripe/{terminal → resources/terminal}/connection_token.rb +2 -1
  97. data/lib/stripe/{terminal → resources/terminal}/location.rb +2 -1
  98. data/lib/stripe/{terminal → resources/terminal}/reader.rb +2 -1
  99. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +2 -1
  100. data/lib/stripe/{token.rb → resources/token.rb} +2 -1
  101. data/lib/stripe/{topup.rb → resources/topup.rb} +8 -3
  102. data/lib/stripe/resources/transfer.rb +27 -0
  103. data/lib/stripe/resources/usage_record.rb +7 -0
  104. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  105. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +2 -1
  106. data/lib/stripe/resources.rb +85 -0
  107. data/lib/stripe/singleton_api_resource.rb +3 -1
  108. data/lib/stripe/stripe_client.rb +576 -254
  109. data/lib/stripe/stripe_configuration.rb +194 -0
  110. data/lib/stripe/stripe_object.rb +80 -61
  111. data/lib/stripe/stripe_response.rb +53 -21
  112. data/lib/stripe/util.rb +76 -121
  113. data/lib/stripe/version.rb +1 -1
  114. data/lib/stripe/webhook.rb +43 -10
  115. data/lib/stripe.rb +42 -203
  116. data/stripe.gemspec +23 -7
  117. metadata +107 -284
  118. data/.gitattributes +0 -4
  119. data/.github/ISSUE_TEMPLATE.md +0 -5
  120. data/.gitignore +0 -8
  121. data/.rubocop.yml +0 -32
  122. data/.rubocop_todo.yml +0 -50
  123. data/.travis.yml +0 -42
  124. data/lib/stripe/alipay_account.rb +0 -27
  125. data/lib/stripe/application_fee.rb +0 -23
  126. data/lib/stripe/application_fee_refund.rb +0 -22
  127. data/lib/stripe/bank_account.rb +0 -32
  128. data/lib/stripe/charge.rb +0 -86
  129. data/lib/stripe/credit_note.rb +0 -18
  130. data/lib/stripe/customer.rb +0 -95
  131. data/lib/stripe/dispute.rb +0 -23
  132. data/lib/stripe/invoice.rb +0 -48
  133. data/lib/stripe/issuer_fraud_record.rb +0 -9
  134. data/lib/stripe/login_link.rb +0 -11
  135. data/lib/stripe/order.rb +0 -34
  136. data/lib/stripe/payment_intent.rb +0 -30
  137. data/lib/stripe/payout.rb +0 -24
  138. data/lib/stripe/reversal.rb +0 -22
  139. data/lib/stripe/subscription_item.rb +0 -17
  140. data/lib/stripe/subscription_schedule.rb +0 -35
  141. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  142. data/lib/stripe/tax_id.rb +0 -22
  143. data/lib/stripe/transfer.rb +0 -25
  144. data/lib/stripe/usage_record.rb +0 -14
  145. data/test/api_stub_helpers.rb +0 -1
  146. data/test/openapi/README.md +0 -9
  147. data/test/stripe/account_link_test.rb +0 -18
  148. data/test/stripe/account_test.rb +0 -428
  149. data/test/stripe/alipay_account_test.rb +0 -37
  150. data/test/stripe/api_operations_test.rb +0 -81
  151. data/test/stripe/api_resource_test.rb +0 -526
  152. data/test/stripe/apple_pay_domain_test.rb +0 -46
  153. data/test/stripe/application_fee_refund_test.rb +0 -37
  154. data/test/stripe/application_fee_test.rb +0 -58
  155. data/test/stripe/balance_test.rb +0 -13
  156. data/test/stripe/bank_account_test.rb +0 -36
  157. data/test/stripe/capability_test.rb +0 -45
  158. data/test/stripe/charge_test.rb +0 -80
  159. data/test/stripe/checkout/session_test.rb +0 -41
  160. data/test/stripe/country_spec_test.rb +0 -20
  161. data/test/stripe/coupon_test.rb +0 -61
  162. data/test/stripe/credit_note_test.rb +0 -61
  163. data/test/stripe/customer_card_test.rb +0 -42
  164. data/test/stripe/customer_test.rb +0 -226
  165. data/test/stripe/dispute_test.rb +0 -51
  166. data/test/stripe/ephemeral_key_test.rb +0 -93
  167. data/test/stripe/errors_test.rb +0 -20
  168. data/test/stripe/exchange_rate_test.rb +0 -20
  169. data/test/stripe/file_link_test.rb +0 -41
  170. data/test/stripe/file_test.rb +0 -97
  171. data/test/stripe/file_upload_test.rb +0 -79
  172. data/test/stripe/invoice_item_test.rb +0 -66
  173. data/test/stripe/invoice_line_item_test.rb +0 -8
  174. data/test/stripe/invoice_test.rb +0 -213
  175. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  176. data/test/stripe/issuing/authorization_test.rb +0 -72
  177. data/test/stripe/issuing/card_test.rb +0 -62
  178. data/test/stripe/issuing/cardholder_test.rb +0 -53
  179. data/test/stripe/issuing/dispute_test.rb +0 -45
  180. data/test/stripe/issuing/transaction_test.rb +0 -48
  181. data/test/stripe/list_object_test.rb +0 -156
  182. data/test/stripe/login_link_test.rb +0 -37
  183. data/test/stripe/oauth_test.rb +0 -88
  184. data/test/stripe/order_return_test.rb +0 -21
  185. data/test/stripe/order_test.rb +0 -75
  186. data/test/stripe/payment_intent_test.rb +0 -107
  187. data/test/stripe/payment_method_test.rb +0 -84
  188. data/test/stripe/payout_test.rb +0 -57
  189. data/test/stripe/person_test.rb +0 -46
  190. data/test/stripe/plan_test.rb +0 -98
  191. data/test/stripe/product_test.rb +0 -59
  192. data/test/stripe/radar/value_list_item_test.rb +0 -48
  193. data/test/stripe/radar/value_list_test.rb +0 -61
  194. data/test/stripe/recipient_test.rb +0 -62
  195. data/test/stripe/refund_test.rb +0 -39
  196. data/test/stripe/reporting/report_run_test.rb +0 -33
  197. data/test/stripe/reporting/report_type_test.rb +0 -22
  198. data/test/stripe/reversal_test.rb +0 -43
  199. data/test/stripe/review_test.rb +0 -27
  200. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  201. data/test/stripe/sku_test.rb +0 -60
  202. data/test/stripe/source_test.rb +0 -99
  203. data/test/stripe/source_transaction_test.rb +0 -19
  204. data/test/stripe/stripe_client_test.rb +0 -842
  205. data/test/stripe/stripe_object_test.rb +0 -525
  206. data/test/stripe/stripe_response_test.rb +0 -49
  207. data/test/stripe/subscription_item_test.rb +0 -63
  208. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  209. data/test/stripe/subscription_schedule_test.rb +0 -116
  210. data/test/stripe/subscription_test.rb +0 -80
  211. data/test/stripe/tax_id_test.rb +0 -31
  212. data/test/stripe/tax_rate_test.rb +0 -43
  213. data/test/stripe/terminal/connection_token_test.rb +0 -16
  214. data/test/stripe/terminal/location_test.rb +0 -68
  215. data/test/stripe/terminal/reader_test.rb +0 -62
  216. data/test/stripe/three_d_secure_test.rb +0 -23
  217. data/test/stripe/topup_test.rb +0 -62
  218. data/test/stripe/transfer_test.rb +0 -88
  219. data/test/stripe/usage_record_summary_test.rb +0 -19
  220. data/test/stripe/usage_record_test.rb +0 -28
  221. data/test/stripe/util_test.rb +0 -402
  222. data/test/stripe/webhook_endpoint_test.rb +0 -59
  223. data/test/stripe/webhook_test.rb +0 -96
  224. data/test/stripe_mock.rb +0 -77
  225. data/test/stripe_test.rb +0 -63
  226. data/test/test_data.rb +0 -61
  227. data/test/test_helper.rb +0 -73
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require ::File.expand_path("../../test_helper", __FILE__)
4
-
5
- module Stripe
6
- class AlipayAccountTest < Test::Unit::TestCase
7
- context "#resource_url" do
8
- should "return a resource URL" do
9
- alipay_account = Stripe::AlipayAccount.construct_from(
10
- id: "aliacc_123",
11
- customer: "cus_123"
12
- )
13
- assert_equal "/v1/customers/cus_123/sources/aliacc_123",
14
- alipay_account.resource_url
15
- end
16
-
17
- should "raise without a customer" do
18
- alipay_account = Stripe::AlipayAccount.construct_from(id: "aliacc_123")
19
- assert_raises NotImplementedError do
20
- alipay_account.resource_url
21
- end
22
- end
23
- end
24
-
25
- should "raise on #retrieve" do
26
- assert_raises NotImplementedError do
27
- Stripe::AlipayAccount.retrieve("aliacc_123")
28
- end
29
- end
30
-
31
- should "raise on #update" do
32
- assert_raises NotImplementedError do
33
- Stripe::AlipayAccount.update("aliacc_123", {})
34
- end
35
- end
36
- end
37
- end
@@ -1,81 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- # frozen_string_literal: true
3
-
4
- require ::File.expand_path("../../test_helper", __FILE__)
5
-
6
- module Stripe
7
- class ApiOperationsTest < Test::Unit::TestCase
8
- class UpdateableResource < APIResource
9
- include Stripe::APIOperations::Save
10
-
11
- OBJECT_NAME = "updateableresource".freeze
12
-
13
- def self.protected_fields
14
- [:protected]
15
- end
16
- end
17
-
18
- context ".update" do
19
- should "post the correct parameters to the resource URL" do
20
- stub_request(:post, "#{Stripe.api_base}/v1/updateableresources/id")
21
- .with(body: { foo: "bar" })
22
- .to_return(body: JSON.generate(foo: "bar"))
23
- resource = UpdateableResource.update("id", foo: "bar")
24
- assert_equal("bar", resource.foo)
25
- end
26
-
27
- should "error on protected fields" do
28
- e = assert_raises do
29
- UpdateableResource.update("id", protected: "bar")
30
- end
31
- assert_equal "Cannot update protected field: protected", e.message
32
- end
33
- end
34
-
35
- context ".nested_resource_class_methods" do
36
- class MainResource < APIResource
37
- extend Stripe::APIOperations::NestedResource
38
- OBJECT_NAME = "mainresource".freeze
39
- nested_resource_class_methods :nested,
40
- operations: %i[create retrieve update delete list]
41
- end
42
-
43
- should "define a create method" do
44
- stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds")
45
- .with(body: { foo: "bar" })
46
- .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
47
- nested_resource = MainResource.create_nested("id", foo: "bar")
48
- assert_equal "bar", nested_resource.foo
49
- end
50
-
51
- should "define a retrieve method" do
52
- stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
53
- .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
54
- nested_resource = MainResource.retrieve_nested("id", "nested_id")
55
- assert_equal "bar", nested_resource.foo
56
- end
57
-
58
- should "define an update method" do
59
- stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
60
- .with(body: { foo: "baz" })
61
- .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "baz"))
62
- nested_resource = MainResource.update_nested("id", "nested_id", foo: "baz")
63
- assert_equal "baz", nested_resource.foo
64
- end
65
-
66
- should "define a delete method" do
67
- stub_request(:delete, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
68
- .to_return(body: JSON.generate(id: "nested_id", object: "nested", deleted: true))
69
- nested_resource = MainResource.delete_nested("id", "nested_id")
70
- assert_equal true, nested_resource.deleted
71
- end
72
-
73
- should "define a list method" do
74
- stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds")
75
- .to_return(body: JSON.generate(object: "list", data: []))
76
- nested_resources = MainResource.list_nesteds("id")
77
- assert nested_resources.data.is_a?(Array)
78
- end
79
- end
80
- end
81
- end
@@ -1,526 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- # frozen_string_literal: true
3
-
4
- require ::File.expand_path("../../test_helper", __FILE__)
5
-
6
- module Stripe
7
- class ApiResourceTest < Test::Unit::TestCase
8
- class NestedTestAPIResource < APIResource
9
- save_nested_resource :external_account
10
- end
11
-
12
- context ".save_nested_resource" do
13
- should "can have a scalar set" do
14
- r = NestedTestAPIResource.new("test_resource")
15
- r.external_account = "tok_123"
16
- assert_equal "tok_123", r.external_account
17
- end
18
-
19
- should "set a flag if given an object source" do
20
- r = NestedTestAPIResource.new("test_resource")
21
- r.external_account = {
22
- object: "card",
23
- }
24
- assert_equal true, r.external_account.save_with_parent
25
- end
26
- end
27
-
28
- should "creating a new APIResource should not fetch over the network" do
29
- Stripe::Customer.new("someid")
30
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
31
- end
32
-
33
- should "creating a new APIResource from a hash should not fetch over the network" do
34
- Stripe::Customer.construct_from(id: "somecustomer",
35
- card: { id: "somecard", object: "card" },
36
- object: "customer")
37
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
38
- end
39
-
40
- should "setting an attribute should not cause a network request" do
41
- c = Stripe::Customer.new("cus_123")
42
- c.card = { id: "somecard", object: "card" }
43
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
44
- assert_not_requested :post, %r{#{Stripe.api_base}/.*}
45
- end
46
-
47
- should "accessing id should not issue a fetch" do
48
- c = Stripe::Customer.new("cus_123")
49
- c.id
50
- assert_not_requested :get, %r{#{Stripe.api_base}/.*}
51
- end
52
-
53
- should "not specifying api credentials should raise an exception" do
54
- Stripe.api_key = nil
55
- assert_raises Stripe::AuthenticationError do
56
- Stripe::Customer.new("cus_123").refresh
57
- end
58
- end
59
-
60
- should "using a nil api key should raise an exception" do
61
- assert_raises TypeError do
62
- Stripe::Customer.list({}, nil)
63
- end
64
- assert_raises TypeError do
65
- Stripe::Customer.list({}, api_key: nil)
66
- end
67
- end
68
-
69
- should "specifying api credentials containing whitespace should raise an exception" do
70
- Stripe.api_key = "key "
71
- assert_raises Stripe::AuthenticationError do
72
- Stripe::Customer.new("cus_123").refresh
73
- end
74
- end
75
-
76
- should "send expand on fetch properly" do
77
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
78
- .with(query: { "expand" => ["customer"] })
79
- .to_return(body: JSON.generate(charge_fixture))
80
-
81
- Stripe::Charge.retrieve(id: "ch_123", expand: [:customer])
82
- end
83
-
84
- should "preserve expand across refreshes" do
85
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
86
- .with(query: { "expand" => ["customer"] })
87
- .to_return(body: JSON.generate(charge_fixture))
88
-
89
- ch = Stripe::Charge.retrieve(id: "ch_123", expand: [:customer])
90
- ch.refresh
91
- end
92
-
93
- should "send expand when fetching through ListObject" do
94
- stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
95
- .to_return(body: JSON.generate(customer_fixture))
96
-
97
- stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123/sources/cc_test_card")
98
- .with(query: { "expand" => ["customer"] })
99
- .to_return(body: JSON.generate(customer_fixture))
100
-
101
- customer = Stripe::Customer.retrieve("cus_123")
102
- customer.sources.retrieve(id: "cc_test_card", expand: [:customer])
103
- end
104
-
105
- context "when specifying per-object credentials" do
106
- context "with no global API key set" do
107
- should "use the per-object credential when creating" do
108
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
109
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
110
- .to_return(body: JSON.generate(charge_fixture))
111
-
112
- Stripe::Charge.create({ source: "tok_visa" },
113
- "sk_test_local")
114
- end
115
- end
116
-
117
- context "with a global API key set" do
118
- setup do
119
- Stripe.api_key = "global"
120
- end
121
-
122
- teardown do
123
- Stripe.api_key = nil
124
- end
125
-
126
- should "use the per-object credential when creating" do
127
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
128
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
129
- .to_return(body: JSON.generate(charge_fixture))
130
-
131
- Stripe::Charge.create({ source: "tok_visa" },
132
- "sk_test_local")
133
- end
134
-
135
- should "use the per-object credential when retrieving and making other calls" do
136
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
137
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
138
- .to_return(body: JSON.generate(charge_fixture))
139
- stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_123/refunds")
140
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
141
- .to_return(body: "{}")
142
-
143
- ch = Stripe::Charge.retrieve("ch_123", "sk_test_local")
144
- ch.refunds.create
145
- end
146
- end
147
- end
148
-
149
- context "with valid credentials" do
150
- should "urlencode values in GET params" do
151
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
152
- .with(query: { customer: "test customer" })
153
- .to_return(body: JSON.generate(data: [charge_fixture]))
154
- charges = Stripe::Charge.list(customer: "test customer").data
155
- assert charges.is_a? Array
156
- end
157
-
158
- should "construct URL properly with base query parameters" do
159
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
160
- .with(query: { customer: "cus_123" })
161
- .to_return(body: JSON.generate(data: [charge_fixture],
162
- url: "/v1/charges"))
163
- charges = Stripe::Charge.list(customer: "cus_123")
164
-
165
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
166
- .with(query: { customer: "cus_123", created: "123" })
167
- .to_return(body: JSON.generate(data: [charge_fixture],
168
- url: "/v1/charges"))
169
- charges.list(created: 123)
170
- end
171
-
172
- should "setting a nil value for a param should exclude that param from the request" do
173
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
174
- .with(query: { offset: 5, sad: false })
175
- .to_return(body: JSON.generate(count: 1, data: [charge_fixture]))
176
- Stripe::Charge.list(count: nil, offset: 5, sad: false)
177
-
178
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
179
- .with(body: { "amount" => "50", "currency" => "usd" })
180
- .to_return(body: JSON.generate(count: 1, data: [charge_fixture]))
181
- Stripe::Charge.create(amount: 50, currency: "usd", card: { number: nil })
182
- end
183
-
184
- should "not trigger a warning if a known opt, such as idempotency_key, is in opts" do
185
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
186
- .to_return(body: JSON.generate(charge_fixture))
187
- old_stderr = $stderr
188
- $stderr = StringIO.new
189
- begin
190
- Stripe::Charge.create({ amount: 100, currency: "usd", card: "sc_token" }, idempotency_key: "12345")
191
- assert $stderr.string.empty?
192
- ensure
193
- $stderr = old_stderr
194
- end
195
- end
196
-
197
- should "trigger a warning if a known opt, such as idempotency_key, is in params" do
198
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
199
- .to_return(body: JSON.generate(charge_fixture))
200
- old_stderr = $stderr
201
- $stderr = StringIO.new
202
- begin
203
- Stripe::Charge.create(amount: 100, currency: "usd", card: "sc_token", idempotency_key: "12345")
204
- assert_match Regexp.new("WARNING:"), $stderr.string
205
- ensure
206
- $stderr = old_stderr
207
- end
208
- end
209
-
210
- should "requesting with a unicode ID should result in a request" do
211
- stub_request(:get, "#{Stripe.api_base}/v1/customers/%E2%98%83")
212
- .to_return(body: JSON.generate(make_missing_id_error), status: 404)
213
- c = Stripe::Customer.new("☃")
214
- assert_raises(Stripe::InvalidRequestError) { c.refresh }
215
- end
216
-
217
- should "requesting with no ID should result in an InvalidRequestError with no request" do
218
- c = Stripe::Customer.new
219
- assert_raises(Stripe::InvalidRequestError) { c.refresh }
220
- end
221
-
222
- should "making a GET request with parameters should have a query string and no body" do
223
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
224
- .with(query: { limit: 1 })
225
- .to_return(body: JSON.generate(data: [charge_fixture]))
226
- Stripe::Charge.list(limit: 1)
227
- end
228
-
229
- should "making a POST request with parameters should have a body and no query string" do
230
- stub_request(:post, "#{Stripe.api_base}/v1/charges")
231
- .with(body: { "amount" => "100", "currency" => "usd", "card" => "sc_token" })
232
- .to_return(body: JSON.generate(charge_fixture))
233
- Stripe::Charge.create(amount: 100, currency: "usd", card: "sc_token")
234
- end
235
-
236
- should "loading an object should issue a GET request" do
237
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
238
- .to_return(body: JSON.generate(charge_fixture))
239
- c = Stripe::Charge.new("ch_123")
240
- c.refresh
241
- end
242
-
243
- should "using array accessors should be the same as the method interface" do
244
- stub_request(:get, "#{Stripe.api_base}/v1/charges/ch_123")
245
- .to_return(body: JSON.generate(charge_fixture))
246
- c = Stripe::Charge.new("cus_123")
247
- c.refresh
248
- assert_equal c.created, c[:created]
249
- assert_equal c.created, c["created"]
250
- c["created"] = 12_345
251
- assert_equal c.created, 12_345
252
- end
253
-
254
- should "accessing a property other than id or parent on an unfetched object should fetch it" do
255
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
256
- .with(query: { customer: "cus_123" })
257
- .to_return(body: JSON.generate(customer_fixture))
258
- c = Stripe::Customer.new("cus_123")
259
- c.charges
260
- end
261
-
262
- should "updating an object should issue a POST request with only the changed properties" do
263
- stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
264
- .with(body: { "description" => "another_mn" })
265
- .to_return(body: JSON.generate(customer_fixture))
266
- c = Stripe::Customer.construct_from(customer_fixture)
267
- c.description = "another_mn"
268
- c.save
269
- end
270
-
271
- should "updating should merge in returned properties" do
272
- stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
273
- .with(body: { "description" => "another_mn" })
274
- .to_return(body: JSON.generate(customer_fixture))
275
- c = Stripe::Customer.new("cus_123")
276
- c.description = "another_mn"
277
- c.save
278
- assert_equal false, c.livemode
279
- end
280
-
281
- should "updating should fail if api_key is overwritten with nil" do
282
- c = Stripe::Customer.new
283
- assert_raises TypeError do
284
- c.save({}, api_key: nil)
285
- end
286
- end
287
-
288
- should "updating should use the supplied api_key" do
289
- stub_request(:post, "#{Stripe.api_base}/v1/customers")
290
- .with(headers: { "Authorization" => "Bearer sk_test_local" })
291
- .to_return(body: JSON.generate(customer_fixture))
292
- c = Stripe::Customer.new
293
- c.save({}, api_key: "sk_test_local")
294
- assert_equal false, c.livemode
295
- end
296
-
297
- should "deleting should send no props and result in an object that has no props other deleted" do
298
- stub_request(:delete, "#{Stripe.api_base}/v1/customers/cus_123")
299
- .to_return(body: JSON.generate("id" => "cus_123", "deleted" => true))
300
- c = Stripe::Customer.construct_from(customer_fixture)
301
- c.delete
302
- end
303
-
304
- should "loading all of an APIResource should return an array of recursively instantiated objects" do
305
- stub_request(:get, "#{Stripe.api_base}/v1/charges")
306
- .to_return(body: JSON.generate(data: [charge_fixture]))
307
- charges = Stripe::Charge.list.data
308
- assert charges.is_a? Array
309
- assert charges[0].is_a? Stripe::Charge
310
- assert charges[0].source.is_a?(Stripe::StripeObject)
311
- end
312
-
313
- should "passing in a stripe_account header should pass it through on call" do
314
- stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
315
- .with(headers: { "Stripe-Account" => "acct_123" })
316
- .to_return(body: JSON.generate(customer_fixture))
317
- Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
318
- end
319
-
320
- should "passing in a stripe_account header should pass it through on save" do
321
- stub_request(:get, "#{Stripe.api_base}/v1/customers/cus_123")
322
- .with(headers: { "Stripe-Account" => "acct_123" })
323
- .to_return(body: JSON.generate(customer_fixture))
324
- c = Stripe::Customer.retrieve("cus_123", stripe_account: "acct_123")
325
-
326
- stub_request(:post, "#{Stripe.api_base}/v1/customers/cus_123")
327
- .with(headers: { "Stripe-Account" => "acct_123" })
328
- .to_return(body: JSON.generate(customer_fixture))
329
- c.description = "FOO"
330
- c.save
331
- end
332
-
333
- should "add key to nested objects" do
334
- acct = Stripe::Account.construct_from(id: "myid",
335
- legal_entity: {
336
- size: "l",
337
- score: 4,
338
- height: 10,
339
- })
340
-
341
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
342
- .with(body: { legal_entity: { first_name: "Bob" } })
343
- .to_return(body: JSON.generate("id" => "myid"))
344
-
345
- acct.legal_entity.first_name = "Bob"
346
- acct.save
347
- end
348
-
349
- should "save nothing if nothing changes" do
350
- acct = Stripe::Account.construct_from(id: "acct_id",
351
- metadata: {
352
- key: "value",
353
- })
354
-
355
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_id")
356
- .with(body: {})
357
- .to_return(body: JSON.generate("id" => "acct_id"))
358
-
359
- acct.save
360
- end
361
-
362
- should "not save nested API resources" do
363
- ch = Stripe::Charge.construct_from(id: "ch_id",
364
- customer: {
365
- object: "customer",
366
- id: "customer_id",
367
- })
368
-
369
- stub_request(:post, "#{Stripe.api_base}/v1/charges/ch_id")
370
- .with(body: {})
371
- .to_return(body: JSON.generate("id" => "ch_id"))
372
-
373
- ch.customer.description = "Bob"
374
- ch.save
375
- end
376
-
377
- should "correctly handle replaced nested objects" do
378
- acct = Stripe::Account.construct_from(
379
- id: "acct_123",
380
- company: {
381
- name: "company_name",
382
- address: {
383
- line1: "test",
384
- city: "San Francisco",
385
- },
386
- }
387
- )
388
-
389
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/acct_123")
390
- .with(body: { company: { address: { line1: "Test2", city: "" } } })
391
- .to_return(body: JSON.generate("id" => "my_id"))
392
-
393
- acct.company.address = { line1: "Test2" }
394
- acct.save
395
- end
396
-
397
- should "correctly handle array setting" do
398
- acct = Stripe::Account.construct_from(id: "myid",
399
- legal_entity: {})
400
-
401
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
402
- .with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
403
- .to_return(body: JSON.generate("id" => "myid"))
404
-
405
- acct.legal_entity.additional_owners = [{ first_name: "Bob" }]
406
- acct.save
407
- end
408
-
409
- should "correctly handle array insertion" do
410
- acct = Stripe::Account.construct_from(id: "myid",
411
- legal_entity: {
412
- additional_owners: [],
413
- })
414
-
415
- # Note that this isn't a perfect check because we're using webmock's
416
- # data decoding, which isn't aware of the Stripe array encoding that we
417
- # use here.
418
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
419
- .with(body: { legal_entity: { additional_owners: [{ first_name: "Bob" }] } })
420
- .to_return(body: JSON.generate("id" => "myid"))
421
-
422
- acct.legal_entity.additional_owners << { first_name: "Bob" }
423
- acct.save
424
- end
425
-
426
- should "correctly handle array updates" do
427
- acct = Stripe::Account.construct_from(id: "myid",
428
- legal_entity: {
429
- additional_owners: [{ first_name: "Bob" }, { first_name: "Jane" }],
430
- })
431
-
432
- # Note that this isn't a perfect check because we're using webmock's
433
- # data decoding, which isn't aware of the Stripe array encoding that we
434
- # use here.
435
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
436
- .with(body: { legal_entity: { additional_owners: [{ first_name: "Janet" }] } })
437
- .to_return(body: JSON.generate("id" => "myid"))
438
-
439
- acct.legal_entity.additional_owners[1].first_name = "Janet"
440
- acct.save
441
- end
442
-
443
- should "correctly handle array noops" do
444
- acct = Stripe::Account.construct_from(id: "myid",
445
- legal_entity: {
446
- additional_owners: [{ first_name: "Bob" }],
447
- },
448
- currencies_supported: %w[usd cad])
449
-
450
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
451
- .with(body: {})
452
- .to_return(body: JSON.generate("id" => "myid"))
453
-
454
- acct.save
455
- end
456
-
457
- should "correctly handle hash noops" do
458
- acct = Stripe::Account.construct_from(id: "myid",
459
- legal_entity: {
460
- address: { line1: "1 Two Three" },
461
- })
462
-
463
- stub_request(:post, "#{Stripe.api_base}/v1/accounts/myid")
464
- .with(body: {})
465
- .to_return(body: JSON.generate("id" => "myid"))
466
-
467
- acct.save
468
- end
469
-
470
- should "should create a new resource when an object without an id is saved" do
471
- account = Stripe::Account.construct_from(id: nil,
472
- display_name: nil)
473
-
474
- stub_request(:post, "#{Stripe.api_base}/v1/accounts")
475
- .with(body: { display_name: "stripe" })
476
- .to_return(body: JSON.generate("id" => "acct_123"))
477
-
478
- account.display_name = "stripe"
479
- account.save
480
- end
481
-
482
- should "set attributes as part of save" do
483
- account = Stripe::Account.construct_from(id: nil,
484
- display_name: nil)
485
-
486
- stub_request(:post, "#{Stripe.api_base}/v1/accounts")
487
- .with(body: { display_name: "stripe", metadata: { key: "value" } })
488
- .to_return(body: JSON.generate("id" => "acct_123"))
489
-
490
- account.save(display_name: "stripe", metadata: { key: "value" })
491
- end
492
- end
493
-
494
- @@fixtures = {}
495
- setup do
496
- if @@fixtures.empty?
497
- cache_fixture(:charge) do
498
- Charge.retrieve("ch_123")
499
- end
500
- cache_fixture(:customer) do
501
- Customer.retrieve("cus_123")
502
- end
503
- end
504
- end
505
-
506
- private
507
-
508
- def charge_fixture
509
- @@fixtures[:charge]
510
- end
511
-
512
- def customer_fixture
513
- @@fixtures[:customer]
514
- end
515
-
516
- # Expects to retrieve a fixture from stripe-mock (an API call should be
517
- # included in the block to yield to) and does very simple memoization.
518
- def cache_fixture(key)
519
- return @@fixtures[key] if @@fixtures.key?(key)
520
-
521
- obj = yield
522
- @@fixtures[key] = obj.instance_variable_get(:@values).freeze
523
- @@fixtures[key]
524
- end
525
- end
526
- end