Я реализовал Auto-Renewable subscriptions
около 2 месяцев назад, и они работали нормально.
Однако, когда я вернулся, чтобы проверить их в песочнице, я получил No value with key expires_date found
.
Я отладил и распечатал latest_receipt_info
, и это явно строка.Почему это происходит?Я снова посмотрел на квитанцию, и Apple изменила некоторые ключи на Int
, но моя дата expires
остается прежней.Куда я иду не так?
Вот мой код и квитанция.Ошибка говорит
"Нет значения, связанного с ключом CodingKeys (stringValue: \" expires_date \ ", intValue: nil) (\" expires_date \ ")."
Последняя квитанция
"latest_receipt_info" = (
{
"expires_date" = "2019-02-06 15:50:21 Etc/GMT";
"expires_date_ms" = 1549468221000;
"expires_date_pst" = "2019-02-06 07:50:21 America/Los_Angeles";
"is_in_intro_offer_period" = false;
"is_trial_period" = false;
"original_purchase_date" = "2019-03-21 08:37:40 Etc/GMT";
"original_purchase_date_ms" = 1553157460000;
"original_purchase_date_pst" = "2019-03-21 01:37:40 America/Los_Angeles";
"original_transaction_id" = 1000000495291060;
"product_id" = "com.myApp.myApp.autoRenewableSubscription";
"purchase_date" = "2019-02-06 15:45:21 Etc/GMT";
"purchase_date_ms" = 1549467921000;
"purchase_date_pst" = "2019-02-06 07:45:21 America/Los_Angeles";
quantity = 1;
"transaction_id" = 1000000512208509;
"web_order_line_item_id" = 1000000042609485;
}
Информация о квитанции
struct IAPReceiptInfo: Decodable {
let expiresDate: String
private enum CodingKeys: String, CodingKey {
case expiresDate = "expires_date"
}
}
struct IAPReceipt: Decodable {
let latestReceipt: String
let status: Int
var receiptInfo = [IAPReceiptInfo]()
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Codingkeys.self)
self.latestReceipt = try container.decode(String.self, forKey: .latestReceipt)
self.status = try container.decode(Int.self, forKey: .status)
self.receiptInfo = try container.decode([IAPReceiptInfo].self, forKey: .latestInfos)
}
var latestInfo: IAPReceiptInfo? {
return self.receiptInfo.last
}
var IAPLatestReceipt: String {
return self.latestReceipt
}
private enum Codingkeys: String, CodingKey {
case latestReceipt = "latest_receipt"
case status
case latestInfos = "latest_receipt_info"
}
}
IAPReceiptAPI
func sendReceiptForToServer(completion pCompletion: @escaping (([IAPReceiptInfo]?, Error?) -> Void)) {
let receiptData = self.getReceipt?.base64EncodedString()
let requestReceiptDict = ["receipt": receiptData]
if receiptData == nil {
let error = NSError(domain: "No Rec", code: 0, userInfo: nil)
pCompletion(nil, error)
}
do {
let data = try requestReceiptDict.vyEncode()
guard let validationUrl = URL(string: "https://us-central1-myApp-a8e27.cloudfunctions.net/receiptValidation") else { return }
let session = URLSession(configuration: .default)
var request = URLRequest(url: validationUrl, cachePolicy: .reloadIgnoringLocalCacheData)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = data
let task = session.uploadTask(with:request, from: data) { (data, response, error) in
guard let data = data, error == nil else { return }
do {
let receipt = try IAPReceipt.vyDecode(data: data)
if let receiptLatestInfo = receipt.latestInfo {
pCompletion([receiptLatestInfo], nil)
}
} catch let error {
pCompletion(nil, error)
}
}
task.resume()
} catch let error {
pCompletion(nil, error)
}
}
}