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

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  // Package subscription provides the /subscriptions APIs
     8  package subscription
     9  
    10  import (
    11  	"net/http"
    12  
    13  	stripe "github.com/stripe/stripe-go/v76"
    14  	"github.com/stripe/stripe-go/v76/form"
    15  )
    16  
    17  // Client is used to invoke /subscriptions APIs.
    18  type Client struct {
    19  	B   stripe.Backend
    20  	Key string
    21  }
    22  
    23  // New creates a new subscription.
    24  func New(params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
    25  	return getC().New(params)
    26  }
    27  
    28  // New creates a new subscription.
    29  func (c Client) New(params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
    30  	subscription := &stripe.Subscription{}
    31  	err := c.B.Call(
    32  		http.MethodPost,
    33  		"/v1/subscriptions",
    34  		c.Key,
    35  		params,
    36  		subscription,
    37  	)
    38  	return subscription, err
    39  }
    40  
    41  // Get returns the details of a subscription.
    42  func Get(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
    43  	return getC().Get(id, params)
    44  }
    45  
    46  // Get returns the details of a subscription.
    47  func (c Client) Get(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
    48  	path := stripe.FormatURLPath("/v1/subscriptions/%s", id)
    49  	subscription := &stripe.Subscription{}
    50  	err := c.B.Call(http.MethodGet, path, c.Key, params, subscription)
    51  	return subscription, err
    52  }
    53  
    54  // Update updates a subscription's properties.
    55  func Update(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
    56  	return getC().Update(id, params)
    57  }
    58  
    59  // Update updates a subscription's properties.
    60  func (c Client) Update(id string, params *stripe.SubscriptionParams) (*stripe.Subscription, error) {
    61  	path := stripe.FormatURLPath("/v1/subscriptions/%s", id)
    62  	subscription := &stripe.Subscription{}
    63  	err := c.B.Call(http.MethodPost, path, c.Key, params, subscription)
    64  	return subscription, err
    65  }
    66  
    67  // Cancel is the method for the `DELETE /v1/subscriptions/{subscription_exposed_id}` API.
    68  func Cancel(id string, params *stripe.SubscriptionCancelParams) (*stripe.Subscription, error) {
    69  	return getC().Cancel(id, params)
    70  }
    71  
    72  // Cancel is the method for the `DELETE /v1/subscriptions/{subscription_exposed_id}` API.
    73  func (c Client) Cancel(id string, params *stripe.SubscriptionCancelParams) (*stripe.Subscription, error) {
    74  	path := stripe.FormatURLPath("/v1/subscriptions/%s", id)
    75  	subscription := &stripe.Subscription{}
    76  	err := c.B.Call(http.MethodDelete, path, c.Key, params, subscription)
    77  	return subscription, err
    78  }
    79  
    80  // DeleteDiscount is the method for the `DELETE /v1/subscriptions/{subscription_exposed_id}/discount` API.
    81  func DeleteDiscount(id string, params *stripe.SubscriptionDeleteDiscountParams) (*stripe.Subscription, error) {
    82  	return getC().DeleteDiscount(id, params)
    83  }
    84  
    85  // DeleteDiscount is the method for the `DELETE /v1/subscriptions/{subscription_exposed_id}/discount` API.
    86  func (c Client) DeleteDiscount(id string, params *stripe.SubscriptionDeleteDiscountParams) (*stripe.Subscription, error) {
    87  	path := stripe.FormatURLPath("/v1/subscriptions/%s/discount", id)
    88  	subscription := &stripe.Subscription{}
    89  	err := c.B.Call(http.MethodDelete, path, c.Key, params, subscription)
    90  	return subscription, err
    91  }
    92  
    93  // Resume is the method for the `POST /v1/subscriptions/{subscription}/resume` API.
    94  func Resume(id string, params *stripe.SubscriptionResumeParams) (*stripe.Subscription, error) {
    95  	return getC().Resume(id, params)
    96  }
    97  
    98  // Resume is the method for the `POST /v1/subscriptions/{subscription}/resume` API.
    99  func (c Client) Resume(id string, params *stripe.SubscriptionResumeParams) (*stripe.Subscription, error) {
   100  	path := stripe.FormatURLPath("/v1/subscriptions/%s/resume", id)
   101  	subscription := &stripe.Subscription{}
   102  	err := c.B.Call(http.MethodPost, path, c.Key, params, subscription)
   103  	return subscription, err
   104  }
   105  
   106  // List returns a list of subscriptions.
   107  func List(params *stripe.SubscriptionListParams) *Iter {
   108  	return getC().List(params)
   109  }
   110  
   111  // List returns a list of subscriptions.
   112  func (c Client) List(listParams *stripe.SubscriptionListParams) *Iter {
   113  	return &Iter{
   114  		Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
   115  			list := &stripe.SubscriptionList{}
   116  			err := c.B.CallRaw(http.MethodGet, "/v1/subscriptions", c.Key, b, p, list)
   117  
   118  			ret := make([]interface{}, len(list.Data))
   119  			for i, v := range list.Data {
   120  				ret[i] = v
   121  			}
   122  
   123  			return ret, list, err
   124  		}),
   125  	}
   126  }
   127  
   128  // Iter is an iterator for subscriptions.
   129  type Iter struct {
   130  	*stripe.Iter
   131  }
   132  
   133  // Subscription returns the subscription which the iterator is currently pointing to.
   134  func (i *Iter) Subscription() *stripe.Subscription {
   135  	return i.Current().(*stripe.Subscription)
   136  }
   137  
   138  // SubscriptionList returns the current list object which the iterator is
   139  // currently using. List objects will change as new API calls are made to
   140  // continue pagination.
   141  func (i *Iter) SubscriptionList() *stripe.SubscriptionList {
   142  	return i.List().(*stripe.SubscriptionList)
   143  }
   144  
   145  // Search returns a search result containing subscriptions.
   146  func Search(params *stripe.SubscriptionSearchParams) *SearchIter {
   147  	return getC().Search(params)
   148  }
   149  
   150  // Search returns a search result containing subscriptions.
   151  func (c Client) Search(params *stripe.SubscriptionSearchParams) *SearchIter {
   152  	return &SearchIter{
   153  		SearchIter: stripe.GetSearchIter(params, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.SearchContainer, error) {
   154  			list := &stripe.SubscriptionSearchResult{}
   155  			err := c.B.CallRaw(http.MethodGet, "/v1/subscriptions/search", c.Key, b, p, list)
   156  
   157  			ret := make([]interface{}, len(list.Data))
   158  			for i, v := range list.Data {
   159  				ret[i] = v
   160  			}
   161  
   162  			return ret, list, err
   163  		}),
   164  	}
   165  }
   166  
   167  // SearchIter is an iterator for subscriptions.
   168  type SearchIter struct {
   169  	*stripe.SearchIter
   170  }
   171  
   172  // Subscription returns the subscription which the iterator is currently pointing to.
   173  func (i *SearchIter) Subscription() *stripe.Subscription {
   174  	return i.Current().(*stripe.Subscription)
   175  }
   176  
   177  // SubscriptionSearchResult returns the current list object which the iterator is
   178  // currently using. List objects will change as new API calls are made to
   179  // continue pagination.
   180  func (i *SearchIter) SubscriptionSearchResult() *stripe.SubscriptionSearchResult {
   181  	return i.SearchResult().(*stripe.SubscriptionSearchResult)
   182  }
   183  
   184  func getC() Client {
   185  	return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
   186  }