Skip to content

Commit

Permalink
Perform check on the most recent purchase receipt
Browse files Browse the repository at this point in the history
Issue #333
  • Loading branch information
eofster committed Dec 17, 2016
1 parent 4be2830 commit 46dcc87
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions ReceiptValidation/PurchaseReceiptAttributesValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ final class PurchaseReceiptAttributesValidation: NSObject {

extension PurchaseReceiptAttributesValidation: ReceiptValidation {
func validateReceipt(_ receipt: Data, completion: (_ result: Result, _ expiration: Date) -> Void) {
if let expiration = latestExpirationOfValidPurchaseReceipts(from: receipt) {
completion(.receiptIsValid, expiration)
if let r = purchaseReceipts(from: receipt).max(by: hasEarlierExpirationDate), isValid(r) {
completion(.receiptIsValid, r.expiration)
} else {
completion(.noActivePurchases, Date.distantPast)
}
}

private func latestExpirationOfValidPurchaseReceipts(from receipt: Data) -> Date? {
return validPurchaseReceipts(from: receipt).max(by: hasEarlierExpirationDate)?.expiration
private func purchaseReceipts(from receipt: Data) -> [ASN1PurchaseReceipt] {
return ASN1PurchaseReceipts(payload: ASN1ReceiptPayload(container: PKCS7Container(data: receipt)!)!).filter(hasExpectedID)
}

private func validPurchaseReceipts(from receipt: Data) -> [ASN1PurchaseReceipt] {
return ASN1PurchaseReceipts(payload: ASN1ReceiptPayload(container: PKCS7Container(data: receipt)!)!).filter(isReceiptValid)
}

private func isReceiptValid(_ receipt: ASN1PurchaseReceipt) -> Bool {
return !receipt.isCancelled && sixMonths(after: receipt.expiration) >= Date() && identifiers.contains(receipt.identifier)
private func hasExpectedID(_ receipt: ASN1PurchaseReceipt) -> Bool {
return identifiers.contains(receipt.identifier)
}
}

private func hasEarlierExpirationDate(_ lhs: ASN1PurchaseReceipt, _ rhs: ASN1PurchaseReceipt) -> Bool {
return lhs.expiration < rhs.expiration
}

private func isValid(_ receipt: ASN1PurchaseReceipt) -> Bool {
return !receipt.isCancelled && sixMonths(after: receipt.expiration) >= Date()
}

private func sixMonths(after date: Date) -> Date {
return date.addingTimeInterval(60 * 60 * 24 * 30 * 6)
}

0 comments on commit 46dcc87

Please sign in to comment.