Fix purchase "already purchased" detection for Apple API changes

Apple now returns HTTP 200 with FailureType "5002" for already-purchased
apps instead of HTTP 500. Also treat FailureType "1008" (device
verification failed) as a stale token that triggers re-authentication.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Barnabas BirmacherandClaude Opus 4.6 committed 2026-02-21 23:41:44 +01:00
1 parent a1b49251cf
commit 438e8e8c30
3 files changed
+72 -9

No files matched your search

+9 -2
View File
@@ -11,6 +11,7 @@ import (
var (
ErrPasswordTokenExpired = errors.New("password token is expired")
ErrLicenseAlreadyExists = errors.New("license already exists")
ErrSubscriptionRequired = errors.New("subscription required")
ErrTemporarilyUnavailable = errors.New("item is temporarily unavailable")
)
@@ -72,10 +73,16 @@ func (t *appstore) purchaseWithParams(acc Account, app App, guid string, pricing
return ErrSubscriptionRequired
}
if res.Data.FailureType == FailureTypePasswordTokenExpired || res.Data.CustomerMessage == CustomerMessagePasswordChanged {
if res.Data.FailureType == FailureTypePasswordTokenExpired ||
res.Data.FailureType == FailureTypeDeviceVerificationFailed ||
res.Data.CustomerMessage == CustomerMessagePasswordChanged {
return ErrPasswordTokenExpired
}
if res.Data.FailureType == FailureTypeLicenseAlreadyExists {
return ErrLicenseAlreadyExists
}
if res.Data.FailureType != "" && res.Data.CustomerMessage != "" {
return NewErrorWithMetadata(errors.New(res.Data.CustomerMessage), res)
}
@@ -85,7 +92,7 @@ func (t *appstore) purchaseWithParams(acc Account, app App, guid string, pricing
}
if res.StatusCode == gohttp.StatusInternalServerError {
return fmt.Errorf("license already exists")
return ErrLicenseAlreadyExists
}
if res.Data.JingleDocType != "purchaseSuccess" || res.Data.Status != 0 {
+57 -3
View File
@@ -229,7 +229,34 @@ var _ = Describe("AppStore (Purchase)", func() {
})
})
When("account already has a license for the app", func() {
When("account already has a license for the app (failure type)", func() {
BeforeEach(func() {
mockMachine.EXPECT().
MacAddress().
Return("00:00:00:00:00:00", nil)
mockPurchaseClient.EXPECT().
Send(gomock.Any()).
Return(http.Result[purchaseResult]{
StatusCode: 200,
Data: purchaseResult{
FailureType: FailureTypeLicenseAlreadyExists,
CustomerMessage: "An unknown error has occurred",
},
}, nil)
})
It("returns license already exists error", func() {
err := as.Purchase(PurchaseInput{
Account: Account{
StoreFront: "143441",
},
})
Expect(err).To(MatchError(ErrLicenseAlreadyExists))
})
})
When("account already has a license for the app (HTTP 500 legacy)", func() {
BeforeEach(func() {
mockMachine.EXPECT().
MacAddress().
@@ -243,13 +270,40 @@ var _ = Describe("AppStore (Purchase)", func() {
}, nil)
})
It("returns error", func() {
It("returns license already exists error", func() {
err := as.Purchase(PurchaseInput{
Account: Account{
StoreFront: "143441",
},
})
Expect(err).To(HaveOccurred())
Expect(err).To(MatchError(ErrLicenseAlreadyExists))
})
})
When("device verification fails", func() {
BeforeEach(func() {
mockMachine.EXPECT().
MacAddress().
Return("00:00:00:00:00:00", nil)
mockPurchaseClient.EXPECT().
Send(gomock.Any()).
Return(http.Result[purchaseResult]{
StatusCode: 200,
Data: purchaseResult{
FailureType: FailureTypeDeviceVerificationFailed,
CustomerMessage: "Your device or computer could not be verified. Contact support for assistance.",
},
}, nil)
})
It("returns password token expired error", func() {
err := as.Purchase(PurchaseInput{
Account: Account{
StoreFront: "143441",
},
})
Expect(err).To(MatchError(ErrPasswordTokenExpired))
})
})
+6 -4
View File
@@ -1,10 +1,12 @@
package appstore
const (
FailureTypeInvalidCredentials = "-5000"
FailureTypePasswordTokenExpired = "2034"
FailureTypeLicenseNotFound = "9610"
FailureTypeTemporarilyUnavailable = "2059"
FailureTypeInvalidCredentials = "-5000"
FailureTypePasswordTokenExpired = "2034"
FailureTypeLicenseNotFound = "9610"
FailureTypeTemporarilyUnavailable = "2059"
FailureTypeLicenseAlreadyExists = "5002"
FailureTypeDeviceVerificationFailed = "1008"
CustomerMessageBadLogin = "MZFinance.BadLogin.Configurator_message"
CustomerMessageAccountDisabled = "Your account is disabled."