payments

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SOL  = "So11111111111111111111111111111111111111112"
	USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"
	USDT = "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"
)
View Source
const (
	TastMarkPaymentsAsExpired     = "mark_payments_as_expired"
	TaskCheckPaymentByReference   = "check_payment_by_reference"
	TaskMarkTransactionsAsExpired = "mark_transactions_as_expired"
	TaskCheckPendingTransactions  = "check_pending_transactions"
)

Task names.

Variables

This section is empty.

Functions

func IsSOL

func IsSOL(currency string) bool

IsSOL checks if the currency is SOL.

func MintAddress

func MintAddress(currency string, fallback string) string

MintAddress returns the mint address by symbol. If the symbol is not found, it returns the fallback address. Supports only default mints.

func ReferenceAccountNotificationListener

func ReferenceAccountNotificationListener(service PaymentService, enq eventsEnqueuer) events.Listener

ReferenceAccountNotificationListener is a listener for the transaction.reference.notification event.

func TransactionCreatedListener

func TransactionCreatedListener(service PaymentService, enq eventsEnqueuer) events.Listener

TransactionCreatedListener is a listener for the transaction.created event.

func UpdateTransactionStatusListener

func UpdateTransactionStatusListener(service PaymentService) events.Listener

UpdateTransactionStatusListener is a listener for the transaction.updated event.

Types

type Config

type Config struct {
	ApplyBonus           bool
	BonusMintAddress     string
	BonusAuthAccount     string
	MaxApplyBonusAmount  uint64
	MaxApplyBonusPercent uint16 // 10000 = 100%, 100 = 1%, 1 = 0.01%
	AccrueBonus          bool
	AccrueBonusRate      uint64
	DestinationMint      string
	DestinationWallet    string
	PaymentTTL           time.Duration
	SolPayBaseURL        string
}

type Enqueuer

type Enqueuer struct {
	// contains filtered or unexported fields
}

Enqueuer is a helper struct for enqueuing email tasks.

func NewEnqueuer

func NewEnqueuer(client *asynq.Client, opt ...EnqueuerOption) *Enqueuer

NewEnqueuer creates a new email enqueuer. This function accepts EnqueuerOption to configure the enqueuer. Default values are used if no option is provided. Default values are:

  • queue name: "default"
  • task deadline: 1 minute
  • max retry: 3

func (*Enqueuer) CheckPaymentByReference

func (e *Enqueuer) CheckPaymentByReference(ctx context.Context, reference string) error

FireEvent enqueues a task to fire an event. This function returns an error if the task could not be enqueued.

type EnqueuerOption

type EnqueuerOption func(*Enqueuer)

EnqueuerOption is a function that configures an enqueuer.

func WithMaxRetry

func WithMaxRetry(n int) EnqueuerOption

WithMaxRetry configures the max retry.

func WithQueueName

func WithQueueName(name string) EnqueuerOption

WithQueueName configures the queue name.

func WithTaskDeadline

func WithTaskDeadline(d time.Duration) EnqueuerOption

WithTaskDeadline configures the task deadline.

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Errorf(format string, args ...interface{})
}

type Payment

type Payment struct {
	ID                uuid.UUID     `json:"id,omitempty"`
	ExternalID        string        `json:"external_id,omitempty"`
	DestinationWallet string        `json:"destination_wallet,omitempty"`
	DestinationMint   string        `json:"destination_mint,omitempty"`
	Amount            uint64        `json:"amount,omitempty"`
	Status            PaymentStatus `json:"status,omitempty"`
	Message           string        `json:"message,omitempty"`
	ExpiresAt         *time.Time    `json:"expires_at,omitempty"`
}

Payment represents an initial payment request.

type PaymentBuilder

type PaymentBuilder struct {
	// contains filtered or unexported fields
}

PaymentBuilder is a builder for creating a payment transaction.

func NewPaymentTransactionBuilder

func NewPaymentTransactionBuilder(sc solanaClient, jc jupiterClient, config Config) *PaymentBuilder

NewPaymentTransactionBuilder creates a new PaymentTransactionBuilder.

func (*PaymentBuilder) Build

Build builds the payment transaction.

func (*PaymentBuilder) GetReferenceAddress

func (b *PaymentBuilder) GetReferenceAddress() string

GetReferenceAddress returns the reference address.

func (*PaymentBuilder) SetTransaction

func (b *PaymentBuilder) SetTransaction(tx *Transaction, p *Payment) *PaymentBuilder

SetTransaction sets the transaction.

type PaymentService

type PaymentService interface {
	// CreatePayment creates a new payment.
	CreatePayment(ctx context.Context, payment *Payment) (*Payment, error)
	// GetPayment returns the payment with the given ID.
	GetPayment(ctx context.Context, id uuid.UUID) (*Payment, error)
	// GetPaymentByExternalID returns the payment with the given external ID.
	GetPaymentByExternalID(ctx context.Context, externalID string) (*Payment, error)
	// GeneratePaymentLink generates a new payment link for the given payment.
	GeneratePaymentLink(ctx context.Context, paymentID uuid.UUID, mint string, applyBonus bool) (string, error)
	// UpdatePaymentStatus updates the status of the payment with the given ID.
	UpdatePaymentStatus(ctx context.Context, id uuid.UUID, status PaymentStatus) error
	// CancelPayment cancels the payment with the given ID.
	CancelPayment(ctx context.Context, id uuid.UUID) error
	// CancelPaymentByExternalID cancels the payment with the given external ID.
	CancelPaymentByExternalID(ctx context.Context, externalID string) error
	// MarkPaymentsAsExpired marks all payments that are expired as expired.
	MarkPaymentsAsExpired(ctx context.Context) error
	// BuildTransaction builds a new transaction for the given payment.
	BuildTransaction(ctx context.Context, tx *Transaction) (*Transaction, error)
	// GetTransactionByReference returns the transaction with the given reference.
	GetTransactionByReference(ctx context.Context, reference string) (*Transaction, error)
	// UpdateTransaction updates the status and signature of the transaction with the given reference.
	UpdateTransaction(ctx context.Context, reference string, status TransactionStatus, signature string) error
	// GetPendingTransactions returns all pending transactions.
	GetPendingTransactions(ctx context.Context) ([]*Transaction, error)
	// MarkTransactionsAsExpired marks all transactions that are expired as expired.
	MarkTransactionsAsExpired(ctx context.Context) error
}

PaymentService is the interface that wraps the basic payment operations.

type PaymentStatus

type PaymentStatus string

PaymentStatus represents the status of a payment.

const (
	PaymentStatusNew       PaymentStatus = "new"
	PaymentStatusPending   PaymentStatus = "pending"
	PaymentStatusCompleted PaymentStatus = "completed"
	PaymentStatusFailed    PaymentStatus = "failed"
	PaymentStatusCanceled  PaymentStatus = "canceled"
	PaymentStatusExpired   PaymentStatus = "expired"
)

Predefined payment statuses.

type ReferencePayload

type ReferencePayload struct {
	Reference string `json:"reference"`
}

Reference payload to check payment by reference task.

type Scheduler

type Scheduler struct{}

Scheduler is a task scheduler for iam service.

func NewScheduler

func NewScheduler() *Scheduler

NewScheduler creates a new task scheduler for iam service.

func (*Scheduler) Schedule

func (s *Scheduler) Schedule(scheduler *asynq.Scheduler)

Schedule tasks for auth service.

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(repo paymentRepository, sol solanaClient, jup jupiterClient, conf Config) *Service

NewService creates a new payment service instance.

func (*Service) BuildTransaction

func (s *Service) BuildTransaction(ctx context.Context, tx *Transaction) (*Transaction, error)

BuildTransaction builds a new transaction for the given payment.

func (*Service) CancelPayment

func (s *Service) CancelPayment(ctx context.Context, id uuid.UUID) error

CancelPayment cancels the payment with the given ID.

func (*Service) CancelPaymentByExternalID

func (s *Service) CancelPaymentByExternalID(ctx context.Context, externalID string) error

CancelPaymentByExternalID cancels the payment with the given external ID.

func (*Service) CreatePayment

func (s *Service) CreatePayment(ctx context.Context, payment *Payment) (*Payment, error)

CreatePayment creates a new payment.

func (s *Service) GeneratePaymentLink(ctx context.Context, paymentID uuid.UUID, mint string, applyBonus bool) (string, error)

GeneratePaymentLink generates a new payment link for the given payment.

func (*Service) GetPayment

func (s *Service) GetPayment(ctx context.Context, id uuid.UUID) (*Payment, error)

GetPayment returns the payment with the given ID.

func (*Service) GetPaymentByExternalID

func (s *Service) GetPaymentByExternalID(ctx context.Context, externalID string) (*Payment, error)

GetPaymentByExternalID returns the payment with the given external ID.

func (*Service) GetPendingTransactions

func (s *Service) GetPendingTransactions(ctx context.Context) ([]*Transaction, error)

GetPendingTransactions returns all pending transactions.

func (*Service) GetTransactionByReference

func (s *Service) GetTransactionByReference(ctx context.Context, reference string) (*Transaction, error)

GetTransactionByReference returns the transaction with the given reference.

func (*Service) MarkPaymentsAsExpired

func (s *Service) MarkPaymentsAsExpired(ctx context.Context) error

MarkPaymentsAsExpired marks all payments that are expired as expired.

func (*Service) MarkTransactionsAsExpired

func (s *Service) MarkTransactionsAsExpired(ctx context.Context) error

MarkTransactionsAsExpired marks all transactions that are expired as expired.

func (*Service) UpdatePaymentStatus

func (s *Service) UpdatePaymentStatus(ctx context.Context, id uuid.UUID, status PaymentStatus) error

UpdatePaymentStatus updates the status of the payment with the given ID.

func (*Service) UpdateTransaction

func (s *Service) UpdateTransaction(ctx context.Context, reference string, status TransactionStatus, signature string) error

UpdateTransaction updates the status and signature of the transaction with the given reference.

type ServiceEvents

type ServiceEvents struct {
	PaymentService
	// contains filtered or unexported fields
}

func NewServiceEvents

func NewServiceEvents(svc PaymentService, eventFn fireEventFunc) *ServiceEvents

func (*ServiceEvents) BuildTransaction

func (s *ServiceEvents) BuildTransaction(ctx context.Context, tx *Transaction) (*Transaction, error)

BuildTransaction builds a new transaction for the given payment.

func (*ServiceEvents) CancelPayment

func (s *ServiceEvents) CancelPayment(ctx context.Context, id uuid.UUID) error

CancelPayment cancels the payment with the given ID.

func (*ServiceEvents) CancelPaymentByExternalID

func (s *ServiceEvents) CancelPaymentByExternalID(ctx context.Context, externalID string) error

CancelPaymentByExternalID cancels the payment with the given external ID.

func (*ServiceEvents) CreatePayment

func (s *ServiceEvents) CreatePayment(ctx context.Context, payment *Payment) (*Payment, error)

CreatePayment creates a new payment.

func (s *ServiceEvents) GeneratePaymentLink(ctx context.Context, paymentID uuid.UUID, mint string, applyBonus bool) (string, error)

GeneratePaymentLink generates a new payment link for the given payment.

func (*ServiceEvents) UpdatePaymentStatus

func (s *ServiceEvents) UpdatePaymentStatus(ctx context.Context, id uuid.UUID, status PaymentStatus) error

UpdatePaymentStatus updates the status of the payment with the given ID.

func (*ServiceEvents) UpdateTransaction

func (s *ServiceEvents) UpdateTransaction(ctx context.Context, reference string, status TransactionStatus, signature string) error

UpdateTransaction updates the status and signature of the transaction with the given reference.

type ServiceLogger

type ServiceLogger struct {
	PaymentService
	// contains filtered or unexported fields
}

func NewServiceLogger

func NewServiceLogger(svc PaymentService, log Logger) *ServiceLogger

func (*ServiceLogger) BuildTransaction

func (s *ServiceLogger) BuildTransaction(ctx context.Context, tx *Transaction) (*Transaction, error)

BuildTransaction builds a new transaction for the given payment.

func (*ServiceLogger) CancelPayment

func (s *ServiceLogger) CancelPayment(ctx context.Context, id uuid.UUID) error

CancelPayment cancels the payment with the given ID.

func (*ServiceLogger) CancelPaymentByExternalID

func (s *ServiceLogger) CancelPaymentByExternalID(ctx context.Context, externalID string) error

CancelPaymentByExternalID cancels the payment with the given external ID.

func (*ServiceLogger) CreatePayment

func (s *ServiceLogger) CreatePayment(ctx context.Context, payment *Payment) (*Payment, error)

CreatePayment creates a new payment.

func (s *ServiceLogger) GeneratePaymentLink(ctx context.Context, paymentID uuid.UUID, mint string, applyBonus bool) (string, error)

GeneratePaymentLink generates a new payment link for the given payment.

func (*ServiceLogger) GetPayment

func (s *ServiceLogger) GetPayment(ctx context.Context, id uuid.UUID) (*Payment, error)

GetPayment returns the payment with the given ID.

func (*ServiceLogger) GetPaymentByExternalID

func (s *ServiceLogger) GetPaymentByExternalID(ctx context.Context, externalID string) (*Payment, error)

GetPaymentByExternalID returns the payment with the given external ID.

func (*ServiceLogger) GetPendingTransactions

func (s *ServiceLogger) GetPendingTransactions(ctx context.Context) ([]*Transaction, error)

GetPendingTransactions returns all pending transactions.

func (*ServiceLogger) GetTransactionByReference

func (s *ServiceLogger) GetTransactionByReference(ctx context.Context, reference string) (*Transaction, error)

GetTransactionByReference returns the transaction with the given reference.

func (*ServiceLogger) MarkPaymentsAsExpired

func (s *ServiceLogger) MarkPaymentsAsExpired(ctx context.Context) error

MarkPaymentsAsExpired marks all payments that are expired as expired.

func (*ServiceLogger) MarkTransactionsAsExpired

func (s *ServiceLogger) MarkTransactionsAsExpired(ctx context.Context) error

MarkTransactionsAsExpired marks all transactions that are expired as expired.

func (*ServiceLogger) UpdatePaymentStatus

func (s *ServiceLogger) UpdatePaymentStatus(ctx context.Context, id uuid.UUID, status PaymentStatus) error

UpdatePaymentStatus updates the status of the payment with the given ID.

func (*ServiceLogger) UpdateTransaction

func (s *ServiceLogger) UpdateTransaction(ctx context.Context, reference string, status TransactionStatus, signature string) error

UpdateTransaction updates the status and signature of the transaction with the given reference.

type Transaction

type Transaction struct {
	ID                 uuid.UUID         `json:"id,omitempty"`
	PaymentID          uuid.UUID         `json:"payment_id,omitempty"`
	Reference          string            `json:"reference,omitempty"`
	SourceWallet       string            `json:"source_wallet,omitempty"`
	SourceMint         string            `json:"source_mint,omitempty"`
	DestinationWallet  string            `json:"destination_wallet,omitempty"`
	DestinationMint    string            `json:"destination_mint,omitempty"`
	Amount             uint64            `json:"amount,omitempty"`
	DiscountAmount     uint64            `json:"discount_amount,omitempty"`
	TotalAmount        uint64            `json:"total_amount,omitempty"`
	AccruedBonusAmount uint64            `json:"accrued_bonus_amount,omitempty"`
	Message            string            `json:"message,omitempty"`
	Memo               string            `json:"memo,omitempty"`
	ApplyBonus         bool              `json:"apply_bonus,omitempty"`
	Transaction        string            `json:"transaction,omitempty"`
	Status             TransactionStatus `json:"status,omitempty"`
	Signature          string            `json:"signature,omitempty"`
}

type TransactionStatus

type TransactionStatus string

TransactionStatus represents the status of a transaction.

const (
	TransactionStatusPending   TransactionStatus = "pending"
	TransactionStatusCompleted TransactionStatus = "completed"
	TransactionStatusFailed    TransactionStatus = "failed"
)

Predefined transaction statuses.

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker is a task handler for email delivery.

func NewWorker

func NewWorker(svc paymentService, sol workerSolanaClient, enq paymentEnqueuer) *Worker

NewWorker creates a new payments task handler.

func (*Worker) CheckPaymentByReference

func (w *Worker) CheckPaymentByReference(ctx context.Context, t *asynq.Task) error

CheckPaymentByReference checks payment status by reference and unsubscribes from account notifications.

func (*Worker) CheckPendingTransactions

func (w *Worker) CheckPendingTransactions(ctx context.Context, t *asynq.Task) error

CheckPendingTransactions checks pending transactions.

func (*Worker) MarkPaymentsAsExpired

func (w *Worker) MarkPaymentsAsExpired(ctx context.Context, t *asynq.Task) error

FireEvent sends a webhook event to the specified URL.

func (*Worker) MarkTransactionsAsExpired

func (w *Worker) MarkTransactionsAsExpired(ctx context.Context, t *asynq.Task) error

MarkTransactionsAsExpired marks transactions as expired.

func (*Worker) Register

func (w *Worker) Register(mux *asynq.ServeMux)

Register registers task handlers for email delivery.

Jump to

Keyboard shortcuts

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