验证App Store上的应用内购买收据

时间:2017-06-13 13:11:03

标签: ios swift in-app-purchase

我使用以下功能验证收据。如您所见,我使用了沙盒URL,因为我在将它们提交到商店之前测试了收据。我想要学习的是:在我提交之前,我应该在storeUrl部分写什么?如何访问该URL或找到它?根据我的研究,我需要把我的服务器地址或东西,但我想提交的产品是非消费品,所以我没有任何服务器。

func verifyReciept (transaction : SKPaymentTransaction?) {

        let recieptURL = Bundle.main.appStoreReceiptURL!
        if let reciept = NSData(contentsOf: recieptURL){
            let requestContents = ["receipt-data" : reciept.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue:0))]

            do {

                let requestData = try JSONSerialization.data(withJSONObject: requestContents, options: JSONSerialization.WritingOptions(rawValue : 0))

                let storeURL = NSURL(string: "https:/sandbox.itunes.apple.com/verifyReceipt")
                let request = NSMutableURLRequest(url: storeURL! as URL)
                request.httpMethod = "Post"
                request.httpBody = requestData

               let session = URLSession.shared
                let task = session.dataTask(with: request as URLRequest, completionHandler: { (responseData: Data?, response : URLResponse?, error : Error?) -> Void in

                do {
                let json = try JSONSerialization.jsonObject(with: responseData!, options: .mutableLeaves) as! NSDictionary
                    print(json)

                    if(json.object(forKey: "status") as! NSNumber) == 0 {

                        let receipt_dict = json["receipt"] as! NSDictionary
                        if let purchases = receipt_dict["in_app"] as? NSArray {
                            self.validatePurchaseArray(purchases: purchases)
                        }
                        if transaction != nil {
                            SKPaymentQueue.default().finishTransaction(transaction!)
                        }
                    }

                    else {
                    print(json.object(forKey: "status") as! NSNumber)

                    }

                }

                catch{
                    print(error)

                    }
               })

                task.resume()

            } catch {

                print(error)
            }
        }
        else {

            print("No Reciept")
        }

    }

1 个答案:

答案 0 :(得分:4)

您应该使用prod URL替换它以验证收据:

"https://buy.itunes.apple.com/verifyReceipt"

您正在谈论涉及服务器的事情是您可以采取的另一项措施。对于服务器端验证,您需要将收据发送到服务器,然后从服务器运行相同的功能。