stripe 1.30.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +4 -0
  3. data/.github/ISSUE_TEMPLATE.md +5 -0
  4. data/.travis.yml +3 -14
  5. data/Gemfile +28 -4
  6. data/History.txt +180 -0
  7. data/README.md +147 -0
  8. data/Rakefile +10 -0
  9. data/VERSION +1 -1
  10. data/bin/stripe-console +12 -5
  11. data/lib/data/ca-certificates.crt +3868 -5114
  12. data/lib/stripe/account.rb +43 -23
  13. data/lib/stripe/alipay_account.rb +20 -0
  14. data/lib/stripe/api_operations/create.rb +2 -2
  15. data/lib/stripe/api_operations/delete.rb +2 -2
  16. data/lib/stripe/api_operations/list.rb +2 -3
  17. data/lib/stripe/api_operations/request.rb +9 -3
  18. data/lib/stripe/api_operations/save.rb +85 -0
  19. data/lib/stripe/api_resource.rb +38 -5
  20. data/lib/stripe/apple_pay_domain.rb +12 -0
  21. data/lib/stripe/application_fee.rb +8 -8
  22. data/lib/stripe/application_fee_refund.rb +7 -3
  23. data/lib/stripe/balance_transaction.rb +1 -1
  24. data/lib/stripe/bank_account.rb +13 -4
  25. data/lib/stripe/bitcoin_receiver.rb +6 -6
  26. data/lib/stripe/bitcoin_transaction.rb +1 -1
  27. data/lib/stripe/card.rb +9 -5
  28. data/lib/stripe/charge.rb +38 -20
  29. data/lib/stripe/country_spec.rb +9 -0
  30. data/lib/stripe/coupon.rb +1 -1
  31. data/lib/stripe/customer.rb +12 -10
  32. data/lib/stripe/dispute.rb +4 -5
  33. data/lib/stripe/errors.rb +92 -0
  34. data/lib/stripe/file_upload.rb +1 -1
  35. data/lib/stripe/invoice.rb +7 -7
  36. data/lib/stripe/invoice_item.rb +1 -1
  37. data/lib/stripe/list_object.rb +8 -7
  38. data/lib/stripe/order.rb +12 -4
  39. data/lib/stripe/order_return.rb +9 -0
  40. data/lib/stripe/plan.rb +1 -1
  41. data/lib/stripe/product.rb +2 -10
  42. data/lib/stripe/recipient.rb +1 -1
  43. data/lib/stripe/refund.rb +1 -1
  44. data/lib/stripe/reversal.rb +7 -3
  45. data/lib/stripe/singleton_api_resource.rb +3 -3
  46. data/lib/stripe/sku.rb +2 -2
  47. data/lib/stripe/source.rb +11 -0
  48. data/lib/stripe/stripe_client.rb +396 -0
  49. data/lib/stripe/stripe_object.rb +167 -91
  50. data/lib/stripe/stripe_response.rb +48 -0
  51. data/lib/stripe/subscription.rb +15 -9
  52. data/lib/stripe/subscription_item.rb +12 -0
  53. data/lib/stripe/three_d_secure.rb +9 -0
  54. data/lib/stripe/transfer.rb +4 -5
  55. data/lib/stripe/util.rb +105 -33
  56. data/lib/stripe/version.rb +1 -1
  57. data/lib/stripe.rb +69 -266
  58. data/spec/fixtures.json +1409 -0
  59. data/spec/fixtures.yaml +1153 -0
  60. data/spec/spec.json +19949 -0
  61. data/spec/spec.yaml +15504 -0
  62. data/stripe.gemspec +5 -18
  63. data/test/api_fixtures.rb +29 -0
  64. data/test/api_stub_helpers.rb +125 -0
  65. data/test/stripe/account_test.rb +163 -211
  66. data/test/stripe/alipay_account_test.rb +19 -0
  67. data/test/stripe/api_operations_test.rb +31 -0
  68. data/test/stripe/api_resource_test.rb +174 -340
  69. data/test/stripe/apple_pay_domain_test.rb +33 -0
  70. data/test/stripe/application_fee_refund_test.rb +22 -31
  71. data/test/stripe/application_fee_test.rb +6 -14
  72. data/test/stripe/balance_test.rb +3 -3
  73. data/test/stripe/bank_account_test.rb +41 -0
  74. data/test/stripe/bitcoin_receiver_test.rb +51 -42
  75. data/test/stripe/bitcoin_transaction_test.rb +11 -19
  76. data/test/stripe/charge_test.rb +39 -98
  77. data/test/stripe/country_spec_test.rb +20 -0
  78. data/test/stripe/coupon_test.rb +35 -11
  79. data/test/stripe/customer_card_test.rb +25 -46
  80. data/test/stripe/customer_test.rb +89 -61
  81. data/test/stripe/dispute_test.rb +28 -31
  82. data/test/stripe/errors_test.rb +18 -0
  83. data/test/stripe/file_upload_test.rb +32 -24
  84. data/test/stripe/invoice_item_test.rb +55 -0
  85. data/test/stripe/invoice_test.rb +50 -24
  86. data/test/stripe/list_object_test.rb +57 -45
  87. data/test/stripe/order_return_test.rb +21 -0
  88. data/test/stripe/order_test.rb +41 -34
  89. data/test/stripe/plan_test.rb +52 -0
  90. data/test/stripe/product_test.rb +31 -25
  91. data/test/stripe/recipient_card_test.rb +23 -40
  92. data/test/stripe/recipient_test.rb +50 -0
  93. data/test/stripe/refund_test.rb +20 -36
  94. data/test/stripe/reversal_test.rb +27 -31
  95. data/test/stripe/sku_test.rb +39 -13
  96. data/test/stripe/source_test.rb +43 -0
  97. data/test/stripe/stripe_client_test.rb +428 -0
  98. data/test/stripe/stripe_object_test.rb +186 -13
  99. data/test/stripe/stripe_response_test.rb +46 -0
  100. data/test/stripe/subscription_item_test.rb +54 -0
  101. data/test/stripe/subscription_test.rb +40 -52
  102. data/test/stripe/three_d_secure_test.rb +23 -0
  103. data/test/stripe/transfer_test.rb +38 -13
  104. data/test/stripe/util_test.rb +48 -16
  105. data/test/stripe_test.rb +25 -0
  106. data/test/test_data.rb +5 -621
  107. data/test/test_helper.rb +24 -24
  108. metadata +60 -139
  109. data/README.rdoc +0 -68
  110. data/gemfiles/default-with-activesupport.gemfile +0 -10
  111. data/gemfiles/json.gemfile +0 -12
  112. data/gemfiles/yajl.gemfile +0 -12
  113. data/lib/stripe/api_operations/update.rb +0 -58
  114. data/lib/stripe/errors/api_connection_error.rb +0 -4
  115. data/lib/stripe/errors/api_error.rb +0 -4
  116. data/lib/stripe/errors/authentication_error.rb +0 -4
  117. data/lib/stripe/errors/card_error.rb +0 -12
  118. data/lib/stripe/errors/invalid_request_error.rb +0 -11
  119. data/lib/stripe/errors/rate_limit_error.rb +0 -4
  120. data/lib/stripe/errors/stripe_error.rb +0 -26
  121. data/test/stripe/charge_refund_test.rb +0 -55
  122. data/test/stripe/metadata_test.rb +0 -129
@@ -1,11 +1,18 @@
1
1
  module Stripe
2
2
  class Account < APIResource
3
+ extend Gem::Deprecate
3
4
  extend Stripe::APIOperations::Create
4
- include Stripe::APIOperations::Delete
5
5
  extend Stripe::APIOperations::List
6
- include Stripe::APIOperations::Update
6
+ include Stripe::APIOperations::Delete
7
+ include Stripe::APIOperations::Save
8
+
9
+ save_nested_resource :external_account
7
10
 
8
- def url
11
+ # This method is deprecated. Please use `#external_account=` instead.
12
+ save_nested_resource :bank_account
13
+ deprecate :bank_account=, "#external_account=", 2017, 8
14
+
15
+ def resource_url
9
16
  if self['id']
10
17
  super
11
18
  else
@@ -25,10 +32,15 @@ module Stripe
25
32
  opts = id
26
33
  id = nil
27
34
  end
28
-
29
35
  super(id, opts)
30
36
  end
31
37
 
38
+ def reject(params={}, opts={})
39
+ opts = Util.normalize_opts(opts)
40
+ resp, opts = request(:post, resource_url + '/reject', params, opts)
41
+ initialize_from(resp.data, opts)
42
+ end
43
+
32
44
  # Somewhat unfortunately, we attempt to do a special encoding trick when
33
45
  # serializing `additional_owners` under an account: when updating a value,
34
46
  # we actually send the update parameters up as an integer-indexed hash
@@ -52,21 +64,22 @@ module Stripe
52
64
  #
53
65
  # We're trying to get this overturned on the server side, but for now,
54
66
  # patch in a special allowance.
55
- def self.serialize_params(obj, original_value=nil)
56
- update_hash = StripeObject.serialize_params(obj, original_value)
57
- case obj
58
- when StripeObject
59
- obj_values = obj.instance_variable_get(:@values)
60
- obj_values.each do |k, v|
61
- if k == :additional_owners && v.is_a?(Array)
62
- update_hash[k] = serialize_additional_owners(obj, v)
63
- end
67
+ def serialize_params(options = {})
68
+ serialize_params_account(self, super)
69
+ end
70
+
71
+ def serialize_params_account(obj, update_hash)
72
+ if entity = @values[:legal_entity]
73
+ if owners = entity[:additional_owners]
74
+ entity_update = update_hash[:legal_entity] ||= {}
75
+ entity_update[:additional_owners] =
76
+ serialize_additional_owners(entity, owners)
64
77
  end
65
78
  end
66
79
  update_hash
67
80
  end
68
81
 
69
- def protected_fields
82
+ def self.protected_fields
70
83
  [:legal_entity]
71
84
  end
72
85
 
@@ -80,18 +93,18 @@ module Stripe
80
93
 
81
94
  def deauthorize(client_id, opts={})
82
95
  opts = {:api_base => Stripe.connect_base}.merge(Util.normalize_opts(opts))
83
- response, opts = request(:post, '/oauth/deauthorize', { 'client_id' => client_id, 'stripe_user_id' => self.id }, opts)
96
+ resp, opts = request(:post, '/oauth/deauthorize', { 'client_id' => client_id, 'stripe_user_id' => self.id }, opts)
84
97
  opts.delete(:api_base) # the api_base here is a one-off, don't persist it
85
- Util.convert_to_stripe_object(response, opts)
98
+ Util.convert_to_stripe_object(resp.data, opts)
86
99
  end
87
100
 
88
101
  ARGUMENT_NOT_PROVIDED = Object.new
89
102
 
90
103
  private
91
104
 
92
- def self.serialize_additional_owners(obj, value)
93
- original_value = obj.instance_variable_get(:@original_values)[:additional_owners]
94
- if original_value && original_value.length > value.length
105
+ def serialize_additional_owners(legal_entity, additional_owners)
106
+ original_value = legal_entity.instance_variable_get(:@original_values)[:additional_owners]
107
+ if original_value && original_value.length > additional_owners.length
95
108
  # url params provide no mechanism for deleting an item in an array,
96
109
  # just overwriting the whole array or adding new items. So let's not
97
110
  # allow deleting without a full overwrite until we have a solution.
@@ -101,10 +114,17 @@ module Stripe
101
114
  end
102
115
 
103
116
  update_hash = {}
104
- value.each_with_index do |v, i|
105
- update = StripeObject.serialize_params(v)
106
- if update != {} && (!original_value || update != original_value[i])
107
- update_hash[i.to_s] = update
117
+ additional_owners.each_with_index do |v, i|
118
+ # We will almost always see a StripeObject except in the case of a Hash
119
+ # that's been appended to an array of `additional_owners`. We may be
120
+ # able to normalize that ugliness by using an array proxy object with
121
+ # StripeObjects that can detect appends and replace a hash with a
122
+ # StripeObject.
123
+ update = v.is_a?(StripeObject) ? v.serialize_params : v
124
+
125
+ if update != {} && (!original_value ||
126
+ update != legal_entity.serialize_params_value(original_value[i], nil, false, true))
127
+ update_hash[i.to_s] = update
108
128
  end
109
129
  end
110
130
  update_hash
@@ -0,0 +1,20 @@
1
+ module Stripe
2
+ class AlipayAccount < APIResource
3
+ include Stripe::APIOperations::Save
4
+ include Stripe::APIOperations::Delete
5
+
6
+ def resource_url
7
+ if respond_to?(:customer) && !self.customer.nil?
8
+ "#{Customer.resource_url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
9
+ end
10
+ end
11
+
12
+ def self.update(id, params=nil, opts=nil)
13
+ raise NotImplementedError.new("Alipay accounts cannot be updated without a customer ID. Update an Alipay account by `a = customer.sources.retrieve('alipay_account_id'); a.save`")
14
+ end
15
+
16
+ def self.retrieve(id, opts=nil)
17
+ raise NotImplementedError.new("Alipay accounts cannot be retrieved without a customer ID. Retrieve an Alipay account using customer.sources.retrieve('alipay_account_id')")
18
+ end
19
+ end
20
+ end
@@ -2,8 +2,8 @@ module Stripe
2
2
  module APIOperations
3
3
  module Create
4
4
  def create(params={}, opts={})
5
- response, opts = request(:post, url, params, opts)
6
- Util.convert_to_stripe_object(response, opts)
5
+ resp, opts = request(:post, resource_url, params, opts)
6
+ Util.convert_to_stripe_object(resp.data, opts)
7
7
  end
8
8
  end
9
9
  end
@@ -3,8 +3,8 @@ module Stripe
3
3
  module Delete
4
4
  def delete(params={}, opts={})
5
5
  opts = Util.normalize_opts(opts)
6
- response, opts = request(:delete, url, params, opts)
7
- initialize_from(response, opts)
6
+ resp, opts = request(:delete, resource_url, params, opts)
7
+ initialize_from(resp.data, opts)
8
8
  end
9
9
  end
10
10
  end
@@ -3,10 +3,9 @@ module Stripe
3
3
  module List
4
4
  def list(filters={}, opts={})
5
5
  opts = Util.normalize_opts(opts)
6
- opts = @opts.merge(opts) if @opts
7
6
 
8
- response, opts = request(:get, url, filters, opts)
9
- obj = ListObject.construct_from(response, opts)
7
+ resp, opts = request(:get, resource_url, filters, opts)
8
+ obj = ListObject.construct_from(resp.data, opts)
10
9
 
11
10
  # set filters so that we can fetch the same limit, expansions, and
12
11
  # predicates when accessing the next and previous pages
@@ -2,17 +2,23 @@ module Stripe
2
2
  module APIOperations
3
3
  module Request
4
4
  module ClassMethods
5
- OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :stripe_account, :stripe_version]
5
+ OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :client, :stripe_account, :stripe_version]
6
6
 
7
7
  def request(method, url, params={}, opts={})
8
8
  opts = Util.normalize_opts(opts)
9
+ opts[:client] ||= StripeClient.active_client
9
10
 
10
11
  headers = opts.clone
11
12
  api_key = headers.delete(:api_key)
12
13
  api_base = headers.delete(:api_base)
14
+ client = headers.delete(:client)
13
15
  # Assume all remaining opts must be headers
14
16
 
15
- response, opts[:api_key] = Stripe.request(method, url, api_key, params, headers, api_base)
17
+ resp, opts[:api_key] = client.execute_request(
18
+ method, url,
19
+ api_base: api_base, api_key: api_key,
20
+ headers: headers, params: params
21
+ )
16
22
 
17
23
  # Hash#select returns an array before 1.9
18
24
  opts_to_persist = {}
@@ -22,7 +28,7 @@ module Stripe
22
28
  end
23
29
  end
24
30
 
25
- [response, opts_to_persist]
31
+ [resp, opts_to_persist]
26
32
  end
27
33
  end
28
34
 
@@ -0,0 +1,85 @@
1
+ module Stripe
2
+ module APIOperations
3
+ module Save
4
+ module ClassMethods
5
+ # Updates an API resource
6
+ #
7
+ # Updates the identified resource with the passed in parameters.
8
+ #
9
+ # ==== Attributes
10
+ #
11
+ # * +id+ - ID of the resource to update.
12
+ # * +params+ - A hash of parameters to pass to the API
13
+ # * +opts+ - A Hash of additional options (separate from the params /
14
+ # object values) to be added to the request. E.g. to allow for an
15
+ # idempotency_key to be passed in the request headers, or for the
16
+ # api_key to be overwritten. See {APIOperations::Request.request}.
17
+ def update(id, params={}, opts={})
18
+ params.each do |k, v|
19
+ if self.protected_fields.include?(k)
20
+ raise ArgumentError, "Cannot update protected field: #{k}"
21
+ end
22
+ end
23
+
24
+ resp, opts = request(:post, "#{resource_url}/#{id}", params, opts)
25
+ Util.convert_to_stripe_object(resp.data, opts)
26
+ end
27
+ end
28
+
29
+ # Creates or updates an API resource.
30
+ #
31
+ # If the resource doesn't yet have an assigned ID and the resource is one
32
+ # that can be created, then the method attempts to create the resource.
33
+ # The resource is updated otherwise.
34
+ #
35
+ # ==== Attributes
36
+ #
37
+ # * +params+ - Overrides any parameters in the resource's serialized data
38
+ # and includes them in the create or update. If +:req_url:+ is included
39
+ # in the list, it overrides the update URL used for the create or
40
+ # update.
41
+ # * +opts+ - A Hash of additional options (separate from the params /
42
+ # object values) to be added to the request. E.g. to allow for an
43
+ # idempotency_key to be passed in the request headers, or for the
44
+ # api_key to be overwritten. See {APIOperations::Request.request}.
45
+ def save(params={}, opts={})
46
+ # We started unintentionally (sort of) allowing attributes sent to
47
+ # +save+ to override values used during the update. So as not to break
48
+ # the API, this makes that official here.
49
+ update_attributes(params)
50
+
51
+ # Now remove any parameters that look like object attributes.
52
+ params = params.reject { |k, _| respond_to?(k) }
53
+
54
+ values = self.serialize_params(self).merge(params)
55
+
56
+ # note that id gets removed here our call to #url above has already
57
+ # generated a uri for this object with an identifier baked in
58
+ values.delete(:id)
59
+
60
+ resp, opts = request(:post, save_url, values, opts)
61
+ initialize_from(resp.data, opts)
62
+ end
63
+
64
+ def self.included(base)
65
+ base.extend(ClassMethods)
66
+ end
67
+
68
+ private
69
+
70
+ def save_url
71
+ # This switch essentially allows us "upsert"-like functionality. If the
72
+ # API resource doesn't have an ID set (suggesting that it's new) and
73
+ # its class responds to .create (which comes from
74
+ # Stripe::APIOperations::Create), then use the URL to create a new
75
+ # resource. Otherwise, generate a URL based on the object's identifier
76
+ # for a normal update.
77
+ if self[:id] == nil && self.class.respond_to?(:create)
78
+ self.class.resource_url
79
+ else
80
+ resource_url
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -2,27 +2,60 @@ module Stripe
2
2
  class APIResource < StripeObject
3
3
  include Stripe::APIOperations::Request
4
4
 
5
+ # A flag that can be set a behavior that will cause this resource to be
6
+ # encoded and sent up along with an update of its parent resource. This is
7
+ # usually not desirable because resources are updated individually on their
8
+ # own endpoints, but there are certain cases, replacing a customer's source
9
+ # for example, where this is allowed.
10
+ attr_accessor :save_with_parent
11
+
5
12
  def self.class_name
6
13
  self.name.split('::')[-1]
7
14
  end
8
15
 
9
- def self.url
16
+ def self.resource_url
10
17
  if self == APIResource
11
18
  raise NotImplementedError.new('APIResource is an abstract class. You should perform actions on its subclasses (Charge, Customer, etc.)')
12
19
  end
13
20
  "/v1/#{CGI.escape(class_name.downcase)}s"
14
21
  end
15
22
 
16
- def url
23
+ # A metaprogramming call that specifies that a field of a resource can be
24
+ # its own type of API resource (say a nested card under an account for
25
+ # example), and if that resource is set, it should be transmitted to the
26
+ # API on a create or update. Doing so is not the default behavior because
27
+ # API resources should normally be persisted on their own RESTful
28
+ # endpoints.
29
+ def self.save_nested_resource(name)
30
+ define_method(:"#{name}=") do |value|
31
+ super(value)
32
+
33
+ # The parent setter will perform certain useful operations like
34
+ # converting to an APIResource if appropriate. Refresh our argument
35
+ # value to whatever it mutated to.
36
+ value = send(name)
37
+
38
+ # Note that the value may be subresource, but could also be a scalar
39
+ # (like a tokenized card's ID for example), so we check the type before
40
+ # setting #save_with_parent here.
41
+ if value.is_a?(APIResource)
42
+ value.save_with_parent = true
43
+ end
44
+
45
+ value
46
+ end
47
+ end
48
+
49
+ def resource_url
17
50
  unless id = self['id']
18
51
  raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
19
52
  end
20
- "#{self.class.url}/#{CGI.escape(id)}"
53
+ "#{self.class.resource_url}/#{CGI.escape(id)}"
21
54
  end
22
55
 
23
56
  def refresh
24
- response, opts = request(:get, url, @retrieve_params)
25
- initialize_from(response, opts)
57
+ resp, opts = request(:get, resource_url, @retrieve_params)
58
+ initialize_from(resp.data, opts)
26
59
  end
27
60
 
28
61
  def self.retrieve(id, opts={})
@@ -0,0 +1,12 @@
1
+ module Stripe
2
+ # Domains registered for Apple Pay on the Web
3
+ class ApplePayDomain < APIResource
4
+ extend Stripe::APIOperations::Create
5
+ include Stripe::APIOperations::Delete
6
+ extend Stripe::APIOperations::List
7
+
8
+ def self.resource_url
9
+ '/v1/apple_pay/domains'
10
+ end
11
+ end
12
+ end
@@ -2,19 +2,19 @@ module Stripe
2
2
  class ApplicationFee < APIResource
3
3
  extend Stripe::APIOperations::List
4
4
 
5
- def self.url
5
+ def self.resource_url
6
6
  '/v1/application_fees'
7
7
  end
8
8
 
9
+ # If you don't need access to an updated fee object after the refund, it's
10
+ # more performant to just call `fee.refunds.create` directly.
9
11
  def refund(params={}, opts={})
10
- response, opts = request(:post, refund_url, params, opts)
11
- initialize_from(response, opts)
12
- end
13
-
14
- private
12
+ self.refunds.create(params, opts)
15
13
 
16
- def refund_url
17
- url + '/refund'
14
+ # now that a refund has been created, we expect the state of this object
15
+ # to change as well (i.e. `refunded` will now be `true`) so refresh it
16
+ # from the server
17
+ self.refresh
18
18
  end
19
19
  end
20
20
  end
@@ -1,10 +1,14 @@
1
1
  module Stripe
2
2
  class ApplicationFeeRefund < APIResource
3
- include Stripe::APIOperations::Update
3
+ include Stripe::APIOperations::Save
4
4
  extend Stripe::APIOperations::List
5
5
 
6
- def url
7
- "#{ApplicationFee.url}/#{CGI.escape(fee)}/refunds/#{CGI.escape(id)}"
6
+ def resource_url
7
+ "#{ApplicationFee.resource_url}/#{CGI.escape(fee)}/refunds/#{CGI.escape(id)}"
8
+ end
9
+
10
+ def self.update(id, params=nil, opts=nil)
11
+ raise NotImplementedError.new("Refunds cannot be updated without an application fee ID. Update a refund by using `a = appfee.refunds.retrieve('refund_id'); a.save`")
8
12
  end
9
13
 
10
14
  def self.retrieve(id, api_key=nil)
@@ -2,7 +2,7 @@ module Stripe
2
2
  class BalanceTransaction < APIResource
3
3
  extend Stripe::APIOperations::List
4
4
 
5
- def self.url
5
+ def self.resource_url
6
6
  '/v1/balance/history'
7
7
  end
8
8
  end
@@ -1,17 +1,26 @@
1
1
  module Stripe
2
2
  class BankAccount < APIResource
3
- include Stripe::APIOperations::Update
3
+ include Stripe::APIOperations::Save
4
4
  include Stripe::APIOperations::Delete
5
5
  extend Stripe::APIOperations::List
6
6
 
7
- def url
7
+ def verify(params={}, opts={})
8
+ resp, opts = request(:post, resource_url + '/verify', params, opts)
9
+ initialize_from(resp.data, opts)
10
+ end
11
+
12
+ def resource_url
8
13
  if respond_to?(:customer)
9
- "#{Customer.url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
14
+ "#{Customer.resource_url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
10
15
  elsif respond_to?(:account)
11
- "#{Account.url}/#{CGI.escape(account)}/external_accounts/#{CGI.escape(id)}"
16
+ "#{Account.resource_url}/#{CGI.escape(account)}/external_accounts/#{CGI.escape(id)}"
12
17
  end
13
18
  end
14
19
 
20
+ def self.update(id, params=nil, opts=nil)
21
+ raise NotImplementedError.new("Bank accounts cannot be updated without an account ID. Update a bank account by using `a = account.external_accounts.retrieve('card_id'); a.save`")
22
+ end
23
+
15
24
  def self.retrieve(id, opts=nil)
16
25
  raise NotImplementedError.new("Bank accounts cannot be retrieved without an account ID. Retrieve a bank account using account.external_accounts.retrieve('card_id')")
17
26
  end
@@ -1,19 +1,19 @@
1
1
  module Stripe
2
2
  class BitcoinReceiver < APIResource
3
3
  extend Stripe::APIOperations::Create
4
- include Stripe::APIOperations::Update
4
+ include Stripe::APIOperations::Save
5
5
  include Stripe::APIOperations::Delete
6
6
  extend Stripe::APIOperations::List
7
7
 
8
- def self.url
8
+ def self.resource_url
9
9
  "/v1/bitcoin/receivers"
10
10
  end
11
11
 
12
- def url
13
- if respond_to?(:customer)
14
- "#{Customer.url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
12
+ def resource_url
13
+ if respond_to?(:customer) && !self.customer.nil? && self.customer != ""
14
+ "#{Customer.resource_url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
15
15
  else
16
- "#{self.class.url}/#{CGI.escape(id)}"
16
+ "#{self.class.resource_url}/#{CGI.escape(id)}"
17
17
  end
18
18
  end
19
19
  end
@@ -2,7 +2,7 @@ module Stripe
2
2
  class BitcoinTransaction < APIResource
3
3
  extend Stripe::APIOperations::List
4
4
 
5
- def self.url
5
+ def self.resource_url
6
6
  "/v1/bitcoin/transactions"
7
7
  end
8
8
  end
data/lib/stripe/card.rb CHANGED
@@ -1,19 +1,23 @@
1
1
  module Stripe
2
2
  class Card < APIResource
3
- include Stripe::APIOperations::Update
3
+ include Stripe::APIOperations::Save
4
4
  include Stripe::APIOperations::Delete
5
5
  extend Stripe::APIOperations::List
6
6
 
7
- def url
7
+ def resource_url
8
8
  if respond_to?(:recipient)
9
- "#{Recipient.url}/#{CGI.escape(recipient)}/cards/#{CGI.escape(id)}"
9
+ "#{Recipient.resource_url}/#{CGI.escape(recipient)}/cards/#{CGI.escape(id)}"
10
10
  elsif respond_to?(:customer)
11
- "#{Customer.url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
11
+ "#{Customer.resource_url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
12
12
  elsif respond_to?(:account)
13
- "#{Account.url}/#{CGI.escape(account)}/external_accounts/#{CGI.escape(id)}"
13
+ "#{Account.resource_url}/#{CGI.escape(account)}/external_accounts/#{CGI.escape(id)}"
14
14
  end
15
15
  end
16
16
 
17
+ def self.update(id, params=nil, opts=nil)
18
+ raise NotImplementedError.new("Cards cannot be updated without a customer ID. Update a card using `c = customer.sources.retrieve('card_id'); c.save`")
19
+ end
20
+
17
21
  def self.retrieve(id, opts=nil)
18
22
  raise NotImplementedError.new("Cards cannot be retrieved without a customer ID. Retrieve a card using customer.sources.retrieve('card_id')")
19
23
  end
data/lib/stripe/charge.rb CHANGED
@@ -2,61 +2,79 @@ module Stripe
2
2
  class Charge < APIResource
3
3
  extend Stripe::APIOperations::List
4
4
  extend Stripe::APIOperations::Create
5
- include Stripe::APIOperations::Update
5
+ include Stripe::APIOperations::Save
6
6
 
7
7
  def refund(params={}, opts={})
8
- response, opts = request(:post, refund_url, params, opts)
9
- initialize_from(response, opts)
8
+ # Old versions of charge objects included a `refunds` field that was just
9
+ # a vanilla array instead of a Stripe list object.
10
+ #
11
+ # Where possible, we'd still like to use the new refund endpoint (thus
12
+ # `self.refunds.create`), but detect the old API version by looking for
13
+ # an `Array` and fall back to the old refund URL if necessary so as to
14
+ # maintain internal compatibility.
15
+ unless self.refunds.is_a?(Array)
16
+ self.refunds.create(params, opts)
17
+
18
+ # now that a refund has been created, we expect the state of this object
19
+ # to change as well (i.e. `refunded` will now be `true`) so refresh it
20
+ # from the server
21
+ self.refresh
22
+ else
23
+ resp, opts = request(:post, refund_url, params, opts)
24
+ initialize_from(resp.data, opts)
25
+ end
10
26
  end
11
27
 
12
28
  def capture(params={}, opts={})
13
- response, opts = request(:post, capture_url, params, opts)
14
- initialize_from(response, opts)
29
+ resp, opts = request(:post, capture_url, params, opts)
30
+ initialize_from(resp.data, opts)
15
31
  end
16
32
 
17
33
  def update_dispute(params={}, opts={})
18
- response, opts = request(:post, dispute_url, params, opts)
19
- initialize_from({ :dispute => response }, opts, true)
34
+ resp, opts = request(:post, dispute_url, params, opts)
35
+ initialize_from({ :dispute => resp.data }, opts, true)
20
36
  dispute
21
37
  end
22
38
 
23
39
  def close_dispute(params={}, opts={})
24
- response, opts = request(:post, close_dispute_url, params, opts)
25
- initialize_from(response, opts)
40
+ resp, opts = request(:post, close_dispute_url, params, opts)
41
+ initialize_from(resp.data, opts)
26
42
  end
27
43
 
28
44
  def mark_as_fraudulent
29
45
  params = {
30
46
  :fraud_details => { :user_report => 'fraudulent' }
31
47
  }
32
- response, opts = request(:post, url, params)
33
- initialize_from(response, opts)
48
+ resp, opts = request(:post, resource_url, params)
49
+ initialize_from(resp.data, opts)
34
50
  end
35
51
 
36
52
  def mark_as_safe
37
53
  params = {
38
54
  :fraud_details => { :user_report => 'safe' }
39
55
  }
40
- response, opts = request(:post, url, params)
41
- initialize_from(response, opts)
56
+ resp, opts = request(:post, resource_url, params)
57
+ initialize_from(resp.data, opts)
42
58
  end
43
59
 
44
60
  private
45
61
 
46
- def refund_url
47
- url + '/refund'
48
- end
49
-
50
62
  def capture_url
51
- url + '/capture'
63
+ resource_url + '/capture'
52
64
  end
53
65
 
54
66
  def dispute_url
55
- url + '/dispute'
67
+ resource_url + '/dispute'
56
68
  end
57
69
 
58
70
  def close_dispute_url
59
- url + '/dispute/close'
71
+ resource_url + '/dispute/close'
72
+ end
73
+
74
+ # Note that this is actually the *old* refund URL and its use is no longer
75
+ # preferred.
76
+ def refund_url
77
+ resource_url + '/refund'
60
78
  end
61
79
  end
62
80
  end
@@ -0,0 +1,9 @@
1
+ module Stripe
2
+ class CountrySpec < APIResource
3
+ extend Stripe::APIOperations::List
4
+
5
+ def self.resource_url
6
+ '/v1/country_specs'
7
+ end
8
+ end
9
+ end
data/lib/stripe/coupon.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Stripe
2
2
  class Coupon < APIResource
3
3
  extend Stripe::APIOperations::Create
4
- include Stripe::APIOperations::Update
4
+ include Stripe::APIOperations::Save
5
5
  include Stripe::APIOperations::Delete
6
6
  extend Stripe::APIOperations::List
7
7
  end