/
coupon.go
370 lines (321 loc) · 14.2 KB
/
coupon.go
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
368
369
370
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"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/internal/shared"
"github.com/orbcorp/orb-go/option"
"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 *shared.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) *shared.PageAutoPager[Coupon] {
return shared.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...)
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...)
path := fmt.Sprintf("coupons/%s", couponID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// A coupon represents a reusable discount configuration, and have an attached
// redemption code that can be issued to your end users. Coupons are most often
// used in self-serve signup or upgrade flows in your checkout experience or
// billing portal.
//
// To redeem a coupon, pass the `redemption_code` property in the
// [create subscription](create-subscription.api.mdx) or
// [schedule plan change](schedule-plan-change.api.mdx) request.
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
}
// Union satisfied by [CouponDiscountPercentageDiscount] or
// [CouponDiscountAmountDiscount].
type CouponDiscount interface {
implementsCouponDiscount()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*CouponDiscount)(nil)).Elem(),
"discount_type",
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(CouponDiscountPercentageDiscount{}),
DiscriminatorValue: "percentage",
},
apijson.UnionVariant{
TypeFilter: gjson.JSON,
Type: reflect.TypeOf(CouponDiscountAmountDiscount{}),
DiscriminatorValue: "amount",
},
)
}
type CouponDiscountPercentageDiscount struct {
// List of price_ids that this discount applies to. For plan/plan phase discounts,
// this can be a subset of prices.
AppliesToPriceIDs []string `json:"applies_to_price_ids,required"`
DiscountType CouponDiscountPercentageDiscountDiscountType `json:"discount_type,required"`
// Only available if discount_type is `percentage`. This is a number between 0
// and 1.
PercentageDiscount float64 `json:"percentage_discount,required"`
Reason string `json:"reason,nullable"`
JSON couponDiscountPercentageDiscountJSON `json:"-"`
}
// couponDiscountPercentageDiscountJSON contains the JSON metadata for the struct
// [CouponDiscountPercentageDiscount]
type couponDiscountPercentageDiscountJSON struct {
AppliesToPriceIDs apijson.Field
DiscountType apijson.Field
PercentageDiscount apijson.Field
Reason apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CouponDiscountPercentageDiscount) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r couponDiscountPercentageDiscountJSON) RawJSON() string {
return r.raw
}
func (r CouponDiscountPercentageDiscount) implementsCouponDiscount() {}
type CouponDiscountPercentageDiscountDiscountType string
const (
CouponDiscountPercentageDiscountDiscountTypePercentage CouponDiscountPercentageDiscountDiscountType = "percentage"
)
func (r CouponDiscountPercentageDiscountDiscountType) IsKnown() bool {
switch r {
case CouponDiscountPercentageDiscountDiscountTypePercentage:
return true
}
return false
}
type CouponDiscountAmountDiscount struct {
// Only available if discount_type is `amount`.
AmountDiscount string `json:"amount_discount,required"`
// List of price_ids that this discount applies to. For plan/plan phase discounts,
// this can be a subset of prices.
AppliesToPriceIDs []string `json:"applies_to_price_ids,required"`
DiscountType CouponDiscountAmountDiscountDiscountType `json:"discount_type,required"`
Reason string `json:"reason,nullable"`
JSON couponDiscountAmountDiscountJSON `json:"-"`
}
// couponDiscountAmountDiscountJSON contains the JSON metadata for the struct
// [CouponDiscountAmountDiscount]
type couponDiscountAmountDiscountJSON struct {
AmountDiscount apijson.Field
AppliesToPriceIDs apijson.Field
DiscountType apijson.Field
Reason apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *CouponDiscountAmountDiscount) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r couponDiscountAmountDiscountJSON) RawJSON() string {
return r.raw
}
func (r CouponDiscountAmountDiscount) implementsCouponDiscount() {}
type CouponDiscountAmountDiscountDiscountType string
const (
CouponDiscountAmountDiscountDiscountTypeAmount CouponDiscountAmountDiscountDiscountType = "amount"
)
func (r CouponDiscountAmountDiscountDiscountType) IsKnown() bool {
switch r {
case CouponDiscountAmountDiscountDiscountTypeAmount:
return true
}
return false
}
type CouponNewParams struct {
Discount param.Field[CouponNewParamsDiscount] `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)
}
// Satisfied by [CouponNewParamsDiscountPercentageDiscount],
// [CouponNewParamsDiscountAmountDiscount].
type CouponNewParamsDiscount interface {
implementsCouponNewParamsDiscount()
}
type CouponNewParamsDiscountPercentageDiscount struct {
// List of price_ids that this discount applies to. For plan/plan phase discounts,
// this can be a subset of prices.
AppliesToPriceIDs param.Field[[]string] `json:"applies_to_price_ids,required"`
DiscountType param.Field[CouponNewParamsDiscountPercentageDiscountDiscountType] `json:"discount_type,required"`
// Only available if discount_type is `percentage`. This is a number between 0
// and 1.
PercentageDiscount param.Field[float64] `json:"percentage_discount,required"`
Reason param.Field[string] `json:"reason"`
}
func (r CouponNewParamsDiscountPercentageDiscount) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CouponNewParamsDiscountPercentageDiscount) implementsCouponNewParamsDiscount() {}
type CouponNewParamsDiscountPercentageDiscountDiscountType string
const (
CouponNewParamsDiscountPercentageDiscountDiscountTypePercentage CouponNewParamsDiscountPercentageDiscountDiscountType = "percentage"
)
func (r CouponNewParamsDiscountPercentageDiscountDiscountType) IsKnown() bool {
switch r {
case CouponNewParamsDiscountPercentageDiscountDiscountTypePercentage:
return true
}
return false
}
type CouponNewParamsDiscountAmountDiscount struct {
// Only available if discount_type is `amount`.
AmountDiscount param.Field[string] `json:"amount_discount,required"`
// List of price_ids that this discount applies to. For plan/plan phase discounts,
// this can be a subset of prices.
AppliesToPriceIDs param.Field[[]string] `json:"applies_to_price_ids,required"`
DiscountType param.Field[CouponNewParamsDiscountAmountDiscountDiscountType] `json:"discount_type,required"`
Reason param.Field[string] `json:"reason"`
}
func (r CouponNewParamsDiscountAmountDiscount) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r CouponNewParamsDiscountAmountDiscount) implementsCouponNewParamsDiscount() {}
type CouponNewParamsDiscountAmountDiscountDiscountType string
const (
CouponNewParamsDiscountAmountDiscountDiscountTypeAmount CouponNewParamsDiscountAmountDiscountDiscountType = "amount"
)
func (r CouponNewParamsDiscountAmountDiscountDiscountType) IsKnown() bool {
switch r {
case CouponNewParamsDiscountAmountDiscountDiscountTypeAmount:
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,
})
}