Skip to content

Commit

Permalink
Merge pull request #89 from AppSci/release/2.0.5
Browse files Browse the repository at this point in the history
Release: 2.0.5
  • Loading branch information
denysdanyliukboosters authored Oct 25, 2023
2 parents b31a56c + 4ae3e7b commit 9bad98d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions Sources/PandaSDK/Views/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -618,34 +618,25 @@ extension WebViewController {
}
}

func sendLocalizedPrices(products: [Product]) {
let localizedPricesToSend = products.map { product -> [String : Any] in
func sendLocalizedPrices(products: [Product]) async {
var localizedPricesToSend: [[String: Any]] = []
for product in products {
var localizedPriceInfo = [String: Any]()
var price: Decimal
if let introductoryOffer = product.subscription?.introductoryOffer {
switch introductoryOffer.paymentMode {
case .payAsYouGo, .payUpFront:
price = introductoryOffer.price
default:
price = product.price
}
} else {
price = product.price
}
var price = await productPrice(for: product)
var roundedValue = Decimal()
NSDecimalRound(&roundedValue, &price, 2, .bankers)
let micros = roundedValue * Decimal(1_000_000)
localizedPriceInfo["productId"] = product.id
localizedPriceInfo["priceAmountMicros"] = micros
localizedPriceInfo["priceCurrencyCode"] = product.priceFormatStyle.currencyCode
return localizedPriceInfo
localizedPricesToSend.append(localizedPriceInfo)
}

let localizedPricesToSendJSON = localizedPricesToSend.toJSONString()
let jsFunction = "pricingLoaded(\(localizedPricesToSendJSON))"

DispatchQueue.main.async {
self.wv.evaluateJavaScript(jsFunction) { (result, error) in
self.wv.evaluateJavaScript(jsFunction) { result, error in
if let error = error {
pandaLog("\(error)")
}
Expand All @@ -655,6 +646,20 @@ extension WebViewController {
}
}
}

private func productPrice(for product: Product) async -> Decimal {
let subscription = product.subscription
if let subscription, await subscription.isEligibleForIntroOffer, let introductoryOffer = subscription.introductoryOffer {
switch introductoryOffer.paymentMode {
case .payAsYouGo, .payUpFront:
return introductoryOffer.price
default:
return product.price
}
} else {
return product.price
}
}
}

extension WebViewController {
Expand Down

0 comments on commit 9bad98d

Please sign in to comment.