Last active
August 5, 2016 16:31
-
-
Save eofster/d8d45aeb587ce9bf241096130f9442e2 to your computer and use it in GitHub Desktop.
Swift version of the receipt attributes validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class ReceiptAttributesValidation: NSObject { | |
private let origin: ReceiptValidation | |
private let attributes: ReceiptAttributes | |
init(origin: ReceiptValidation, attributes: ReceiptAttributes) { | |
self.origin = origin | |
self.attributes = attributes | |
} | |
} | |
extension ReceiptAttributesValidation: ReceiptValidation { | |
func validateReceipt(data: NSData, completion: (Result) -> Void) { | |
if let payload = ReceiptPayload(data: data) where validate(payload) { | |
origin.validateReceipt(data, completion: completion) | |
} else { | |
completion(.ReceiptIsInvalid) | |
} | |
} | |
private func validate(payload: ReceiptPayload) -> Bool { | |
let checksum = ReceiptChecksum(guid: attributes.guid, opaque: payload.opaque, identifier: payload.identifierData) | |
return payload.identifier == attributes.identifier && | |
payload.version == attributes.version && | |
payload.checksum == checksum.dataValue | |
} | |
} | |
protocol ReceiptAttributes { | |
var identifier: String { get } | |
var version: String { get } | |
var guid: NSData { get } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment