apollo-backend/internal/api/receipt.go

64 lines
1.4 KiB
Go
Raw Permalink 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"
2022-05-23 18:17:25 +00:00
"go.uber.org/zap"
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 {
2022-05-23 18:17:25 +00:00
a.logger.Info("failed to verify receipt", zap.Error(err))
2022-05-21 14:00:21 +00:00
a.errorResponse(w, r, 500, err)
2021-09-11 14:53:19 +00:00
return
}
if apns != "" {
dev, err := a.deviceRepo.GetByAPNSToken(ctx, apns)
if err != nil {
2022-05-21 14:00:21 +00:00
a.errorResponse(w, r, 500, err)
2021-09-11 14:53:19 +00:00
return
}
if iapr.DeleteDevice {
2022-05-23 23:05:31 +00:00
if dev.GracePeriodExpiresAt.Before(time.Now()) {
accs, err := a.accountRepo.GetByAPNSToken(ctx, apns)
if err != nil {
a.errorResponse(w, r, 500, err)
return
}
for _, acc := range accs {
_ = a.accountRepo.Disassociate(ctx, &acc, &dev)
}
_ = a.deviceRepo.Delete(ctx, apns)
2022-03-28 21:05:01 +00:00
}
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
}