github.com/stripe/stripe-go/v76@v76.25.0/paymentsource.go (about)

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  package stripe
     8  
     9  import (
    10  	"encoding/json"
    11  	"fmt"
    12  	"github.com/stripe/stripe-go/v76/form"
    13  )
    14  
    15  type PaymentSourceType string
    16  
    17  // List of values that PaymentSourceType can take
    18  const (
    19  	PaymentSourceTypeAccount     PaymentSourceType = "account"
    20  	PaymentSourceTypeBankAccount PaymentSourceType = "bank_account"
    21  	PaymentSourceTypeCard        PaymentSourceType = "card"
    22  	PaymentSourceTypeSource      PaymentSourceType = "source"
    23  )
    24  
    25  // List sources for a specified customer.
    26  type PaymentSourceListParams struct {
    27  	ListParams `form:"*"`
    28  	Customer   *string `form:"-"` // Included in URL
    29  	// Specifies which fields in the response should be expanded.
    30  	Expand []*string `form:"expand"`
    31  	// Filter sources according to a particular object type.
    32  	Object *string `form:"object"`
    33  }
    34  
    35  // AddExpand appends a new field to expand.
    36  func (p *PaymentSourceListParams) AddExpand(f string) {
    37  	p.Expand = append(p.Expand, &f)
    38  }
    39  
    40  // PaymentSourceSourceParams is a union struct used to describe an
    41  // arbitrary payment source.
    42  type PaymentSourceSourceParams struct {
    43  	Card  *CardParams `form:"-"`
    44  	Token *string     `form:"source"`
    45  }
    46  
    47  // AppendTo implements custom encoding logic for PaymentSourceSourceParams.
    48  func (p *PaymentSourceSourceParams) AppendTo(body *form.Values, keyParts []string) {
    49  	if p.Card != nil {
    50  		p.Card.AppendToAsCardSourceOrExternalAccount(body, keyParts)
    51  	}
    52  }
    53  
    54  // SourceParamsFor creates PaymentSourceSourceParams objects around supported
    55  // payment sources, returning errors if not.
    56  //
    57  // Currently supported payment source types are Card (CardParams) and
    58  // Tokens/IDs (string), where Tokens could be single use card
    59  // tokens
    60  func SourceParamsFor(obj interface{}) (*PaymentSourceSourceParams, error) {
    61  	var sp *PaymentSourceSourceParams
    62  	var err error
    63  	switch p := obj.(type) {
    64  	case *CardParams:
    65  		sp = &PaymentSourceSourceParams{
    66  			Card: p,
    67  		}
    68  	case string:
    69  		sp = &PaymentSourceSourceParams{
    70  			Token: &p,
    71  		}
    72  	default:
    73  		err = fmt.Errorf("Unsupported source type %s", p)
    74  	}
    75  	return sp, err
    76  }
    77  
    78  // When you create a new credit card, you must specify a customer or recipient on which to create it.
    79  //
    80  // If the card's owner has no default card, then the new card will become the default.
    81  // However, if the owner already has a default, then it will not change.
    82  // To change the default, you should [update the customer](https://stripe.com/docs/api#update_customer) to have a new default_source.
    83  type PaymentSourceParams struct {
    84  	Params   `form:"*"`
    85  	Customer *string `form:"-"` // Included in URL
    86  	// The name of the person or business that owns the bank account.
    87  	AccountHolderName *string `form:"account_holder_name"`
    88  	// The type of entity that holds the account. This can be either `individual` or `company`.
    89  	AccountHolderType *string `form:"account_holder_type"`
    90  	// City/District/Suburb/Town/Village.
    91  	AddressCity *string `form:"address_city"`
    92  	// Billing address country, if provided when creating card.
    93  	AddressCountry *string `form:"address_country"`
    94  	// Address line 1 (Street address/PO Box/Company name).
    95  	AddressLine1 *string `form:"address_line1"`
    96  	// Address line 2 (Apartment/Suite/Unit/Building).
    97  	AddressLine2 *string `form:"address_line2"`
    98  	// State/County/Province/Region.
    99  	AddressState *string `form:"address_state"`
   100  	// ZIP or postal code.
   101  	AddressZip *string `form:"address_zip"`
   102  	// Specifies which fields in the response should be expanded.
   103  	Expand []*string `form:"expand"`
   104  	// Two digit number representing the card's expiration month.
   105  	ExpMonth *string `form:"exp_month"`
   106  	// Four digit number representing the card's expiration year.
   107  	ExpYear *string `form:"exp_year"`
   108  	// 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`.
   109  	Metadata map[string]string `form:"metadata"`
   110  	// Cardholder name.
   111  	Name  *string                   `form:"name"`
   112  	Owner *PaymentSourceOwnerParams `form:"owner"`
   113  	// Please refer to full [documentation](https://stripe.com/docs/api) instead.
   114  	Source   *PaymentSourceSourceParams `form:"*"` // PaymentSourceSourceParams has custom encoding so brought to top level with "*"
   115  	Validate *bool                      `form:"validate"`
   116  }
   117  
   118  // AddExpand appends a new field to expand.
   119  func (p *PaymentSourceParams) AddExpand(f string) {
   120  	p.Expand = append(p.Expand, &f)
   121  }
   122  
   123  // AddMetadata adds a new key-value pair to the Metadata.
   124  func (p *PaymentSourceParams) AddMetadata(key string, value string) {
   125  	if p.Metadata == nil {
   126  		p.Metadata = make(map[string]string)
   127  	}
   128  
   129  	p.Metadata[key] = value
   130  }
   131  
   132  type PaymentSourceOwnerParams struct {
   133  	// Owner's address.
   134  	Address *AddressParams `form:"address"`
   135  	// Owner's email address.
   136  	Email *string `form:"email"`
   137  	// Owner's full name.
   138  	Name *string `form:"name"`
   139  	// Owner's phone number.
   140  	Phone *string `form:"phone"`
   141  }
   142  
   143  // Verify a specified bank account for a given customer.
   144  type PaymentSourceVerifyParams struct {
   145  	Params   `form:"*"`
   146  	Customer *string `form:"-"` // Included in URL
   147  	// Two positive integers, in *cents*, equal to the values of the microdeposits sent to the bank account.
   148  	Amounts [2]int64 `form:"amounts"` // Amounts is used when verifying bank accounts
   149  	// Specifies which fields in the response should be expanded.
   150  	Expand []*string `form:"expand"`
   151  	Values []*string `form:"values"` // Values is used when verifying sources
   152  }
   153  
   154  // AddExpand appends a new field to expand.
   155  func (p *PaymentSourceVerifyParams) AddExpand(f string) {
   156  	p.Expand = append(p.Expand, &f)
   157  }
   158  
   159  type PaymentSource struct {
   160  	APIResource
   161  	BankAccount *BankAccount      `json:"-"`
   162  	Card        *Card             `json:"-"`
   163  	Deleted     bool              `json:"deleted"`
   164  	ID          string            `json:"id"`
   165  	Source      *Source           `json:"-"`
   166  	Type        PaymentSourceType `json:"object"`
   167  }
   168  
   169  // PaymentSourceList is a list of PaymentSources as retrieved from a list endpoint.
   170  type PaymentSourceList struct {
   171  	APIResource
   172  	ListMeta
   173  	Data []*PaymentSource `json:"data"`
   174  }
   175  
   176  // UnmarshalJSON handles deserialization of a PaymentSource.
   177  // This custom unmarshaling is needed because the specific
   178  // type of payment instrument it refers to is specified in the JSON
   179  func (s *PaymentSource) UnmarshalJSON(data []byte) error {
   180  	if id, ok := ParseID(data); ok {
   181  		s.ID = id
   182  		return nil
   183  	}
   184  
   185  	type paymentSource PaymentSource
   186  	var v paymentSource
   187  	if err := json.Unmarshal(data, &v); err != nil {
   188  		return err
   189  	}
   190  
   191  	var err error
   192  	*s = PaymentSource(v)
   193  
   194  	switch s.Type {
   195  	case PaymentSourceTypeBankAccount:
   196  		err = json.Unmarshal(data, &s.BankAccount)
   197  	case PaymentSourceTypeCard:
   198  		err = json.Unmarshal(data, &s.Card)
   199  	case PaymentSourceTypeSource:
   200  		err = json.Unmarshal(data, &s.Source)
   201  	}
   202  
   203  	return err
   204  }
   205  
   206  // MarshalJSON handles serialization of a PaymentSource.
   207  // This custom marshaling is needed because the specific type
   208  // of payment instrument it represents is specified by the Type
   209  func (s *PaymentSource) MarshalJSON() ([]byte, error) {
   210  	var target interface{}
   211  
   212  	switch s.Type {
   213  	case PaymentSourceTypeCard:
   214  		var customerID *string
   215  		if s.Card.Customer != nil {
   216  			customerID = &s.Card.Customer.ID
   217  		}
   218  
   219  		target = struct {
   220  			*Card
   221  			Customer *string           `json:"customer"`
   222  			Type     PaymentSourceType `json:"object"`
   223  		}{
   224  			Card:     s.Card,
   225  			Customer: customerID,
   226  			Type:     s.Type,
   227  		}
   228  	case PaymentSourceTypeAccount:
   229  		target = struct {
   230  			ID   string            `json:"id"`
   231  			Type PaymentSourceType `json:"object"`
   232  		}{
   233  			ID:   s.ID,
   234  			Type: s.Type,
   235  		}
   236  	case PaymentSourceTypeBankAccount:
   237  		var customerID *string
   238  		if s.BankAccount.Customer != nil {
   239  			customerID = &s.BankAccount.Customer.ID
   240  		}
   241  
   242  		target = struct {
   243  			*BankAccount
   244  			Customer *string           `json:"customer"`
   245  			Type     PaymentSourceType `json:"object"`
   246  		}{
   247  			BankAccount: s.BankAccount,
   248  			Customer:    customerID,
   249  			Type:        s.Type,
   250  		}
   251  	case "":
   252  		target = s.ID
   253  	}
   254  
   255  	return json.Marshal(target)
   256  }