cryptomus

package
v1.15.19 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package cryptomus implements the Cryptomus crypto-payment gateway protocol: invoice creation, invoice lookup and webhook signature verification. https://doc.cryptomus.com/

Index

Constants

View Source
const (
	StatusPaid        = "paid"
	StatusPaidOver    = "paid_over"
	StatusWrongAmount = "wrong_amount"
	StatusLocked      = "locked"
)

Payment statuses documented by Cryptomus. paid_over means the payer sent more than the invoice amount; the invoice itself is still settled in full. wrong_amount is a final state in which the payer sent less than the invoice amount, and locked is a final state in which received funds were frozen by the AML program: both hold money without covering the order.

View Source
const (
	DefaultBaseURL = "https://api.cryptomus.com"
)

Variables

This section is empty.

Functions

func FormatMoney

func FormatMoney(amount int64) string

FormatMoney renders a minor-unit amount as the two-decimal string the gateway expects.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports that the gateway answered "no such payment". Callers use it to distinguish "invoice never existed" from transport failures.

func PaidStatus

func PaidStatus(status string) bool

PaidStatus reports whether an invoice status represents a completed payment.

func ParseMoney

func ParseMoney(value string) (int64, error)

ParseMoney converts a gateway decimal amount to integer minor units. The gateway may report fiat amounts with up to eight decimal places; only trailing zeros beyond two decimals are tolerated, anything else cannot be represented in minor units and is rejected.

Types

type APIError

type APIError struct {
	HTTPStatus int
	State      int
	Message    string
}

APIError is a gateway-level failure: an HTTP error status or state != 0.

func (*APIError) Error

func (e *APIError) Error() string

type Client

type Client struct {
	Config
	// contains filtered or unexported fields
}

func NewClient

func NewClient(config Config) *Client

func (*Client) CreateInvoice

func (c *Client) CreateInvoice(order Order) (*Invoice, error)

CreateInvoice creates a hosted-checkout invoice for the order.

func (*Client) GetInvoice

func (c *Client) GetInvoice(uuid, orderNo string) (*Invoice, error)

GetInvoice fetches the invoice by gateway UUID or, when uuid is empty, by the merchant order number.

func (*Client) VerifyNotificationSign

func (c *Client) VerifyNotificationSign(body []byte) bool

VerifyNotificationSign authenticates a webhook payload. Cryptomus signs the JSON body with the sign member removed: md5(base64(json) + api_key). The remaining bytes must stay untouched, so the sign member is stripped from the raw payload instead of re-encoding it.

type Config

type Config struct {
	MerchantID string
	APIKey     string
	// BaseURL overrides the production API endpoint; tests use it.
	BaseURL string
}

type Invoice

type Invoice struct {
	UUID          string `json:"uuid"`
	OrderNo       string `json:"order_id"`
	Amount        string `json:"amount"`
	Currency      string `json:"currency"`
	PayerCurrency string `json:"payer_currency"`
	PaymentAmount string `json:"payment_amount"`
	URL           string `json:"url"`
	ExpiredAt     int64  `json:"expired_at"`
	Status        string `json:"status"`
	PaymentStatus string `json:"payment_status"`
	IsFinal       bool   `json:"is_final"`
}

Invoice is the gateway's view of a payment. The info endpoint reports the settlement state in payment_status while the create endpoint uses status; Paid accepts either so callers do not depend on which endpoint produced it.

func (*Invoice) Paid

func (i *Invoice) Paid() bool

func (*Invoice) State

func (i *Invoice) State() string

State returns the invoice's settlement state. The create endpoint reports it only in payment_status while the info endpoint fills both fields; the status field wins when both are present.

type Notification

type Notification struct {
	Type          string `json:"type"`
	UUID          string `json:"uuid"`
	OrderNo       string `json:"order_id"`
	Amount        string `json:"amount"`
	PaymentAmount string `json:"payment_amount"`
	Currency      string `json:"currency"`
	PayerCurrency string `json:"payer_currency"`
	Network       string `json:"network"`
	TxID          string `json:"txid"`
	IsFinal       bool   `json:"is_final"`
	Status        string `json:"status"`
	Sign          string `json:"sign"`
}

Notification is the webhook payload for invoice payments.

func ParseNotification

func ParseNotification(body []byte) (*Notification, error)

ParseNotification decodes a webhook payload without verifying it.

type Order

type Order struct {
	OrderNo   string
	Amount    int64
	Currency  string
	NotifyURL string
	ReturnURL string
	// Lifetime is the invoice validity in seconds; zero keeps the gateway
	// default (3600).
	Lifetime int64
}

Order describes the invoice to create. Amount is in minor units of Currency.

Jump to

Keyboard shortcuts

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