-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
p2p_payment_destination.go
73 lines (61 loc) · 2.23 KB
/
p2p_payment_destination.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package server
import (
"net/http"
"github.com/julienschmidt/httprouter"
apirouter "github.com/mrz1836/go-api-router"
"github.com/tonicpow/go-paymail"
)
/*
Incoming Data Object Example:
{
"satoshis": 1000100,
}
*/
// p2pDestination will return an output script(s) for a destination (used with SendP2PTransaction)
//
// Specs: https://docs.moneybutton.com/docs/paymail-07-p2p-payment-destination.html
func (c *Configuration) p2pDestination(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Get the params & paymail address submitted via URL request
params := apirouter.GetParams(req)
incomingPaymail := params.GetString("paymailAddress")
// Parse, sanitize and basic validation
alias, domain, paymailAddress := paymail.SanitizePaymail(incomingPaymail)
if len(paymailAddress) == 0 {
ErrorResponse(w, req, ErrorInvalidParameter, "invalid paymail: "+incomingPaymail, http.StatusBadRequest)
return
} else if !c.IsAllowedDomain(domain) {
ErrorResponse(w, req, ErrorUnknownDomain, "domain unknown: "+domain, http.StatusBadRequest)
return
}
// Start the PaymentRequest
paymentRequest := &paymail.PaymentRequest{
Satoshis: params.GetUint64("satoshis"),
}
// Did we get some satoshis?
if paymentRequest.Satoshis == 0 {
ErrorResponse(w, req, ErrorMissingSatoshis, "missing parameter: satoshis", http.StatusBadRequest)
return
}
// Create the metadata struct
md := CreateMetadata(req, alias, domain, "")
md.PaymentDestination = paymentRequest
// Get from the data layer
foundPaymail, err := c.actions.GetPaymailByAlias(req.Context(), alias, domain, md)
if err != nil {
ErrorResponse(w, req, ErrorFindingPaymail, err.Error(), http.StatusExpectationFailed)
return
} else if foundPaymail == nil {
ErrorResponse(w, req, ErrorPaymailNotFound, "paymail not found", http.StatusNotFound)
return
}
// Create the response
var response *paymail.PaymentDestinationPayload
if response, err = c.actions.CreateP2PDestinationResponse(
req.Context(), alias, domain, paymentRequest.Satoshis, md,
); err != nil {
ErrorResponse(w, req, ErrorScript, "error creating output script(s): "+err.Error(), http.StatusExpectationFailed)
return
}
// Return the response
apirouter.ReturnResponse(w, req, http.StatusOK, response)
}