Crypto Pay is a payment system based on @CryptoBot, which allows you to accept payments in cryptocurrency using the API.
This library help you to work with Crypto Pay via Crypto Pay API.
go get -u github.com/vitaliy-ukiru/go-cryptopayFor start, you need to create your application and get an API token.
Open @CryptoBot
or @CryptoTestnetBot (for testnet), send a command /pay to
create a new app and get API Token.
In this library, the high-level is the Client.
Internally, it calls the ApiCore methods, which is lower-level and is essentially a gateway for API
requests. The Client methods, in addition to the usual errors, can return an api error (ApiError). If
you want to check whether the received error is such, call the GetApiError function.
if apiErr := cryptopay.GetApiError(err); apiErr != nil {
// handling error of api.
// apiErr is *ApiError
}- Token - token of you app.
- ApiHost - url to api host. Default mainnet.
- HttpClient - client for make requests. Default
http.DefaultClient. - Webhook - webhook configure
- OnError - handler for error handling in webhook.
- DefaultHandler - set of default handlers. Default empty.
| Net | Bot | Hostname | Code reference |
|---|---|---|---|
| mainnet | @CryptoBot | https://pay.crypt.bot/ | cryptopay.MainNetHost |
| testnet | @CryptoTestnetBot | https://testnet-pay.crypt.bot/ | cryptopay.TestNetHost |
To get started, send /pay command to bot, choose "My Apps", select application, open "Webhooks" and set
your endpoint.
To work with webhooks, you need to start the server yourself and install Webhook.ServeHTTP as a handler
to the endpoint. If you are running a "net/http" server, you can pass the Webhook as http.Handler
type. But if you don't use std server, see the Adaptation section.
getMe
package main
import (
"fmt"
"github.com/vitaliy-ukiru/go-cryptopay"
)
func main() {
client := cryptopay.NewClient(cryptopay.ClientSettings{
Token: "your_token_here",
ApiHost: cryptopay.TestNetHost,
})
app, err := client.GetMe()
if err != nil {
panic(err)
}
fmt.Printf(
"app_id=%d; name=%q; payment_bot=%q",
app.Id,
app.Name,
app.PaymentBotUsername,
)
}transfer
package main
import (
"fmt"
"time"
"github.com/vitaliy-ukiru/go-cryptopay"
)
func main() {
client := cryptopay.NewClient(cryptopay.ClientSettings{
Token: "your_token",
})
transfer, err := client.DoTransfer(-1, cryptopay.USDT, 100, "generate unique data", cryptopay.DoTransferOptions{
Comment: "You winner!",
})
if err != nil {
panic(err)
}
fmt.Printf("Transfer completed at %s", transfer.CompletedAt.Format(time.RFC850))
}webhook
package main
import (
"fmt"
"net/http"
"time"
"github.com/vitaliy-ukiru/go-cryptopay"
)
func main() {
client := cryptopay.NewClient(cryptopay.ClientSettings{
Token: "your_token", // token required for webhooks, because using for verification updates
Webhook: cryptopay.WebhookSettings{
OnError: func(_ *http.Request, err error) {
panic(err)
},
},
})
client.OnInvoicePaid(func(update *cryptopay.WebhookUpdate) {
invoice := update.Payload
fmt.Printf(
"Invoice № %d for %s %s was paid on %s",
invoice.Id,
invoice.Amount,
invoice.Asset,
invoice.PaidAt.Format(time.RFC850))
})
}If you use other router you can adapt. For this you must create handler that call ServeHTTP method.
For gin-gonic/gin:
// router is gin.Engine
router.POST("/path/", func (c *gin.Context) {
webhook.ServerHTTP(http.ResponseWriter(c.Writer), c.Request)
})// router is httprouter.Router.
router.POST("/path", func (w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
webhook.ServerHTTP(w, r)
})For gorilla/mux
// router is mux.Router
router.Handle("/path", webhook)