github.com/stripe/stripe-go/v76@v76.25.0/charge/client.go (about) 1 // 2 // 3 // File generated from our OpenAPI spec 4 // 5 // 6 7 // Package charge provides the /charges APIs 8 package charge 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 /charges APIs. 18 type Client struct { 19 B stripe.Backend 20 Key string 21 } 22 23 // New creates a new charge. 24 func New(params *stripe.ChargeParams) (*stripe.Charge, error) { 25 return getC().New(params) 26 } 27 28 // New creates a new charge. 29 func (c Client) New(params *stripe.ChargeParams) (*stripe.Charge, error) { 30 charge := &stripe.Charge{} 31 err := c.B.Call(http.MethodPost, "/v1/charges", c.Key, params, charge) 32 return charge, err 33 } 34 35 // Get returns the details of a charge. 36 func Get(id string, params *stripe.ChargeParams) (*stripe.Charge, error) { 37 return getC().Get(id, params) 38 } 39 40 // Get returns the details of a charge. 41 func (c Client) Get(id string, params *stripe.ChargeParams) (*stripe.Charge, error) { 42 path := stripe.FormatURLPath("/v1/charges/%s", id) 43 charge := &stripe.Charge{} 44 err := c.B.Call(http.MethodGet, path, c.Key, params, charge) 45 return charge, err 46 } 47 48 // Update updates a charge's properties. 49 func Update(id string, params *stripe.ChargeParams) (*stripe.Charge, error) { 50 return getC().Update(id, params) 51 } 52 53 // Update updates a charge's properties. 54 func (c Client) Update(id string, params *stripe.ChargeParams) (*stripe.Charge, error) { 55 path := stripe.FormatURLPath("/v1/charges/%s", id) 56 charge := &stripe.Charge{} 57 err := c.B.Call(http.MethodPost, path, c.Key, params, charge) 58 return charge, err 59 } 60 61 // Capture is the method for the `POST /v1/charges/{charge}/capture` API. 62 func Capture(id string, params *stripe.ChargeCaptureParams) (*stripe.Charge, error) { 63 return getC().Capture(id, params) 64 } 65 66 // Capture is the method for the `POST /v1/charges/{charge}/capture` API. 67 func (c Client) Capture(id string, params *stripe.ChargeCaptureParams) (*stripe.Charge, error) { 68 path := stripe.FormatURLPath("/v1/charges/%s/capture", id) 69 charge := &stripe.Charge{} 70 err := c.B.Call(http.MethodPost, path, c.Key, params, charge) 71 return charge, err 72 } 73 74 // List returns a list of charges. 75 func List(params *stripe.ChargeListParams) *Iter { 76 return getC().List(params) 77 } 78 79 // List returns a list of charges. 80 func (c Client) List(listParams *stripe.ChargeListParams) *Iter { 81 return &Iter{ 82 Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) { 83 list := &stripe.ChargeList{} 84 err := c.B.CallRaw(http.MethodGet, "/v1/charges", c.Key, b, p, list) 85 86 ret := make([]interface{}, len(list.Data)) 87 for i, v := range list.Data { 88 ret[i] = v 89 } 90 91 return ret, list, err 92 }), 93 } 94 } 95 96 // Iter is an iterator for charges. 97 type Iter struct { 98 *stripe.Iter 99 } 100 101 // Charge returns the charge which the iterator is currently pointing to. 102 func (i *Iter) Charge() *stripe.Charge { 103 return i.Current().(*stripe.Charge) 104 } 105 106 // ChargeList returns the current list object which the iterator is 107 // currently using. List objects will change as new API calls are made to 108 // continue pagination. 109 func (i *Iter) ChargeList() *stripe.ChargeList { 110 return i.List().(*stripe.ChargeList) 111 } 112 113 // Search returns a search result containing charges. 114 func Search(params *stripe.ChargeSearchParams) *SearchIter { 115 return getC().Search(params) 116 } 117 118 // Search returns a search result containing charges. 119 func (c Client) Search(params *stripe.ChargeSearchParams) *SearchIter { 120 return &SearchIter{ 121 SearchIter: stripe.GetSearchIter(params, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.SearchContainer, error) { 122 list := &stripe.ChargeSearchResult{} 123 err := c.B.CallRaw(http.MethodGet, "/v1/charges/search", c.Key, b, p, list) 124 125 ret := make([]interface{}, len(list.Data)) 126 for i, v := range list.Data { 127 ret[i] = v 128 } 129 130 return ret, list, err 131 }), 132 } 133 } 134 135 // SearchIter is an iterator for charges. 136 type SearchIter struct { 137 *stripe.SearchIter 138 } 139 140 // Charge returns the charge which the iterator is currently pointing to. 141 func (i *SearchIter) Charge() *stripe.Charge { 142 return i.Current().(*stripe.Charge) 143 } 144 145 // ChargeSearchResult returns the current list object which the iterator is 146 // currently using. List objects will change as new API calls are made to 147 // continue pagination. 148 func (i *SearchIter) ChargeSearchResult() *stripe.ChargeSearchResult { 149 return i.SearchResult().(*stripe.ChargeSearchResult) 150 } 151 152 func getC() Client { 153 return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key} 154 }