Skip to content

Commit

Permalink
Merge pull request #284 from bizz84/feature/fetchReceipt-replace-stri…
Browse files Browse the repository at this point in the history
…ng-with-data

fetchReceipt: replace String with Data
  • Loading branch information
bizz84 authored Oct 12, 2017
2 parents dfe88bb + 78b52c6 commit 71f4eb8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ Use this method to get the updated receipt:
```swift
SwiftyStoreKit.fetchReceipt(forceRefresh: true) { result in
switch result {
case .success(let encryptedReceipt):
case .success(let receiptData):
let encryptedReceipt = receiptData.base64EncodedString(options: [])
print("Fetch receipt success:\n\(encryptedReceipt)")
case .error(let error):
print("Fetch receipt failed: \(error)")
Expand Down
5 changes: 3 additions & 2 deletions SwiftyStoreKit/AppleReceiptValidator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public struct AppleReceiptValidator: ReceiptValidator {
self.sharedSecret = sharedSecret
}

public func validate(receipt: String, completion: @escaping (VerifyReceiptResult) -> Void) {
public func validate(receiptData: Data, completion: @escaping (VerifyReceiptResult) -> Void) {

let storeURL = URL(string: service.rawValue)! // safe (until no more)
let storeRequest = NSMutableURLRequest(url: storeURL)
storeRequest.httpMethod = "POST"

let receipt = receiptData.base64EncodedString(options: [])
let requestContents: NSMutableDictionary = [ "receipt-data": receipt ]
// password if defined
if let password = sharedSecret {
Expand Down Expand Up @@ -106,7 +107,7 @@ public struct AppleReceiptValidator: ReceiptValidator {
let receiptStatus = ReceiptStatus(rawValue: status) ?? ReceiptStatus.unknown
if case .testReceipt = receiptStatus {
let sandboxValidator = AppleReceiptValidator(service: .sandbox, sharedSecret: self.sharedSecret)
sandboxValidator.validate(receipt: receipt, completion: completion)
sandboxValidator.validate(receiptData: receiptData, completion: completion)
} else {
if receiptStatus.isValid {
completion(.success(receipt: receiptInfo))
Expand Down
18 changes: 6 additions & 12 deletions SwiftyStoreKit/InAppReceiptVerificator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class InAppReceiptVerificator: NSObject {

fetchReceipt(forceRefresh: forceRefresh, refresh: refresh) { result in
switch result {
case .success(let encryptedReceipt):
self.verify(receipt: encryptedReceipt, using: validator, completion: completion)
case .success(let receiptData):
self.verify(receiptData: receiptData, using: validator, completion: completion)
case .error(let error):
completion(.error(error: error))
}
Expand All @@ -77,7 +77,7 @@ class InAppReceiptVerificator: NSObject {
completion: @escaping (FetchReceiptResult) -> Void) {

if let receiptData = appStoreReceiptData, forceRefresh == false {
fetchReceiptSuccessHandler(receiptData: receiptData, completion: completion)
completion(.success(receiptData: receiptData))
} else {

receiptRefreshRequest = refresh(nil) { result in
Expand All @@ -87,7 +87,7 @@ class InAppReceiptVerificator: NSObject {
switch result {
case .success:
if let receiptData = self.appStoreReceiptData {
self.fetchReceiptSuccessHandler(receiptData: receiptData, completion: completion)
completion(.success(receiptData: receiptData))
} else {
completion(.error(error: .noReceiptData))
}
Expand All @@ -97,21 +97,15 @@ class InAppReceiptVerificator: NSObject {
}
}
}

private func fetchReceiptSuccessHandler(receiptData: Data, completion: (FetchReceiptResult) -> Void) {

let base64EncodedString = receiptData.base64EncodedString(options: [])
completion(.success(encryptedReceipt: base64EncodedString))
}

/**
* - Parameter receiptData: encrypted receipt data
* - Parameter validator: Validator to check the encrypted receipt and return the receipt in readable format
* - Parameter completion: handler for result
*/
private func verify(receipt: String, using validator: ReceiptValidator, completion: @escaping (VerifyReceiptResult) -> Void) {
private func verify(receiptData: Data, using validator: ReceiptValidator, completion: @escaping (VerifyReceiptResult) -> Void) {

validator.validate(receipt: receipt) { result in
validator.validate(receiptData: receiptData) { result in

DispatchQueue.main.async {
completion(result)
Expand Down
10 changes: 2 additions & 8 deletions SwiftyStoreKit/SwiftyStoreKit+Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct PurchaseDetails {

//Conform to this protocol to provide custom receipt validator
public protocol ReceiptValidator {
func validate(receipt: String, completion: @escaping (VerifyReceiptResult) -> Void)
func validate(receiptData: Data, completion: @escaping (VerifyReceiptResult) -> Void)
}

// Payment transaction
Expand Down Expand Up @@ -83,15 +83,9 @@ public typealias ShouldAddStorePaymentHandler = (_ payment: SKPayment, _ product
// Info for receipt returned by server
public typealias ReceiptInfo = [String: AnyObject]

// Refresh receipt result
public enum RefreshReceiptResult {
case success(receiptData: Data)
case error(error: Error)
}

// Fetch receipt result
public enum FetchReceiptResult {
case success(encryptedReceipt: String)
case success(receiptData: Data)
case error(error: ReceiptError)
}

Expand Down
2 changes: 1 addition & 1 deletion SwiftyStoreKitTests/InAppReceiptVerificatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import XCTest

class TestReceiptValidator: ReceiptValidator {
var validateCalled = false
func validate(receipt: String, completion: @escaping (VerifyReceiptResult) -> Void) {
func validate(receiptData: Data, completion: @escaping (VerifyReceiptResult) -> Void) {
validateCalled = true
completion(.success(receipt: [:]))
}
Expand Down

0 comments on commit 71f4eb8

Please sign in to comment.