Esat kemal Ekren
Esat kemal Ekren

Reputation: 556

Verify Receipt for In-App Purchase on App Store

I use the function below to verify receipts. As you can see, I used sandbox URL because I test the reciepts before I submit them to the store. What I want to learn is this: before I submit, what should I write in the storeUrl section? How can I access that URL or find it? According to my research, I need to put my server address or something, but the product that I want to submit is non-consumable so I do not have any server for that.

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")
        }

    }

Upvotes: 1

Views: 1366

Answers (1)

l-l
l-l

Reputation: 3864

You should replace that with the prod URL for verifying receipts:

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

The thing you are talking about involving you server is an additional measure you can take. For the server side verification, you need to send the receipt to your server and then run this same function from your server.

Upvotes: 4

Related Questions