在ios中购买应用程序产品中获取可再生资源的收据信息

时间:2018-06-21 05:15:26

标签: ios in-app-purchase

如何在ios中获取可更新的应用内产品的最新收货信息,以及如何检查收货是否过期? 我的代码是: -获取收据

            -(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions
            {
                hide_HUD
                SKPaymentTransaction *transaction = transactions.lastObject;
                switch (transaction.transactionState) {
                    case SKPaymentTransactionStatePurchased:
                    {
                        [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
                        NSLog(@"Order id ======>> %@",transaction.transactionIdentifier);
                        NSData *recData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
                        NSString* receiptString = [[NSString alloc] initWithData:recData encoding:NSUTF8StringEncoding];

                        [self getReceiptFromAppStore:recData Transaction:transaction isBackground:NO];
                        break;
                    }

                    case SKPaymentTransactionStateFailed:
                        NSLog(@"Purchase failed ");
                        break;

                    case SKPaymentTransactionStateRestored:
                        [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
                        break;

                    default:
                        //NSLog(@"Purchase failed ");
                        break;
                }
            }
  • 获取收据

        -(void)getReceiptFromAppStore:(NSData*)ReceiptData Transaction:(SKPaymentTransaction*)AutoTransaction isBackground:(BOOL)isBackground
        {
            NSData *receipt=ReceiptData; // Sent to the server by the device
    
            // Create the JSON object that describes the request
            NSError *error;
            NSDictionary *requestContents = @{
                                              @"receipt-data": [receipt base64EncodedStringWithOptions:0],
                                              @"password" : @“*********************”
                                              };
            NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                                  options:0
                                                                    error:&error];
    
            if (!requestData) { /* ... Handle error ... */ }
    
            // Create a POST request with the receipt data.
            NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]; //for Testing
            //    NSURL *storeURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"];//for live
            NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
            [storeRequest setHTTPMethod:@"POST"];
            [storeRequest setHTTPBody:requestData];
    
            // Make a connection to the iTunes Store on a background queue.
            NSOperationQueue *queue = [[NSOperationQueue alloc] init];
            [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                                       if (connectionError) {
    
                                       } else {
                                           NSError *error;
                                           NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
                                           if (error == nil && [jsonResponse valueForKey:@"latest_receipt_info"] != nil && [[jsonResponse valueForKey:@"latest_receipt_info"] count] > 0) {
                                              /* 
                                             What I have to do here to check receipt is expire.  
                                             My goal is to user can not redirect to my   
                                             app’s home page if he unsubscribe.  
                                             For example if user purchased 1 month . 
                                             renewable plan on 20-1-2017 and on 25-1-2017
                                             he/she unsubscribe for that plan and after 1 month (on 22-2-2017),  
                                             if he/she open my app , he should not get   
                                             redirected to my app’s home page.
                                             */
                                           }
                                           else{
                                               NSLog(@"%@",error);
                                           }
                                       }
                                   }];
        }
    

我有收据,但我不知道如何从收据中获取数据以及如何使用它来防止用户进入应用程序的主页。

1 个答案:

答案 0 :(得分:1)

请仔细阅读收据字段,您将获得所有信息,例如到期,被苹果公司取消,帐单信息,取消等。

Link

您必须访问 latest_receipt_info 数组中的最新对象,并检查字段 expires_date

订阅的到期日期,表示自1970年1月1日格林威治标准时间00:00:00以来的毫秒数。

ASN.1字段类型1708

ASN.1字段值IA5STRING,解释为RFC 3339日期

JSON字段名称expires_date

JSON字段值字符串,解释为RFC 3339日期

此密钥仅在自动续订的订阅收据中显示。使用此值来标识订阅将续订或到期的日期,以确定客户是否应有权访问内容或服务。验证最新收据后,如果最新续订交易的订阅到期日期是过去的日期,则可以安全地假定订阅已过期。