forked from RevenueCat/purchases-ios-spm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalReceiptFetcher.swift
68 lines (55 loc) · 2.07 KB
/
LocalReceiptFetcher.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// 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
//
// LocalReceiptFetcher.swift
//
// Created by Mark Villacampa on 8/1/24.
import Foundation
internal protocol LocalReceiptFetcherType: Sendable {
func fetchAndParseLocalReceipt() throws -> AppleReceipt
}
internal final class LocalReceiptFetcher: LocalReceiptFetcherType {
// Note: this is a simplified version of `ReceiptFetcher`
// available for public use.
/// Fetches and parses the local receipt
/// - Returns: ``AppleReceipt`` of the parsed local receipt.
/// - Throws: ``PurchasesReceiptParser/Error`` if fetching or parsing failed.
///
/// - Note: this method won't use ``SKReceiptRefreshRequest`` to
/// fetch the receipt if it's not already available.
///
/// ### Related Symbols
/// - ``SKReceiptRefreshRequest``
/// - ``Bundle/appStoreReceiptURL``
/// - ``PurchasesReceiptParser/parse(from:)``
func fetchAndParseLocalReceipt() throws -> AppleReceipt {
return try self.fetchAndParseLocalReceipt(reader: DefaultFileReader(),
bundle: .main,
receiptParser: .default)
}
internal func fetchAndParseLocalReceipt(
reader: FileReader,
bundle: Bundle,
receiptParser: PurchasesReceiptParser
) throws -> AppleReceipt {
return try receiptParser.parse(from: self.fetchReceipt(reader, bundle))
}
}
private extension LocalReceiptFetcher {
func fetchReceipt(_ reader: FileReader, _ bundle: Bundle) throws -> Data {
guard let url = bundle.appStoreReceiptURL else {
throw PurchasesReceiptParser.Error.receiptNotPresent
}
do {
return try reader.contents(of: url)
} catch {
throw PurchasesReceiptParser.Error.failedToLoadLocalReceipt(error)
}
}
}