MishkaPay.Coupon (Mishka Pay v0.0.1)

Copy Markdown View Source

A discount the provider keeps: a percent or an amount off, once, for some months of a subscription, or forever. A guarded_struct. Buyers redeem it through a MishkaPay.Coupon.PromotionCode they type, or a checkout applies it directly (MishkaPay.Checkout.Discounts).

Made with MishkaPay.create_coupon/1, read with MishkaPay.fetch_coupon/3 and MishkaPay.list_coupons/3, removed with MishkaPay.delete_coupon/3 (codes already given keep their discount on what they were applied to).

What the caller gives:

  • :provider, :tenant: which provider, with whose keys.
  • :id: optional, the coupon's own id ("SPRING"); the provider makes one when it is left out.
  • :name: what the buyer sees on the checkout and the invoice.
  • :percent_off (more than 0, at most 100) or :amount_off (a Money): one of them.
  • :duration: :once (the default), :repeating for :duration_in_months of a subscription, or :forever.
  • :max_redemptions: how many times it may be used in all.
  • :redeem_by: the last moment it may be used.
  • :metadata: extra pairs, string keys to string values.

What the provider fills in: :valid (it can still be used), :times_redeemed, :created_at.

Summary

Types

t()

The struct that builder/1 returns.

Functions

A sample %MishkaPay.Coupon{} populated from defaults + type-based fallbacks.

Types

t()

@type t() :: %MishkaPay.Coupon{
  amount_off: Money.t() | nil,
  created_at: DateTime.t() | nil,
  duration: (:once | :repeating | :forever) | nil,
  duration_in_months: pos_integer() | nil,
  id: String.t() | nil,
  max_redemptions: pos_integer() | nil,
  metadata: map() | nil,
  name: String.t() | nil,
  percent_off: Decimal.t() | nil,
  promotion_code: MishkaPay.Coupon.PromotionCode.t() | nil,
  provider: String.t() | nil,
  redeem_by: DateTime.t() | nil,
  tenant: term() | nil,
  times_redeemed: non_neg_integer() | nil,
  valid: boolean() | nil
}

The struct that builder/1 returns.

FieldTypeRequiredDefaultDescriptionDerives
:providerString.t() | nilnosanitize(trim) validate(string, not_empty)
:tenantterm() | nilno
:idString.t() | nilnosanitize(trim) validate(string, not_empty, max_len=200)
:nameString.t() | nilnosanitize(trim) validate(string, not_empty, max_len=200)
:percent_offDecimal.t() | nilnovalidator {MishkaPay.Coupon, :percent_off}
:amount_offMoney.t() | nilnovalidator {MishkaPay.Money, :field}
:duration:once | :repeating | :forever | nilno:oncevalidate(not_nil_value, enum=Atom[once::repeating::forever])<br>validator {MishkaPay.Coupon, :duration}
:duration_in_monthspos_integer() | nilnovalidate(integer, min_len=1)
:max_redemptionspos_integer() | nilnovalidate(integer, min_len=1)
:redeem_byDateTime.t() | nilnovalidate(struct)
:metadatamap() | nilno%{}validate(map)<br>validator {MishkaPay.Checkout, :metadata}
:validboolean() | nilnovalidate(boolean)
:times_redeemednon_neg_integer() | nilnovalidate(integer)
:created_atDateTime.t() | nilnovalidate(struct)
:promotion_codeMishkaPay.Coupon.PromotionCode.t() | nilnoA code a buyer types at the checkout (SPRING10) that applies a MishkaPay.Coupon, with its own limits. A guarded_struct. A checkout lets buyers type codes with allow_promotion_codes: true, or applies one itself through discounts (MishkaPay.Checkout.Discounts).

Functions

builder(attrs_or_input, error \\ false)

enforce_keys()

enforce_keys(field)

example()

A sample %MishkaPay.Coupon{} populated from defaults + type-based fallbacks.

keys()

keys(field)