博客> 苹果iap内购之验证介绍
苹果iap内购之验证介绍
2017-08-16 20:55 评论:0 阅读:410 tianya2416
iap iOS内购 应用内支付 验证

前言

前面我介绍到使用RMStore来使用苹果支付,今天就来说说付完钱后如何验证苹果下发的收据是否有效。

1、苹果官方说明文档

苹果官方关于验证方法的说明文档 Enter your link description here:

2、服务器地址

//沙箱环境下使用 https://sandbox.itunes.apple.com/verifyReceipt  
//上架后使用 use https://buy.itunes.apple.com/verifyReceipt 

3、封装验证代码

   //如果上架就要改为正式的服务器,这边为了方便调试我们使用沙箱环境。
   NSString * theAppStoreUrl = @"https://sandbox.itunes.apple.com/verifyReceipt";
 //receipt 苹果下发的支付收据
   NSDictionary *requestContents = @{
                                      @"receipt-data": receipt
                                      };
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                          options:0
                                                            error:&error];
    NSURL *URL = [NSURL URLWithString:theAppStoreUrl];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:URL];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:requestData];
    NSURLSession * session = [NSURLSession sharedSession];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSLog(@"%@",jsonResponse);
    }];
    [task resume];

4、苹果服务器返回的验证结果

    {
    environment = Sandbox;
    receipt =     {
        "adam_id" = 0;
        "app_item_id" = 0;
        "application_version" = "4.0.160630";
        "bundle_id" = "私人隐藏";
        "download_id" = 0;
        "in_app" =         (
                        {
                "is_trial_period" = false;
                "original_purchase_date" = "2015-12-29 07:36:44 Etc/GMT";
                "original_purchase_date_ms" = 1451374604000;
                "original_purchase_date_pst" = "2015-12-28 23:36:44 America/Los_Angeles";
                "original_transaction_id" = 1000000186808098;
                "product_id" = "consumable.coins.6";
                "purchase_date" = "2015-12-29 07:36:44 Etc/GMT";
                "purchase_date_ms" = 1451374604000;
                "purchase_date_pst" = "2015-12-28 23:36:44 America/Los_Angeles";
                quantity = 1;
                "transaction_id" = 1000000186808098;
            },
                        {
                "is_trial_period" = false;
                "original_purchase_date" = "2016-06-23 08:23:09 Etc/GMT";
                "original_purchase_date_ms" = 1466670189000;
                "original_purchase_date_pst" = "2016-06-23 01:23:09 America/Los_Angeles";
                "original_transaction_id" = 1000000219299797;
                "product_id" = "私人隐藏";
                "purchase_date" = "2016-06-23 08:23:09 Etc/GMT";
                "purchase_date_ms" = 1466670189000;
                "purchase_date_pst" = "2016-06-23 01:23:09 America/Los_Angeles";
                quantity = 1;
                "transaction_id" = 1000000219299797;
            }
        );
        "original_application_version" = "1.0";
        "original_purchase_date" = "2013-08-01 07:00:00 Etc/GMT";
        "original_purchase_date_ms" = 1375340400000;
        "original_purchase_date_pst" = "2013-08-01 00:00:00 America/Los_Angeles";
        "receipt_creation_date" = "2016-06-23 08:23:10 Etc/GMT";
        "receipt_creation_date_ms" = 1466670190000;
        "receipt_creation_date_pst" = "2016-06-23 01:23:10 America/Los_Angeles";
        "receipt_type" = ProductionSandbox;
        "request_date" = "2016-06-23 08:23:18 Etc/GMT";
        "request_date_ms" = 1466670198149;
        "request_date_pst" = "2016-06-23 01:23:18 America/Los_Angeles";
        "version_external_identifier" = 0;
    };
    status = 0;
}

当返回的status = 0时说明已经验证通过,重返回的信息我们可以清楚的看到app一些信息bundleid 和产品id,这里由于私人原因隐藏起来了。 当然出于安全性考虑不建议把验证这个流程放在客户端来做,在服务端来做会显得更安全,我们可以把这个支付收据通过某些加密算法加密后传给服务端。这样整个流程就更加安全。 未完待续。

收藏
1
sina weixin mail 回到顶部