Peacemoon
Peacemoon

Reputation: 3328

How to find the latest receipt of in app purchase using iOS 7 style receipt

I'm trying to get the latest receipt information locally on device when user purchase an in app item. My problem is i can not figure out how to get the latest receipt (should be the one that generated with the latest purchase) from the array of receipts in latest_receipt_info field. Somehow all pruchase_date_ms fields in all receipt have the same value.

 {
        "expires_date" = "2014-07-09 20:32:10 Etc/GMT";
        "expires_date_ms" = 1404937930000;
        "expires_date_pst" = "2014-07-09 13:32:10 America/Los_Angeles";
        "is_trial_period" = false;
        "original_purchase_date" = "2014-07-09 20:27:10 Etc/GMT";
        "original_purchase_date_ms" = 1404937630000;
        "original_purchase_date_pst" = "2014-07-09 13:27:10 America/Los_Angeles";
        "original_transaction_id" = 1000000116335839;
        "product_id" = "product1";
        "purchase_date" = "2014-10-13 10:28:39 Etc/GMT";
        "purchase_date_ms" = 1413196119831;
        "purchase_date_pst" = "2014-10-13 03:28:39 America/Los_Angeles";
        quantity = 1;
        "transaction_id" = 1000000116358850;
        "web_order_line_item_id" = 1000000028383484;
    },
            {
        "is_trial_period" = false;
        "original_purchase_date" = "2014-09-15 13:34:17 Etc/GMT";
        "original_purchase_date_ms" = 1410788057000;
        "original_purchase_date_pst" = "2014-09-15 06:34:17 America/Los_Angeles";
        "original_transaction_id" = 1000000123804432;
        "product_id" = "product2";
        "purchase_date" = "2014-10-13 10:28:39 Etc/GMT";
        "purchase_date_ms" = 1413196119831;
        "purchase_date_pst" = "2014-10-13 03:28:39 America/Los_Angeles";
        quantity = 1;
        "transaction_id" = 1000000123804432;
    }

In the example above, i actually make a test purchase for product2 , but now i also have the receipt the product1 with the same purchase_date_ms. Anyone knows why?

Upvotes: 1

Views: 2893

Answers (3)

4t8dds
4t8dds

Reputation: 673

As I found the latest_receipt_info is sorted already. Just get the latest one.

Upvotes: 0

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16514

Just sort of by expires_time_ms in the descending order, and then get last. Ruby code can be seen below:

data['receipt']['in_app'].sort_by { |x| x['expires_date'] }.last

Upvotes: 0

Yogesh Solanki
Yogesh Solanki

Reputation: 487

This code can be used to get the last transaction receipt for auto renewable in-App purchases

if let receipt =  NSBundle.mainBundle().appStoreReceiptURL {
        if let data = NSData(contentsOfURL: receipt) {
            let requestContents:[String:String] = ["receipt-data":data.base64EncodedStringWithOptions([]), "password": “Your Shared Secret from iTunes Connect App”]
            let requestData = try! NSJSONSerialization.dataWithJSONObject(requestContents,options: [])
            let request = NSMutableURLRequest(URL: NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!)
            request.HTTPMethod = "POST"
            request.HTTPBody = requestData
            let (param, _) = Alamofire.ParameterEncoding.URL.encode(request, parameters: nil)

            Alamofire.request(param)
                .responseJSON(options: [], completionHandler: { (result) -> Void in

                    if(result.result.isSuccess)
                    {
                        let dictionary : NSDictionary = result.result.value as! NSDictionary

                        let status = dictionary["status"] as! Int

                        if(status == 0)
                        {
                            let receipts: NSMutableArray = dictionary["latest_receipt_info"] as! NSMutableArray

                            let latestRecipt = receipts.lastObject as? NSDictionary
                           // This is receipt of your last transaction for auto renewable in-App purchases only
                              print(“Last Receipt \(latestRecipt)")
                        }
                        else
                        {
                            NSLog("Something went wrong Status = \(status)")
                            if(status == 21006)
                            {
                                NSLog("subscription is Expired or cancelled")
                            }

                        }

                    }

                })
        } else {
            NSLog("Reciept has no data")
        }
    }else {
        NSLog("Reciept not found")

    }

Upvotes: 1

Related Questions