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

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  // Package source provides the /sources APIs
     8  package source
     9  
    10  import (
    11  	"fmt"
    12  	"net/http"
    13  
    14  	stripe "github.com/stripe/stripe-go/v76"
    15  )
    16  
    17  // Client is used to invoke /sources APIs.
    18  type Client struct {
    19  	B   stripe.Backend
    20  	Key string
    21  }
    22  
    23  // New creates a new source.
    24  func New(params *stripe.SourceParams) (*stripe.Source, error) {
    25  	return getC().New(params)
    26  }
    27  
    28  // New creates a new source.
    29  func (c Client) New(params *stripe.SourceParams) (*stripe.Source, error) {
    30  	source := &stripe.Source{}
    31  	err := c.B.Call(http.MethodPost, "/v1/sources", c.Key, params, source)
    32  	return source, err
    33  }
    34  
    35  // Get returns the details of a source.
    36  func Get(id string, params *stripe.SourceParams) (*stripe.Source, error) {
    37  	return getC().Get(id, params)
    38  }
    39  
    40  // Get returns the details of a source.
    41  func (c Client) Get(id string, params *stripe.SourceParams) (*stripe.Source, error) {
    42  	path := stripe.FormatURLPath("/v1/sources/%s", id)
    43  	source := &stripe.Source{}
    44  	err := c.B.Call(http.MethodGet, path, c.Key, params, source)
    45  	return source, err
    46  }
    47  
    48  // Update updates a source's properties.
    49  func Update(id string, params *stripe.SourceParams) (*stripe.Source, error) {
    50  	return getC().Update(id, params)
    51  }
    52  
    53  // Update updates a source's properties.
    54  func (c Client) Update(id string, params *stripe.SourceParams) (*stripe.Source, error) {
    55  	path := stripe.FormatURLPath("/v1/sources/%s", id)
    56  	source := &stripe.Source{}
    57  	err := c.B.Call(http.MethodPost, path, c.Key, params, source)
    58  	return source, err
    59  }
    60  
    61  // Detach is the method for the `DELETE /v1/customers/{customer}/sources/{id}` API.
    62  func Detach(id string, params *stripe.SourceDetachParams) (*stripe.Source, error) {
    63  	return getC().Detach(id, params)
    64  }
    65  
    66  // Detach is the method for the `DELETE /v1/customers/{customer}/sources/{id}` API.
    67  func (c Client) Detach(id string, params *stripe.SourceDetachParams) (*stripe.Source, error) {
    68  	if params.Customer == nil {
    69  		return nil, fmt.Errorf(
    70  			"Invalid source detach params: Customer needs to be set",
    71  		)
    72  	}
    73  	path := stripe.FormatURLPath(
    74  		"/v1/customers/%s/sources/%s",
    75  		stripe.StringValue(params.Customer),
    76  		id,
    77  	)
    78  	source := &stripe.Source{}
    79  	err := c.B.Call(http.MethodDelete, path, c.Key, params, source)
    80  	return source, err
    81  }
    82  
    83  func getC() Client {
    84  	return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
    85  }