-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayment.go
52 lines (44 loc) · 1.46 KB
/
payment.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
49
50
51
52
package server
import (
"github.com/bitcoin-sv/go-paymail"
"github.com/bitcoin-sv/go-paymail/errors"
"github.com/gin-gonic/gin"
)
// GetPaymailAndCreateMetadata is a helper function to get the paymail from the request, check it in database and create the metadata based on that.
func (c *Configuration) GetPaymailAndCreateMetadata(context *gin.Context, satoshis uint64) (alias, domain string, md *RequestMetadata, ok bool) {
incomingPaymail := context.Param(PaymailAddressParamName)
// Parse, sanitize and basic validation
alias, domain, paymailAddress := paymail.SanitizePaymail(incomingPaymail)
if len(paymailAddress) == 0 {
errors.ErrorResponse(context, errors.ErrInvalidPaymail)
return
}
if !c.IsAllowedDomain(domain) {
errors.ErrorResponse(context, errors.ErrDomainUnknown)
return
}
// Start the PaymentRequest
paymentRequest := &paymail.PaymentRequest{
Satoshis: satoshis,
}
// Did we get some satoshis?
if paymentRequest.Satoshis == 0 {
errors.ErrorResponse(context, errors.ErrMissingFieldSatoshis)
return
}
// Create the metadata struct
md = CreateMetadata(context.Request, alias, domain, "")
md.PaymentDestination = paymentRequest
// Get from the data layer
foundPaymail, err := c.actions.GetPaymailByAlias(context.Request.Context(), alias, domain, md)
if err != nil {
errors.ErrorResponse(context, err)
return
}
if foundPaymail == nil {
errors.ErrorResponse(context, errors.ErrCouldNotFindPaymail)
return
}
ok = true
return
}