Skip to content

Commit 3ee9923

Browse files
committed
mutex apple and android
1 parent ba91563 commit 3ee9923

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

api/v1/subscription/subscription.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"net/http"
1818
"os"
1919
"strings"
20+
"sync"
2021
"time"
2122

2223
"github.com/gin-gonic/gin"
@@ -61,7 +62,13 @@ func getOffers(c *gin.Context) {
6162
c.JSON(http.StatusOK, offers)
6263
}
6364

65+
var androidLock sync.Mutex
66+
6467
func createSubscriptionAndroid(c *gin.Context) {
68+
69+
androidLock.Lock()
70+
defer androidLock.Unlock()
71+
6572
var receipt model.PurchaseRceipt
6673

6774
if err := json.NewDecoder(c.Request.Body).Decode(&receipt); err != nil {
@@ -70,6 +77,13 @@ func createSubscriptionAndroid(c *gin.Context) {
7077
}
7178
log.Infof("android: %s", receipt)
7279

80+
// Check if the subscription already exists
81+
s, err := core.GetSubscriptionByReceipt(receipt.Receipt)
82+
if err == nil {
83+
c.JSON(http.StatusOK, s)
84+
return
85+
}
86+
7387
// Validate the receipt with Google
7488
result, err := validateReceiptAndroid(receipt)
7589
if err != nil || result == nil {
@@ -117,7 +131,7 @@ func createSubscriptionAndroid(c *gin.Context) {
117131
relays := 0
118132
autoRenew := false
119133
issued := time.Now()
120-
expires := time.Now().AddDate(1, 0, 0)
134+
expires := time.Now().AddDate(0, 2, 0)
121135

122136
switch receipt.ProductID {
123137
case "basic_monthly":
@@ -632,6 +646,7 @@ func fetchApplePublicKeyFromX5C(x5c string) (*ecdsa.PublicKey, error) {
632646
}
633647

634648
func handleAppleWebhook(c *gin.Context) {
649+
635650
bytes, err := io.ReadAll(c.Request.Body)
636651
if err != nil {
637652
log.WithFields(log.Fields{
@@ -958,7 +973,13 @@ func validateAppleSignature(header, payload, signature string) (bool, error) {
958973
return valid, nil
959974
}
960975

976+
var appleLock sync.Mutex
977+
961978
func createSubscriptionApple(c *gin.Context) {
979+
980+
appleLock.Lock()
981+
defer appleLock.Unlock()
982+
962983
var receipt model.PurchaseRceipt
963984

964985
err := json.NewDecoder(c.Request.Body).Decode(&receipt)
@@ -1054,7 +1075,7 @@ func createSubscriptionApple(c *gin.Context) {
10541075
relays := 0
10551076
autoRenew := false
10561077
issued := time.Now()
1057-
expires := time.Now().AddDate(1, 0, 0)
1078+
expires := time.Now().AddDate(0, 2, 0)
10581079

10591080
switch receipt.ProductID {
10601081
case "basic_monthly":

0 commit comments

Comments
 (0)