feat: configure webhook
This commit is contained in:
@@ -2,7 +2,6 @@ package btc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BitcoinService struct {
|
||||
@@ -10,7 +9,6 @@ type BitcoinService struct {
|
||||
auth string
|
||||
zmqAddress string
|
||||
walletAddress string
|
||||
client *http.Client
|
||||
testNet bool
|
||||
}
|
||||
|
||||
@@ -20,7 +18,6 @@ func NewBitcoinService(host, auth, zmqAddress, walletAddress string) *BitcoinSer
|
||||
auth: auth,
|
||||
zmqAddress: zmqAddress,
|
||||
walletAddress: walletAddress,
|
||||
client: &http.Client{},
|
||||
}
|
||||
|
||||
from, err := bs.getAddressGroupings(false)
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
|
||||
@@ -14,12 +20,14 @@ const defaultExpirationTime = time.Minute * 30
|
||||
type Order struct {
|
||||
repo *order.Repo
|
||||
expiration time.Duration
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewOrder(repo *order.Repo) *Order {
|
||||
return &Order{
|
||||
repo: repo,
|
||||
expiration: defaultExpirationTime,
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +35,6 @@ func (o *Order) WithExpiration(expiration time.Duration) *Order {
|
||||
o.expiration = expiration
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *Order) NewOrder(ctx context.Context, orderID, clientID, email string, amount float64) (*domain.Order, error) {
|
||||
order := domain.Order{
|
||||
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) {
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user