aboutsummaryrefslogtreecommitdiffstats
path: root/callback_handler_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'callback_handler_test.go')
-rw-r--r--callback_handler_test.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/callback_handler_test.go b/callback_handler_test.go
new file mode 100644
index 0000000..50b4ab5
--- /dev/null
+++ b/callback_handler_test.go
@@ -0,0 +1,116 @@
+package xmrpayclnt_test
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "log"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "time"
+
+ "gitlab.com/moneropay/moneropay/v2/pkg/model"
+
+ xmrpay "git.samanthony.xyz/xmrpayclnt"
+)
+
+func ExampleCallbackHandler() {
+ // Mock the MoneroPay server's POST /receive endpoint for the
+ // sake of the example
+ xmrPaySrv := httptest.NewServer(mockReceivePostHandler{})
+ xmrPaySrvUrl, err := url.Parse(xmrPaySrv.URL)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Create a callback handler that will listen on
+ // http://localhost:57904/moneropay?abcxyz... for callbacks from
+ // the MoneroPay server
+ cbUrl, err := url.Parse("http://localhost:57904/moneropay")
+ if err != nil {
+ log.Fatal(err)
+ }
+ cbSrv := http.NewServeMux()
+ cbHlr := xmrpay.NewCallbackHandler(cbUrl)
+ cbSrv.Handle("POST "+cbUrl.Path, cbHlr)
+ go func() {
+ if err := http.ListenAndServe(cbUrl.Host, cbSrv); err != nil {
+ log.Fatal(err)
+ }
+ }()
+
+ // Request a transfer and register a callback for it
+ clnt := xmrpay.New(xmrPaySrvUrl)
+ resp, cb, err := clnt.ReceiveWithCallback(context.Background(), 123, "lorem ipsum", cbHlr)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer cb.Close()
+ fmt.Printf("POST /receive response: %s %d %q\n", resp.Address, resp.Amount, resp.Description)
+
+ // Wait for the transaction to finish
+ <-cb.Complete
+ fmt.Println("funds have been received")
+
+ // Output:
+ // POST /receive response: abcxyz 123 "lorem ipsum"
+ // funds have been received
+}
+
+type mockReceivePostHandler struct{}
+
+func (mockReceivePostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost || r.URL.Path != "/receive" {
+ panic(fmt.Sprintf("bad request: %s %s", r.Method, r.URL))
+ }
+
+ var req model.ReceivePostRequest
+ dec := json.NewDecoder(r.Body)
+ if err := dec.Decode(&req); err != nil {
+ panic(err)
+ }
+
+ // Send callbacks
+ go func() {
+ var cb model.CallbackResponse
+ cb.Amount.Expected = req.Amount
+ cb.Description = req.Description
+ cbData, err := json.Marshal(cb)
+ if err != nil {
+ panic(err)
+ }
+ period := time.Second
+ timer := time.NewTimer(period)
+ for i := 0; i < 3; i++ {
+ <-timer.C
+ timer.Reset(period)
+ _, err := http.Post(req.CallbackUrl, "application/json", bytes.NewBuffer(cbData))
+ if err != nil {
+ panic(err)
+ }
+ }
+
+ // Transaction is finally complete
+ cb.Complete = true
+ cbData, err = json.Marshal(cb)
+ if err != nil {
+ panic(err)
+ }
+ _, err = http.Post(req.CallbackUrl, "application/json", bytes.NewBuffer(cbData))
+ if err != nil {
+ panic(err)
+ }
+ }()
+
+ resp := model.ReceivePostResponse{
+ Address: "abcxyz",
+ Amount: req.Amount,
+ Description: req.Description,
+ }
+ enc := json.NewEncoder(w)
+ if err := enc.Encode(resp); err != nil {
+ panic(err)
+ }
+}