Skip to content

Commit

Permalink
Merge pull request #1641 from stripe/richardm-hoist-metadata
Browse files Browse the repository at this point in the history
Move `Metadata` from embedded `Params` onto specific `Params` structs
  • Loading branch information
richardm-stripe authored Aug 4, 2023
2 parents b05fb41 + d220630 commit 2266970
Show file tree
Hide file tree
Showing 59 changed files with 1,029 additions and 91 deletions.
15 changes: 13 additions & 2 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ type AccountParams struct {
ExternalAccount *AccountExternalAccountParams `form:"external_account"`
// Information about the person represented by the account. This field is null unless `business_type` is set to `individual`.
Individual *PersonParams `form:"individual"`
// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
Metadata map[string]string `form:"metadata"`
// Options for customizing how the account functions within Stripe.
Settings *AccountSettingsParams `form:"settings"`
// Details on the account's acceptance of the [Stripe Services Agreement](https://stripe.com/docs/connect/updating-accounts#tos-acceptance).
Expand All @@ -177,6 +179,15 @@ type AccountParams struct {
Type *string `form:"type"`
}

// AddMetadata adds a new key-value pair to the Metadata.
func (p *AccountParams) AddMetadata(key string, value string) {
if p.Metadata == nil {
p.Metadata = make(map[string]string)
}

p.Metadata[key] = value
}

// An estimate of the monthly revenue of the business. Only accepted for accounts in Brazil and India.
type AccountBusinessProfileMonthlyEstimatedRevenueParams struct {
// A non-negative integer representing how much to charge in the [smallest currency unit](https://stripe.com/docs/currencies#zero-decimal).
Expand Down Expand Up @@ -735,8 +746,8 @@ type AccountSettingsPayoutsScheduleParams struct {
}

// AppendTo implements custom encoding logic for AccountSettingsPayoutsScheduleParams.
func (a *AccountSettingsPayoutsScheduleParams) AppendTo(body *form.Values, keyParts []string) {
if BoolValue(a.DelayDaysMinimum) {
func (p *AccountSettingsPayoutsScheduleParams) AppendTo(body *form.Values, keyParts []string) {
if BoolValue(p.DelayDaysMinimum) {
body.Add(form.FormatKey(append(keyParts, "delay_days")), "minimum")
}
}
Expand Down
47 changes: 29 additions & 18 deletions bankaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ type BankAccountParams struct {
ExpMonth *string `form:"exp_month"`
// Four digit number representing the card's expiration year.
ExpYear *string `form:"exp_year"`
// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
Metadata map[string]string `form:"metadata"`
// Cardholder name.
Name *string `form:"name"`
// The routing number, sort code, or other country-appropriate institution number for the bank account. For US bank accounts, this is required and should be the ACH routing number, not the wire routing number. If you are providing an IBAN for `account_number`, this field is not required.
Expand All @@ -232,13 +234,13 @@ type BankAccountParams struct {
// This is not a pattern that we want to push forward, and this largely exists
// because the bank accounts endpoint is a little unusual. There is one other
// resource like it, which is cards.
func (a *BankAccountParams) AppendToAsSourceOrExternalAccount(body *form.Values) {
func (p *BankAccountParams) AppendToAsSourceOrExternalAccount(body *form.Values) {
// Rather than being called in addition to `AppendTo`, this function
// *replaces* `AppendTo`, so we must also make sure to handle the encoding
// of `Params` so metadata and the like is included in the encoded payload.
form.AppendTo(body, a.Params)
form.AppendTo(body, p.Params)

isCustomer := a.Customer != nil
isCustomer := p.Customer != nil

var sourceType string
if isCustomer {
Expand All @@ -248,42 +250,51 @@ func (a *BankAccountParams) AppendToAsSourceOrExternalAccount(body *form.Values)
}

// Use token (if exists) or a dictionary containing a user’s bank account details.
if a.Token != nil {
body.Add(sourceType, StringValue(a.Token))
if p.Token != nil {
body.Add(sourceType, StringValue(p.Token))

if a.DefaultForCurrency != nil {
if p.DefaultForCurrency != nil {
body.Add(
"default_for_currency",
strconv.FormatBool(BoolValue(a.DefaultForCurrency)),
strconv.FormatBool(BoolValue(p.DefaultForCurrency)),
)
}
} else {
body.Add(sourceType+"[object]", "bank_account")
body.Add(sourceType+"[country]", StringValue(a.Country))
body.Add(sourceType+"[account_number]", StringValue(a.AccountNumber))
body.Add(sourceType+"[currency]", StringValue(a.Currency))
body.Add(sourceType+"[country]", StringValue(p.Country))
body.Add(sourceType+"[account_number]", StringValue(p.AccountNumber))
body.Add(sourceType+"[currency]", StringValue(p.Currency))

// These are optional and the API will fail if we try to send empty
// values in for them, so make sure to check that they're actually set
// before encoding them.
if a.AccountHolderName != nil {
body.Add(sourceType+"[account_holder_name]", StringValue(a.AccountHolderName))
if p.AccountHolderName != nil {
body.Add(sourceType+"[account_holder_name]", StringValue(p.AccountHolderName))
}

if a.AccountHolderType != nil {
body.Add(sourceType+"[account_holder_type]", StringValue(a.AccountHolderType))
if p.AccountHolderType != nil {
body.Add(sourceType+"[account_holder_type]", StringValue(p.AccountHolderType))
}

if a.RoutingNumber != nil {
body.Add(sourceType+"[routing_number]", StringValue(a.RoutingNumber))
if p.RoutingNumber != nil {
body.Add(sourceType+"[routing_number]", StringValue(p.RoutingNumber))
}

if a.DefaultForCurrency != nil {
body.Add(sourceType+"[default_for_currency]", strconv.FormatBool(BoolValue(a.DefaultForCurrency)))
if p.DefaultForCurrency != nil {
body.Add(sourceType+"[default_for_currency]", strconv.FormatBool(BoolValue(p.DefaultForCurrency)))
}
}
}

// AddMetadata adds a new key-value pair to the Metadata.
func (p *BankAccountParams) AddMetadata(key string, value string) {
if p.Metadata == nil {
p.Metadata = make(map[string]string)
}

p.Metadata[key] = value
}

type BankAccountListParams struct {
ListParams `form:"*"`
// The identifier of the parent account under which the bank accounts are
Expand Down
12 changes: 12 additions & 0 deletions billingportal_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,19 @@ type BillingPortalConfigurationParams struct {
Features *BillingPortalConfigurationFeaturesParams `form:"features"`
// The hosted login page for this configuration. Learn more about the portal login page in our [integration docs](https://stripe.com/docs/billing/subscriptions/integrating-customer-portal#share).
LoginPage *BillingPortalConfigurationLoginPageParams `form:"login_page"`
// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
Metadata map[string]string `form:"metadata"`
}

// AddMetadata adds a new key-value pair to the Metadata.
func (p *BillingPortalConfigurationParams) AddMetadata(key string, value string) {
if p.Metadata == nil {
p.Metadata = make(map[string]string)
}

p.Metadata[key] = value
}

type BillingPortalConfigurationBusinessProfile struct {
// The messaging shown to customers in the portal.
Headline string `json:"headline"`
Expand Down
75 changes: 43 additions & 32 deletions card.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ type CardParams struct {
ExpMonth *string `form:"exp_month"`
// Four digit number representing the card's expiration year.
ExpYear *string `form:"exp_year"`
// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
Metadata map[string]string `form:"metadata"`
// Cardholder name.
Name *string `form:"name"`
// The card number, as a string without any separators.
Expand All @@ -157,99 +159,108 @@ type CardParams struct {
// This is not a pattern that we want to push forward, and this largely exists
// because the cards endpoint is a little unusual. There is one other resource
// like it, which is bank account.
func (c *CardParams) AppendToAsCardSourceOrExternalAccount(body *form.Values, keyParts []string) {
func (p *CardParams) AppendToAsCardSourceOrExternalAccount(body *form.Values, keyParts []string) {
// Rather than being called in addition to `AppendTo`, this function
// *replaces* `AppendTo`, so we must also make sure to handle the encoding
// of `Params` so metadata and the like is included in the encoded payload.
form.AppendToPrefixed(body, c.Params, keyParts)
form.AppendToPrefixed(body, p.Params, keyParts)

if c.DefaultForCurrency != nil {
if p.DefaultForCurrency != nil {
body.Add(
form.FormatKey(append(keyParts, "default_for_currency")),
strconv.FormatBool(BoolValue(c.DefaultForCurrency)),
strconv.FormatBool(BoolValue(p.DefaultForCurrency)),
)
}

if c.Token != nil {
if c.Account != nil {
body.Add(form.FormatKey(append(keyParts, "external_account")), StringValue(c.Token))
if p.Token != nil {
if p.Account != nil {
body.Add(form.FormatKey(append(keyParts, "external_account")), StringValue(p.Token))
} else {
body.Add(form.FormatKey(append(keyParts, cardSource)), StringValue(c.Token))
body.Add(form.FormatKey(append(keyParts, cardSource)), StringValue(p.Token))
}
}

if c.Number != nil {
if p.Number != nil {
body.Add(form.FormatKey(append(keyParts, cardSource, "object")), "card")
body.Add(form.FormatKey(append(keyParts, cardSource, "number")), StringValue(c.Number))
body.Add(form.FormatKey(append(keyParts, cardSource, "number")), StringValue(p.Number))
}
if c.CVC != nil {
if p.CVC != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "cvc")),
StringValue(c.CVC),
StringValue(p.CVC),
)
}
if c.Currency != nil {
if p.Currency != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "currency")),
StringValue(c.Currency),
StringValue(p.Currency),
)
}
if c.ExpMonth != nil {
if p.ExpMonth != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "exp_month")),
StringValue(c.ExpMonth),
StringValue(p.ExpMonth),
)
}
if c.ExpYear != nil {
if p.ExpYear != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "exp_year")),
StringValue(c.ExpYear),
StringValue(p.ExpYear),
)
}
if c.Name != nil {
if p.Name != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "name")),
StringValue(c.Name),
StringValue(p.Name),
)
}
if c.AddressCity != nil {
if p.AddressCity != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "address_city")),
StringValue(c.AddressCity),
StringValue(p.AddressCity),
)
}
if c.AddressCountry != nil {
if p.AddressCountry != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "address_country")),
StringValue(c.AddressCountry),
StringValue(p.AddressCountry),
)
}
if c.AddressLine1 != nil {
if p.AddressLine1 != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "address_line1")),
StringValue(c.AddressLine1),
StringValue(p.AddressLine1),
)
}
if c.AddressLine2 != nil {
if p.AddressLine2 != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "address_line2")),
StringValue(c.AddressLine2),
StringValue(p.AddressLine2),
)
}
if c.AddressState != nil {
if p.AddressState != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "address_state")),
StringValue(c.AddressState),
StringValue(p.AddressState),
)
}
if c.AddressZip != nil {
if p.AddressZip != nil {
body.Add(
form.FormatKey(append(keyParts, cardSource, "address_zip")),
StringValue(c.AddressZip),
StringValue(p.AddressZip),
)
}
}

// AddMetadata adds a new key-value pair to the Metadata.
func (p *CardParams) AddMetadata(key string, value string) {
if p.Metadata == nil {
p.Metadata = make(map[string]string)
}

p.Metadata[key] = value
}

type CardListParams struct {
ListParams `form:"*"`
Account *string `form:"-"` // Included in URL
Expand Down
11 changes: 11 additions & 0 deletions charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ type ChargeParams struct {
// A set of key-value pairs you can attach to a charge giving information about its riskiness. If you believe a charge is fraudulent, include a `user_report` key with a value of `fraudulent`. If you believe a charge is safe, include a `user_report` key with a value of `safe`. Stripe will use the information you send to improve our fraud detection algorithms.
FraudDetails *ChargeFraudDetailsParams `form:"fraud_details"`
Level3 *ChargeLevel3Params `form:"level3"`
// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
Metadata map[string]string `form:"metadata"`
// The Stripe account ID for which these funds are intended. Automatically set if you use the `destination` parameter. For details, see [Creating Separate Charges and Transfers](https://stripe.com/docs/connect/separate-charges-and-transfers#on-behalf-of).
OnBehalfOf *string `form:"on_behalf_of"`
// Options to configure Radar. See [Radar Session](https://stripe.com/docs/radar/radar-session) for more information.
Expand Down Expand Up @@ -352,6 +354,15 @@ func (p *ChargeParams) SetSource(sp interface{}) error {
return err
}

// AddMetadata adds a new key-value pair to the Metadata.
func (p *ChargeParams) AddMetadata(key string, value string) {
if p.Metadata == nil {
p.Metadata = make(map[string]string)
}

p.Metadata[key] = value
}

// A set of key-value pairs you can attach to a charge giving information about its riskiness. If you believe a charge is fraudulent, include a `user_report` key with a value of `fraudulent`. If you believe a charge is safe, include a `user_report` key with a value of `safe`. Stripe will use the information you send to improve our fraud detection algorithms.
type ChargeFraudDetailsParams struct {
// Either `safe` or `fraudulent`.
Expand Down
Loading

0 comments on commit 2266970

Please sign in to comment.