subscription

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2020 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller

type Controller interface {
	// contains filtered or unexported methods
}

Controller is payment gateway interface for subscription handling

type CreateResponse

type CreateResponse struct {
	ID                    string
	Status                Status
	LastCreatedInvoiceURL string
}

CreateResponse is a wrapper for create subscription response

type IntervalUnit

type IntervalUnit string

IntervalUnit defines the unit of recurrence period

const (
	// IntervalUnitDay recurrence happend every day
	IntervalUnitDay IntervalUnit = "day"
	// IntervalUnitWeek ...
	IntervalUnitWeek IntervalUnit = "week"
	// IntervalUnitMonth ...
	IntervalUnitMonth IntervalUnit = "month"
)

func NewIntervalUnit

func NewIntervalUnit(s string) IntervalUnit

NewIntervalUnit return an IntervalUnit based on the string given

func (IntervalUnit) Duration

func (iu IntervalUnit) Duration() time.Duration

type MissedPaymentAction

type MissedPaymentAction string

MissedPaymentAction defines action should be taken if user miss the payment

const (
	// MissedPaymentActionIgnore subscription will be continue on the
	// next term
	MissedPaymentActionIgnore MissedPaymentAction = "ignore"
	// MissedPaymentActionStop subscription will be stopped immediately
	MissedPaymentActionStop MissedPaymentAction = "stop"
)

type Schedule

type Schedule struct {
	payment.Model
	SubscriptionID      uint64       `json:"-" gorm:"index:schedule_subs_id"`
	Interval            int          `json:"interval"`
	IntervalUnit        IntervalUnit `json:"interval_unit"`
	StartAt             *time.Time   `json:"start_at"`
	PreviousExecutionAt *time.Time   `json:"previous_execution_at"`
	NextExecutionAt     *time.Time   `json:"next_execution_at"`
}

Schedule tells when subscription starts and charges

func NewSchedule

func NewSchedule(interval int, unit IntervalUnit, start *time.Time) *Schedule

NewSchedule create new payment schedule

func (*Schedule) NextAfterPause

func (s *Schedule) NextAfterPause() *time.Time

NextAfterPause calculate when the next payment should be executed after it is paused

func (*Schedule) NextSince

func (s *Schedule) NextSince(t time.Time) time.Time

NextSince ...

type Status

type Status int

Status shows subscription status

const (
	// StatusActive is active
	StatusActive Status = iota
	// StatusPaused subscription may be resumed
	StatusPaused
	// StatusStop subscription cant be resumed
	StatusStop
)

func (Status) String

func (s Status) String() string

type Subscription

type Subscription struct {
	payment.Model
	Number              string              `json:"number" gorm:"unique_index:subs_number_k"`
	Name                string              `json:"name"`
	Description         string              `json:"description" gorm:"type:text"`
	Amount              float64             `json:"amount"`
	UserID              string              `json:"user_id"`
	Currency            string              `json:"currency"`
	Schedule            Schedule            `json:"schedule" gorm:"ForeignKey:SubscriptionID"`
	TotalReccurence     int                 `json:"total_recurrence"`
	InvoiceDuration     time.Duration       `json:"invoice_duration"`
	ShouldSendEmail     bool                `json:"should_send_email"`
	MissedPaymentAction MissedPaymentAction `json:"missed_payment_action"`
	Recharge            bool                `json:"recharge"`
	CardToken           string              `json:"card_token"`
	GatewayRecurringID  string              `json:"gateway_recurring_id"`
	Gateway             string              `json:"gateway"`
	Invoices            []invoice.Invoice   `json:"invoices"`
	// ChargeImmediately will create first invoice no matter
	// what the startat value is
	ChargeImmediately  bool   `json:"charge_immediately"`
	LastCreatedInvoice string `json:"last_created_invoice"`
	Status             Status `json:"-"`
}

Subscription is object recording the recurring payment

func New

func New() *Subscription

New creates empty subscription with valid UUID

func (*Subscription) MarshalJSON

func (s *Subscription) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*Subscription) Pause

func (s *Subscription) Pause(ctx context.Context, p pauser) error

Pause change the subscription status to paused and stop the schedule

func (*Subscription) Resume

func (s *Subscription) Resume(ctx context.Context, r resumer) error

Resume ...

func (*Subscription) Save

func (s *Subscription) Save(inv *invoice.Invoice) error

Save stores invoice created for subscription and renew subscription schedule

func (*Subscription) Start

func (s *Subscription) Start(ctx context.Context, c creator) error

Start will create subscription to the payment gateway and update its properties

func (*Subscription) Stop

func (s *Subscription) Stop(ctx context.Context, st stopper) error

Stop should stop subscription

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL