mjpablo23
mjpablo23

Reputation: 731

swift 3 iTunes subscription receipt validation live mode

I am trying to get subscriptions working. I have the receipt validation working in sandbox mode, but it does not work yet when I use the live url for receipt validation. Here is the code I have:

func receiptValidation() {

    // appStoreReceiptURL still says sandboxReceipt

    if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
        FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {

        do {
            let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)

            let receiptString = receiptData.base64EncodedString(options: [])
            let dict = ["receipt-data" : receiptString, "password" : iTunesMasterAppSecret] as [String : Any]

            do {
                let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)

var receiptUrl:String
                    if (liveMode) {
                        receiptUrl = "https://buy.itunes.apple.com/verifyReceipt"
                    }
                    else {
                        receiptUrl = "https://sandbox.itunes.apple.com/verifyReceipt"
                    }

                // appStoreReceiptURL still says sandboxReceipt

                if let sandboxURL = Foundation.URL(string:receiptUrl) {
                    var request = URLRequest(url: sandboxURL)
                    request.httpMethod = "POST"
                    request.httpBody = jsonData
                    let session = URLSession(configuration: URLSessionConfiguration.default)
                    let task = session.dataTask(with: request) {
 if let receivedData = data,
                                let httpResponse = response as? HTTPURLResponse,
                                error == nil,
                                httpResponse.statusCode == 200 {
                                do {
                                    if let jsonResponse = try JSONSerialization.jsonObject(with: receivedData, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<String, AnyObject>

When I get the jsonResponse, I am getting "[\"status\": 21007]", which is described in the Apple docs as:

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.

Can someone help me get the subscriptions into live mode?

Upvotes: 1

Views: 561

Answers (1)

Kymer
Kymer

Reputation: 1214

Testing your in-app purchases (both with and without TestFlight) will always happen in sandbox mode. Only apps signed by Apple for distribution in the App Store will hit production when buying IAPs. If your IAP flow is working in sandbox, you should be fine in production as well.

By the way: I'm not sure how you are determining you are in 'live' mode, but Apple recommends to always send the receipt to the production URL first, and if you receive error code 21007 to send it to the sandbox URL as a fallback. And you really need that fallback mechanism. As part of the review process Apple will test your IAP flow. Their testing will happen in sandbox mode, so if you hardcode your liveMode bool to true and never hit the sandbox verification URL the IAP purchase will fail and your build will not be approved.

Upvotes: 1

Related Questions