Class: Stripe::Coupon

Inherits:
APIResource show all
Extended by:
APIOperations::Create, APIOperations::List
Includes:
APIOperations::Delete, APIOperations::Save
Defined in:
lib/stripe/resources/coupon.rb

Overview

A coupon contains information about a percent-off or amount-off discount you might want to apply to a customer. Coupons may be applied to [subscriptions](stripe.com/docs/api#subscriptions), [invoices](stripe.com/docs/api#invoices), [checkout sessions](stripe.com/docs/api/checkout/sessions), [quotes](stripe.com/docs/api#quotes), and more. Coupons do not work with conventional one-off [charges](stripe.com/docs/api#create_charge) or [payment intents](stripe.com/docs/api/payment_intents).

Defined Under Namespace

Classes: AppliesTo, CurrencyOptions

Constant Summary collapse

OBJECT_NAME =
"coupon"

Constants inherited from StripeObject

StripeObject::RESERVED_FIELD_NAMES

Instance Attribute Summary collapse

Attributes inherited from APIResource

#save_with_parent

Attributes inherited from StripeObject

#last_response

Class Method Summary collapse

Instance Method Summary collapse

Methods included from APIOperations::Create

create

Methods included from APIOperations::List

list

Methods included from APIOperations::Save

included, #save

Methods included from APIOperations::Delete

included

Methods inherited from APIResource

class_name, custom_method, #refresh, #request_stripe_object, resource_url, #resource_url, retrieve, save_nested_resource

Methods included from APIOperations::Request

included

Methods inherited from StripeObject

#==, #[], #[]=, additive_object_param, additive_object_param?, #as_json, construct_from, #deleted?, #dirty!, #each, #eql?, #hash, #initialize, #inspect, #keys, #marshal_dump, #marshal_load, protected_fields, #serialize_params, #to_hash, #to_json, #to_s, #update_attributes, #values

Constructor Details

This class inherits a constructor from Stripe::StripeObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Stripe::StripeObject

Instance Attribute Details

#amount_offObject (readonly)

Amount (in the ‘currency` specified) that will be taken off the subtotal of any invoices for this customer.



27
28
29
# File 'lib/stripe/resources/coupon.rb', line 27

def amount_off
  @amount_off
end

#applies_toObject (readonly)

Attribute for field applies_to



29
30
31
# File 'lib/stripe/resources/coupon.rb', line 29

def applies_to
  @applies_to
end

#createdObject (readonly)

Time at which the object was created. Measured in seconds since the Unix epoch.



31
32
33
# File 'lib/stripe/resources/coupon.rb', line 31

def created
  @created
end

#currencyObject (readonly)

If ‘amount_off` has been set, the three-letter [ISO code for the currency](stripe.com/docs/currencies) of the amount to take off.



33
34
35
# File 'lib/stripe/resources/coupon.rb', line 33

def currency
  @currency
end

#currency_optionsObject (readonly)

Coupons defined in each available currency option. Each key must be a three-letter [ISO currency code](www.iso.org/iso-4217-currency-codes.html) and a [supported currency](stripe.com/docs/currencies).



35
36
37
# File 'lib/stripe/resources/coupon.rb', line 35

def currency_options
  @currency_options
end

#deletedObject (readonly)

Always true for a deleted object



61
62
63
# File 'lib/stripe/resources/coupon.rb', line 61

def deleted
  @deleted
end

#durationObject (readonly)

One of ‘forever`, `once`, and `repeating`. Describes how long a customer who applies this coupon will get the discount.



37
38
39
# File 'lib/stripe/resources/coupon.rb', line 37

def duration
  @duration
end

#duration_in_monthsObject (readonly)

If ‘duration` is `repeating`, the number of months the coupon applies. Null if coupon `duration` is `forever` or `once`.



39
40
41
# File 'lib/stripe/resources/coupon.rb', line 39

def duration_in_months
  @duration_in_months
end

#idObject (readonly)

Unique identifier for the object.



41
42
43
# File 'lib/stripe/resources/coupon.rb', line 41

def id
  @id
end

#livemodeObject (readonly)

Has the value ‘true` if the object exists in live mode or the value `false` if the object exists in test mode.



43
44
45
# File 'lib/stripe/resources/coupon.rb', line 43

def livemode
  @livemode
end

#max_redemptionsObject (readonly)

Maximum number of times this coupon can be redeemed, in total, across all customers, before it is no longer valid.



45
46
47
# File 'lib/stripe/resources/coupon.rb', line 45

def max_redemptions
  @max_redemptions
end

#metadataObject (readonly)

Set of [key-value pairs](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.



47
48
49
# File 'lib/stripe/resources/coupon.rb', line 47

def 
  @metadata
end

#nameObject (readonly)

Name of the coupon displayed to customers on for instance invoices or receipts.



49
50
51
# File 'lib/stripe/resources/coupon.rb', line 49

def name
  @name
end

#objectObject (readonly)

String representing the object’s type. Objects of the same type share the same value.



51
52
53
# File 'lib/stripe/resources/coupon.rb', line 51

def object
  @object
end

#percent_offObject (readonly)

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.



53
54
55
# File 'lib/stripe/resources/coupon.rb', line 53

def percent_off
  @percent_off
end

#redeem_byObject (readonly)

Date after which the coupon can no longer be redeemed.



55
56
57
# File 'lib/stripe/resources/coupon.rb', line 55

def redeem_by
  @redeem_by
end

#times_redeemedObject (readonly)

Number of times this coupon has been applied to a customer.



57
58
59
# File 'lib/stripe/resources/coupon.rb', line 57

def times_redeemed
  @times_redeemed
end

#validObject (readonly)

Taking account of the above properties, whether this coupon can still be applied to a customer.



59
60
61
# File 'lib/stripe/resources/coupon.rb', line 59

def valid
  @valid
end

Class Method Details

.create(params = {}, opts = {}) ⇒ Object

You can create coupons easily via the [coupon management](dashboard.stripe.com/coupons) page of the Stripe dashboard. Coupon creation is also accessible via the API if you need to create coupons on the fly.

A coupon has either a percent_off or an amount_off and currency. If you set an amount_off, that amount will be subtracted from any invoice’s subtotal. For example, an invoice with a subtotal of 100 will have a final total of 0 if a coupon with an amount_off of 200 is applied to it and an invoice with a subtotal of 300 will have a final total of 100 if a coupon with an amount_off of 200 is applied to it.



66
67
68
# File 'lib/stripe/resources/coupon.rb', line 66

def self.create(params = {}, opts = {})
  request_stripe_object(method: :post, path: "/v1/coupons", params: params, opts: opts)
end

.delete(id, params = {}, opts = {}) ⇒ Object

You can delete coupons via the [coupon management](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.



71
72
73
74
75
76
77
78
# File 'lib/stripe/resources/coupon.rb', line 71

def self.delete(id, params = {}, opts = {})
  request_stripe_object(
    method: :delete,
    path: format("/v1/coupons/%<id>s", { id: CGI.escape(id) }),
    params: params,
    opts: opts
  )
end

.list(filters = {}, opts = {}) ⇒ Object

Returns a list of your coupons.



91
92
93
# File 'lib/stripe/resources/coupon.rb', line 91

def self.list(filters = {}, opts = {})
  request_stripe_object(method: :get, path: "/v1/coupons", params: filters, opts: opts)
end

.object_nameObject



15
16
17
# File 'lib/stripe/resources/coupon.rb', line 15

def self.object_name
  "coupon"
end

.update(id, params = {}, opts = {}) ⇒ Object

Updates the metadata of a coupon. Other coupon details (currency, duration, amount_off) are, by design, not editable.



96
97
98
99
100
101
102
103
# File 'lib/stripe/resources/coupon.rb', line 96

def self.update(id, params = {}, opts = {})
  request_stripe_object(
    method: :post,
    path: format("/v1/coupons/%<id>s", { id: CGI.escape(id) }),
    params: params,
    opts: opts
  )
end

Instance Method Details

#delete(params = {}, opts = {}) ⇒ Object

You can delete coupons via the [coupon management](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.



81
82
83
84
85
86
87
88
# File 'lib/stripe/resources/coupon.rb', line 81

def delete(params = {}, opts = {})
  request_stripe_object(
    method: :delete,
    path: format("/v1/coupons/%<coupon>s", { coupon: CGI.escape(self["id"]) }),
    params: params,
    opts: opts
  )
end