forked from orbcorp/orb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoupon.go
More file actions
367 lines (320 loc) · 13.6 KB
/
coupon.go
File metadata and controls
367 lines (320 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"time"
"github.com/orbcorp/orb-go/internal/apijson"
"github.com/orbcorp/orb-go/internal/apiquery"
"github.com/orbcorp/orb-go/internal/param"
"github.com/orbcorp/orb-go/internal/requestconfig"
"github.com/orbcorp/orb-go/option"
"github.com/orbcorp/orb-go/packages/pagination"
"github.com/orbcorp/orb-go/shared"
"github.com/tidwall/gjson"
)
// CouponService contains methods and other services that help with interacting
// with the orb API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewCouponService] method instead.
type CouponService struct {
Options []option.RequestOption
Subscriptions *CouponSubscriptionService
}
// NewCouponService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewCouponService(opts ...option.RequestOption) (r *CouponService) {
r = &CouponService{}
r.Options = opts
r.Subscriptions = NewCouponSubscriptionService(opts...)
return
}
// This endpoint allows the creation of coupons, which can then be redeemed at
// subscription creation or plan change.
func (r *CouponService) New(ctx context.Context, body CouponNewParams, opts ...option.RequestOption) (res *Coupon, err error) {
opts = append(r.Options[:], opts...)
path := "coupons"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// This endpoint returns a list of all coupons for an account in a list format.
//
// The list of coupons is ordered starting from the most recently created coupon.
// The response also includes `pagination_metadata`, which lets the caller retrieve
// the next page of results if they exist. More information about pagination can be
// found in the Pagination-metadata schema.
func (r *CouponService) List(ctx context.Context, query CouponListParams, opts ...option.RequestOption) (res *pagination.Page[Coupon], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "coupons"
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// This endpoint returns a list of all coupons for an account in a list format.
//
// The list of coupons is ordered starting from the most recently created coupon.
// The response also includes `pagination_metadata`, which lets the caller retrieve
// the next page of results if they exist. More information about pagination can be
// found in the Pagination-metadata schema.
func (r *CouponService) ListAutoPaging(ctx context.Context, query CouponListParams, opts ...option.RequestOption) *pagination.PageAutoPager[Coupon] {
return pagination.NewPageAutoPager(r.List(ctx, query, opts...))
}
// This endpoint allows a coupon to be archived. Archived coupons can no longer be
// redeemed, and will be hidden from lists of active coupons. Additionally, once a
// coupon is archived, its redemption code can be reused for a different coupon.
func (r *CouponService) Archive(ctx context.Context, couponID string, opts ...option.RequestOption) (res *Coupon, err error) {
opts = append(r.Options[:], opts...)
if couponID == "" {
err = errors.New("missing required coupon_id parameter")
return
}
path := fmt.Sprintf("coupons/%s/archive", couponID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return
}
// This endpoint retrieves a coupon by its ID. To fetch coupons by their redemption
// code, use the [List coupons](list-coupons) endpoint with the redemption_code
// parameter.
func (r *CouponService) Fetch(ctx context.Context, couponID string, opts ...option.RequestOption) (res *Coupon, err error) {
opts = append(r.Options[:], opts...)
if couponID == "" {
err = errors.New("missing required coupon_id parameter")
return
}
path := fmt.Sprintf("coupons/%s", couponID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// A coupon represents a reusable discount configuration that can be applied either
// as a fixed or percentage amount to an invoice or subscription. Coupons are
// activated using a redemption code, which applies the discount to a subscription
// or invoice. The duration of a coupon determines how long it remains available
// for use by end users.
type Coupon struct {
// Also referred to as coupon_id in this documentation.
ID string `json:"id,required"`
// An archived coupon can no longer be redeemed. Active coupons will have a value
// of null for `archived_at`; this field will be non-null for archived coupons.
ArchivedAt time.Time `json:"archived_at,required,nullable" format:"date-time"`
Discount CouponDiscount `json:"discount,required"`
// This allows for a coupon's discount to apply for a limited time (determined in
// months); a `null` value here means "unlimited time".
DurationInMonths int64 `json:"duration_in_months,required,nullable"`
// The maximum number of redemptions allowed for this coupon before it is
// exhausted; `null` here means "unlimited".
MaxRedemptions int64 `json:"max_redemptions,required,nullable"`
// This string can be used to redeem this coupon for a given subscription.
RedemptionCode string `json:"redemption_code,required"`
// The number of times this coupon has been redeemed.
TimesRedeemed int64 `json:"times_redeemed,required"`
JSON couponJSON `json:"-"`
}
// couponJSON contains the JSON metadata for the struct [Coupon]
type couponJSON struct {
ID apijson.Field
ArchivedAt apijson.Field
Discount apijson.Field
DurationInMonths apijson.Field
MaxRedemptions apijson.Field
RedemptionCode apijson.Field
TimesRedeemed apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Coupon) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r couponJSON) RawJSON() string {
return r.raw
}
type CouponDiscount struct {
DiscountType CouponDiscountDiscountType `json:"discount_type,required"`
// Only available if discount_type is `amount`.
AmountDiscount string `json:"amount_discount"`
// This field can have the runtime type of [[]string].
AppliesToPriceIDs interface{} `json:"applies_to_price_ids"`
// This field can have the runtime type of [[]shared.TransformPriceFilter].
Filters interface{} `json:"filters"`
// Only available if discount_type is `percentage`. This is a number between 0
// and 1.
PercentageDiscount float64 `json:"percentage_discount"`
Reason string `json:"reason,nullable"`
JSON couponDiscountJSON `json:"-"`
union CouponDiscountUnion
}
// couponDiscountJSON contains the JSON metadata for the struct [CouponDiscount]
type couponDiscountJSON struct {
DiscountType apijson.Field
AmountDiscount apijson.Field
AppliesToPriceIDs apijson.Field
Filters apijson.Field
PercentageDiscount apijson.Field
Reason apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r couponDiscountJSON) RawJSON() string {
return r.raw
}
func (r *CouponDiscount) UnmarshalJSON(data []byte) (err error) {
*r = CouponDiscount{}
err = apijson.UnmarshalRoot(data, &r.union)
if err != nil {
return err
}
return apijson.Port(r.union, &r)
}
// AsUnion returns a [CouponDiscountUnion] interface which you can cast to the
// specific types for more type safety.
//
// Possible runtime types of the union are [shared.PercentageDiscount],
// [shared.AmountDiscount].
func (r CouponDiscount) AsUnion() CouponDiscountUnion {
return r.union
}
// Union satisfied by [shared.PercentageDiscount] or [shared.AmountDiscount].
type CouponDiscountUnion interface {
ImplementsCouponDiscount()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*CouponDiscountUnion)(nil)).Elem(),
"discount_type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(shared.PercentageDiscount{}),
DiscriminatorValue: "percentage",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(shared.AmountDiscount{}),
DiscriminatorValue: "amount",
},
)
}
type CouponDiscountDiscountType string
const (
CouponDiscountDiscountTypePercentage CouponDiscountDiscountType = "percentage"
CouponDiscountDiscountTypeAmount CouponDiscountDiscountType = "amount"
)
func (r CouponDiscountDiscountType) IsKnown() bool {
switch r {
case CouponDiscountDiscountTypePercentage, CouponDiscountDiscountTypeAmount:
return true
}
return false
}
type CouponNewParams struct {
Discount param.Field[CouponNewParamsDiscountUnion] `json:"discount,required"`
// This string can be used to redeem this coupon for a given subscription.
RedemptionCode param.Field[string] `json:"redemption_code,required"`
// This allows for a coupon's discount to apply for a limited time (determined in
// months); a `null` value here means "unlimited time".
DurationInMonths param.Field[int64] `json:"duration_in_months"`
// The maximum number of redemptions allowed for this coupon before it is
// exhausted;`null` here means "unlimited".
MaxRedemptions param.Field[int64] `json:"max_redemptions"`
}
func (r CouponNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type CouponNewParamsDiscount struct {
DiscountType param.Field[CouponNewParamsDiscountDiscountType] `json:"discount_type,required"`
AmountDiscount param.Field[string] `json:"amount_discount"`
PercentageDiscount param.Field[float64] `json:"percentage_discount"`
}
func (r CouponNewParamsDiscount) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CouponNewParamsDiscount) implementsCouponNewParamsDiscountUnion() {}
// Satisfied by [CouponNewParamsDiscountNewCouponPercentageDiscount],
// [CouponNewParamsDiscountNewCouponAmountDiscount], [CouponNewParamsDiscount].
type CouponNewParamsDiscountUnion interface {
implementsCouponNewParamsDiscountUnion()
}
type CouponNewParamsDiscountNewCouponPercentageDiscount struct {
DiscountType param.Field[CouponNewParamsDiscountNewCouponPercentageDiscountDiscountType] `json:"discount_type,required"`
PercentageDiscount param.Field[float64] `json:"percentage_discount,required"`
}
func (r CouponNewParamsDiscountNewCouponPercentageDiscount) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CouponNewParamsDiscountNewCouponPercentageDiscount) implementsCouponNewParamsDiscountUnion() {
}
type CouponNewParamsDiscountNewCouponPercentageDiscountDiscountType string
const (
CouponNewParamsDiscountNewCouponPercentageDiscountDiscountTypePercentage CouponNewParamsDiscountNewCouponPercentageDiscountDiscountType = "percentage"
)
func (r CouponNewParamsDiscountNewCouponPercentageDiscountDiscountType) IsKnown() bool {
switch r {
case CouponNewParamsDiscountNewCouponPercentageDiscountDiscountTypePercentage:
return true
}
return false
}
type CouponNewParamsDiscountNewCouponAmountDiscount struct {
AmountDiscount param.Field[string] `json:"amount_discount,required"`
DiscountType param.Field[CouponNewParamsDiscountNewCouponAmountDiscountDiscountType] `json:"discount_type,required"`
}
func (r CouponNewParamsDiscountNewCouponAmountDiscount) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CouponNewParamsDiscountNewCouponAmountDiscount) implementsCouponNewParamsDiscountUnion() {}
type CouponNewParamsDiscountNewCouponAmountDiscountDiscountType string
const (
CouponNewParamsDiscountNewCouponAmountDiscountDiscountTypeAmount CouponNewParamsDiscountNewCouponAmountDiscountDiscountType = "amount"
)
func (r CouponNewParamsDiscountNewCouponAmountDiscountDiscountType) IsKnown() bool {
switch r {
case CouponNewParamsDiscountNewCouponAmountDiscountDiscountTypeAmount:
return true
}
return false
}
type CouponNewParamsDiscountDiscountType string
const (
CouponNewParamsDiscountDiscountTypePercentage CouponNewParamsDiscountDiscountType = "percentage"
CouponNewParamsDiscountDiscountTypeAmount CouponNewParamsDiscountDiscountType = "amount"
)
func (r CouponNewParamsDiscountDiscountType) IsKnown() bool {
switch r {
case CouponNewParamsDiscountDiscountTypePercentage, CouponNewParamsDiscountDiscountTypeAmount:
return true
}
return false
}
type CouponListParams struct {
// Cursor for pagination. This can be populated by the `next_cursor` value returned
// from the initial request.
Cursor param.Field[string] `query:"cursor"`
// The number of items to fetch. Defaults to 20.
Limit param.Field[int64] `query:"limit"`
// Filter to coupons matching this redemption code.
RedemptionCode param.Field[string] `query:"redemption_code"`
// Show archived coupons as well (by default, this endpoint only returns active
// coupons).
ShowArchived param.Field[bool] `query:"show_archived"`
}
// URLQuery serializes [CouponListParams]'s query parameters as `url.Values`.
func (r CouponListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}