apollo-backend/internal/api/receipt.go

69 lines
1.5 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
"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) {
2022-05-07 19:04:35 +00:00
ctx := r.Context()
2021-09-11 14:53:19 +00:00
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 {
2022-03-28 21:05:01 +00:00
if dev.GracePeriodExpiresAt.After(time.Now()) {
w.WriteHeader(http.StatusOK)
return
}
2021-09-11 14:53:19 +00:00
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 {
2022-03-28 21:05:01 +00:00
dev.ExpiresAt = time.Now().Add(domain.DeviceActiveAfterReceitCheckDuration)
dev.GracePeriodExpiresAt = dev.ExpiresAt.Add(domain.DeviceGracePeriodAfterReceiptExpiry)
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
}