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

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  // Package bankaccount provides the bankaccount related APIs
     8  package bankaccount
     9  
    10  import (
    11  	"fmt"
    12  	"net/http"
    13  
    14  	stripe "github.com/stripe/stripe-go/v76"
    15  	"github.com/stripe/stripe-go/v76/form"
    16  )
    17  
    18  // Client is used to invoke bankaccount related APIs.
    19  type Client struct {
    20  	B   stripe.Backend
    21  	Key string
    22  }
    23  
    24  // New creates a new bank account.
    25  func New(params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
    26  	return getC().New(params)
    27  }
    28  
    29  // New creates a new bank account.
    30  func (c Client) New(params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
    31  	if params == nil {
    32  		return nil, fmt.Errorf("params should not be nil")
    33  	}
    34  
    35  	var path string
    36  	if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) {
    37  		return nil, fmt.Errorf("Invalid bank account params: exactly one of Account or Customer need to be set")
    38  	} else if params.Account != nil {
    39  		path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts", stripe.StringValue(params.Account))
    40  	} else if params.Customer != nil {
    41  		path = stripe.FormatURLPath("/v1/customers/%s/sources", stripe.StringValue(params.Customer))
    42  	}
    43  
    44  	body := &form.Values{}
    45  
    46  	// Note that we call this special append method instead of the standard one
    47  	// from the form package. We should not use form's because doing so will
    48  	// include some parameters that are undesirable here.
    49  	params.AppendToAsSourceOrExternalAccount(body)
    50  
    51  	// Because bank account creation uses the custom append above, we have to
    52  	// make an explicit call using a form and CallRaw instead of the standard
    53  	// Call (which takes a set of parameters).
    54  	bankaccount := &stripe.BankAccount{}
    55  	err := c.B.CallRaw(http.MethodPost, path, c.Key, body, &params.Params, bankaccount)
    56  	return bankaccount, err
    57  }
    58  
    59  // Get returns the details of a bank account.
    60  func Get(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
    61  	return getC().Get(id, params)
    62  }
    63  
    64  // Get returns the details of a bank account.
    65  func (c Client) Get(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
    66  	if params == nil {
    67  		return nil, fmt.Errorf("params should not be nil")
    68  	}
    69  
    70  	var path string
    71  	if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) {
    72  		return nil, fmt.Errorf("Invalid bank account params: exactly one of Account or Customer need to be set")
    73  	} else if params.Account != nil {
    74  		path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
    75  	} else if params.Customer != nil {
    76  		path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
    77  	}
    78  
    79  	bankaccount := &stripe.BankAccount{}
    80  	err := c.B.Call(http.MethodGet, path, c.Key, params, bankaccount)
    81  	return bankaccount, err
    82  }
    83  
    84  // Update updates a bank account's properties.
    85  func Update(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
    86  	return getC().Update(id, params)
    87  }
    88  
    89  // Update updates a bank account's properties.
    90  func (c Client) Update(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
    91  	if params == nil {
    92  		return nil, fmt.Errorf("params should not be nil")
    93  	}
    94  
    95  	var path string
    96  	if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) {
    97  		return nil, fmt.Errorf("Invalid bank account params: exactly one of Account or Customer need to be set")
    98  	} else if params.Account != nil {
    99  		path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
   100  	} else if params.Customer != nil {
   101  		path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
   102  	}
   103  
   104  	bankaccount := &stripe.BankAccount{}
   105  	err := c.B.Call(http.MethodPost, path, c.Key, params, bankaccount)
   106  	return bankaccount, err
   107  }
   108  
   109  // Del removes a bank account.
   110  func Del(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
   111  	return getC().Del(id, params)
   112  }
   113  
   114  // Del removes a bank account.
   115  func (c Client) Del(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
   116  	if params == nil {
   117  		return nil, fmt.Errorf("params should not be nil")
   118  	}
   119  
   120  	var path string
   121  	if (params.Account != nil && params.Customer != nil) || (params.Account == nil && params.Customer == nil) {
   122  		return nil, fmt.Errorf("Invalid bank account params: exactly one of Account or Customer need to be set")
   123  	} else if params.Account != nil {
   124  		path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts/%s", stripe.StringValue(params.Account), id)
   125  	} else if params.Customer != nil {
   126  		path = stripe.FormatURLPath("/v1/customers/%s/sources/%s", stripe.StringValue(params.Customer), id)
   127  	}
   128  
   129  	bankaccount := &stripe.BankAccount{}
   130  	err := c.B.Call(http.MethodDelete, path, c.Key, params, bankaccount)
   131  	return bankaccount, err
   132  }
   133  
   134  // List returns a list of bank accounts.
   135  func List(params *stripe.BankAccountListParams) *Iter {
   136  	return getC().List(params)
   137  }
   138  
   139  // List returns a list of bank accounts.
   140  func (c Client) List(listParams *stripe.BankAccountListParams) *Iter {
   141  	var path string
   142  	var outerErr error
   143  
   144  	// There's no bank accounts list URL, so we use one sources or external
   145  	// accounts. An override on BankAccountListParam's `AppendTo` will add the
   146  	// filter `object=bank_account` to make sure that only bank accounts come
   147  	// back with the response.
   148  	if listParams == nil {
   149  		outerErr = fmt.Errorf("params should not be nil")
   150  	} else if (listParams.Account != nil && listParams.Customer != nil) || (listParams.Account == nil && listParams.Customer == nil) {
   151  		outerErr = fmt.Errorf("Invalid bank account params: exactly one of Account or Customer need to be set")
   152  	} else if listParams.Account != nil {
   153  		path = stripe.FormatURLPath("/v1/accounts/%s/external_accounts",
   154  			stripe.StringValue(listParams.Account))
   155  	} else if listParams.Customer != nil {
   156  		path = stripe.FormatURLPath("/v1/customers/%s/sources",
   157  			stripe.StringValue(listParams.Customer))
   158  	}
   159  	return &Iter{
   160  		Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
   161  			list := &stripe.BankAccountList{}
   162  
   163  			if outerErr != nil {
   164  				return nil, list, outerErr
   165  			}
   166  
   167  			err := c.B.CallRaw(http.MethodGet, path, c.Key, b, p, list)
   168  
   169  			ret := make([]interface{}, len(list.Data))
   170  			for i, v := range list.Data {
   171  				ret[i] = v
   172  			}
   173  
   174  			return ret, list, err
   175  		}),
   176  	}
   177  }
   178  
   179  // Iter is an iterator for bank accounts.
   180  type Iter struct {
   181  	*stripe.Iter
   182  }
   183  
   184  // BankAccount returns the bank account which the iterator is currently pointing to.
   185  func (i *Iter) BankAccount() *stripe.BankAccount {
   186  	return i.Current().(*stripe.BankAccount)
   187  }
   188  
   189  // BankAccountList returns the current list object which the iterator is
   190  // currently using. List objects will change as new API calls are made to
   191  // continue pagination.
   192  func (i *Iter) BankAccountList() *stripe.BankAccountList {
   193  	return i.List().(*stripe.BankAccountList)
   194  }
   195  
   196  func getC() Client {
   197  	return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
   198  }