Files
purchases-ios/Sources/Purchasing/StoreKitAbstractions/SK1StoreTransaction.swift
T
NachoSoto 1857932e0e SK1StoreProductDiscount: handle SKProductDiscount.priceLocale being nil and created StoreKitWorkarounds (#1545)
Fixes #1521.
Despite `SKProductDiscount.priceLocale` being non-optional, StoreKit might return `nil` `NSLocale`s.
This works around that to make sure the SDK doesn't crash when bridging to `Locale`.
2022-05-12 08:55:02 -07:00

77 lines
2.2 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// SK1StoreTransaction.swift
//
// Created by Nacho Soto on 1/4/22.
import StoreKit
internal struct SK1StoreTransaction: StoreTransactionType {
init(sk1Transaction: SK1Transaction) {
self.underlyingSK1Transaction = sk1Transaction
self.productIdentifier = sk1Transaction.productIdentifier ?? ""
self.purchaseDate = sk1Transaction.purchaseDate
self.transactionIdentifier = sk1Transaction.transactionID
self.quantity = sk1Transaction.quantity
}
let underlyingSK1Transaction: SK1Transaction
let productIdentifier: String
let purchaseDate: Date
let transactionIdentifier: String
let quantity: Int
}
extension SKPaymentTransaction {
var productIdentifier: String? {
guard let payment = self.paymentIfPresent else { return nil }
guard let productIdentifier = payment.productIdentifier as String?,
!productIdentifier.isEmpty else {
Logger.appleWarning(Strings.purchase.skpayment_missing_product_identifier)
return nil
}
return productIdentifier
}
fileprivate var purchaseDate: Date {
guard let date = self.transactionDate else {
Logger.appleWarning(Strings.purchase.sktransaction_missing_transaction_date)
return Date(timeIntervalSince1970: 0)
}
return date
}
fileprivate var transactionID: String {
guard let identifier = self.transactionIdentifier else {
Logger.appleWarning(Strings.purchase.sktransaction_missing_transaction_identifier)
return UUID().uuidString
}
return identifier
}
fileprivate var quantity: Int {
// Note: multi-quanity purchases aren't supported.
// Defaulting to `1` if `self.payment` is `nil` (which shouldn't happen) as a reasonable default.
return self.paymentIfPresent?.quantity ?? 1
}
}