forked from tigwyk/accept-banano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
48 lines (43 loc) · 1.73 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"time"
"github.com/tigwyk/accept-banano/internal/units"
"github.com/shopspring/decimal"
)
// Response that we return from API endpoints.
type Response struct {
Token string `json:"token"`
Account string `json:"account"`
Amount decimal.Decimal `json:"amount"`
AmountInCurrency decimal.Decimal `json:"amountInCurrency"`
Currency string `json:"currency"`
Balance decimal.Decimal `json:"balance"`
SubPayments map[string]SubPaymentResponse `json:"subPayments"`
RemainingSeconds int `json:"remainingSeconds"`
State string `json:"state"`
Fulfilled bool `json:"fulfilled"`
MerchantNotified bool `json:"merchantNotified"`
}
type SubPaymentResponse struct {
Amount decimal.Decimal `json:"amount"`
Account string `json:"account"`
}
func NewResponse(p *Payment, token string) *Response {
subPayments := make(map[string]SubPaymentResponse, len(p.SubPayments))
for k, v := range p.SubPayments {
subPayments[k] = SubPaymentResponse{Account: v.Account, Amount: units.RawToBanano(v.Amount)}
}
return &Response{
Token: token,
Account: p.account,
Amount: units.RawToBanano(p.Amount),
AmountInCurrency: p.AmountInCurrency,
Currency: p.Currency,
Balance: units.RawToBanano(p.Balance),
State: p.State,
SubPayments: subPayments,
RemainingSeconds: int(p.remainingDuration() / time.Second),
Fulfilled: p.FulfilledAt != nil,
MerchantNotified: p.NotifiedAt != nil,
}
}