Browse Source

Expose localReceiptData so that the receipt from the bundle can be loaded

Andrea Bizzotto 8 years ago
parent
commit
0df3b92b4a
2 changed files with 28 additions and 20 deletions
  1. 16 16
      SwiftyStoreKit/InAppReceipt.swift
  2. 12 4
      SwiftyStoreKit/SwiftyStoreKit.swift

+ 16 - 16
SwiftyStoreKit/InAppReceipt.swift

@@ -154,30 +154,30 @@ internal class InAppReceipt {
         case sandbox = "https://sandbox.itunes.apple.com/verifyReceipt"
     }
 
-    static var URL: Foundation.URL? {
+    static var appStoreReceiptUrl: URL? {
         return Bundle.main.appStoreReceiptURL
     }
 
-    static var data: Data? {
-        if let receiptDataURL = URL, let data = try? Data(contentsOf: receiptDataURL) {
-            return data
+    static var appStoreReceiptData: Data? {
+        guard let receiptDataURL = appStoreReceiptUrl, let data = try? Data(contentsOf: receiptDataURL) else {
+            return nil
         }
-        return nil
+        return data
     }
 
     // The base64 encoded receipt data.
-    static var base64EncodedString: String? {
-        return data?.base64EncodedString(options: [])
+    static var appStoreReceiptBase64Encoded: String? {
+        return appStoreReceiptData?.base64EncodedString(options: [])
     }
 
     // https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
 
-   /**
-    *  - Parameter receiptVerifyURL: receipt verify url (default: Production)
-    *  - Parameter password: Only used for receipts that contain auto-renewable subscriptions. Your app’s shared secret (a hexadecimal string).
-    *  - Parameter session: the session used to make remote call.
-    *  - Parameter completion: handler for result
-    */
+    /**
+     *  - Parameter receiptVerifyURL: receipt verify url (default: Production)
+     *  - Parameter password: Only used for receipts that contain auto-renewable subscriptions. Your app’s shared secret (a hexadecimal string).
+     *  - Parameter session: the session used to make remote call.
+     *  - Parameter completion: handler for result
+     */
     class func verify(
         urlType: VerifyReceiptURLType = .production,
         password autoRenewPassword: String? = nil,
@@ -185,18 +185,18 @@ internal class InAppReceipt {
         completion: @escaping (SwiftyStoreKit.VerifyReceiptResult) -> ()) {
 
             // If no receipt is present, validation fails.
-            guard let base64EncodedString = self.base64EncodedString else {
+            guard let base64EncodedString = appStoreReceiptBase64Encoded else {
                 completion(.error(error: .noReceiptData))
                 return
             }
 
             // Create request
-            let storeURL = Foundation.URL(string: urlType.rawValue)! // safe (until no more)
+            let storeURL = URL(string: urlType.rawValue)! // safe (until no more)
             let storeRequest = NSMutableURLRequest(url: storeURL)
             storeRequest.httpMethod = "POST"
 
 
-            let requestContents :NSMutableDictionary = [ "receipt-data" : base64EncodedString]
+            let requestContents: NSMutableDictionary = [ "receipt-data" : base64EncodedString ]
             // password if defined
             if let password = autoRenewPassword {
                 requestContents.setValue(password, forKey: "password")

+ 12 - 4
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -77,7 +77,7 @@ public class SwiftyStoreKit {
         public let restoreFailedProducts: [(Swift.Error, String?)]
     }
     public enum RefreshReceiptResult {
-        case success
+        case success(receiptData: Data)
         case error(error: Error)
     }
     public struct CompletedTransaction {
@@ -152,6 +152,13 @@ public class SwiftyStoreKit {
         }
     }
 
+    /**
+     * Return receipt data from the application bundle. This is read from Bundle.main.appStoreReceiptURL
+     */
+    public static var localReceiptData: Data? {
+        return InAppReceipt.appStoreReceiptData
+    }
+    
     /**
      *  Verify application receipt
      *  - Parameter password: Only used for receipts that contain auto-renewable subscriptions. Your app’s shared secret (a hexadecimal string).
@@ -209,10 +216,11 @@ public class SwiftyStoreKit {
 
             switch result {
             case .success:
-                if InAppReceipt.data == nil {
+                if let appStoreReceiptData = InAppReceipt.appStoreReceiptData {
+                    completion(.success(receiptData: appStoreReceiptData))
+                }
+                else {
                     completion(.error(error: ReceiptError.noReceiptData))
-                } else {
-                    completion(.success)
                 }
             case .error(let e):
                 completion(.error(error: e))