Просмотр исходного кода

Add remote receipt validation and refresh(iOS)

phimage 9 лет назад
Родитель
Сommit
2e6773b2e4

+ 18 - 6
SwiftStoreOSXDemo/Base.lproj/Main.storyboard

@@ -669,11 +669,11 @@
             <objects>
                 <viewController id="XfG-lQ-9wD" customClass="ViewController" customModule="SwiftStoreOSXDemo" customModuleProvider="target" sceneMemberID="viewController">
                     <view key="view" id="m2S-Jp-Qdl">
-                        <rect key="frame" x="0.0" y="0.0" width="480" height="270"/>
+                        <rect key="frame" x="0.0" y="0.0" width="480" height="305"/>
                         <autoresizingMask key="autoresizingMask"/>
                         <subviews>
                             <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="4ix-eb-y5G">
-                                <rect key="frame" x="162" y="13" width="156" height="32"/>
+                                <rect key="frame" x="162" y="48" width="156" height="32"/>
                                 <buttonCell key="cell" type="push" title="Restore Purchases" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="mmO-hG-Xyq">
                                     <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
                                     <font key="font" metaFont="system"/>
@@ -683,7 +683,7 @@
                                 </buttonCell>
                             </button>
                             <customView translatesAutoresizingMaskIntoConstraints="NO" id="aBh-sv-MN6">
-                                <rect key="frame" x="0.0" y="49" width="480" height="176"/>
+                                <rect key="frame" x="0.0" y="84" width="480" height="176"/>
                                 <subviews>
                                     <splitView focusRingType="none" dividerStyle="thin" vertical="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dZR-88-4H6">
                                         <rect key="frame" x="0.0" y="0.0" width="480" height="176"/>
@@ -787,19 +787,31 @@
                                 </constraints>
                             </customView>
                             <textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="fQC-8G-INF">
-                                <rect key="frame" x="175" y="233" width="131" height="17"/>
+                                <rect key="frame" x="175" y="268" width="131" height="17"/>
                                 <textFieldCell key="cell" sendsActionOnEndEditing="YES" title="Available purchases:" id="Ntg-PD-43y">
                                     <font key="font" metaFont="system"/>
                                     <color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
                                     <color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
                                 </textFieldCell>
                             </textField>
+                            <button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Yzl-nj-7ol">
+                                <rect key="frame" x="178" y="15" width="125" height="32"/>
+                                <buttonCell key="cell" type="push" title="Verify Receipt" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="6bY-Fv-W6P">
+                                    <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
+                                    <font key="font" metaFont="system"/>
+                                </buttonCell>
+                                <connections>
+                                    <action selector="verifyReceipt:" target="XfG-lQ-9wD" id="uf9-Pe-MRz"/>
+                                </connections>
+                            </button>
                         </subviews>
                         <constraints>
+                            <constraint firstAttribute="bottom" secondItem="Yzl-nj-7ol" secondAttribute="bottom" constant="22" id="979-EH-fNb"/>
+                            <constraint firstItem="Yzl-nj-7ol" firstAttribute="top" secondItem="4ix-eb-y5G" secondAttribute="bottom" constant="12" id="B88-P1-tBj"/>
                             <constraint firstAttribute="trailing" secondItem="aBh-sv-MN6" secondAttribute="trailing" id="F8t-R2-cad"/>
                             <constraint firstItem="fQC-8G-INF" firstAttribute="centerX" secondItem="m2S-Jp-Qdl" secondAttribute="centerX" id="RDD-02-SB0"/>
                             <constraint firstItem="4ix-eb-y5G" firstAttribute="top" secondItem="aBh-sv-MN6" secondAttribute="bottom" constant="8" id="b6U-zT-POc"/>
-                            <constraint firstAttribute="bottom" secondItem="4ix-eb-y5G" secondAttribute="bottom" constant="20" id="crv-3H-ZKn"/>
+                            <constraint firstItem="Yzl-nj-7ol" firstAttribute="centerX" secondItem="m2S-Jp-Qdl" secondAttribute="centerX" id="dQi-Vh-Bwb"/>
                             <constraint firstItem="aBh-sv-MN6" firstAttribute="leading" secondItem="m2S-Jp-Qdl" secondAttribute="leading" id="enN-Hg-kUA"/>
                             <constraint firstItem="fQC-8G-INF" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" constant="20" id="kMt-Ye-T67"/>
                             <constraint firstItem="aBh-sv-MN6" firstAttribute="top" secondItem="fQC-8G-INF" secondAttribute="bottom" constant="8" id="rz5-by-FYa"/>
@@ -809,7 +821,7 @@
                 </viewController>
                 <customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
             </objects>
-            <point key="canvasLocation" x="75" y="655"/>
+            <point key="canvasLocation" x="75" y="672.5"/>
         </scene>
     </scenes>
 </document>

+ 18 - 0
SwiftStoreOSXDemo/ViewController.swift

@@ -85,6 +85,7 @@ class ViewController: NSViewController {
             }
         }
     }
+
     @IBAction func restorePurchases(sender: AnyObject?) {
 
         SwiftyStoreKit.restorePurchases() { result in
@@ -104,5 +105,22 @@ class ViewController: NSViewController {
         }
     }
 
+    @IBAction func verifyReceipt(ender: AnyObject?) {
+
+        SwiftyStoreKit.verifyReceipt() { result in
+            switch result {
+            case .Success(let receipt):
+                self.showMessage("Receipt verified", message: "Receipt verified remotly")
+                print("Verify receipt Success: \(receipt)")
+                break
+            case .Error(let error):
+                print("Verify receipt Failed: \(error)")
+                self.showMessage("Receipt verification failed", message: "The application will exit to create receipt data. You must have signed the application with your developper id to test and be outside of XCode")
+                exit(ReceiptExitCode.NotValid.rawValue)
+                break
+            }
+        }
+    }
+
 }
 

+ 27 - 11
SwiftyStoreDemo/Base.lproj/Main.storyboard

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14E11f" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9531" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
     <dependencies>
         <deployment identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
     </dependencies>
     <scenes>
         <!--View Controller-->
@@ -22,22 +22,37 @@
                             </imageView>
                             <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="R2x-3P-rjx" userLabel="Opaque">
                                 <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
+                                <subviews>
+                                    <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Hpv-ED-Dlg">
+                                        <rect key="frame" x="233" y="391" width="135" height="36"/>
+                                        <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
+                                        <state key="normal" title="Verify Receipt">
+                                            <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                                        </state>
+                                        <connections>
+                                            <action selector="verifyReceipt" destination="BYZ-38-t0r" eventType="touchUpInside" id="61I-gF-E2O"/>
+                                        </connections>
+                                    </button>
+                                </subviews>
                                 <color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.29999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
+                                <constraints>
+                                    <constraint firstItem="Hpv-ED-Dlg" firstAttribute="centerX" secondItem="R2x-3P-rjx" secondAttribute="centerX" id="Zsj-pM-1iO"/>
+                                </constraints>
                             </view>
                             <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="SwiftyStoreKit" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FTs-3H-z8C">
-                                <rect key="frame" x="218" y="40" width="163" height="32.5"/>
+                                <rect key="frame" x="218" y="40" width="163" height="33"/>
                                 <fontDescription key="fontDescription" style="UICTFontTextStyleTitle1"/>
                                 <color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
                                 <nil key="highlightedColor"/>
                             </label>
                             <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Lightweight In App Purchases framework for iOS 8.0+" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="gg1-bw-Mzz">
-                                <rect key="frame" x="10" y="74.5" width="580" height="16"/>
+                                <rect key="frame" x="10" y="75" width="580" height="16"/>
                                 <fontDescription key="fontDescription" type="system" pointSize="13"/>
                                 <color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
                                 <nil key="highlightedColor"/>
                             </label>
                             <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Available purchases:" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="s8k-6i-mKn">
-                                <rect key="frame" x="222" y="143.5" width="156" height="20.5"/>
+                                <rect key="frame" x="222" y="143" width="156" height="21"/>
                                 <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                 <color key="textColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="calibratedRGB"/>
                                 <nil key="highlightedColor"/>
@@ -49,13 +64,13 @@
                                         <rect key="frame" x="0.0" y="0.0" width="91" height="113"/>
                                         <subviews>
                                             <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="5 days" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="uz9-cT-1WH">
-                                                <rect key="frame" x="0.0" y="0.0" width="91" height="20.5"/>
+                                                <rect key="frame" x="0.0" y="0.0" width="91" height="21"/>
                                                 <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                 <color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                                 <nil key="highlightedColor"/>
                                             </label>
                                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="miS-cF-iGP">
-                                                <rect key="frame" x="0.0" y="35.5" width="91" height="36"/>
+                                                <rect key="frame" x="0.0" y="36" width="91" height="36"/>
                                                 <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                 <state key="normal" title="Get Info">
                                                     <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
@@ -65,7 +80,7 @@
                                                 </connections>
                                             </button>
                                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="DnE-MP-9Li">
-                                                <rect key="frame" x="0.0" y="76.5" width="91" height="36"/>
+                                                <rect key="frame" x="0.0" y="77" width="91" height="36"/>
                                                 <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                 <state key="normal" title="Purchase">
                                                     <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
@@ -107,13 +122,13 @@
                                         <rect key="frame" x="191" y="0.0" width="91" height="113"/>
                                         <subviews>
                                             <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="10 days" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6UI-uh-w5n">
-                                                <rect key="frame" x="0.0" y="0.0" width="91" height="20.5"/>
+                                                <rect key="frame" x="0.0" y="0.0" width="91" height="21"/>
                                                 <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                                 <color key="textColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                                 <nil key="highlightedColor"/>
                                             </label>
                                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="L02-jg-tgd">
-                                                <rect key="frame" x="0.0" y="35.5" width="91" height="36"/>
+                                                <rect key="frame" x="0.0" y="36" width="91" height="36"/>
                                                 <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                 <state key="normal" title="Get Info">
                                                     <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
@@ -123,7 +138,7 @@
                                                 </connections>
                                             </button>
                                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XP6-KY-8rd">
-                                                <rect key="frame" x="0.0" y="76.5" width="91" height="36"/>
+                                                <rect key="frame" x="0.0" y="77" width="91" height="36"/>
                                                 <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                 <state key="normal" title="Purchase">
                                                     <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
@@ -184,6 +199,7 @@
                         </subviews>
                         <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                         <constraints>
+                            <constraint firstItem="Hpv-ED-Dlg" firstAttribute="top" secondItem="CG8-Ue-vcg" secondAttribute="bottom" constant="8" id="1yX-Os-AKT"/>
                             <constraint firstItem="FII-Z2-VOo" firstAttribute="top" secondItem="s8k-6i-mKn" secondAttribute="bottom" constant="30" id="4jU-Ih-evH"/>
                             <constraint firstItem="FII-Z2-VOo" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="50" id="5RH-5E-DHg"/>
                             <constraint firstAttribute="trailing" secondItem="JDz-7n-4vD" secondAttribute="trailing" id="GBV-Mt-YQc"/>

+ 36 - 1
SwiftyStoreDemo/ViewController.swift

@@ -104,7 +104,42 @@ class ViewController: UIViewController {
             }
         }
     }
-    
+
+    @IBAction func verifyReceipt() {
+
+        NetworkActivityIndicatorManager.networkOperationStarted()
+        SwiftyStoreKit.verifyReceipt() { result in
+            NetworkActivityIndicatorManager.networkOperationFinished()
+            switch result {
+            case .Success(let receipt):
+                self.showMessage("Receipt verified", message: "Receipt verified remotly")
+                print("Verify receipt Success: \(receipt)")
+                break
+            case .Error(let error):
+                print("Verify receipt Failed: \(error)")
+                switch (error) {
+                case .NoReceiptData :
+                    self.showMessage("Receipt verification", message: "No receipt data, application will try to get a new one. Try again.")
+
+                    SwiftyStoreKit.receiptRefresh { (result) -> () in
+                        switch result {
+                        case .Success:
+                            self.showMessage("Receipt refreshed", message: "Receipt refreshed with success")
+                            print("Receipt refreshed Success")
+                            break
+                        case .Error(let error):
+                            print("Receipt refreshed Failed: \(error)")
+                            break
+                        }
+                    }
+
+                default: break
+                }
+                break
+            }
+        }
+    }
+
     override func preferredStatusBarStyle() -> UIStatusBarStyle {
         return .LightContent
     }

+ 11 - 0
SwiftyStoreKit.xcodeproj/project.pbxproj

@@ -19,8 +19,11 @@
 		6502F63B1B985CA1004E342D /* InAppProductQueryRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6502F6231B98586A004E342D /* InAppProductQueryRequest.swift */; };
 		6502F63C1B985CA4004E342D /* SwiftyStoreKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6502F6241B98586A004E342D /* SwiftyStoreKit.swift */; };
 		65C1B5DF1BB9DE9B00F7BF4E /* NetworkActivityIndicatorManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65C1B5DE1BB9DE9B00F7BF4E /* NetworkActivityIndicatorManager.swift */; };
+		C4083C551C2AADB500295248 /* InAppReceipt.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A7C7621C29B8D00053ED64 /* InAppReceipt.swift */; };
+		C4083C571C2AB0A900295248 /* InAppReceiptRefreshRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4083C561C2AB0A900295248 /* InAppReceiptRefreshRequest.swift */; };
 		C40C68101C29414C00B60B7E /* OS.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C680F1C29414C00B60B7E /* OS.swift */; };
 		C40C68111C29419500B60B7E /* OS.swift in Sources */ = {isa = PBXBuildFile; fileRef = C40C680F1C29414C00B60B7E /* OS.swift */; };
+		C4A7C7631C29B8D00053ED64 /* InAppReceipt.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4A7C7621C29B8D00053ED64 /* InAppReceipt.swift */; };
 		C4D74BBE1C24CECA0071AD3E /* SwiftyStoreKitOSX.h in Headers */ = {isa = PBXBuildFile; fileRef = C4D74BBD1C24CECA0071AD3E /* SwiftyStoreKitOSX.h */; settings = {ATTRIBUTES = (Public, ); }; };
 		C4D74BC31C24CEDC0071AD3E /* InAppProductPurchaseRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6502F6221B98586A004E342D /* InAppProductPurchaseRequest.swift */; };
 		C4D74BC41C24CEDC0071AD3E /* InAppProductQueryRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6502F6231B98586A004E342D /* InAppProductQueryRequest.swift */; };
@@ -90,7 +93,9 @@
 		6502F62F1B985C40004E342D /* SwiftyStoreKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftyStoreKit.h; sourceTree = "<group>"; };
 		6502F6311B985C40004E342D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		65C1B5DE1BB9DE9B00F7BF4E /* NetworkActivityIndicatorManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkActivityIndicatorManager.swift; sourceTree = "<group>"; };
+		C4083C561C2AB0A900295248 /* InAppReceiptRefreshRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InAppReceiptRefreshRequest.swift; sourceTree = "<group>"; };
 		C40C680F1C29414C00B60B7E /* OS.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OS.swift; sourceTree = "<group>"; };
+		C4A7C7621C29B8D00053ED64 /* InAppReceipt.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InAppReceipt.swift; sourceTree = "<group>"; };
 		C4D74BBB1C24CEC90071AD3E /* SwiftyStoreKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftyStoreKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		C4D74BBD1C24CECA0071AD3E /* SwiftyStoreKitOSX.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftyStoreKitOSX.h; sourceTree = "<group>"; };
 		C4D74BBF1C24CECA0071AD3E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -163,6 +168,8 @@
 			children = (
 				6502F6221B98586A004E342D /* InAppProductPurchaseRequest.swift */,
 				6502F6231B98586A004E342D /* InAppProductQueryRequest.swift */,
+				C4083C561C2AB0A900295248 /* InAppReceiptRefreshRequest.swift */,
+				C4A7C7621C29B8D00053ED64 /* InAppReceipt.swift */,
 				6502F6241B98586A004E342D /* SwiftyStoreKit.swift */,
 				C40C680F1C29414C00B60B7E /* OS.swift */,
 				6502F62F1B985C40004E342D /* SwiftyStoreKit.h */,
@@ -399,8 +406,10 @@
 			buildActionMask = 2147483647;
 			files = (
 				6502F63A1B985C9E004E342D /* InAppProductPurchaseRequest.swift in Sources */,
+				C4A7C7631C29B8D00053ED64 /* InAppReceipt.swift in Sources */,
 				C40C68101C29414C00B60B7E /* OS.swift in Sources */,
 				6502F63B1B985CA1004E342D /* InAppProductQueryRequest.swift in Sources */,
+				C4083C571C2AB0A900295248 /* InAppReceiptRefreshRequest.swift in Sources */,
 				6502F63C1B985CA4004E342D /* SwiftyStoreKit.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -410,6 +419,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				C4D74BC31C24CEDC0071AD3E /* InAppProductPurchaseRequest.swift in Sources */,
+				C4083C551C2AADB500295248 /* InAppReceipt.swift in Sources */,
 				C40C68111C29419500B60B7E /* OS.swift in Sources */,
 				C4D74BC41C24CEDC0071AD3E /* InAppProductQueryRequest.swift in Sources */,
 				C4D74BC51C24CEDC0071AD3E /* SwiftyStoreKit.swift in Sources */,
@@ -745,6 +755,7 @@
 				C4FD3A0F1C2954C10035CFF3 /* Release */,
 			);
 			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
 		};
 /* End XCConfigurationList section */
 	};

+ 220 - 0
SwiftyStoreKit/InAppReceipt.swift

@@ -0,0 +1,220 @@
+//
+//  InAppReceipt.swift
+//  SwiftyStoreKit
+//
+//  Created by phimage on 22/12/15.
+// Copyright (c) 2015 Andrea Bizzotto (bizz84@gmail.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import Foundation
+
+// Info for receipt return by server
+public typealias ReceiptInfo = [String: AnyObject]
+
+// MARK: - Enumeration
+
+public enum ValidReceiptResultType {
+    case Success(receipt: ReceiptInfo)
+    case Error(error: ReceiptError)
+}
+
+public enum ReceiptError : ErrorType {
+    case NoReceiptData
+    case NoRemoteData
+    case RequestBodyEncodeError(error: ErrorType)
+    case JSONDecodeError(string: String?)
+    case NetworkError(error: ErrorType)
+    case AppStore(status: Int)
+    case ReceiptInvalid(receipt: ReceiptInfo, status: ReceiptStatus)
+}
+
+// Status code returned by remote server
+// see Table 2-1  Status codes
+public enum ReceiptStatus: Int {
+    case Unknown = -2 // Not decodable status
+    case None = -1 // No status returned
+
+    case Valid = 0 // valid status
+
+    case JSONNotReadable = 21000 // The App Store could not read the JSON object you provided.
+    case MalformedOrMissingData = 21002 // The data in the receipt-data property was malformed or missing.
+
+    case ReceiptCouldNotBeAuthenticated = 21003 // The receipt could not be authenticated.
+    case SecretNotMatching = 21004 // The shared secret you provided does not match the shared secret on file for your account.
+    case ReceiptServerUnavailable = 21005 // The receipt server is not currently available.
+    case SubscriptionExpired = 21006 // This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.
+    case TestReceipt = 21007 //  This receipt is from the test environment, but it was sent to the production environment for verification. Send it to the test environment instead.
+    case ProductionEnvironment = 21008 // This receipt is from the production environment, but it was sent to the test environment for verification. Send it to the production environment instead.
+}
+
+// Receipt field as defined in : https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1
+public enum ReceiptInfoField {
+    case bundle_id // Bundle Identifier. This corresponds to the value of CFBundleIdentifier in the Info.plist file.
+    case application_version //  The app’s version number.This corresponds to the value of CFBundleVersion (in iOS) or CFBundleShortVersionString (in OS X) in the Info.plist.
+    case  original_application_version // The version of the app that was originally purchased. This corresponds to the value of CFBundleVersion (in iOS) or CFBundleShortVersionString (in OS X) in the Info.plist file when the purchase was originally made.
+    case creation_date // The date when the app receipt was created.
+    case expiration_date // The date that the app receipt expires. This key is present only for apps purchased through the Volume Purchase Program.
+
+    case in_app // The receipt for an in-app purchase.
+
+    public enum InApp {
+
+        case quantity// The number of items purchased. This value corresponds to the quantity property of the SKPayment object stored in the transaction’s payment property.
+
+        case product_id // The product identifier of the item that was purchased. This value corresponds to the productIdentifier property of the SKPayment object stored in the transaction’s payment property.
+
+        case transaction_id // The transaction identifier of the item that was purchased. This value corresponds to the transaction’s transactionIdentifier property.
+
+        case original_transaction_id// For a transaction that restores a previous transaction, the transaction identifier of the original transaction. Otherwise, identical to the transaction identifier. This value corresponds to the original transaction’s transactionIdentifier property. All receipts in a chain of renewals for an auto-renewable subscription have the same value for this field.
+
+        case purchase_date // The date and time that the item was purchased. This value corresponds to the transaction’s transactionDate property.
+
+        case original_purchase_date // For a transaction that restores a previous transaction, the date of the original transaction. This value corresponds to the original transaction’s transactionDate property. In an auto-renewable subscription receipt, this indicates the beginning of the subscription period, even if the subscription has been renewed.
+
+        case expires_date// The expiration date for the subscription, expressed as the number of milliseconds since January 1, 1970, 00:00:00 GMT. This key is only present for auto-renewable subscription receipts.
+
+        case cancellation_date // For a transaction that was canceled by Apple customer support, the time and date of the cancellation. Treat a canceled receipt the same as if no purchase had ever been made.
+
+        #if os(iOS)
+        case app_item_id // A string that the App Store uses to uniquely identify the application that created the transaction. If your server supports multiple applications, you can use this value to differentiate between them. Apps are assigned an identifier only in the production environment, so this key is not present for receipts created in the test environment. This field is not present for Mac apps. See also Bundle Identifier.
+        #endif
+
+        case version_external_identifier // An arbitrary number that uniquely identifies a revision of your application. This key is not present for receipts created in the test environment.
+        
+        case web_order_line_item_id // The primary key for identifying subscription purchases.
+    }
+}
+
+// URL used to verify
+public enum ReceiptVerifyURL: String {
+    case Production = "https://buy.itunes.apple.com/verifyReceipt"
+    case Test = "https://sandbox.itunes.apple.com/verifyReceipt"
+}
+extension Bool {
+    var toReceiptVerifyURL: ReceiptVerifyURL {
+        return self ? .Test : .Production
+    }
+}
+
+#if os(OSX)
+    public enum ReceiptExitCode: Int32 {
+        case NotValid = 173 // If validation fails in OS X, call exit with a status of 173. This exit status notifies the system that your application has determined that its receipt is invalid. At this point, the system attempts to obtain a valid receipt and may prompt for the user’s iTunes credentials
+    }
+#endif
+
+// MARK - receipt mangement
+internal class InAppReceipt {
+
+    static var URL: NSURL? {
+        return NSBundle.mainBundle().appStoreReceiptURL
+    }
+
+    static var data: NSData? {
+        if let receiptDataURL = URL, data = NSData(contentsOfURL: receiptDataURL) {
+            return data
+        }
+        print(NSBundle.mainBundle().appStoreReceiptURL)
+        return nil
+    }
+
+    // The base64 encoded receipt data.
+    static var base64EncodedString: String? {
+        return data?.base64EncodedStringWithOptions([])
+    }
+
+    // https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
+
+   /**
+    *  - Parameter test: if true use test environment remote URL otherwise production one (default: true)
+    *  - 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(
+        test: Bool = true,
+        password autoRenewPassword: String? = nil,
+        session: NSURLSession = NSURLSession.sharedSession(),
+        completion:(result: ValidReceiptResultType) -> ()) {
+
+            // If no receipt is present, validation fails.
+            guard let base64EncodedString = self.base64EncodedString else {
+                completion(result: .Error(error: .NoReceiptData))
+                return
+            }
+
+            // Create request
+            let storeURL = NSURL(string: test.toReceiptVerifyURL.rawValue)! // safe (until no more)
+            let storeRequest = NSMutableURLRequest(URL: storeURL)
+            storeRequest.HTTPMethod = "POST"
+
+
+            let requestContents :NSMutableDictionary = [ "receipt-data" : base64EncodedString]
+            // password if defined
+            if let password = autoRenewPassword {
+                requestContents.setValue(password, forKey: "password")
+            }
+
+            // Encore request body
+            do {
+                storeRequest.HTTPBody = try NSJSONSerialization.dataWithJSONObject(requestContents, options: [])
+            } catch let e {
+                completion(result: .Error(error: .RequestBodyEncodeError(error: e)))
+                return
+            }
+
+            // Remote task
+            let task = session.dataTaskWithRequest(storeRequest) { data, response, error -> Void in
+
+                // there is an error
+                if let networkError = error {
+                    completion(result: .Error(error: .NetworkError(error: networkError)))
+                    return
+                }
+
+                // there is no data
+                guard let safeData = data else {
+                    completion(result:.Error(error: .NoRemoteData))
+                    return
+                }
+
+                // cannot decode data
+                guard let receiptInfo = try? NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? ReceiptInfo ?? [:] else {
+                    let jsonStr = String(data: safeData, encoding: NSUTF8StringEncoding)
+                    completion(result: .Error(error: .JSONDecodeError(string: jsonStr)))
+                    return
+                }
+
+                // get status from info
+                if let status = receiptInfo["status"] as? Int {
+                    if status == ReceiptStatus.Valid.rawValue {
+                        completion(result: .Success(receipt: receiptInfo))
+                    }
+                    else {
+                        completion(result: .Error(error: .ReceiptInvalid(receipt: receiptInfo, status: ReceiptStatus(rawValue: status) ?? ReceiptStatus.Unknown)))
+                    }
+                }
+                else {
+                    completion(result: .Error(error: .ReceiptInvalid(receipt: receiptInfo, status: ReceiptStatus.None)))
+                }
+            }
+            task.resume()
+    }
+    
+}

+ 74 - 0
SwiftyStoreKit/InAppReceiptRefreshRequest.swift

@@ -0,0 +1,74 @@
+//
+//  InAppReceiptRefreshRequest.swift
+//  SwiftyStoreKit
+//
+//  Created by phimage on 23/12/15.
+// Copyright (c) 2015 Andrea Bizzotto (bizz84@gmail.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+import StoreKit
+import Foundation
+
+#if os(iOS)
+    class InAppReceiptRefreshRequest: NSObject, SKRequestDelegate {
+
+        enum ResultType {
+            case Success
+            case Error(e: NSError)
+        }
+
+        typealias RequestCallback = (result: ResultType) -> ()
+
+        class func refresh(receiptProperties: [String : AnyObject]? = nil, callback: RequestCallback) -> InAppReceiptRefreshRequest {
+            let request = InAppReceiptRefreshRequest(receiptProperties: receiptProperties, callback: callback)
+            request.start()
+            return request
+        }
+
+        let refreshReceiptRequest: SKReceiptRefreshRequest
+        let callback: RequestCallback
+
+        init(receiptProperties: [String : AnyObject]? = nil, callback: RequestCallback) {
+            self.callback = callback
+            self.refreshReceiptRequest = SKReceiptRefreshRequest(receiptProperties: receiptProperties)
+            super.init()
+            self.refreshReceiptRequest.delegate = self
+        }
+
+        func start() {
+            self.refreshReceiptRequest.start()
+        }
+
+        func requestDidFinish(request: SKRequest) {
+            /*if let resoreRequest = request as? SKReceiptRefreshRequest {
+                let receiptProperties = resoreRequest.receiptProperties ?? [:]
+                for (k, v) in receiptProperties {
+                    print("\(k): \(v)")
+                }
+            }*/
+            callback(result: .Success)
+        }
+        func request(request: SKRequest, didFailWithError error: NSError) {
+            // XXX could here check domain and error code to return typed exception
+            callback(result: .Error(e: error))
+        }
+        
+    }
+#endif

+ 45 - 1
SwiftyStoreKit/SwiftyStoreKit.swift

@@ -41,7 +41,9 @@ public class SwiftyStoreKit {
     private var inflightQueries: [String: InAppProductQueryRequest] = [:]
     private var inflightPurchases: [String: InAppProductPurchaseRequest] = [:]
     private var restoreRequest: InAppProductPurchaseRequest?
-
+    #if os(iOS)
+    private var receiptRefreshRequest: InAppReceiptRefreshRequest?
+    #endif
     // MARK: Enums
     public enum PurchaseResultType {
         case Success(productId: String)
@@ -56,6 +58,11 @@ public class SwiftyStoreKit {
         case Error(error: ErrorType)
         case NothingToRestore
     }
+    public enum RefreshReceiptResultType {
+        case Success
+        case Error(error: ErrorType)
+    }
+
 
     // MARK: Singleton
     private static let sharedInstance = SwiftyStoreKit()
@@ -109,6 +116,43 @@ public class SwiftyStoreKit {
         }
     }
 
+    /**
+     *  - Parameter test: if true use test environment remote URL otherwise production one (default: true)
+     *  - 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
+     */
+    public class func verifyReceipt(
+        test: Bool = true,
+        password: String? = nil,
+        session: NSURLSession = NSURLSession.sharedSession(),
+        completion:(result: ValidReceiptResultType) -> ()) {
+            InAppReceipt.verify(test, password: password, session: session, completion: completion)
+    }
+
+    #if os(iOS)
+    public class func receiptRefresh(receiptProperties: [String : AnyObject]? = nil, completion: (result: RefreshReceiptResultType) -> ()) {
+        sharedInstance.receiptRefreshRequest = InAppReceiptRefreshRequest.refresh(receiptProperties) { result in
+
+            sharedInstance.receiptRefreshRequest = nil
+
+            switch result {
+            case .Success:
+                if InAppReceipt.data == nil {
+                    completion(result: .Error(error: ReceiptError.NoReceiptData))
+                } else {
+                    completion(result: .Success)
+                }
+                break
+            case .Error(let e):
+                completion(result: .Error(error: e))
+                break
+            }
+        }
+
+    }
+    #endif
+
     // MARK: private methods
     private func purchase(product product: SKProduct, completion: (result: PurchaseResultType) -> ()) {
         guard let productIdentifier = product._productIdentifier else {