Skip to content

Instantly share code, notes, and snippets.

@eofster
Last active August 5, 2016 16:31
Show Gist options
  • Save eofster/d8d45aeb587ce9bf241096130f9442e2 to your computer and use it in GitHub Desktop.
Save eofster/d8d45aeb587ce9bf241096130f9442e2 to your computer and use it in GitHub Desktop.
Swift version of the receipt attributes validation
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