feat: configure webhook
This commit is contained in:
parent
e80e75cb8c
commit
44e9315706
|
@ -38,6 +38,9 @@ The application uses the following environment variables:
|
||||||
MAIL_USER, MAIL_PASSWORD, MAIL_HOST, MAIL_PORT, MAIL_FROM, MAIL_TEMPLATES_DIR: Configuration for the mail service.
|
MAIL_USER, MAIL_PASSWORD, MAIL_HOST, MAIL_PORT, MAIL_FROM, MAIL_TEMPLATES_DIR: Configuration for the mail service.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Webhook configuration
|
||||||
|
If you want some http endpoint to be called by **POST** you have to set up `WEBHOOK_URL` as environment variable with absolute URL of your endpoint. After payment is completed by client, order will be send through http `POST`.
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
The application uses several external packages:
|
The application uses several external packages:
|
||||||
|
|
|
@ -80,7 +80,6 @@ func main() {
|
||||||
|
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
|
|
||||||
log.Println("on shutdown")
|
|
||||||
if restServer != nil {
|
if restServer != nil {
|
||||||
if err := restServer.Shutdown(); err != nil {
|
if err := restServer.Shutdown(); err != nil {
|
||||||
panic(fmt.Errorf("restServer.Shutdown: %w", err))
|
panic(fmt.Errorf("restServer.Shutdown: %w", err))
|
||||||
|
|
|
@ -108,6 +108,15 @@ func (s *RestServer) onNotification(ctx context.Context, notifChan chan domain.N
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// notify
|
||||||
|
if s.config.WebhookUrl != "" {
|
||||||
|
go func() {
|
||||||
|
if err := s.orderSrv.Webhook(ctx, s.config.WebhookUrl, order); err != nil {
|
||||||
|
log.Println("orderSrv.Webhook:", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
// Send email to client and provider
|
// Send email to client and provider
|
||||||
if err := s.mailSrv.SendProviderConfirm(mail.SendOK{
|
if err := s.mailSrv.SendProviderConfirm(mail.SendOK{
|
||||||
Tx: order.Tx,
|
Tx: order.Tx,
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Order struct {
|
type Order struct {
|
||||||
ID primitive.ObjectID `bson:"_id" json:"_id"`
|
ID primitive.ObjectID `bson:"_id" json:"-"`
|
||||||
OrderID string `json:"order_id"`
|
OrderID string `bson:"order_id" json:"order_id"`
|
||||||
ClientID string `json:"client_id"`
|
ClientID string `bson:"client_id" json:"client_id"`
|
||||||
Email string `json:"email"`
|
Email string `bson:"email" json:"email"`
|
||||||
Amount float64 `bson:"amount" json:"amount"`
|
Amount float64 `bson:"amount" json:"amount"`
|
||||||
Tx string `bson:"tx" json:"tx"`
|
Tx string `bson:"tx" json:"tx"`
|
||||||
Block string `bson:"block" json:"block"`
|
Block string `bson:"block" json:"block"`
|
||||||
|
|
|
@ -2,7 +2,6 @@ package btc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BitcoinService struct {
|
type BitcoinService struct {
|
||||||
|
@ -10,7 +9,6 @@ type BitcoinService struct {
|
||||||
auth string
|
auth string
|
||||||
zmqAddress string
|
zmqAddress string
|
||||||
walletAddress string
|
walletAddress string
|
||||||
client *http.Client
|
|
||||||
testNet bool
|
testNet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +18,6 @@ func NewBitcoinService(host, auth, zmqAddress, walletAddress string) *BitcoinSer
|
||||||
auth: auth,
|
auth: auth,
|
||||||
zmqAddress: zmqAddress,
|
zmqAddress: zmqAddress,
|
||||||
walletAddress: walletAddress,
|
walletAddress: walletAddress,
|
||||||
client: &http.Client{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
from, err := bs.getAddressGroupings(false)
|
from, err := bs.getAddressGroupings(false)
|
||||||
|
|
|
@ -1,7 +1,13 @@
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
|
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
|
||||||
|
@ -14,12 +20,14 @@ const defaultExpirationTime = time.Minute * 30
|
||||||
type Order struct {
|
type Order struct {
|
||||||
repo *order.Repo
|
repo *order.Repo
|
||||||
expiration time.Duration
|
expiration time.Duration
|
||||||
|
client *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOrder(repo *order.Repo) *Order {
|
func NewOrder(repo *order.Repo) *Order {
|
||||||
return &Order{
|
return &Order{
|
||||||
repo: repo,
|
repo: repo,
|
||||||
expiration: defaultExpirationTime,
|
expiration: defaultExpirationTime,
|
||||||
|
client: &http.Client{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +35,6 @@ func (o *Order) WithExpiration(expiration time.Duration) *Order {
|
||||||
o.expiration = expiration
|
o.expiration = expiration
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Order) NewOrder(ctx context.Context, orderID, clientID, email string, amount float64) (*domain.Order, error) {
|
func (o *Order) NewOrder(ctx context.Context, orderID, clientID, email string, amount float64) (*domain.Order, error) {
|
||||||
order := domain.Order{
|
order := domain.Order{
|
||||||
ID: primitive.NewObjectID(),
|
ID: primitive.NewObjectID(),
|
||||||
|
@ -54,3 +61,43 @@ func (o *Order) FromAmount(ctx context.Context, amount float64, timestamp time.T
|
||||||
func (o *Order) OrderCompleted(ctx context.Context, order *domain.Order) (*domain.Order, error) {
|
func (o *Order) OrderCompleted(ctx context.Context, order *domain.Order) (*domain.Order, error) {
|
||||||
return o.repo.OrderCompleted(ctx, order)
|
return o.repo.OrderCompleted(ctx, order)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *Order) Webhook(ctx context.Context, webhookUrl string, order *domain.Order) error {
|
||||||
|
if webhookUrl == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
reqBody, err := json.Marshal(order)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := bytes.NewReader(reqBody)
|
||||||
|
client := &http.Client{}
|
||||||
|
req, err := http.NewRequest(http.MethodPost, webhookUrl, payload)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("Accept", "application/json")
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("client.Do: %w", err)
|
||||||
|
}
|
||||||
|
if res.StatusCode != http.StatusOK {
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Join(fmt.Errorf("response status code is: %d", res.StatusCode), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("response status code is: %d | response body %s body", res.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ type Config struct {
|
||||||
RpcAuth string `required:"true" split_words:"true"`
|
RpcAuth string `required:"true" split_words:"true"`
|
||||||
RpcHost string `required:"true" split_words:"true"`
|
RpcHost string `required:"true" split_words:"true"`
|
||||||
WalletAddress string `required:"true" split_words:"true"`
|
WalletAddress string `required:"true" split_words:"true"`
|
||||||
|
WebhookUrl string `required:"false" split_words:"true"`
|
||||||
ApiPort string `required:"true" split_words:"true"`
|
ApiPort string `required:"true" split_words:"true"`
|
||||||
Views string `required:"true" split_words:"true"`
|
Views string `required:"true" split_words:"true"`
|
||||||
ConversorApi string `required:"true" split_words:"true"`
|
ConversorApi string `required:"true" split_words:"true"`
|
||||||
|
|
Loading…
Reference in New Issue