如何使用iOS 7样式收据查找应用内购买的最新收据

时间:2014-10-13 10:52:45

标签: ios in-app-purchase receipt-validation

当用户购买应用商品时,我正试图在设备上本地获取最新的收据信息。我的问题是我无法弄清楚如何从latest_receipt_info字段中的收据数组中获取最新收据(应该是最新购买的收据)。不知何故,所有收据中的所有pruchase_date_ms字段都具有相同的值。

 {
        "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;
    }

在上面的示例中,我实际上是为 product2 进行测试购买,但现在我还收到 product1 的收据,其中 purchase_date_ms 。谁知道为什么?

2 个答案:

答案 0 :(得分:1)

此代码可用于获取自动续订应用内购买的最后一笔交易收据

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

    }

答案 1 :(得分:0)

按降序顺序按data['receipt']['in_app'].sort_by { |x| x['expires_date'] }.last 排序,然后排在最后。 Ruby代码如下所示:

import {todos} from '../reducers/todos';
import { createSelector } from 'reselect'
var visibilityFilter='SHOW_ALL';

var getVisibilityFilter = (state) =>  visibilityFilter;
var getTodos = (state) => todos;

export const getVisibleTodos = createSelector(
  [ getVisibilityFilter, getTodos ],
  (visibilityFilter, todos) => {


    switch (visibilityFilter) {
      case 'SHOW_ALL':
        return todos
      case 'SHOW_COMPLETED':
        return todos.filter(t => t.completed)
      case 'SHOW_ACTIVE':
        return todos.filter(t => !t.completed)
    }


  }  
)

export default getVisibleTodos;