From a86821e8d7e58b93597fdf2fb05b829a8ccb37f2 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 24 Nov 2021 09:20:30 +0600 Subject: [PATCH] Add "is_upgraded" properties for ReceiptItem --- Sources/SwiftyStoreKit/InAppReceipt.swift | 1 + Sources/SwiftyStoreKit/SwiftyStoreKit+Types.swift | 6 +++++- Tests/SwiftyStoreKitTests/InAppReceiptTests.swift | 5 +++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Sources/SwiftyStoreKit/InAppReceipt.swift b/Sources/SwiftyStoreKit/InAppReceipt.swift index a31b23a2..b78df1b8 100644 --- a/Sources/SwiftyStoreKit/InAppReceipt.swift +++ b/Sources/SwiftyStoreKit/InAppReceipt.swift @@ -69,6 +69,7 @@ extension ReceiptItem { } else { self.isInIntroOfferPeriod = false } + self.isUpgraded = receiptInfo["is_upgraded"] as? Bool ?? false } private static func parseDate(from receiptInfo: ReceiptInfo, key: String) -> Date? { diff --git a/Sources/SwiftyStoreKit/SwiftyStoreKit+Types.swift b/Sources/SwiftyStoreKit/SwiftyStoreKit+Types.swift index addf9b78..0714ee23 100644 --- a/Sources/SwiftyStoreKit/SwiftyStoreKit+Types.swift +++ b/Sources/SwiftyStoreKit/SwiftyStoreKit+Types.swift @@ -219,7 +219,10 @@ public struct ReceiptItem: Purchased, Codable { /// Indicates whether or not the subscription item is currently within an intro offer period. public var isInIntroOfferPeriod: Bool - public init(productId: String, quantity: Int, transactionId: String, originalTransactionId: String, purchaseDate: Date, originalPurchaseDate: Date, webOrderLineItemId: String?, subscriptionExpirationDate: Date?, cancellationDate: Date?, isTrialPeriod: Bool, isInIntroOfferPeriod: Bool) { + /// An indicator that a subscription has been canceled due to an upgrade. This field is only present for upgrade transactions. + public var isUpgraded: Bool + + public init(productId: String, quantity: Int, transactionId: String, originalTransactionId: String, purchaseDate: Date, originalPurchaseDate: Date, webOrderLineItemId: String?, subscriptionExpirationDate: Date?, cancellationDate: Date?, isTrialPeriod: Bool, isInIntroOfferPeriod: Bool, isUpgraded: Bool) { self.productId = productId self.quantity = quantity self.transactionId = transactionId @@ -231,6 +234,7 @@ public struct ReceiptItem: Purchased, Codable { self.cancellationDate = cancellationDate self.isTrialPeriod = isTrialPeriod self.isInIntroOfferPeriod = isInIntroOfferPeriod + self.isUpgraded = isUpgraded } } diff --git a/Tests/SwiftyStoreKitTests/InAppReceiptTests.swift b/Tests/SwiftyStoreKitTests/InAppReceiptTests.swift index 3d3493c5..ca6d8051 100644 --- a/Tests/SwiftyStoreKitTests/InAppReceiptTests.swift +++ b/Tests/SwiftyStoreKitTests/InAppReceiptTests.swift @@ -34,8 +34,8 @@ private extension TimeInterval { extension ReceiptItem: Equatable { - init(productId: String, purchaseDate: Date, subscriptionExpirationDate: Date? = nil, cancellationDate: Date? = nil, isTrialPeriod: Bool = false, isInIntroOfferPeriod: Bool = false) { - self.init(productId: productId, quantity: 1, transactionId: UUID().uuidString, originalTransactionId: UUID().uuidString, purchaseDate: purchaseDate, originalPurchaseDate: purchaseDate, webOrderLineItemId: UUID().uuidString, subscriptionExpirationDate: subscriptionExpirationDate, cancellationDate: cancellationDate, isTrialPeriod: isTrialPeriod, isInIntroOfferPeriod: isInIntroOfferPeriod) + init(productId: String, purchaseDate: Date, subscriptionExpirationDate: Date? = nil, cancellationDate: Date? = nil, isTrialPeriod: Bool = false, isInIntroOfferPeriod: Bool = false, isUpgraded: Bool = false) { + self.init(productId: productId, quantity: 1, transactionId: UUID().uuidString, originalTransactionId: UUID().uuidString, purchaseDate: purchaseDate, originalPurchaseDate: purchaseDate, webOrderLineItemId: UUID().uuidString, subscriptionExpirationDate: subscriptionExpirationDate, cancellationDate: cancellationDate, isTrialPeriod: isTrialPeriod, isInIntroOfferPeriod: isInIntroOfferPeriod, isUpgraded: isUpgraded) self.productId = productId self.quantity = 1 self.purchaseDate = purchaseDate @@ -47,6 +47,7 @@ extension ReceiptItem: Equatable { self.webOrderLineItemId = UUID().uuidString self.isTrialPeriod = isTrialPeriod self.isInIntroOfferPeriod = isInIntroOfferPeriod + self.isUpgraded = isUpgraded } var receiptInfo: NSDictionary {