Documentation
¶
Overview ¶
Package sagapay provides a Go client for the SagaPay blockchain payment gateway API.
SagaPay is the world's first free, non-custodial blockchain payment gateway service provider. This package enables Go developers to integrate cryptocurrency payments without holding customer funds.
Index ¶
- Constants
- func SendErrorResponse(w http.ResponseWriter, err error)
- func SendSuccessResponse(w http.ResponseWriter)
- type APIError
- type AddressType
- type Balance
- type CheckTransactionStatusOptions
- type Client
- func (c *Client) CheckTransactionStatus(ctx context.Context, transactionType TransactionType, ...) (*TransactionStatusResponse, error)
- func (c *Client) CreateDeposit(ctx context.Context, params CreateDepositParams) (*DepositResponse, error)
- func (c *Client) CreateWithdrawal(ctx context.Context, params CreateWithdrawalParams) (*WithdrawalResponse, error)
- func (c *Client) FetchWalletBalance(ctx context.Context, address string, networkType NetworkType, ...) (*WalletBalanceResponse, error)
- func (c *Client) VerifyIPN(ctx context.Context, params VerifyIPNParams) (*VerifyIPNResponse, error)
- type Config
- type CreateDepositParams
- type CreateWithdrawalParams
- type DepositResponse
- type IPNType
- type NetworkType
- type Token
- type Transaction
- type TransactionStatus
- type TransactionStatusResponse
- type TransactionType
- type VerifyIPNParams
- type VerifyIPNResponse
- type WalletBalanceResponse
- type WebhookHandler
- type WebhookPayload
- type WithdrawalResponse
Constants ¶
const ( // DefaultBaseURL is the default base URL for the SagaPay API DefaultBaseURL = "https://api2.sagapay.net" // DefaultTimeout is the default timeout for API requests DefaultTimeout = 30 * time.Second )
Variables ¶
This section is empty.
Functions ¶
func SendErrorResponse ¶
func SendErrorResponse(w http.ResponseWriter, err error)
SendErrorResponse sends an error response for a webhook
func SendSuccessResponse ¶
func SendSuccessResponse(w http.ResponseWriter)
SendSuccessResponse sends a success response for a webhook
Types ¶
type APIError ¶
type APIError struct {
// ErrMessage is the message from the API's "error" field
ErrMessage string `json:"error"`
// Code is the HTTP status code of the error response
Code int `json:"-"`
}
APIError represents an error response from the API
type AddressType ¶
type AddressType string
AddressType represents the type of address
const ( AddressTypeTemporary AddressType = "TEMPORARY" AddressTypePermanent AddressType = "PERMANENT" )
Address types
type CheckTransactionStatusOptions ¶
CheckTransactionStatusOptions holds optional parameters for CheckTransactionStatus
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the SagaPay API client
func (*Client) CheckTransactionStatus ¶
func (c *Client) CheckTransactionStatus(ctx context.Context, transactionType TransactionType, opts CheckTransactionStatusOptions) (*TransactionStatusResponse, error)
CheckTransactionStatus gets the status of transactions by address or ID
func (*Client) CreateDeposit ¶
func (c *Client) CreateDeposit(ctx context.Context, params CreateDepositParams) (*DepositResponse, error)
CreateDeposit creates a new deposit address for receiving cryptocurrency
func (*Client) CreateWithdrawal ¶
func (c *Client) CreateWithdrawal(ctx context.Context, params CreateWithdrawalParams) (*WithdrawalResponse, error)
CreateWithdrawal creates a cryptocurrency withdrawal request
func (*Client) FetchWalletBalance ¶
func (c *Client) FetchWalletBalance(ctx context.Context, address string, networkType NetworkType, contractAddress string) (*WalletBalanceResponse, error)
FetchWalletBalance gets the balance of a specific wallet address for a token or native currency
func (*Client) VerifyIPN ¶
func (c *Client) VerifyIPN(ctx context.Context, params VerifyIPNParams) (*VerifyIPNResponse, error)
VerifyIPN verifies an IPN notification against the SagaPay API. This is the primary way to confirm that a received webhook notification is genuine.
The verify-ipn endpoint takes the API credentials in the request body rather than in headers, so this request is sent without the authentication headers.
type Config ¶
type Config struct {
// BaseURL is the base URL for the SagaPay API
BaseURL string
// APIKey is your SagaPay API key
APIKey string
// APISecret is your SagaPay API secret
APISecret string
// Timeout is the timeout for API requests
Timeout time.Duration
// HTTPClient is the HTTP client to use for API requests
HTTPClient *http.Client
}
Config contains the configuration options for the SagaPay client
type CreateDepositParams ¶
type CreateDepositParams struct {
NetworkType NetworkType `json:"networkType"`
ContractAddress string `json:"contractAddress"`
Amount string `json:"amount"`
IPNUrl string `json:"ipnUrl"`
UDF string `json:"udf,omitempty"`
Type AddressType `json:"type,omitempty"`
TransferBalance *bool `json:"transferBalance,omitempty"` // Optional: defaults to true when omitted
}
CreateDepositParams represents the parameters for creating a deposit
func (*CreateDepositParams) Validate ¶
func (p *CreateDepositParams) Validate() error
Validate validates the create deposit parameters
type CreateWithdrawalParams ¶
type CreateWithdrawalParams struct {
NetworkType NetworkType `json:"networkType"`
ContractAddress string `json:"contractAddress"`
Address string `json:"address"`
Amount string `json:"amount"`
IPNUrl string `json:"ipnUrl"`
UDF string `json:"udf,omitempty"`
}
CreateWithdrawalParams represents the parameters for creating a withdrawal
func (*CreateWithdrawalParams) Validate ¶
func (p *CreateWithdrawalParams) Validate() error
Validate validates the create withdrawal parameters
type DepositResponse ¶
type DepositResponse struct {
ID string `json:"id"`
Address string `json:"address"`
ExpiresAt *time.Time `json:"expiresAt"`
Amount string `json:"amount"`
Status TransactionStatus `json:"status"`
}
DepositResponse represents the response from creating a deposit
type IPNType ¶
type IPNType string
IPNType represents the transaction type reported in IPN (webhook) notifications. Unlike TransactionType, IPN notifications use uppercase values.
type NetworkType ¶
type NetworkType string
NetworkType represents the blockchain network type
const ( NetworkTypeERC20 NetworkType = "ERC20" NetworkTypeBEP20 NetworkType = "BEP20" NetworkTypeTRC20 NetworkType = "TRC20" NetworkTypePOLYGON NetworkType = "POLYGON" NetworkTypeSOLANA NetworkType = "SOLANA" )
Network types
type Token ¶
type Token struct {
NetworkType NetworkType `json:"networkType"`
ContractAddress string `json:"contractAddress"`
Symbol string `json:"symbol"`
Name string `json:"name"`
Decimals int `json:"decimals"`
}
Token represents a cryptocurrency token
type Transaction ¶
type Transaction struct {
ID string `json:"id"`
TransactionType TransactionType `json:"transactionType"`
Status TransactionStatus `json:"status"`
Amount string `json:"amount"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
TxHash string `json:"txHash,omitempty"`
NetworkType NetworkType `json:"networkType"`
ContractAddress string `json:"contractAddress"`
Address string `json:"address"`
UDF string `json:"udf,omitempty"`
Token Token `json:"token"`
Confirmations *int `json:"confirmations,omitempty"` // Deposit transactions only
Fee *string `json:"fee,omitempty"` // Withdrawal transactions only
ProcessedAt *time.Time `json:"processedAt,omitempty"` // Withdrawal transactions only, nullable
}
Transaction represents a cryptocurrency transaction
type TransactionStatus ¶
type TransactionStatus string
TransactionStatus represents the status of a transaction
const ( TransactionStatusPending TransactionStatus = "PENDING" TransactionStatusProcessing TransactionStatus = "PROCESSING" TransactionStatusCompleted TransactionStatus = "COMPLETED" TransactionStatusFailed TransactionStatus = "FAILED" TransactionStatusCancelled TransactionStatus = "CANCELLED" )
Transaction statuses
type TransactionStatusResponse ¶
type TransactionStatusResponse struct {
Address string `json:"address"`
TransactionType TransactionType `json:"transactionType"`
Count int `json:"count"`
Transactions []Transaction `json:"transactions"`
}
TransactionStatusResponse represents the response from checking transaction status
type TransactionType ¶
type TransactionType string
TransactionType represents the type of transaction
const ( TransactionTypeDeposit TransactionType = "deposit" TransactionTypeWithdrawal TransactionType = "withdrawal" )
Transaction types
type VerifyIPNParams ¶
type VerifyIPNParams struct {
TxnHash string `json:"txnHash"`
Type IPNType `json:"type"`
Amount string `json:"amount"`
Address string `json:"address"`
}
VerifyIPNParams represents the parameters for verifying an IPN notification
func (*VerifyIPNParams) Validate ¶
func (p *VerifyIPNParams) Validate() error
Validate validates the verify IPN parameters
type VerifyIPNResponse ¶
type VerifyIPNResponse struct {
Verified bool `json:"verified"`
}
VerifyIPNResponse represents the response from verifying an IPN notification
type WalletBalanceResponse ¶
type WalletBalanceResponse struct {
Address string `json:"address"`
NetworkType NetworkType `json:"networkType"`
ContractAddress string `json:"contractAddress"`
Token Token `json:"token"`
Balance Balance `json:"balance"`
}
WalletBalanceResponse represents the response from fetching wallet balance
type WebhookHandler ¶
type WebhookHandler struct {
// contains filtered or unexported fields
}
WebhookHandler handles SagaPay webhook (IPN) notifications
func NewWebhookHandler ¶
func NewWebhookHandler(ipnSecret string) *WebhookHandler
NewWebhookHandler creates a new webhook handler.
ipnSecret is the platform-issued IPN signing secret (an optional feature), NOT your API secret. If ipnSecret is empty, signature verification is skipped and webhook payloads are only parsed. Either way, Client.VerifyIPN is the primary check for confirming a notification is genuine.
func (*WebhookHandler) HandleRequest ¶
func (h *WebhookHandler) HandleRequest(r *http.Request) (*WebhookPayload, error)
HandleRequest processes a webhook notification from an HTTP request
func (*WebhookHandler) ProcessWebhook ¶
func (h *WebhookHandler) ProcessWebhook(body []byte, signature string) (*WebhookPayload, error)
ProcessWebhook processes a webhook notification from raw body and signature.
If the handler was created without an IPN secret, signature verification is skipped and the payload is only parsed; use Client.VerifyIPN as the primary check before trusting the notification.
func (*WebhookHandler) VerifySignature ¶
func (h *WebhookHandler) VerifySignature(payload []byte, signature string) bool
VerifySignature verifies the HMAC-SHA256 signature of a webhook payload. The signature is the value of the X-Sagapay-Signature header, either in the "sha256=<hex>" form sent by SagaPay or as bare hex.
type WebhookPayload ¶
type WebhookPayload struct {
ID string `json:"id"`
Type IPNType `json:"type"`
Status TransactionStatus `json:"status"` // Currently always COMPLETED
Address string `json:"address"`
NetworkType NetworkType `json:"networkType"`
Amount string `json:"amount"`
UDF string `json:"udf,omitempty"`
TxHash string `json:"txHash,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
WebhookPayload represents the payload sent in webhook (IPN) notifications
type WithdrawalResponse ¶
type WithdrawalResponse struct {
ID string `json:"id"`
Status TransactionStatus `json:"status"`
Fee string `json:"fee"`
}
WithdrawalResponse represents the response from creating a withdrawal