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

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  // Package paymentsource provides the /customers/{customer}/sources APIs
     8  package paymentsource
     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 /customers/{customer}/sources APIs.
    19  type Client struct {
    20  	B   stripe.Backend
    21  	Key string
    22  }
    23  
    24  // New creates a new payment source.
    25  func New(params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    26  	return getC().New(params)
    27  }
    28  
    29  // New creates a new payment source.
    30  func (c Client) New(params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    31  	if params == nil {
    32  		return nil, fmt.Errorf("params should not be nil")
    33  	}
    34  	if params.Customer == nil {
    35  		return nil, fmt.Errorf("Invalid source params: customer needs to be set")
    36  	}
    37  	path := stripe.FormatURLPath(
    38  		"/v1/customers/%s/sources",
    39  		stripe.StringValue(params.Customer),
    40  	)
    41  	paymentsource := &stripe.PaymentSource{}
    42  	err := c.B.Call(http.MethodPost, path, c.Key, params, paymentsource)
    43  	return paymentsource, err
    44  }
    45  
    46  // Get returns the details of a payment source.
    47  func Get(id string, params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    48  	return getC().Get(id, params)
    49  }
    50  
    51  // Get returns the details of a payment source.
    52  func (c Client) Get(id string, params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    53  	if params == nil {
    54  		return nil, fmt.Errorf("params should not be nil")
    55  	}
    56  	if params.Customer == nil {
    57  		return nil, fmt.Errorf("Invalid source params: customer needs to be set")
    58  	}
    59  	path := stripe.FormatURLPath(
    60  		"/v1/customers/%s/sources/%s",
    61  		stripe.StringValue(params.Customer),
    62  		id,
    63  	)
    64  	paymentsource := &stripe.PaymentSource{}
    65  	err := c.B.Call(http.MethodGet, path, c.Key, params, paymentsource)
    66  	return paymentsource, err
    67  }
    68  
    69  // Update updates a payment source's properties.
    70  func Update(id string, params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    71  	return getC().Update(id, params)
    72  }
    73  
    74  // Update updates a payment source's properties.
    75  func (c Client) Update(id string, params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    76  	if params == nil {
    77  		return nil, fmt.Errorf("params should not be nil")
    78  	}
    79  	if params.Customer == nil {
    80  		return nil, fmt.Errorf("Invalid source params: customer needs to be set")
    81  	}
    82  	path := stripe.FormatURLPath(
    83  		"/v1/customers/%s/sources/%s",
    84  		stripe.StringValue(params.Customer),
    85  		id,
    86  	)
    87  	paymentsource := &stripe.PaymentSource{}
    88  	err := c.B.Call(http.MethodPost, path, c.Key, params, paymentsource)
    89  	return paymentsource, err
    90  }
    91  
    92  // Del removes a payment source.
    93  func Del(id string, params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    94  	return getC().Del(id, params)
    95  }
    96  
    97  // Del removes a payment source.
    98  func (c Client) Del(id string, params *stripe.PaymentSourceParams) (*stripe.PaymentSource, error) {
    99  	if params == nil {
   100  		return nil, fmt.Errorf("params should not be nil")
   101  	}
   102  	if params.Customer == nil {
   103  		return nil, fmt.Errorf("Invalid source params: customer needs to be set")
   104  	}
   105  	path := stripe.FormatURLPath(
   106  		"/v1/customers/%s/sources/%s",
   107  		stripe.StringValue(params.Customer),
   108  		id,
   109  	)
   110  	paymentsource := &stripe.PaymentSource{}
   111  	err := c.B.Call(http.MethodDelete, path, c.Key, params, paymentsource)
   112  	return paymentsource, err
   113  }
   114  
   115  // Verify verifies a source which is used for bank accounts.
   116  func Verify(id string, params *stripe.PaymentSourceVerifyParams) (*stripe.PaymentSource, error) {
   117  	return getC().Verify(id, params)
   118  }
   119  
   120  // Verify verifies a source which is used for bank accounts.
   121  func (c Client) Verify(id string, params *stripe.PaymentSourceVerifyParams) (*stripe.PaymentSource, error) {
   122  	if params == nil {
   123  		return nil, fmt.Errorf("params should not be nil")
   124  	}
   125  
   126  	var path string
   127  	if params.Customer != nil {
   128  		path = stripe.FormatURLPath("/v1/customers/%s/sources/%s/verify",
   129  			stripe.StringValue(params.Customer), id)
   130  	} else if len(params.Values) > 0 {
   131  		path = stripe.FormatURLPath("/v1/sources/%s/verify", id)
   132  	} else {
   133  		return nil, fmt.Errorf("Only customer bank accounts or sources can be verified in this manner")
   134  	}
   135  
   136  	source := &stripe.PaymentSource{}
   137  	err := c.B.Call(http.MethodPost, path, c.Key, params, source)
   138  	return source, err
   139  }
   140  
   141  // List returns a list of payment sources.
   142  func List(params *stripe.PaymentSourceListParams) *Iter {
   143  	return getC().List(params)
   144  }
   145  
   146  // List returns a list of payment sources.
   147  func (c Client) List(listParams *stripe.PaymentSourceListParams) *Iter {
   148  	var outerErr error
   149  	var path string
   150  
   151  	if listParams == nil {
   152  		outerErr = fmt.Errorf("params should not be nil")
   153  	} else if listParams.Customer == nil {
   154  		outerErr = fmt.Errorf("Invalid source params: customer needs to be set")
   155  	} else {
   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.PaymentSourceList{}
   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 payment sources.
   180  type Iter struct {
   181  	*stripe.Iter
   182  }
   183  
   184  // PaymentSource returns the payment source which the iterator is currently pointing to.
   185  func (i *Iter) PaymentSource() *stripe.PaymentSource {
   186  	return i.Current().(*stripe.PaymentSource)
   187  }
   188  
   189  // PaymentSourceList 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) PaymentSourceList() *stripe.PaymentSourceList {
   193  	return i.List().(*stripe.PaymentSourceList)
   194  }
   195  
   196  func getC() Client {
   197  	return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
   198  }