stripe 1.31.0 → 1.58.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +5 -0
  3. data/.travis.yml +2 -12
  4. data/Gemfile +29 -4
  5. data/History.txt +168 -0
  6. data/README.md +134 -0
  7. data/Rakefile +10 -0
  8. data/VERSION +1 -1
  9. data/bin/stripe-console +12 -5
  10. data/lib/data/ca-certificates.crt +3868 -5114
  11. data/lib/stripe/account.rb +41 -21
  12. data/lib/stripe/alipay_account.rb +20 -0
  13. data/lib/stripe/api_operations/create.rb +1 -1
  14. data/lib/stripe/api_operations/delete.rb +1 -1
  15. data/lib/stripe/api_operations/list.rb +1 -2
  16. data/lib/stripe/api_operations/save.rb +87 -0
  17. data/lib/stripe/api_resource.rb +37 -4
  18. data/lib/stripe/apple_pay_domain.rb +12 -0
  19. data/lib/stripe/application_fee.rb +8 -8
  20. data/lib/stripe/application_fee_refund.rb +7 -3
  21. data/lib/stripe/balance_transaction.rb +1 -1
  22. data/lib/stripe/bank_account.rb +9 -5
  23. data/lib/stripe/bitcoin_receiver.rb +6 -6
  24. data/lib/stripe/bitcoin_transaction.rb +1 -1
  25. data/lib/stripe/card.rb +9 -5
  26. data/lib/stripe/charge.rb +30 -12
  27. data/lib/stripe/country_spec.rb +9 -0
  28. data/lib/stripe/coupon.rb +1 -1
  29. data/lib/stripe/customer.rb +6 -4
  30. data/lib/stripe/dispute.rb +2 -2
  31. data/lib/stripe/errors.rb +82 -0
  32. data/lib/stripe/file_upload.rb +1 -1
  33. data/lib/stripe/invoice.rb +3 -3
  34. data/lib/stripe/invoice_item.rb +1 -1
  35. data/lib/stripe/list_object.rb +7 -6
  36. data/lib/stripe/order.rb +10 -2
  37. data/lib/stripe/order_return.rb +9 -0
  38. data/lib/stripe/plan.rb +1 -1
  39. data/lib/stripe/product.rb +2 -10
  40. data/lib/stripe/recipient.rb +1 -1
  41. data/lib/stripe/refund.rb +1 -1
  42. data/lib/stripe/reversal.rb +7 -3
  43. data/lib/stripe/singleton_api_resource.rb +3 -3
  44. data/lib/stripe/sku.rb +2 -2
  45. data/lib/stripe/source.rb +11 -0
  46. data/lib/stripe/stripe_object.rb +167 -91
  47. data/lib/stripe/subscription.rb +15 -9
  48. data/lib/stripe/subscription_item.rb +12 -0
  49. data/lib/stripe/three_d_secure.rb +9 -0
  50. data/lib/stripe/transfer.rb +3 -4
  51. data/lib/stripe/util.rb +100 -28
  52. data/lib/stripe/version.rb +1 -1
  53. data/lib/stripe.rb +283 -140
  54. data/stripe.gemspec +5 -18
  55. data/test/stripe/account_test.rb +55 -9
  56. data/test/stripe/alipay_account_test.rb +11 -0
  57. data/test/stripe/api_operations_test.rb +31 -0
  58. data/test/stripe/api_resource_test.rb +204 -10
  59. data/test/stripe/apple_pay_domain_test.rb +34 -0
  60. data/test/stripe/application_fee_test.rb +8 -5
  61. data/test/stripe/bitcoin_receiver_test.rb +2 -2
  62. data/test/stripe/charge_refund_test.rb +12 -0
  63. data/test/stripe/charge_test.rb +32 -4
  64. data/test/stripe/country_spec_test.rb +43 -0
  65. data/test/stripe/coupon_test.rb +9 -1
  66. data/test/stripe/customer_card_test.rb +2 -2
  67. data/test/stripe/customer_test.rb +24 -1
  68. data/test/stripe/dispute_test.rb +8 -0
  69. data/test/stripe/errors_test.rb +18 -0
  70. data/test/stripe/invoice_item_test.rb +19 -0
  71. data/test/stripe/invoice_test.rb +27 -1
  72. data/test/stripe/list_object_test.rb +36 -15
  73. data/test/stripe/order_return_test.rb +25 -0
  74. data/test/stripe/order_test.rb +21 -1
  75. data/test/stripe/plan_test.rb +31 -0
  76. data/test/stripe/product_test.rb +17 -7
  77. data/test/stripe/recipient_card_test.rb +2 -2
  78. data/test/stripe/recipient_test.rb +21 -0
  79. data/test/stripe/refund_test.rb +10 -1
  80. data/test/stripe/sku_test.rb +15 -6
  81. data/test/stripe/source_test.rb +83 -0
  82. data/test/stripe/stripe_object_test.rb +180 -11
  83. data/test/stripe/subscription_item_test.rb +76 -0
  84. data/test/stripe/subscription_test.rb +161 -37
  85. data/test/stripe/three_d_secure_test.rb +22 -0
  86. data/test/stripe/transfer_test.rb +8 -0
  87. data/test/stripe/util_test.rb +48 -16
  88. data/test/stripe_test.rb +58 -0
  89. data/test/test_data.rb +337 -27
  90. data/test/test_helper.rb +7 -3
  91. metadata +47 -133
  92. data/README.rdoc +0 -68
  93. data/gemfiles/default-with-activesupport.gemfile +0 -10
  94. data/gemfiles/json.gemfile +0 -12
  95. data/gemfiles/yajl.gemfile +0 -12
  96. data/lib/stripe/api_operations/update.rb +0 -58
  97. data/lib/stripe/errors/api_connection_error.rb +0 -4
  98. data/lib/stripe/errors/api_error.rb +0 -4
  99. data/lib/stripe/errors/authentication_error.rb +0 -4
  100. data/lib/stripe/errors/card_error.rb +0 -12
  101. data/lib/stripe/errors/invalid_request_error.rb +0 -11
  102. data/lib/stripe/errors/rate_limit_error.rb +0 -4
  103. data/lib/stripe/errors/stripe_error.rb +0 -26
@@ -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
+ response, opts = request(:post, resource_url + '/reject', params, opts)
41
+ initialize_from(response, 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
 
@@ -89,9 +102,9 @@ module Stripe
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,7 +2,7 @@ module Stripe
2
2
  module APIOperations
3
3
  module Create
4
4
  def create(params={}, opts={})
5
- response, opts = request(:post, url, params, opts)
5
+ response, opts = request(:post, resource_url, params, opts)
6
6
  Util.convert_to_stripe_object(response, opts)
7
7
  end
8
8
  end
@@ -3,7 +3,7 @@ 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)
6
+ response, opts = request(:delete, resource_url, params, opts)
7
7
  initialize_from(response, opts)
8
8
  end
9
9
  end
@@ -3,9 +3,8 @@ 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)
7
+ response, opts = request(:get, resource_url, filters, opts)
9
8
  obj = ListObject.construct_from(response, opts)
10
9
 
11
10
  # set filters so that we can fetch the same limit, expansions, and
@@ -0,0 +1,87 @@
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
+ response, opts = request(:post, "#{resource_url}/#{id}", params, opts)
25
+ Util.convert_to_stripe_object(response, 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
+ response, opts = request(:post, save_url, values, opts)
61
+ initialize_from(response, opts)
62
+
63
+ self
64
+ end
65
+
66
+ def self.included(base)
67
+ base.extend(ClassMethods)
68
+ end
69
+
70
+ private
71
+
72
+ def save_url
73
+ # This switch essentially allows us "upsert"-like functionality. If the
74
+ # API resource doesn't have an ID set (suggesting that it's new) and
75
+ # its class responds to .create (which comes from
76
+ # Stripe::APIOperations::Create), then use the URL to create a new
77
+ # resource. Otherwise, generate a URL based on the object's identifier
78
+ # for a normal update.
79
+ if self[:id] == nil && self.class.respond_to?(:create)
80
+ self.class.resource_url
81
+ else
82
+ resource_url
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -2,26 +2,59 @@ 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)
57
+ response, opts = request(:get, resource_url, @retrieve_params)
25
58
  initialize_from(response, opts)
26
59
  end
27
60
 
@@ -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,22 +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
7
  def verify(params={}, opts={})
8
- response, opts = request(:post, url + '/verify', params, opts)
8
+ response, opts = request(:post, resource_url + '/verify', params, opts)
9
9
  initialize_from(response, opts)
10
10
  end
11
11
 
12
- def url
12
+ def resource_url
13
13
  if respond_to?(:customer)
14
- "#{Customer.url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
14
+ "#{Customer.resource_url}/#{CGI.escape(customer)}/sources/#{CGI.escape(id)}"
15
15
  elsif respond_to?(:account)
16
- "#{Account.url}/#{CGI.escape(account)}/external_accounts/#{CGI.escape(id)}"
16
+ "#{Account.resource_url}/#{CGI.escape(account)}/external_accounts/#{CGI.escape(id)}"
17
17
  end
18
18
  end
19
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
+
20
24
  def self.retrieve(id, opts=nil)
21
25
  raise NotImplementedError.new("Bank accounts cannot be retrieved without an account ID. Retrieve a bank account using account.external_accounts.retrieve('card_id')")
22
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?
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,11 +2,27 @@ 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
+ response, opts = request(:post, refund_url, params, opts)
24
+ initialize_from(response, opts)
25
+ end
10
26
  end
11
27
 
12
28
  def capture(params={}, opts={})
@@ -29,7 +45,7 @@ module Stripe
29
45
  params = {
30
46
  :fraud_details => { :user_report => 'fraudulent' }
31
47
  }
32
- response, opts = request(:post, url, params)
48
+ response, opts = request(:post, resource_url, params)
33
49
  initialize_from(response, opts)
34
50
  end
35
51
 
@@ -37,26 +53,28 @@ module Stripe
37
53
  params = {
38
54
  :fraud_details => { :user_report => 'safe' }
39
55
  }
40
- response, opts = request(:post, url, params)
56
+ response, opts = request(:post, resource_url, params)
41
57
  initialize_from(response, 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
@@ -2,9 +2,11 @@ module Stripe
2
2
  class Customer < APIResource
3
3
  extend Stripe::APIOperations::Create
4
4
  include Stripe::APIOperations::Delete
5
- include Stripe::APIOperations::Update
5
+ include Stripe::APIOperations::Save
6
6
  extend Stripe::APIOperations::List
7
7
 
8
+ save_nested_resource :source
9
+
8
10
  def add_invoice_item(params, opts={})
9
11
  opts = @opts.merge(Util.normalize_opts(opts))
10
12
  InvoiceItem.create(params.merge(:customer => id), opts)
@@ -61,15 +63,15 @@ module Stripe
61
63
  private
62
64
 
63
65
  def discount_url
64
- url + '/discount'
66
+ resource_url + '/discount'
65
67
  end
66
68
 
67
69
  def subscription_url
68
- url + '/subscription'
70
+ resource_url + '/subscription'
69
71
  end
70
72
 
71
73
  def subscriptions_url
72
- url + '/subscriptions'
74
+ resource_url + '/subscriptions'
73
75
  end
74
76
  end
75
77
  end
@@ -2,7 +2,7 @@ module Stripe
2
2
  class Dispute < 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 close(params={}, opts={})
8
8
  response, opts = request(:post, close_url, params, opts)
@@ -10,7 +10,7 @@ module Stripe
10
10
  end
11
11
 
12
12
  def close_url
13
- url + '/close'
13
+ resource_url + '/close'
14
14
  end
15
15
  end
16
16
  end