-
Notifications
You must be signed in to change notification settings - Fork 552
/
account.rb
196 lines (172 loc) · 6.81 KB
/
account.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File generated from our OpenAPI spec
# frozen_string_literal: true
module Stripe
# This is an object representing a Stripe account. You can retrieve it to see
# properties on the account like its current e-mail address or if the account is
# enabled yet to make live charges.
#
# Some properties, marked below, are available only to platforms that want to
# [create and manage Express or Custom accounts](https://stripe.com/docs/connect/accounts).
class Account < APIResource
extend Gem::Deprecate
extend Stripe::APIOperations::Create
include Stripe::APIOperations::Delete
extend Stripe::APIOperations::List
include Stripe::APIOperations::Save
extend Stripe::APIOperations::NestedResource
OBJECT_NAME = "account"
nested_resource_class_methods :capability,
operations: %i[retrieve update list],
resource_plural: "capabilities"
nested_resource_class_methods :person,
operations: %i[create retrieve update delete list]
def persons(params = {}, opts = {})
request_stripe_object(
method: :get,
path: format("/v1/accounts/%<account>s/persons", { account: CGI.escape(self["id"]) }),
params: params,
opts: opts
)
end
def reject(params = {}, opts = {})
request_stripe_object(
method: :post,
path: format("/v1/accounts/%<account>s/reject", { account: CGI.escape(self["id"]) }),
params: params,
opts: opts
)
end
def self.persons(account, params = {}, opts = {})
request_stripe_object(
method: :get,
path: format("/v1/accounts/%<account>s/persons", { account: CGI.escape(account) }),
params: params,
opts: opts
)
end
def self.reject(account, params = {}, opts = {})
request_stripe_object(
method: :post,
path: format("/v1/accounts/%<account>s/reject", { account: CGI.escape(account) }),
params: params,
opts: opts
)
end
save_nested_resource :external_account
nested_resource_class_methods :external_account,
operations: %i[create retrieve update delete list]
nested_resource_class_methods :login_link, operations: %i[create]
def resource_url
if self["id"]
super
else
"/v1/account"
end
end
# @override To make id optional
def self.retrieve(id = nil, opts = {})
Util.check_string_argument!(id) if id
# Account used to be a singleton, where this method's signature was
# `(opts={})`. For the sake of not breaking folks who pass in an OAuth
# key in opts, let's lurkily string match for it.
if opts == {} && id.is_a?(String) && id.start_with?("sk_")
# `super` properly assumes a String opts is the apiKey and normalizes
# as expected.
opts = id
id = nil
end
super(id, opts)
end
# We are not adding a helper for capabilities here as the Account object
# already has a capabilities property which is a hash and not the sub-list
# of capabilities.
# Somewhat unfortunately, we attempt to do a special encoding trick when
# serializing `additional_owners` under an account: when updating a value,
# we actually send the update parameters up as an integer-indexed hash
# rather than an array. So instead of this:
#
# field[]=item1&field[]=item2&field[]=item3
#
# We send this:
#
# field[0]=item1&field[1]=item2&field[2]=item3
#
# There are two major problems with this technique:
#
# * Entities are addressed by array index, which is not stable and can
# easily result in unexpected results between two different requests.
#
# * A replacement of the array's contents is ambiguous with setting a
# subset of the array. Because of this, the only way to shorten an
# array is to unset it completely by making sure it goes into the
# server as an empty string, then setting its contents again.
#
# We're trying to get this overturned on the server side, but for now,
# patch in a special allowance.
def serialize_params(options = {})
serialize_params_account(self, super, options)
end
def serialize_params_account(_obj, update_hash, options = {})
if (entity = @values[:legal_entity])
if (owners = entity[:additional_owners])
entity_update = update_hash[:legal_entity] ||= {}
entity_update[:additional_owners] =
serialize_additional_owners(entity, owners)
end
end
if (individual = @values[:individual])
if individual.is_a?(Person) && !update_hash.key?(:individual)
update_hash[:individual] = individual.serialize_params(options)
end
end
update_hash
end
def self.protected_fields
[:legal_entity]
end
def legal_entity
self["legal_entity"]
end
def legal_entity=(_legal_entity)
raise NoMethodError,
"Overriding legal_entity can cause serious issues. Instead, set " \
"the individual fields of legal_entity like " \
"`account.legal_entity.first_name = 'Blah'`"
end
def deauthorize(client_id = nil, opts = {})
params = {
client_id: client_id,
stripe_user_id: id,
}
opts = @opts.merge(Util.normalize_opts(opts))
OAuth.deauthorize(params, opts)
end
private def serialize_additional_owners(legal_entity, additional_owners)
original_value =
legal_entity
.instance_variable_get(:@original_values)[:additional_owners]
if original_value && original_value.length > additional_owners.length
# url params provide no mechanism for deleting an item in an array,
# just overwriting the whole array or adding new items. So let's not
# allow deleting without a full overwrite until we have a solution.
raise ArgumentError,
"You cannot delete an item from an array, you must instead " \
"set a new array"
end
update_hash = {}
additional_owners.each_with_index do |v, i|
# We will almost always see a StripeObject except in the case of a Hash
# that's been appended to an array of `additional_owners`. We may be
# able to normalize that ugliness by using an array proxy object with
# StripeObjects that can detect appends and replace a hash with a
# StripeObject.
update = v.is_a?(StripeObject) ? v.serialize_params : v
next unless update != {} && (!original_value ||
update != legal_entity.serialize_params_value(original_value[i], nil,
false, true))
update_hash[i.to_s] = update
end
update_hash
end
end
end