stripe 1.27.2 → 5.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +942 -0
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +29 -4
  5. data/History.txt +1 -361
  6. data/README.md +349 -0
  7. data/Rakefile +33 -3
  8. data/VERSION +1 -1
  9. data/bin/stripe-console +14 -5
  10. data/lib/data/ca-certificates.crt +4016 -5138
  11. data/lib/stripe.rb +102 -320
  12. data/lib/stripe/api_operations/create.rb +5 -9
  13. data/lib/stripe/api_operations/delete.rb +32 -4
  14. data/lib/stripe/api_operations/list.rb +11 -9
  15. data/lib/stripe/api_operations/nested_resource.rb +73 -0
  16. data/lib/stripe/api_operations/request.rb +66 -11
  17. data/lib/stripe/api_operations/save.rb +97 -0
  18. data/lib/stripe/api_resource.rb +96 -12
  19. data/lib/stripe/connection_manager.rb +164 -0
  20. data/lib/stripe/error_object.rb +94 -0
  21. data/lib/stripe/errors.rb +177 -0
  22. data/lib/stripe/instrumentation.rb +82 -0
  23. data/lib/stripe/list_object.rb +118 -13
  24. data/lib/stripe/multipart_encoder.rb +131 -0
  25. data/lib/stripe/oauth.rb +65 -0
  26. data/lib/stripe/object_types.rb +104 -0
  27. data/lib/stripe/resources.rb +87 -0
  28. data/lib/stripe/resources/account.rb +170 -0
  29. data/lib/stripe/resources/account_link.rb +10 -0
  30. data/lib/stripe/resources/alipay_account.rb +34 -0
  31. data/lib/stripe/resources/apple_pay_domain.rb +17 -0
  32. data/lib/stripe/resources/application_fee.rb +14 -0
  33. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  34. data/lib/stripe/resources/balance.rb +8 -0
  35. data/lib/stripe/resources/balance_transaction.rb +10 -0
  36. data/lib/stripe/resources/bank_account.rb +43 -0
  37. data/lib/stripe/resources/billing_portal/configuration.rb +14 -0
  38. data/lib/stripe/resources/billing_portal/session.rb +12 -0
  39. data/lib/stripe/resources/bitcoin_receiver.rb +24 -0
  40. data/lib/stripe/resources/bitcoin_transaction.rb +15 -0
  41. data/lib/stripe/resources/capability.rb +33 -0
  42. data/lib/stripe/resources/card.rb +38 -0
  43. data/lib/stripe/resources/charge.rb +23 -0
  44. data/lib/stripe/resources/checkout/session.rb +16 -0
  45. data/lib/stripe/resources/country_spec.rb +10 -0
  46. data/lib/stripe/resources/coupon.rb +13 -0
  47. data/lib/stripe/resources/credit_note.rb +33 -0
  48. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  49. data/lib/stripe/resources/customer.rb +41 -0
  50. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  51. data/lib/stripe/resources/discount.rb +7 -0
  52. data/lib/stripe/resources/dispute.rb +22 -0
  53. data/lib/stripe/resources/ephemeral_key.rb +20 -0
  54. data/lib/stripe/resources/event.rb +10 -0
  55. data/lib/stripe/resources/exchange_rate.rb +10 -0
  56. data/lib/stripe/resources/file.rb +36 -0
  57. data/lib/stripe/resources/file_link.rb +12 -0
  58. data/lib/stripe/resources/identity/verification_report.rb +12 -0
  59. data/lib/stripe/resources/identity/verification_session.rb +35 -0
  60. data/lib/stripe/resources/invoice.rb +74 -0
  61. data/lib/stripe/resources/invoice_item.rb +13 -0
  62. data/lib/stripe/resources/invoice_line_item.rb +7 -0
  63. data/lib/stripe/resources/issuing/authorization.rb +34 -0
  64. data/lib/stripe/resources/issuing/card.rb +25 -0
  65. data/lib/stripe/resources/issuing/card_details.rb +9 -0
  66. data/lib/stripe/resources/issuing/cardholder.rb +14 -0
  67. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  68. data/lib/stripe/resources/issuing/transaction.rb +13 -0
  69. data/lib/stripe/resources/line_item.rb +7 -0
  70. data/lib/stripe/resources/login_link.rb +14 -0
  71. data/lib/stripe/resources/mandate.rb +8 -0
  72. data/lib/stripe/resources/order.rb +33 -0
  73. data/lib/stripe/resources/order_return.rb +10 -0
  74. data/lib/stripe/resources/payment_intent.rb +43 -0
  75. data/lib/stripe/resources/payment_method.rb +33 -0
  76. data/lib/stripe/resources/payout.rb +33 -0
  77. data/lib/stripe/resources/person.rb +31 -0
  78. data/lib/stripe/resources/plan.rb +13 -0
  79. data/lib/stripe/resources/price.rb +12 -0
  80. data/lib/stripe/resources/product.rb +13 -0
  81. data/lib/stripe/resources/promotion_code.rb +12 -0
  82. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  83. data/lib/stripe/resources/radar/value_list.rb +15 -0
  84. data/lib/stripe/resources/radar/value_list_item.rb +14 -0
  85. data/lib/stripe/resources/recipient.rb +14 -0
  86. data/lib/stripe/resources/recipient_transfer.rb +7 -0
  87. data/lib/stripe/resources/refund.rb +12 -0
  88. data/lib/stripe/resources/reporting/report_run.rb +13 -0
  89. data/lib/stripe/resources/reporting/report_type.rb +13 -0
  90. data/lib/stripe/resources/reversal.rb +29 -0
  91. data/lib/stripe/resources/review.rb +21 -0
  92. data/lib/stripe/resources/setup_attempt.rb +10 -0
  93. data/lib/stripe/resources/setup_intent.rb +33 -0
  94. data/lib/stripe/resources/sigma/scheduled_query_run.rb +16 -0
  95. data/lib/stripe/resources/sku.rb +13 -0
  96. data/lib/stripe/resources/source.rb +47 -0
  97. data/lib/stripe/resources/source_transaction.rb +7 -0
  98. data/lib/stripe/resources/subscription.rb +26 -0
  99. data/lib/stripe/resources/subscription_item.rb +26 -0
  100. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  101. data/lib/stripe/resources/tax_id.rb +26 -0
  102. data/lib/stripe/resources/tax_rate.rb +12 -0
  103. data/lib/stripe/resources/terminal/connection_token.rb +12 -0
  104. data/lib/stripe/resources/terminal/location.rb +15 -0
  105. data/lib/stripe/resources/terminal/reader.rb +15 -0
  106. data/lib/stripe/resources/three_d_secure.rb +14 -0
  107. data/lib/stripe/resources/token.rb +10 -0
  108. data/lib/stripe/resources/topup.rb +23 -0
  109. data/lib/stripe/resources/transfer.rb +27 -0
  110. data/lib/stripe/resources/usage_record.rb +7 -0
  111. data/lib/stripe/resources/usage_record_summary.rb +7 -0
  112. data/lib/stripe/resources/webhook_endpoint.rb +13 -0
  113. data/lib/stripe/singleton_api_resource.rb +13 -7
  114. data/lib/stripe/stripe_client.rb +989 -0
  115. data/lib/stripe/stripe_configuration.rb +194 -0
  116. data/lib/stripe/stripe_object.rb +481 -148
  117. data/lib/stripe/stripe_response.rb +82 -0
  118. data/lib/stripe/util.rb +265 -70
  119. data/lib/stripe/version.rb +3 -1
  120. data/lib/stripe/webhook.rb +121 -0
  121. data/stripe.gemspec +35 -21
  122. metadata +118 -198
  123. data/.gitignore +0 -4
  124. data/.travis.yml +0 -22
  125. data/README.rdoc +0 -43
  126. data/gemfiles/default-with-activesupport.gemfile +0 -10
  127. data/gemfiles/json.gemfile +0 -12
  128. data/gemfiles/yajl.gemfile +0 -12
  129. data/lib/stripe/account.rb +0 -39
  130. data/lib/stripe/api_operations/update.rb +0 -19
  131. data/lib/stripe/application_fee.rb +0 -20
  132. data/lib/stripe/application_fee_refund.rb +0 -14
  133. data/lib/stripe/balance.rb +0 -4
  134. data/lib/stripe/balance_transaction.rb +0 -9
  135. data/lib/stripe/bank_account.rb +0 -19
  136. data/lib/stripe/bitcoin_receiver.rb +0 -20
  137. data/lib/stripe/bitcoin_transaction.rb +0 -9
  138. data/lib/stripe/card.rb +0 -21
  139. data/lib/stripe/charge.rb +0 -62
  140. data/lib/stripe/coupon.rb +0 -8
  141. data/lib/stripe/customer.rb +0 -75
  142. data/lib/stripe/dispute.rb +0 -16
  143. data/lib/stripe/errors/api_connection_error.rb +0 -4
  144. data/lib/stripe/errors/api_error.rb +0 -4
  145. data/lib/stripe/errors/authentication_error.rb +0 -4
  146. data/lib/stripe/errors/card_error.rb +0 -12
  147. data/lib/stripe/errors/invalid_request_error.rb +0 -11
  148. data/lib/stripe/errors/rate_limit_error.rb +0 -4
  149. data/lib/stripe/errors/stripe_error.rb +0 -26
  150. data/lib/stripe/event.rb +0 -5
  151. data/lib/stripe/file_upload.rb +0 -22
  152. data/lib/stripe/invoice.rb +0 -27
  153. data/lib/stripe/invoice_item.rb +0 -8
  154. data/lib/stripe/order.rb +0 -19
  155. data/lib/stripe/plan.rb +0 -8
  156. data/lib/stripe/product.rb +0 -16
  157. data/lib/stripe/recipient.rb +0 -12
  158. data/lib/stripe/refund.rb +0 -7
  159. data/lib/stripe/reversal.rb +0 -14
  160. data/lib/stripe/sku.rb +0 -8
  161. data/lib/stripe/subscription.rb +0 -25
  162. data/lib/stripe/token.rb +0 -5
  163. data/lib/stripe/transfer.rb +0 -17
  164. data/test/stripe/account_test.rb +0 -118
  165. data/test/stripe/api_resource_test.rb +0 -632
  166. data/test/stripe/application_fee_refund_test.rb +0 -47
  167. data/test/stripe/application_fee_test.rb +0 -22
  168. data/test/stripe/balance_test.rb +0 -11
  169. data/test/stripe/bitcoin_receiver_test.rb +0 -61
  170. data/test/stripe/bitcoin_transaction_test.rb +0 -29
  171. data/test/stripe/charge_refund_test.rb +0 -55
  172. data/test/stripe/charge_test.rb +0 -118
  173. data/test/stripe/coupon_test.rb +0 -20
  174. data/test/stripe/customer_card_test.rb +0 -63
  175. data/test/stripe/customer_test.rb +0 -88
  176. data/test/stripe/dispute_test.rb +0 -45
  177. data/test/stripe/file_upload_test.rb +0 -28
  178. data/test/stripe/invoice_test.rb +0 -40
  179. data/test/stripe/list_object_test.rb +0 -16
  180. data/test/stripe/metadata_test.rb +0 -129
  181. data/test/stripe/order_test.rb +0 -52
  182. data/test/stripe/product_test.rb +0 -41
  183. data/test/stripe/recipient_card_test.rb +0 -57
  184. data/test/stripe/refund_test.rb +0 -55
  185. data/test/stripe/reversal_test.rb +0 -47
  186. data/test/stripe/sku_test.rb +0 -24
  187. data/test/stripe/stripe_object_test.rb +0 -28
  188. data/test/stripe/subscription_test.rb +0 -72
  189. data/test/stripe/transfer_test.rb +0 -25
  190. data/test/stripe/util_test.rb +0 -34
  191. data/test/test_data.rb +0 -666
  192. data/test/test_helper.rb +0 -41
@@ -1,10 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  module APIOperations
3
5
  module Delete
4
- def delete(params={}, opts={})
5
- opts = Util.normalize_opts(opts)
6
- response, opts = request(:delete, url, params, opts)
7
- refresh_from(response, opts)
6
+ module ClassMethods
7
+ # Deletes an API resource
8
+ #
9
+ # Deletes the identified resource with the passed in parameters.
10
+ #
11
+ # ==== Attributes
12
+ #
13
+ # * +id+ - ID of the resource to delete.
14
+ # * +params+ - A hash of parameters to pass to the API
15
+ # * +opts+ - A Hash of additional options (separate from the params /
16
+ # object values) to be added to the request. E.g. to allow for an
17
+ # idempotency_key to be passed in the request headers, or for the
18
+ # api_key to be overwritten. See
19
+ # {APIOperations::Request.execute_resource_request}.
20
+ def delete(id, params = {}, opts = {})
21
+ resp, opts = execute_resource_request(:delete,
22
+ "#{resource_url}/#{id}",
23
+ params, opts)
24
+ Util.convert_to_stripe_object(resp.data, opts)
25
+ end
26
+ end
27
+
28
+ def delete(params = {}, opts = {})
29
+ resp, opts = execute_resource_request(:delete, resource_url,
30
+ params, opts)
31
+ initialize_from(resp.data, opts)
32
+ end
33
+
34
+ def self.included(base)
35
+ base.extend(ClassMethods)
8
36
  end
9
37
  end
10
38
  end
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  module APIOperations
3
5
  module List
4
- module ClassMethods
5
- def all(filters={}, opts={})
6
- opts = Util.normalize_opts(opts)
7
- response, opts = request(:get, url, filters, opts)
8
- Util.convert_to_stripe_object(response, opts)
9
- end
10
- end
6
+ def list(filters = {}, opts = {})
7
+ opts = Util.normalize_opts(opts)
8
+
9
+ resp, opts = execute_resource_request(:get, resource_url, filters, opts)
10
+ obj = ListObject.construct_from(resp.data, opts)
11
11
 
12
- def self.included(base)
13
- base.extend(ClassMethods)
12
+ # set filters so that we can fetch the same limit, expansions, and
13
+ # predicates when accessing the next and previous pages
14
+ obj.filters = filters.dup
15
+ obj
14
16
  end
15
17
  end
16
18
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ # Adds methods to help manipulate a subresource from its parent resource so
6
+ # that it's possible to do so from a static context (i.e. without a
7
+ # pre-existing collection of subresources on the parent).
8
+ #
9
+ # For example, a transfer gains the static methods for reversals so that the
10
+ # methods `.create_reversal`, `.retrieve_reversal`, `.update_reversal`,
11
+ # etc. all become available.
12
+ module NestedResource
13
+ def nested_resource_class_methods(resource, path: nil, operations: nil,
14
+ resource_plural: nil)
15
+ resource_plural ||= "#{resource}s"
16
+ path ||= resource_plural
17
+
18
+ raise ArgumentError, "operations array required" if operations.nil?
19
+
20
+ resource_url_method = :"#{resource}s_url"
21
+
22
+ define_singleton_method(resource_url_method) do |id, nested_id = nil|
23
+ url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(path)}"
24
+ url += "/#{CGI.escape(nested_id)}" unless nested_id.nil?
25
+ url
26
+ end
27
+
28
+ operations.each do |operation|
29
+ case operation
30
+ when :create
31
+ define_singleton_method(:"create_#{resource}") \
32
+ do |id, params = {}, opts = {}|
33
+ url = send(resource_url_method, id)
34
+ resp, opts = execute_resource_request(:post, url, params, opts)
35
+ Util.convert_to_stripe_object(resp.data, opts)
36
+ end
37
+ when :retrieve
38
+ define_singleton_method(:"retrieve_#{resource}") \
39
+ do |id, nested_id, opts = {}|
40
+ url = send(resource_url_method, id, nested_id)
41
+ resp, opts = execute_resource_request(:get, url, {}, opts)
42
+ Util.convert_to_stripe_object(resp.data, opts)
43
+ end
44
+ when :update
45
+ define_singleton_method(:"update_#{resource}") \
46
+ do |id, nested_id, params = {}, opts = {}|
47
+ url = send(resource_url_method, id, nested_id)
48
+ resp, opts = execute_resource_request(:post, url, params, opts)
49
+ Util.convert_to_stripe_object(resp.data, opts)
50
+ end
51
+ when :delete
52
+ define_singleton_method(:"delete_#{resource}") \
53
+ do |id, nested_id, params = {}, opts = {}|
54
+ url = send(resource_url_method, id, nested_id)
55
+ resp, opts = execute_resource_request(:delete, url, params,
56
+ opts)
57
+ Util.convert_to_stripe_object(resp.data, opts)
58
+ end
59
+ when :list
60
+ define_singleton_method(:"list_#{resource_plural}") \
61
+ do |id, params = {}, opts = {}|
62
+ url = send(resource_url_method, id)
63
+ resp, opts = execute_resource_request(:get, url, params, opts)
64
+ Util.convert_to_stripe_object(resp.data, opts)
65
+ end
66
+ else
67
+ raise ArgumentError, "Unknown operation: #{operation.inspect}"
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,28 +1,81 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  module APIOperations
3
5
  module Request
4
6
  module ClassMethods
5
- OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :stripe_account, :stripe_version]
7
+ def execute_resource_request(method, url,
8
+ params = {}, opts = {})
9
+ params ||= {}
10
+
11
+ error_on_invalid_params(params)
12
+ warn_on_opts_in_params(params)
6
13
 
7
- def request(method, url, params={}, opts={})
8
14
  opts = Util.normalize_opts(opts)
15
+ error_on_non_string_user_opts(opts)
16
+
17
+ opts[:client] ||= StripeClient.active_client
9
18
 
10
19
  headers = opts.clone
11
20
  api_key = headers.delete(:api_key)
12
21
  api_base = headers.delete(:api_base)
22
+ client = headers.delete(:client)
13
23
  # Assume all remaining opts must be headers
14
24
 
15
- response, opts[:api_key] = Stripe.request(method, url, api_key, params, headers, api_base)
25
+ resp, opts[:api_key] = client.execute_request(
26
+ method, url,
27
+ api_base: api_base, api_key: api_key,
28
+ headers: headers, params: params
29
+ )
16
30
 
17
31
  # Hash#select returns an array before 1.9
18
32
  opts_to_persist = {}
19
33
  opts.each do |k, v|
20
- if OPTS_KEYS_TO_PERSIST.include?(k)
21
- opts_to_persist[k] = v
22
- end
34
+ opts_to_persist[k] = v if Util::OPTS_PERSISTABLE.include?(k)
23
35
  end
24
36
 
25
- [response, opts_to_persist]
37
+ [resp, opts_to_persist]
38
+ end
39
+
40
+ # This method used to be called `request`, but it's such a short name
41
+ # that it eventually conflicted with the name of a field on an API
42
+ # resource (specifically, `Event#request`), so it was renamed to
43
+ # something more unique.
44
+ #
45
+ # The former name had been around for just about forever though, and
46
+ # although all internal uses have been renamed, I've left this alias in
47
+ # place for backwards compatibility. Consider removing it on the next
48
+ # major.
49
+ alias request execute_resource_request
50
+
51
+ private def error_on_non_string_user_opts(opts)
52
+ Util::OPTS_USER_SPECIFIED.each do |opt|
53
+ next unless opts.key?(opt)
54
+
55
+ val = opts[opt]
56
+ next if val.nil?
57
+ next if val.is_a?(String)
58
+
59
+ raise ArgumentError,
60
+ "request option '#{opt}' should be a string value " \
61
+ "(was a #{val.class})"
62
+ end
63
+ end
64
+
65
+ private def error_on_invalid_params(params)
66
+ return if params.nil? || params.is_a?(Hash)
67
+
68
+ raise ArgumentError,
69
+ "request params should be either a Hash or nil " \
70
+ "(was a #{params.class})"
71
+ end
72
+
73
+ private def warn_on_opts_in_params(params)
74
+ Util::OPTS_USER_SPECIFIED.each do |opt|
75
+ if params.key?(opt)
76
+ warn("WARNING: '#{opt}' should be in opts instead of params.")
77
+ end
78
+ end
26
79
  end
27
80
  end
28
81
 
@@ -30,12 +83,14 @@ module Stripe
30
83
  base.extend(ClassMethods)
31
84
  end
32
85
 
33
- protected
34
-
35
- def request(method, url, params={}, opts={})
86
+ protected def execute_resource_request(method, url,
87
+ params = {}, opts = {})
36
88
  opts = @opts.merge(Util.normalize_opts(opts))
37
- self.class.request(method, url, params, opts)
89
+ self.class.execute_resource_request(method, url, params, opts)
38
90
  end
91
+
92
+ # See notes on `alias` above.
93
+ alias request execute_resource_request
39
94
  end
40
95
  end
41
96
  end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ module Save
6
+ module ClassMethods
7
+ # Updates an API resource
8
+ #
9
+ # Updates the identified resource with the passed in parameters.
10
+ #
11
+ # ==== Attributes
12
+ #
13
+ # * +id+ - ID of the resource to update.
14
+ # * +params+ - A hash of parameters to pass to the API
15
+ # * +opts+ - A Hash of additional options (separate from the params /
16
+ # object values) to be added to the request. E.g. to allow for an
17
+ # idempotency_key to be passed in the request headers, or for the
18
+ # api_key to be overwritten. See
19
+ # {APIOperations::Request.execute_resource_request}.
20
+ def update(id, params = {}, opts = {})
21
+ params.each_key do |k|
22
+ if protected_fields.include?(k)
23
+ raise ArgumentError, "Cannot update protected field: #{k}"
24
+ end
25
+ end
26
+
27
+ resp, opts = execute_resource_request(:post, "#{resource_url}/#{id}",
28
+ params, opts)
29
+ Util.convert_to_stripe_object(resp.data, opts)
30
+ end
31
+ end
32
+
33
+ # Creates or updates an API resource.
34
+ #
35
+ # If the resource doesn't yet have an assigned ID and the resource is one
36
+ # that can be created, then the method attempts to create the resource.
37
+ # The resource is updated otherwise.
38
+ #
39
+ # ==== Attributes
40
+ #
41
+ # * +params+ - Overrides any parameters in the resource's serialized data
42
+ # and includes them in the create or update. If +:req_url:+ is included
43
+ # in the list, it overrides the update URL used for the create or
44
+ # update.
45
+ # * +opts+ - A Hash of additional options (separate from the params /
46
+ # object values) to be added to the request. E.g. to allow for an
47
+ # idempotency_key to be passed in the request headers, or for the
48
+ # api_key to be overwritten. See
49
+ # {APIOperations::Request.execute_resource_request}.
50
+ def save(params = {}, opts = {})
51
+ # We started unintentionally (sort of) allowing attributes sent to
52
+ # +save+ to override values used during the update. So as not to break
53
+ # the API, this makes that official here.
54
+ update_attributes(params)
55
+
56
+ # Now remove any parameters that look like object attributes.
57
+ params = params.reject { |k, _| respond_to?(k) }
58
+
59
+ values = serialize_params(self).merge(params)
60
+
61
+ # note that id gets removed here our call to #url above has already
62
+ # generated a uri for this object with an identifier baked in
63
+ values.delete(:id)
64
+
65
+ resp, opts = execute_resource_request(:post, save_url, values, opts)
66
+ initialize_from(resp.data, opts)
67
+ end
68
+
69
+ def self.included(base)
70
+ # Set `metadata` as additive so that when it's set directly we remember
71
+ # to clear keys that may have been previously set by sending empty
72
+ # values for them.
73
+ #
74
+ # It's possible that not every object with `Save` has `metadata`, but
75
+ # it's a close enough heuristic, and having this option set when there
76
+ # is no `metadata` field is not harmful.
77
+ base.additive_object_param(:metadata)
78
+
79
+ base.extend(ClassMethods)
80
+ end
81
+
82
+ private def save_url
83
+ # This switch essentially allows us "upsert"-like functionality. If the
84
+ # API resource doesn't have an ID set (suggesting that it's new) and
85
+ # its class responds to .create (which comes from
86
+ # Stripe::APIOperations::Create), then use the URL to create a new
87
+ # resource. Otherwise, generate a URL based on the object's identifier
88
+ # for a normal update.
89
+ if self[:id].nil? && self.class.respond_to?(:create)
90
+ self.class.resource_url
91
+ else
92
+ resource_url
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -1,35 +1,119 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Stripe
2
4
  class APIResource < StripeObject
3
5
  include Stripe::APIOperations::Request
4
6
 
7
+ # A flag that can be set a behavior that will cause this resource to be
8
+ # encoded and sent up along with an update of its parent resource. This is
9
+ # usually not desirable because resources are updated individually on their
10
+ # own endpoints, but there are certain cases, replacing a customer's source
11
+ # for example, where this is allowed.
12
+ attr_accessor :save_with_parent
13
+
5
14
  def self.class_name
6
- self.name.split('::')[-1]
15
+ name.split("::")[-1]
7
16
  end
8
17
 
9
- def self.url
18
+ def self.resource_url
10
19
  if self == APIResource
11
- raise NotImplementedError.new('APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)')
20
+ raise NotImplementedError,
21
+ "APIResource is an abstract class. You should perform actions " \
22
+ "on its subclasses (Charge, Customer, etc.)"
12
23
  end
13
- "/v1/#{CGI.escape(class_name.downcase)}s"
24
+ # Namespaces are separated in object names with periods (.) and in URLs
25
+ # with forward slashes (/), so replace the former with the latter.
26
+ "/v1/#{self::OBJECT_NAME.downcase.tr('.', '/')}s"
14
27
  end
15
28
 
16
- def url
17
- unless id = self['id']
18
- raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
29
+ # A metaprogramming call that specifies that a field of a resource can be
30
+ # its own type of API resource (say a nested card under an account for
31
+ # example), and if that resource is set, it should be transmitted to the
32
+ # API on a create or update. Doing so is not the default behavior because
33
+ # API resources should normally be persisted on their own RESTful
34
+ # endpoints.
35
+ def self.save_nested_resource(name)
36
+ define_method(:"#{name}=") do |value|
37
+ super(value)
38
+
39
+ # The parent setter will perform certain useful operations like
40
+ # converting to an APIResource if appropriate. Refresh our argument
41
+ # value to whatever it mutated to.
42
+ value = send(name)
43
+
44
+ # Note that the value may be subresource, but could also be a scalar
45
+ # (like a tokenized card's ID for example), so we check the type before
46
+ # setting #save_with_parent here.
47
+ value.save_with_parent = true if value.is_a?(APIResource)
48
+
49
+ value
19
50
  end
20
- "#{self.class.url}/#{CGI.escape(id)}"
51
+ end
52
+
53
+ # Adds a custom method to a resource class. This is used to add support for
54
+ # non-CRUDL API requests, e.g. capturing charges. custom_method takes the
55
+ # following parameters:
56
+ # - name: the name of the custom method to create (as a symbol)
57
+ # - http_verb: the HTTP verb for the API request (:get, :post, or :delete)
58
+ # - http_path: the path to append to the resource's URL. If not provided,
59
+ # the name is used as the path
60
+ #
61
+ # For example, this call:
62
+ # custom_method :capture, http_verb: post
63
+ # adds a `capture` class method to the resource class that, when called,
64
+ # will send a POST request to `/v1/<object_name>/capture`.
65
+ def self.custom_method(name, http_verb:, http_path: nil)
66
+ unless %i[get post delete].include?(http_verb)
67
+ raise ArgumentError,
68
+ "Invalid http_verb value: #{http_verb.inspect}. Should be one " \
69
+ "of :get, :post or :delete."
70
+ end
71
+ http_path ||= name.to_s
72
+ define_singleton_method(name) do |id, params = {}, opts = {}|
73
+ unless id.is_a?(String)
74
+ raise ArgumentError,
75
+ "id should be a string representing the ID of an API resource"
76
+ end
77
+
78
+ url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(http_path)}"
79
+ resp, opts = execute_resource_request(http_verb, url, params, opts)
80
+ Util.convert_to_stripe_object(resp.data, opts)
81
+ end
82
+ end
83
+
84
+ def resource_url
85
+ unless (id = self["id"])
86
+ raise InvalidRequestError.new(
87
+ "Could not determine which URL to request: #{self.class} instance " \
88
+ "has invalid ID: #{id.inspect}",
89
+ "id"
90
+ )
91
+ end
92
+ "#{self.class.resource_url}/#{CGI.escape(id)}"
21
93
  end
22
94
 
23
95
  def refresh
24
- response, opts = request(:get, url, @retrieve_params)
25
- refresh_from(response, opts)
96
+ resp, opts = execute_resource_request(:get, resource_url,
97
+ @retrieve_params)
98
+ initialize_from(resp.data, opts)
26
99
  end
27
100
 
28
- def self.retrieve(id, opts={})
101
+ def self.retrieve(id, opts = {})
29
102
  opts = Util.normalize_opts(opts)
30
- instance = self.new(id, opts)
103
+ instance = new(id, opts)
31
104
  instance.refresh
32
105
  instance
33
106
  end
107
+
108
+ protected def request_stripe_object(method:, path:, params:, opts: {})
109
+ resp, opts = execute_resource_request(method, path, params, opts)
110
+
111
+ # If we're getting back this thing, update; otherwise, instantiate.
112
+ if Util.object_name_matches_class?(resp.data[:object], self.class)
113
+ initialize_from(resp.data, opts)
114
+ else
115
+ Util.convert_to_stripe_object(resp.data, opts)
116
+ end
117
+ end
34
118
  end
35
119
  end