如何获得当前订阅的用户收据快速

时间:2018-10-12 06:37:25

标签: ios swift xcode in-app-purchase

当前,我正在尝试获取当前已记录的用户收据,但是每当我尝试从另一个帐户登录时,我仍然会收到相同的收据列表,并且在刷新时总是增加+1收据,我只是要在用户订阅后检查,我希望收据在服务器上发送

获取收据的代码

func receiptValidation() {
        let SUBSCRIPTION_SECRET = "xxxx"
        let receiptPath = Bundle.main.appStoreReceiptURL?.path
        if FileManager.default.fileExists(atPath: receiptPath!)
        {
            var receiptData:NSData?
            do{
                receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
            }
            catch{
                print("ERROR: " + error.localizedDescription)
            }
            //let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
            let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)

            print(base64encodedReceipt!)


            let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]

            guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
            do {
                let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
                let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"  // this works but as noted above it's best to use your own trusted server
                guard let validationURL = URL(string: validationURLString) else { print("the validation url could not be created, unlikely error"); return }
                let session = URLSession(configuration: URLSessionConfiguration.default)
                var request = URLRequest(url: validationURL)
                request.httpMethod = "POST"
                request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
                let task = session.uploadTask(with: request, from: requestData) { (data, response, error) in
                    if let data = data , error == nil {
                        do {
                            let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                            print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
                            // if you are using your server this will be a json representation of whatever your server provided
                        } catch let error as NSError {
                            print("json serialization failed with error: \(error)")
                        }
                    } else {
                        print("the upload task returned an error: \(error)")
                    }
                }
                task.resume()
            } catch let error as NSError {
                print("json serialization failed with error: \(error)")
            }



        }
    }

和收据信息

"latest_receipt_info" =     (
                {
            "is_trial_period" = false;
            "original_purchase_date" = "2018-10-09 07:14:17 Etc/GMT";
            "original_purchase_date_ms" = 1539069257000;
            "original_purchase_date_pst" = "2018-10-09 00:14:17 America/Los_Angeles";
            "original_transaction_id" = 1000000454564149;
            "product_id" = "com.MeetAndEat";
            "purchase_date" = "2018-10-09 07:14:17 Etc/GMT";
            "purchase_date_ms" = 1539069257000;
            "purchase_date_pst" = "2018-10-09 00:14:17 America/Los_Angeles";
            quantity = 1;
            "transaction_id" = 1000000454564149;
        },
                {
            "is_trial_period" = false;
            "original_purchase_date" = "2018-10-09 07:14:54 Etc/GMT";
            "original_purchase_date_ms" = 1539069294000;
            "original_purchase_date_pst" = "2018-10-09 00:14:54 America/Los_Angeles";
            "original_transaction_id" = 1000000454564374;
            "product_id" = "com.MeetAndEat";
            "purchase_date" = "2018-10-09 07:14:54 Etc/GMT";
            "purchase_date_ms" = 1539069294000;
            "purchase_date_pst" = "2018-10-09 00:14:54 America/Los_Angeles";
            quantity = 1;
            "transaction_id" = 1000000454564374;
        },
                {
            "is_trial_period" = false;
            "original_purchase_date" = "2018-10-09 08:30:39 Etc/GMT";
            "original_purchase_date_ms" = 1539073839000;
            "original_purchase_date_pst" = "2018-10-09 01:30:39 America/Los_Angeles";
            "original_transaction_id" = 1000000454630243;
            "product_id" = "com.MeetAndEat";
            "purchase_date" = "2018-10-09 08:30:39 Etc/GMT";
            "purchase_date_ms" = 1539073839000;
            "purchase_date_pst" = "2018-10-09 01:30:39 America/Los_Angeles";
            quantity = 1;
            "transaction_id" = 1000000454630243;
        },
                {
            "is_trial_period" = false;
            "original_purchase_date" = "2018-10-09 08:31:45 Etc/GMT";
            "original_purchase_date_ms" = 1539073905000;
            "original_purchase_date_pst" = "2018-10-09 01:31:45 America/Los_Angeles";
            "original_transaction_id" = 1000000454630786;
            "product_id" = "com.MeetAndEat";
            "purchase_date" = "2018-10-09 08:31:45 Etc/GMT";
            "purchase_date_ms" = 1539073905000;
            "purchase_date_pst" = "2018-10-09 01:31:45 America/Los_Angeles";
            quantity = 1;
            "transaction_id" = 1000000454630786;
        },
                {
            "is_trial_period" = false;
            "original_purchase_date" = "2018-10-09 08:42:01 Etc/GMT";
            "original_purchase_date_ms" = 1539074521000;
            "original_purchase_date_pst" = "2018-10-09 01:42:01 America/Los_Angeles";
            "original_transaction_id" = 1000000454640882;
            "product_id" = "com.MeetAndEat";
            "purchase_date" = "2018-10-09 08:42:01 Etc/GMT";
            "purchase_date_ms" = 1539074521000;
            "purchase_date_pst" = "2018-10-09 01:42:01 America/Los_Angeles";
            quantity = 1;
            "transaction_id" = 1000000454640882;
        },

1 个答案:

答案 0 :(得分:1)

这是完美的代码。没有问题。现在,在测试过程中出现了问题。

A new receipt will appear when StoreKit calls updatedTransactions.  Otherwise there may or may not be a refreshed receipt attached to the binary.

注销后会发生什么:

When you logged out from iTunes then you will see your local receipt will be 
removed 

我认为您还应该注销设置-> iTunes&App store->点击帐户。