Skip to content

Commit 9c6f585

Browse files
committed
Ignore API errors when an order already exists
1 parent 8453ed9 commit 9c6f585

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

Networking/Networking/Model/WordPressApiError.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public enum WordPressApiError: Error, Decodable, Equatable {
88
///
99
case unknown(code: String, message: String)
1010

11+
/// An order already exists for this IAP receipt
12+
///
13+
case productPurchased
14+
1115
/// Decodable Initializer.
1216
///
1317
public init(from decoder: Decoder) throws {
@@ -16,6 +20,8 @@ public enum WordPressApiError: Error, Decodable, Equatable {
1620
let message = try container.decode(String.self, forKey: .message)
1721

1822
switch code {
23+
case Constants.productPurchased:
24+
self = .productPurchased
1925
default:
2026
self = .unknown(code: code, message: message)
2127
}
@@ -25,6 +31,7 @@ public enum WordPressApiError: Error, Decodable, Equatable {
2531
/// Constants for Possible Error Identifiers
2632
///
2733
private enum Constants {
34+
static let productPurchased = "product_purchased"
2835
}
2936

3037
/// Coding Keys
@@ -50,6 +57,10 @@ extension WordPressApiError: CustomStringConvertible {
5057

5158
public var description: String {
5259
switch self {
60+
case .productPurchased:
61+
return NSLocalizedString(
62+
"An order aready exists for this receipt",
63+
comment: "Error message when an order already exists in the backend for a given receipt")
5364
case .unknown(let code, let message):
5465
let messageFormat = NSLocalizedString(
5566
"WordPress API Error: [%1$@] %2$@",

Yosemite/Yosemite/Stores/InAppPurchaseStore.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,22 @@ private extension InAppPurchaseStore {
201201
let receiptData = try await getAppReceipt()
202202

203203
logInfo("Sending transaction to API for site \(siteID)")
204-
let orderID = try await remote.createOrder(
205-
for: siteID,
206-
price: priceInCents,
207-
productIdentifier: product.id,
208-
appStoreCountryCode: countryCode,
209-
receiptData: receiptData
210-
)
211-
logInfo("Successfully registered purchase with Order ID \(orderID)")
212-
204+
do {
205+
let orderID = try await remote.createOrder(
206+
for: siteID,
207+
price: priceInCents,
208+
productIdentifier: product.id,
209+
appStoreCountryCode: countryCode,
210+
receiptData: receiptData
211+
)
212+
logInfo("Successfully registered purchase with Order ID \(orderID)")
213+
} catch WordPressApiError.productPurchased {
214+
// Ignore errors for existing purchase
215+
logInfo("Existing order found for transaction \(transaction.id) on site \(siteID), ignoring")
216+
} catch {
217+
// Rethrow any other error
218+
throw error
219+
}
213220
}
214221

215222
func userIsEntitledToProduct(with id: String) async throws -> Bool {

0 commit comments

Comments
 (0)