Documentation
¶
Index ¶
- Constants
- type BillingAddress
- type ChargeResponse
- type CreditCardDetail
- type DraftState
- type FailedState
- type Installment
- type Invoice
- func (i *Invoice) AddDiscount(value float64) error
- func (i *Invoice) AfterFind(tx *gorm.DB) error
- func (i *Invoice) Clear()
- func (i *Invoice) CreateChargeRequest(ctx context.Context, charger PaymentCharger) error
- func (i *Invoice) Fail(ctx context.Context) error
- func (i *Invoice) GetState() State
- func (i *Invoice) GetSubTotal() float64
- func (i *Invoice) GetTitle() string
- func (i *Invoice) GetTotal() float64
- func (i *Invoice) MarshalJSON() ([]byte, error)
- func (i *Invoice) Pay(ctx context.Context, transactionID string) error
- func (i *Invoice) Process(ctx context.Context) error
- func (i *Invoice) Publish(ctx context.Context) error
- func (i *Invoice) RemoveDiscount() error
- func (i *Invoice) Reset(ctx context.Context) error
- func (i *Invoice) SetItems(ctx context.Context, items []LineItem) error
- func (i *Invoice) SetState(state StateController) error
- func (Invoice) TableName() string
- func (i *Invoice) UpdatePaymentMethod(ctx context.Context, payment *Payment, finder paymentMethodFinder, ...) error
- func (i *Invoice) UpsertBillingAddress(name, email, phoneNumber string) error
- type InvoiceError
- type LineItem
- type LineItemError
- type PaidState
- type Payment
- type PaymentCharger
- type ProcessedState
- func (s *ProcessedState) Fail(i *Invoice) error
- func (s *ProcessedState) Pay(i *Invoice, transactionID string) error
- func (s *ProcessedState) Process(i *Invoice) error
- func (s *ProcessedState) Publish(i *Invoice) error
- func (s *ProcessedState) Reset(i *Invoice) error
- func (s *ProcessedState) State(i *Invoice) State
- type PublishedState
- func (s PublishedState) Fail(i *Invoice) error
- func (s PublishedState) Pay(i *Invoice, transactionID string) error
- func (s PublishedState) Process(i *Invoice) error
- func (s PublishedState) Publish(i *Invoice) error
- func (s PublishedState) Reset(i *Invoice) error
- func (s PublishedState) State(i *Invoice) State
- type State
- type StateController
Constants ¶
const ( InvoiceErrorPaymentMethodNotSet = iota InvoiceErrorBillingAddressNotSet InvoiceErrorNoMoreItemExist InvoiceErrorInvalidStateTransition InvoiceErrorNoPaymentSet InvoiceErrorInvalidDiscountValue )
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 ¶
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) 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) 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:"-"`
LineItems []LineItem `json:"items"`
Payment *Payment `json:"payment" gorm:"ForeignKey:InvoiceID"`
BillingAddress *BillingAddress `json:"billing_address" gorm:"ForeignKey:InvoiceID"`
SubscriptionID *uint64 `json:"-" gorm:"sql:index;"`
}
Invoice ...
func NewDefault ¶
func NewDefault() *Invoice
NewDefault creates new invoice with 1 day expiration time
func (*Invoice) AddDiscount ¶
AddDiscount adds discount if any to the invoice. If discount value is less than 0, error is returned.
func (*Invoice) AfterFind ¶
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) GetSubTotal ¶
GetSubTotal returns to total price of all items within the invoice
func (*Invoice) GetTotal ¶
GetTotal adds up the subtotal, tax, service fee and installment fee and deduct the discout value
func (*Invoice) Pay ¶
Pay set the PaidAt time. It later delegate the action to its state controller.
func (*Invoice) Publish ¶
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 ¶
RemoveDiscount sets the discount to 0
func (*Invoice) SetState ¶
func (i *Invoice) SetState(state StateController) error
SetState set the invoice state to the given state
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 ¶
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 ...
type LineItemError ¶
type LineItemError struct {
Code int
}
func (LineItemError) Error ¶
func (l LineItemError) Error() string
func (LineItemError) Unwrap ¶
func (l LineItemError) Unwrap() error
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) WaitingDuration ¶
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) 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) 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 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