Skip to content

Commit

Permalink
This mostly works
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdholtz committed Jan 6, 2025
1 parent a5afdae commit caac5b3
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 53 deletions.
27 changes: 27 additions & 0 deletions RevenueCatUI/Data/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,27 @@ enum TestData {
discounts: [],
locale: Self.locale
)
static let productWithIntroOfferPayUpFront = TestStoreProduct(
localizedTitle: "PRO monthly",
price: 3.99,
localizedPriceString: "$3.99",
productIdentifier: "com.revenuecat.product_5",
productType: .autoRenewableSubscription,
localizedDescription: "PRO monthly",
subscriptionGroupIdentifier: "group",
subscriptionPeriod: .init(value: 1, unit: .month),
introductoryDiscount: .init(
identifier: "intro",
price: 1.99,
localizedPriceString: "$1.99",
paymentMode: .payUpFront,
subscriptionPeriod: .init(value: 1, unit: .week),
numberOfPeriods: 1,
type: .introductory
),
discounts: [],
locale: Self.locale
)
static let productWithNoIntroOffer = TestStoreProduct(
localizedTitle: "PRO annual",
price: 34.99,
Expand Down Expand Up @@ -269,6 +290,12 @@ enum TestData {
storeProduct: productWithIntroOffer.toStoreProduct(),
offeringIdentifier: Self.offeringIdentifier
)
static let packageWithIntroOfferPayUpFront = Package(
identifier: PackageType.monthly.identifier,
packageType: .monthly,
storeProduct: productWithIntroOfferPayUpFront.toStoreProduct(),
offeringIdentifier: Self.offeringIdentifier
)
static let packageWithNoIntroOffer = Package(
identifier: PackageType.annual.identifier,
packageType: .annual,
Expand Down
24 changes: 20 additions & 4 deletions RevenueCatUI/Templates/V2/Variables/VariableHandlerV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,19 +298,35 @@ extension VariablesV2 {
}

func productOfferPricePerDay(package: Package) -> String {
return ""
guard let price = package.storeProduct.introductoryDiscount?.pricePerDay, let formatter = package.storeProduct.priceFormatter else {
return ""
}

return formatter.string(from: price as NSDecimalNumber) ?? ""
}

func productOfferPricePerWeek(package: Package) -> String {
return ""
guard let price = package.storeProduct.introductoryDiscount?.pricePerWeek, let formatter = package.storeProduct.priceFormatter else {
return ""
}

return formatter.string(from: price as NSDecimalNumber) ?? ""
}

func productOfferPricePerMonth(package: Package) -> String {
return ""
guard let price = package.storeProduct.introductoryDiscount?.pricePerMonth, let formatter = package.storeProduct.priceFormatter else {
return ""
}

return formatter.string(from: price as NSDecimalNumber) ?? ""
}

func productOfferPricePerYear(package: Package) -> String {
return ""
guard let price = package.storeProduct.introductoryDiscount?.pricePerYear, let formatter = package.storeProduct.priceFormatter else {
return ""
}

return formatter.string(from: price as NSDecimalNumber) ?? ""
}

func productOfferPeriod(package: Package, localizations: [String: String]) -> String {
Expand Down
32 changes: 32 additions & 0 deletions Sources/Purchasing/StoreKitAbstractions/StoreProductDiscount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,35 @@ extension StoreProductDiscount: Identifiable {
public var id: Data { return Data(discount: self) }

}

public extension StoreProductDiscount {

/// Calculates the price of this subscription product per day.
/// - Returns: `nil` if the product is not a subscription.
@available(iOS 11.2, macOS 10.13.2, tvOS 11.2, watchOS 6.2, *)
@objc var pricePerDay: NSDecimalNumber? {
return self.subscriptionPeriod.pricePerDay(withTotalPrice: self.price) as NSDecimalNumber?
}

/// Calculates the price of this subscription product per week.
/// - Returns: `nil` if the product is not a subscription.
@available(iOS 11.2, macOS 10.13.2, tvOS 11.2, watchOS 6.2, *)
@objc var pricePerWeek: NSDecimalNumber? {
return self.subscriptionPeriod.pricePerWeek(withTotalPrice: self.price) as NSDecimalNumber?
}

/// Calculates the price of this subscription product per month.
/// - Returns: `nil` if the product is not a subscription.
@available(iOS 11.2, macOS 10.13.2, tvOS 11.2, watchOS 6.2, *)
@objc var pricePerMonth: NSDecimalNumber? {
return self.subscriptionPeriod.pricePerMonth(withTotalPrice: self.price) as NSDecimalNumber?
}

/// Calculates the price of this subscription product per year.
/// - Returns: `nil` if the product is not a subscription.
@available(iOS 11.2, macOS 10.13.2, tvOS 11.2, watchOS 6.2, *)
@objc var pricePerYear: NSDecimalNumber? {
return self.subscriptionPeriod.pricePerYear(withTotalPrice: self.price) as NSDecimalNumber?
}

}
148 changes: 99 additions & 49 deletions Tests/RevenueCatUITests/PaywallsV2/VariableHandlerV2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,55 +231,105 @@ class VariableHandlerV2Test: TestCase {
expect(result).to(equal("3 months"))
}

// func testProductOfferPrice() {
// let result = variableHandler.processVariables(
// in: "{{ product.offer_price }}",
// with: TestData.monthlyPackage,
// locale: locale,
// localizations: localizations["en_US"]!
// )
// expect(result).to(equal("4.99"))
// }
//
// func testProductOfferPricePerDay() {
// let result = variableHandler.processVariables(
// in: "{{ product.offer_price_per_day }}",
// with: TestData.monthlyPackage,
// locale: locale,
// localizations: localizations["en_US"]!
// )
// expect(result).to(equal("0.17"))
// }
//
// func testProductOfferPricePerWeek() {
// let result = variableHandler.processVariables(
// in: "{{ product.offer_price_per_week }}",
// with: TestData.monthlyPackage,
// locale: locale,
// localizations: localizations["en_US"]!
// )
// expect(result).to(equal("1.24"))
// }
//
// func testProductOfferPricePerMonth() {
// let result = variableHandler.processVariables(
// in: "{{ product.offer_price_per_month }}",
// with: TestData.monthlyPackage,
// locale: locale,
// localizations: localizations["en_US"]!
// )
// expect(result).to(equal("4.99"))
// }
//
// func testProductOfferPricePerYear() {
// let result = variableHandler.processVariables(
// in: "{{ product.offer_price_per_year }}",
// with: TestData.monthlyPackage,
// locale: locale,
// localizations: localizations["en_US"]!
// )
// expect(result).to(equal("59.88"))
// }
func testProductFreeOfferPrice() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price }}",
with: TestData.packageWithIntroOffer,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$0.00"))
}

func testProductFreeOfferPricePerDay() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_day }}",
with: TestData.packageWithIntroOffer,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$0.00"))
}

func testProductFreeOfferPricePerWeek() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_week }}",
with: TestData.packageWithIntroOffer,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$0.00"))
}

func testProductFreeOfferPricePerMonth() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_month }}",
with: TestData.packageWithIntroOffer,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$0.00"))
}

func testProductFreeOfferPricePerYear() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_year }}",
with: TestData.packageWithIntroOffer,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$0.00"))
}

func testProductPayUpFrontOfferPrice() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price }}",
with: TestData.packageWithIntroOfferPayUpFront,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$1.99"))
}

func testProductPayUpFrontOfferPricePerDay() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_day }}",
with: TestData.packageWithIntroOfferPayUpFront,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$0.28"))
}

func testProductPayUpFrontOfferPricePerWeek() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_week }}",
with: TestData.packageWithIntroOfferPayUpFront,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$1.99"))
}

func testProductPayUpFrontOfferPricePerMonth() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_month }}",
with: TestData.packageWithIntroOfferPayUpFront,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$1.99"))
}

func testProductPayUpFrontOfferPricePerYear() {
let result = variableHandler.processVariables(
in: "{{ product.offer_price_per_year }}",
with: TestData.packageWithIntroOfferPayUpFront,
locale: locale,
localizations: localizations["en_US"]!
)
expect(result).to(equal("$1.99"))
}

func testProductOfferPeriod() {
let result = variableHandler.processVariables(
Expand Down

0 comments on commit caac5b3

Please sign in to comment.