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

     1  //
     2  //
     3  // File generated from our OpenAPI spec
     4  //
     5  //
     6  
     7  package stripe
     8  
     9  import "encoding/json"
    10  
    11  // One of `forever`, `once`, and `repeating`. Describes how long a customer who applies this coupon will get the discount.
    12  type CouponDuration string
    13  
    14  // List of values that CouponDuration can take
    15  const (
    16  	CouponDurationForever   CouponDuration = "forever"
    17  	CouponDurationOnce      CouponDuration = "once"
    18  	CouponDurationRepeating CouponDuration = "repeating"
    19  )
    20  
    21  // You can delete coupons via the [coupon management](https://dashboard.stripe.com/coupons) page of the Stripe dashboard. However, deleting a coupon does not affect any customers who have already applied the coupon; it means that new customers can't redeem the coupon. You can also delete coupons via the API.
    22  type CouponParams struct {
    23  	Params `form:"*"`
    24  	// A positive integer representing the amount to subtract from an invoice total (required if `percent_off` is not passed).
    25  	AmountOff *int64 `form:"amount_off"`
    26  	// A hash containing directions for what this Coupon will apply discounts to.
    27  	AppliesTo *CouponAppliesToParams `form:"applies_to"`
    28  	// Three-letter [ISO code for the currency](https://stripe.com/docs/currencies) of the `amount_off` parameter (required if `amount_off` is passed).
    29  	Currency *string `form:"currency"`
    30  	// Coupons defined in each available currency option (only supported if the coupon is amount-based). Each key must be a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) and a [supported currency](https://stripe.com/docs/currencies).
    31  	CurrencyOptions map[string]*CouponCurrencyOptionsParams `form:"currency_options"`
    32  	// Specifies how long the discount will be in effect if used on a subscription. Defaults to `once`.
    33  	Duration *string `form:"duration"`
    34  	// Required only if `duration` is `repeating`, in which case it must be a positive integer that specifies the number of months the discount will be in effect.
    35  	DurationInMonths *int64 `form:"duration_in_months"`
    36  	// Specifies which fields in the response should be expanded.
    37  	Expand []*string `form:"expand"`
    38  	// Unique string of your choice that will be used to identify this coupon when applying it to a customer. If you don't want to specify a particular code, you can leave the ID blank and we'll generate a random code for you.
    39  	ID *string `form:"id"`
    40  	// A positive integer specifying the number of times the coupon can be redeemed before it's no longer valid. For example, you might have a 50% off coupon that the first 20 readers of your blog can use.
    41  	MaxRedemptions *int64 `form:"max_redemptions"`
    42  	// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
    43  	Metadata map[string]string `form:"metadata"`
    44  	// Name of the coupon displayed to customers on, for instance invoices, or receipts. By default the `id` is shown if `name` is not set.
    45  	Name *string `form:"name"`
    46  	// A positive float larger than 0, and smaller or equal to 100, that represents the discount the coupon will apply (required if `amount_off` is not passed).
    47  	PercentOff *float64 `form:"percent_off"`
    48  	// Unix timestamp specifying the last time at which the coupon can be redeemed. After the redeem_by date, the coupon can no longer be applied to new customers.
    49  	RedeemBy *int64 `form:"redeem_by"`
    50  }
    51  
    52  // AddExpand appends a new field to expand.
    53  func (p *CouponParams) AddExpand(f string) {
    54  	p.Expand = append(p.Expand, &f)
    55  }
    56  
    57  // AddMetadata adds a new key-value pair to the Metadata.
    58  func (p *CouponParams) AddMetadata(key string, value string) {
    59  	if p.Metadata == nil {
    60  		p.Metadata = make(map[string]string)
    61  	}
    62  
    63  	p.Metadata[key] = value
    64  }
    65  
    66  // Coupons defined in each available currency option (only supported if the coupon is amount-based). Each key must be a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) and a [supported currency](https://stripe.com/docs/currencies).
    67  type CouponCurrencyOptionsParams struct {
    68  	// A positive integer representing the amount to subtract from an invoice total.
    69  	AmountOff *int64 `form:"amount_off"`
    70  }
    71  
    72  // Returns a list of your coupons.
    73  type CouponListParams struct {
    74  	ListParams `form:"*"`
    75  	// A filter on the list, based on the object `created` field. The value can be a string with an integer Unix timestamp, or it can be a dictionary with a number of different query options.
    76  	Created *int64 `form:"created"`
    77  	// A filter on the list, based on the object `created` field. The value can be a string with an integer Unix timestamp, or it can be a dictionary with a number of different query options.
    78  	CreatedRange *RangeQueryParams `form:"created"`
    79  	// Specifies which fields in the response should be expanded.
    80  	Expand []*string `form:"expand"`
    81  }
    82  
    83  // AddExpand appends a new field to expand.
    84  func (p *CouponListParams) AddExpand(f string) {
    85  	p.Expand = append(p.Expand, &f)
    86  }
    87  
    88  // A hash containing directions for what this Coupon will apply discounts to.
    89  type CouponAppliesToParams struct {
    90  	// An array of Product IDs that this Coupon will apply to.
    91  	Products []*string `form:"products"`
    92  }
    93  type CouponAppliesTo struct {
    94  	// A list of product IDs this coupon applies to
    95  	Products []string `json:"products"`
    96  }
    97  
    98  // Coupons defined in each available currency option. Each key must be a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) and a [supported currency](https://stripe.com/docs/currencies).
    99  type CouponCurrencyOptions struct {
   100  	// Amount (in the `currency` specified) that will be taken off the subtotal of any invoices for this customer.
   101  	AmountOff int64 `json:"amount_off"`
   102  }
   103  
   104  // A coupon contains information about a percent-off or amount-off discount you
   105  // might want to apply to a customer. Coupons may be applied to [subscriptions](https://stripe.com/docs/api#subscriptions), [invoices](https://stripe.com/docs/api#invoices),
   106  // [checkout sessions](https://stripe.com/docs/api/checkout/sessions), [quotes](https://stripe.com/docs/api#quotes), and more. Coupons do not work with conventional one-off [charges](https://stripe.com/docs/api#create_charge) or [payment intents](https://stripe.com/docs/api/payment_intents).
   107  type Coupon struct {
   108  	APIResource
   109  	// Amount (in the `currency` specified) that will be taken off the subtotal of any invoices for this customer.
   110  	AmountOff int64            `json:"amount_off"`
   111  	AppliesTo *CouponAppliesTo `json:"applies_to"`
   112  	// Time at which the object was created. Measured in seconds since the Unix epoch.
   113  	Created int64 `json:"created"`
   114  	// If `amount_off` has been set, the three-letter [ISO code for the currency](https://stripe.com/docs/currencies) of the amount to take off.
   115  	Currency Currency `json:"currency"`
   116  	// Coupons defined in each available currency option. Each key must be a three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html) and a [supported currency](https://stripe.com/docs/currencies).
   117  	CurrencyOptions map[string]*CouponCurrencyOptions `json:"currency_options"`
   118  	Deleted         bool                              `json:"deleted"`
   119  	// One of `forever`, `once`, and `repeating`. Describes how long a customer who applies this coupon will get the discount.
   120  	Duration CouponDuration `json:"duration"`
   121  	// If `duration` is `repeating`, the number of months the coupon applies. Null if coupon `duration` is `forever` or `once`.
   122  	DurationInMonths int64 `json:"duration_in_months"`
   123  	// Unique identifier for the object.
   124  	ID string `json:"id"`
   125  	// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
   126  	Livemode bool `json:"livemode"`
   127  	// Maximum number of times this coupon can be redeemed, in total, across all customers, before it is no longer valid.
   128  	MaxRedemptions int64 `json:"max_redemptions"`
   129  	// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format.
   130  	Metadata map[string]string `json:"metadata"`
   131  	// Name of the coupon displayed to customers on for instance invoices or receipts.
   132  	Name string `json:"name"`
   133  	// String representing the object's type. Objects of the same type share the same value.
   134  	Object string `json:"object"`
   135  	// Percent that will be taken off the subtotal of any invoices for this customer for the duration of the coupon. For example, a coupon with percent_off of 50 will make a $ (or local equivalent)100 invoice $ (or local equivalent)50 instead.
   136  	PercentOff float64 `json:"percent_off"`
   137  	// Date after which the coupon can no longer be redeemed.
   138  	RedeemBy int64 `json:"redeem_by"`
   139  	// Number of times this coupon has been applied to a customer.
   140  	TimesRedeemed int64 `json:"times_redeemed"`
   141  	// Taking account of the above properties, whether this coupon can still be applied to a customer.
   142  	Valid bool `json:"valid"`
   143  }
   144  
   145  // CouponList is a list of Coupons as retrieved from a list endpoint.
   146  type CouponList struct {
   147  	APIResource
   148  	ListMeta
   149  	Data []*Coupon `json:"data"`
   150  }
   151  
   152  // UnmarshalJSON handles deserialization of a Coupon.
   153  // This custom unmarshaling is needed because the resulting
   154  // property may be an id or the full struct if it was expanded.
   155  func (c *Coupon) UnmarshalJSON(data []byte) error {
   156  	if id, ok := ParseID(data); ok {
   157  		c.ID = id
   158  		return nil
   159  	}
   160  
   161  	type coupon Coupon
   162  	var v coupon
   163  	if err := json.Unmarshal(data, &v); err != nil {
   164  		return err
   165  	}
   166  
   167  	*c = Coupon(v)
   168  	return nil
   169  }