Skip to content

Instantly share code, notes, and snippets.

@EllinaKuznetcova
Last active May 17, 2022 17:45
Show Gist options
  • Save EllinaKuznetcova/1b9b0ac1650494007db799a4f37ee6ee to your computer and use it in GitHub Desktop.
Save EllinaKuznetcova/1b9b0ac1650494007db799a4f37ee6ee to your computer and use it in GitHub Desktop.
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
}
}
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
})
}
@anshulkumar189
Copy link

How to send receipt? I can't understand.

@skaunited
Copy link

yes we can't find your router and your user classes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment