apollo-backend/internal/api/receipt.go

64 lines
1.3 KiB
Go
Raw Normal View History

2021-08-08 18:19:47 +00:00
package api
import (
2021-09-11 14:53:19 +00:00
"context"
"encoding/json"
"io/ioutil"
2021-08-08 18:19:47 +00:00
"net/http"
2021-09-11 14:53:19 +00:00
"time"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
2021-08-08 18:19:47 +00:00
2021-09-11 14:53:19 +00:00
"github.com/christianselig/apollo-backend/internal/domain"
"github.com/christianselig/apollo-backend/internal/itunes"
)
2021-08-08 18:19:47 +00:00
func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) {
2021-09-11 14:53:19 +00:00
ctx := context.Background()
vars := mux.Vars(r)
apns := vars["apns"]
2021-09-25 13:19:42 +00:00
body, _ := ioutil.ReadAll(r.Body)
2021-09-11 14:53:19 +00:00
iapr, err := itunes.NewIAPResponse(string(body), true)
if err != nil {
a.logger.WithFields(logrus.Fields{
"err": err,
}).Info("failed verifying receipt")
a.errorResponse(w, r, 500, err.Error())
return
}
if apns != "" {
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil {
a.errorResponse(w, r, 500, err.Error())
return
}
if iapr.DeleteDevice {
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
if err != nil {
a.errorResponse(w, r, 500, err.Error())
return
}
for _, acc := range accs {
2021-09-25 13:19:42 +00:00
_ = a.accountRepo.Disassociate(ctx, &acc, &dev)
2021-09-11 14:53:19 +00:00
}
2021-09-25 13:19:42 +00:00
_ = a.deviceRepo.Delete(ctx, apns)
2021-09-11 14:53:19 +00:00
} else {
dev.ActiveUntil = time.Now().Unix() + domain.DeviceActiveAfterReceitCheckDuration
2021-09-25 13:19:42 +00:00
_ = a.deviceRepo.Update(ctx, &dev)
2021-09-11 14:53:19 +00:00
}
}
2021-08-08 18:19:47 +00:00
w.WriteHeader(http.StatusOK)
2021-09-11 14:53:19 +00:00
bb, _ := json.Marshal(iapr.VerificationInfo)
2021-09-25 13:19:42 +00:00
_, _ = w.Write(bb)
2021-08-08 18:19:47 +00:00
}