Last active
May 17, 2022 17:45
-
-
Save EllinaKuznetcova/1b9b0ac1650494007db799a4f37ee6ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RTSubscriptionResponse: Mappable { | |
var expirationDate: Date? | |
var isTrial: Bool? | |
var productId: String? | |
required convenience init?(map: Map) { | |
self.init() | |
} | |
func mapping(map: Map) { | |
guard let latestReceiptInfo = (map.JSON["latest_receipt_info"] as? [[String: AnyObject]])?.first else {return} | |
if let expirationDateStringWithTimeZone = latestReceiptInfo["expires_date"] as? String, | |
let range = expirationDateStringWithTimeZone.range(of: "Etc/GMT") { | |
let expirationDateString = expirationDateStringWithTimeZone.substring(to: range.lowerBound) | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyy-MM-dd HH:mm:ss" | |
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) | |
self.expirationDate = dateFormatter.date(from: expirationDateString) | |
} | |
self.isTrial = Bool(latestReceiptInfo["is_trial_period"] as? String ?? "false") | |
self.productId = latestReceiptInfo["product_id"] as? String | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func checkSubscriptionAvailability(_ completionHandler: @escaping (Bool) -> Void) { | |
guard let receiptUrl = Bundle.main.appStoreReceiptURL, | |
let receipt = try? Data(contentsOf: receiptUrl).base64EncodedString() as AnyObject else { | |
completionHandler(false) | |
return | |
} | |
let _ = Router.User.sendReceipt(receipt: receipt).request(baseUrl: "https:sandbox.itunes.apple.com").responseObject { (response: DataResponse<RTSubscriptionResponse>) in | |
switch response.result { | |
case .success(let value): | |
guard let expirationDate = value.expirationDate, | |
let productId = value.productId else {completionHandler(false); return} | |
self.expirationDate = expirationDate | |
self.isTrialPurchased = value.isTrial | |
self.purchasedProduct = ProductType(rawValue: productId) | |
completionHandler(Date().timeIntervalSince1970 < expirationDate.timeIntervalSince1970) | |
case .failure(let error): | |
completionHandler(false) | |
} | |
} | |
} | |
func updateSubscriptionStatus() { | |
self.checkSubscriptionAvailability({ [weak self] (isSubscribed) in | |
self?.isSubscriptionAvailable = isSubscribed | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes we can't find your router and your user classes