invoice

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 2020 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

View Source
const (
	InvoiceErrorPaymentMethodNotSet = iota
	InvoiceErrorBillingAddressNotSet
	InvoiceErrorNoMoreItemExist
	InvoiceErrorInvalidStateTransition
	InvoiceErrorNoPaymentSet
	InvoiceErrorInvalidDiscountValue
)
View Source
const (
	LineItemErrInvalidQty = iota
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BillingAddress

type BillingAddress struct {
	payment.Model
	FullName    string `json:"name"`
	Email       string `json:"email"`
	PhoneNumber string `json:"phone_number"`
	InvoiceID   uint64 `json:"-" gorm:"index:bil_addr_invoice_id_k;not null"`
}

BillingAddress stores information about account making the payment

func NewBillingAddress

func NewBillingAddress(fullName, email, phoneNumber string) (*BillingAddress, error)

NewBillingAddress ...

func (BillingAddress) TableName

func (BillingAddress) TableName() string

func (*BillingAddress) Update

func (b *BillingAddress) Update(name, email, phoneNumber string) error

type ChargeResponse

type ChargeResponse struct {
	TransactionID string
	PaymentToken  string
	PaymentURL    string
}

ChargeResponse stores the important data after a payment charge request has been created

type CreditCardDetail

type CreditCardDetail struct {
	payment.Model
	PaymentID   uint64       `json:"-" gorm:"index:cc_payment_id_k"`
	Installment Installment  `json:"installment" gorm:"embedded;embedded_prefix:installment_"`
	Bank        payment.Bank `json:"bank"`
}

func (CreditCardDetail) TableName

func (CreditCardDetail) TableName() string

type DraftState

type DraftState struct {
}

func (DraftState) Fail

func (s DraftState) Fail(i *Invoice) error

func (DraftState) Pay

func (s DraftState) Pay(i *Invoice, transactionID string) error

func (DraftState) Process

func (s DraftState) Process(i *Invoice) error

func (DraftState) Publish

func (s DraftState) Publish(i *Invoice) error

func (DraftState) Reset

func (s DraftState) Reset(i *Invoice) error

func (DraftState) State

func (s DraftState) State(i *Invoice) State

type FailedState

type FailedState struct {
}

func (*FailedState) Fail

func (s *FailedState) Fail(i *Invoice) error

func (*FailedState) Pay

func (s *FailedState) Pay(i *Invoice, transactionID string) error

func (*FailedState) Process

func (s *FailedState) Process(i *Invoice) error

func (*FailedState) Publish

func (s *FailedState) Publish(i *Invoice) error

func (*FailedState) Reset

func (s *FailedState) Reset(i *Invoice) error

func (*FailedState) State

func (s *FailedState) State(i *Invoice) State

type Installment

type Installment struct {
	Type payment.InstallmentType `json:"type"`
	Term int                     `json:"term"`
}

type Invoice

type Invoice struct {
	payment.Model
	Title           string          `json:"-"`
	Number          string          `json:"number" gorm:"unique_index:inv_number_k"`
	InvoiceDate     time.Time       `json:"invoice_date"`
	DueDate         time.Time       `json:"due_date"`
	PaidAt          *time.Time      `json:"paid_at"`
	Currency        string          `json:"-"`
	SubTotal        float64         `json:"-"`
	Discount        float64         `json:"-"`
	Tax             float64         `json:"-"`
	ServiceFee      float64         `json:"-"`
	InstallmentFee  float64         `json:"-"`
	State           State           `json:"-"`
	StateController StateController `json:"-" gorm:"-"`
	LineItem        *LineItem       `json:"item"`
	Payment         *Payment        `json:"payment" gorm:"ForeignKey:InvoiceID"`
	BillingAddress  *BillingAddress `json:"billing_address" gorm:"ForeignKey:InvoiceID"`
}

Invoice ...

func New

func New(invoiceDate, dueDate time.Time) *Invoice

New accept invoice start and due date and return a new invoice in a draft state.

func NewDefault

func NewDefault() *Invoice

NewDefault creates new invoice with 1 day expiration time

func (*Invoice) AddDiscount

func (i *Invoice) AddDiscount(value float64) error

AddDiscount adds discount if any to the invoice. If discount value is less than 0, error is returned.

func (*Invoice) AfterFind

func (i *Invoice) AfterFind() error

AfterFind assign a state controller after the entity is fetched from database

func (*Invoice) Clear

func (i *Invoice) Clear()

Clear remove all invoice values (subtotal, discount, tax, fee, line item and payment method) and reset the state to draft

func (*Invoice) CreateChargeRequest

func (i *Invoice) CreateChargeRequest(ctx context.Context, charger PaymentCharger) error

CreateChargeRequest will use `charger` to create a charge request to the payment gateway and update invoice payment attributes

func (*Invoice) Fail

func (i *Invoice) Fail(ctx context.Context) error

Fail delegates the action to its state controller

func (*Invoice) GetState

func (i *Invoice) GetState() State

GetState returns the current state of invoice

func (*Invoice) GetSubTotal

func (i *Invoice) GetSubTotal() float64

GetSubTotal returns to total price of all items within the invoice

func (*Invoice) GetTitle

func (i *Invoice) GetTitle() string

GetTitle ...

func (*Invoice) GetTotal

func (i *Invoice) GetTotal() float64

GetTotal adds up the subtotal, tax, service fee and installment fee and deduct the discout value

func (*Invoice) MarshalJSON

func (i *Invoice) MarshalJSON() ([]byte, error)

MarshalJSON ...

func (*Invoice) Pay

func (i *Invoice) Pay(ctx context.Context, transactionID string) error

Pay set the PaidAt time. It later delegate the action to its state controller.

func (*Invoice) Process

func (i *Invoice) Process(ctx context.Context) error

Process delegates the action to its state controller

func (*Invoice) Publish

func (i *Invoice) Publish(ctx context.Context) error

Publish checks whether payment and billing address of invoice are set. Then, it reset the invoice date to the the time of this methods is used. It later delegate the action to its state controller.

func (*Invoice) RemoveDiscount

func (i *Invoice) RemoveDiscount() error

RemoveDiscount sets the discount to 0

func (*Invoice) Reset

func (i *Invoice) Reset(ctx context.Context) error

Reset changes the invoice invoice so that it can be used again

func (*Invoice) SetItem

func (i *Invoice) SetItem(ctx context.Context, item LineItem) error

SetItem set the informations of the invoice item

func (*Invoice) SetState

func (i *Invoice) SetState(state StateController) error

SetState set the invoice state to the given state

func (Invoice) TableName

func (Invoice) TableName() string

TableName returns the table name used for gorm

func (*Invoice) UpdatePaymentMethod

func (i *Invoice) UpdatePaymentMethod(ctx context.Context, payment *Payment, finder paymentMethodFinder, opts ...payment.Option) error

UpdatePaymentMethod set the payment method and recalculate the service and installment fee of the invoice

func (*Invoice) UpsertBillingAddress

func (i *Invoice) UpsertBillingAddress(name, email, phoneNumber string) error

UpsertBillingAddress create new billing address or update the existing one if it exist

type InvoiceError

type InvoiceError struct {
	Code int
}

func (InvoiceError) Error

func (e InvoiceError) Error() string

func (InvoiceError) Unwrap

func (e InvoiceError) Unwrap() error

type LineItem

type LineItem struct {
	payment.Model
	InvoiceID    uint64  `json:"-" gorm:"index:line_item_invoice_id_k"`
	Name         string  `json:"name"`
	Description  string  `json:"description" gorm:"not null;type:text"`
	Category     string  `json:"category"`
	MerchantName string  `json:"merchant_name"`
	Currency     string  `json:"currency"`
	UnitPrice    float64 `json:"unit_price"`
	Qty          int     `json:"qty"`
}

LineItem ...

func NewLineItem

func NewLineItem(
	name, category, merchant, description string,
	unitPrice float64,
	qty int,
	currency string,
) *LineItem

NewLineItem ...

func (*LineItem) DecreaseQty

func (i *LineItem) DecreaseQty() error

DecreaseQty ...

func (*LineItem) IncreaseQty

func (i *LineItem) IncreaseQty() error

IncreaseQty ...

func (LineItem) SubTotal

func (i LineItem) SubTotal() float64

SubTotal ...

func (LineItem) TableName

func (LineItem) TableName() string

type LineItemError

type LineItemError struct {
	Code int
}

func (LineItemError) Error

func (l LineItemError) Error() string

func (LineItemError) Unwrap

func (l LineItemError) Unwrap() error

type PaidState

type PaidState struct {
}

func (*PaidState) Fail

func (s *PaidState) Fail(i *Invoice) error

func (*PaidState) Pay

func (s *PaidState) Pay(i *Invoice, transactionID string) error

func (*PaidState) Process

func (s *PaidState) Process(i *Invoice) error

func (*PaidState) Publish

func (s *PaidState) Publish(i *Invoice) error

func (*PaidState) Reset

func (s *PaidState) Reset(i *Invoice) error

func (*PaidState) State

func (s *PaidState) State(i *Invoice) State

type Payment

type Payment struct {
	payment.Model
	Gateway          string              `json:"gateway" gorm:"not null"`
	PaymentType      payment.PaymentType `json:"payment_type"`
	Token            string              `json:"token"`
	RedirectURL      string              `json:"redirect_url" gorm:"type:text"`
	InvoiceID        uint64              `json:"-" gorm:"sql:index;"`
	TransactionID    string              `json:"transaction_id" gorm:"sql:index;"`
	WaitingTimeMS    *int64              `json:"-"`
	CreditCardDetail *CreditCardDetail   `json:"credit_card,omitempty" gorm:"ForeignKey:PaymentID"`
}

func NewPayment

func NewPayment(cfg config.FeeConfigReader, payType payment.PaymentType, ccDetail *CreditCardDetail) (*Payment, error)

func (*Payment) Reset

func (p *Payment) Reset() error

func (Payment) TableName

func (Payment) TableName() string

func (*Payment) WaitingDuration

func (p *Payment) WaitingDuration() *time.Duration

type PaymentCharger

type PaymentCharger interface {
	Create(ctx context.Context, inv *Invoice) (*ChargeResponse, error)
	Gateway() payment.Gateway
}

PaymentCharger will call the API

type ProcessedState

type ProcessedState struct {
}

func (*ProcessedState) Fail

func (s *ProcessedState) Fail(i *Invoice) error

func (*ProcessedState) Pay

func (s *ProcessedState) Pay(i *Invoice, transactionID string) error

func (*ProcessedState) Process

func (s *ProcessedState) Process(i *Invoice) error

func (*ProcessedState) Publish

func (s *ProcessedState) Publish(i *Invoice) error

func (*ProcessedState) Reset

func (s *ProcessedState) Reset(i *Invoice) error

func (*ProcessedState) State

func (s *ProcessedState) State(i *Invoice) State

type PublishedState

type PublishedState struct {
}

func (PublishedState) Fail

func (s PublishedState) Fail(i *Invoice) error

func (PublishedState) Pay

func (s PublishedState) Pay(i *Invoice, transactionID string) error

func (PublishedState) Process

func (s PublishedState) Process(i *Invoice) error

func (PublishedState) Publish

func (s PublishedState) Publish(i *Invoice) error

func (PublishedState) Reset

func (s PublishedState) Reset(i *Invoice) error

func (PublishedState) State

func (s PublishedState) State(i *Invoice) State

type State

type State int
const (
	Draft State = iota
	Published
	WaitForPayment
	Failed
)

func (State) String

func (s State) String() string

type StateController

type StateController interface {
	State(i *Invoice) State
	Publish(i *Invoice) error
	Process(i *Invoice) error
	Pay(i *Invoice, transactionID string) error
	Fail(i *Invoice) error
	Reset(i *Invoice) error
}

InvoiceStateController ...

func NewState

func NewState(state string) StateController

Jump to

Keyboard shortcuts

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