iOS应用内支付(IAP)详解


iOS应用内支付(IAP)详解


在iOS开发中如果涉及到虚拟物品的购买,就需要使用IAP服务,我们今天来看看如何实现。

在实现代码之前我们先做一些准备工作,一步步来看。


1、IAP流程

IAP流程分为两种,一种是直接使用Apple的服务器进行购买和验证,另一种就是自己假设服务器进行验证。由于国内网络连接Apple服务器验证非常慢,而且也为了防止黑客伪造购买凭证,通用做法是自己架设服务器进行验证。

下面我们通过图来看看两种方式的差别:

1.1、使用Apple服务器


1.2、自己架设服务器


简单说下第二中情况的流程:

  1. 用户进入购买虚拟物品页面,App从后台服务器获取产品列表然后显示给用户
  2. 用户点击购买购买某一个虚拟物品,APP就发送该虚拟物品的productionIdentifier到Apple服务器
  3. Apple服务器根据APP发送过来的productionIdentifier返回相应的物品的信息(描述,价格等)
  4. 用户点击确认键购买该物品,购买请求发送到Apple服务器
  5. Apple服务器完成购买后,返回用户一个完成购买的凭证
  6. APP发送这个凭证到后台服务器验证
  7. 后台服务器把这个凭证发送到Apple验证,Apple返回一个字段给后台服务器表明该凭证是否有效
  8. 后台服务器把验证结果在发送到APP,APP根据验证结果做相应的处理


2、iTunes Connet操作

搞清楚了自己架设服务器是如何完成IAP购买的流程了之后,我们下一步就是登录到iTunes Connet创建应用和指定虚拟物品价格表

2.1、创建自己的App

如下图所示,我们需要创建一个自己的APP,要注意的是这里的Bundle ID一定要跟你的项目中的info.plist中的Bundle ID保证一致。也就是图中红框部分。


2.2、创建虚拟物品价格表

2.2.1、虚拟物品分为如下几种:

  1. 消耗品(Consumable products):比如游戏内金币等。
  2. 不可消耗品(Non-consumable products):简单来说就是一次购买,终身可用(用户可随时从App Store restore)。
  3. 自动更新订阅品(Auto-renewable subscriptions):和不可消耗品的不同点是有失效时间。比如一整年的付费周刊。在这种模式下,开发者定期投递内容,用户在订阅期内随时可以访问这些内容。订阅快要过期时,系统将自动更新订阅(如果用户同意)。
  4. 非自动更新订阅品(Non-renewable subscriptions):一般使用场景是从用户从IAP购买后,购买信息存放在自己的开发者服务器上。失效日期/可用是由开发者服务器自行控制的,而非由App Store控制,这一点与自动更新订阅品有差异。
  5. 免费订阅品(Free subscriptions):在Newsstand中放置免费订阅的一种方式。免费订阅永不过期。只能用于Newsstand-enabled apps。

类型2、3、5都是以Apple ID为粒度的。比如小张有三个iPad,有一个Apple ID购买了不可消耗品,则三个iPad上都可以使用。

类型1、4一般来说则是现买现用。如果开发者自己想做更多控制,一般选4

2.2.2、创建成功后如下所示:


其中产品id是字母或者数字,或者两者的组合,用于唯一表示该虚拟物品,app也是通过请求产品id来从apple服务器获取虚拟物品信息的。

2.3、设置税务和银行卡信息

这一步必须设置,不然是无法从apple获取虚拟产品信息。

设置成功后如下所示:


更多关于iTunes Connet的操作请才看这篇博文http://openfibers.github.io/blog/2015/02/28/in-app-purchase-walk-through/


3、iOS端具体代码实现

完成了上面的准备工作,我们就可以开始着手IAP的代码实现了。

我们假设你已经完成了从后台服务器获取虚拟物品列表这一步操作了,这一步后台服务器还会返回每个虚拟物品所对应的productionIdentifier,假设你也获取到了,并保存在属性self.productIdent中。

需要在工程中引入 storekit.framework。

我们来看看后续如何实现IAP

3.1、确认用户是否允许IAP

//移除监听

-(void)dealloc

{

    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

}


//添加监听

- (void)viewDidLoad{

    [super viewDidLoad];

    [self.tableView.mj_header beginRefreshing];

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

}


- (void)buyProdution:(UIButton *)sender{    

    if ([SKPaymentQueue canMakePayments]) {

        [self getProductInfo:self.productIdent];

    } else {

        [self showMessage:@"用户禁止应用内付费购买"];

    }

}

3.2、发起购买操作

如果用户允许IAP,那么就可以发起购买操作了

//从Apple查询用户点击购买的产品的信息

- (void)getProductInfo:(NSString *)productIdentifier {

    NSArray *product = [[NSArray alloc] initWithObjects:productIdentifier, nil];

    NSSet *set = [NSSet setWithArray:product];

    SKProductsRequest * request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];

    request.delegate = self;

    [request start];

    [self showMessageManualHide:@"正在购买,请稍后"];

}


// 查询成功后的回调

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

    [self hideHUD];

    NSArray *myProduct = response.products;

    if (myProduct.count == 0) {

        [self showMessage:@"无法获取产品信息,请重试"];

        return;

    }

    SKPayment * payment = [SKPayment paymentWithProduct:myProduct[0]];

    [[SKPaymentQueue defaultQueue] addPayment:payment];

}


//查询失败后的回调

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {

    [self hideHUD];

    [self showMessage:[error localizedDescription]];

}

3.3、购买操作后的回调

//购买操作后的回调

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

    [self hideHUD];

    for (SKPaymentTransaction *transaction in transactions)

    {

        switch (transaction.transactionState)

        {

            case SKPaymentTransactionStatePurchased://交易完成

                self.receipt = [GTMBase64 stringByEncodingData:[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]];

                [self checkReceiptIsValid];//把self.receipt发送到服务器验证是否有效

                [self completeTransaction:transaction];

                break;

                

            case SKPaymentTransactionStateFailed://交易失败

                [self failedTransaction:transaction];

                break;

                

            case SKPaymentTransactionStateRestored://已经购买过该商品

                [self showMessage:@"恢复购买成功"];

                [self restoreTransaction:transaction];

                break;

                

            case SKPaymentTransactionStatePurchasing://商品添加进列表

                [self showMessage:@"正在请求付费信息,请稍后"];

                break;

                

            default:

                break;

        }

    }

    

}




- (void)completeTransaction:(SKPaymentTransaction *)transaction {

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}



- (void)failedTransaction:(SKPaymentTransaction *)transaction {

    if(transaction.error.code != SKErrorPaymentCancelled) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"购买失败,请重试"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重试", nil];

        [alertView show];

    } else {

        [self showMessage:@"用户取消交易"];

    }

    

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}



- (void)restoreTransaction:(SKPaymentTransaction *)transaction {

    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

3.4、向服务器端验证购买凭证的有效性

在这一步我们需要向服务器验证Apple服务器返回的购买凭证的有效性,然后把验证结果通知用户

- (void)checkReceiptIsValid{


    AFHTTPSessionManager manager]GET:@"后台服务器地址"  parameters::@"发送的参数(必须包括购买凭证)"

    success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        if(凭证有效){

          你要做的事

        }else{//凭证无效

          你要做的事

        }

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"购买失败,请重试"delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"重试", nil];

            [alertView show];

    }


}

3.5、发送凭证失败的处理

如果出现网络问题,导致无法验证。我们需要持久化保存购买凭证,在用户下次启动APP的时候在后台向服务器再一次发起验证,直到成功然后移除该凭证。

保证如下define可在全局访问:

#define AppStoreInfoLocalFilePath [NSString stringWithFormat:@"%@/%@/", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],@"EACEF35FE363A75A"]


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 0)

    {

        [self saveReceipt];

    }

    else

    {

        [self checkReceiptIsValid];

    }

}



//持久化存储用户购买凭证(这里最好还要存储当前日期,用户id等信息,用于区分不同的凭证)

-(void)saveReceipt{

    NSString *fileName = [AppUtils getUUIDString];

    NSString *savedPath = [NSString stringWithFormat:@"%@%@.plist", AppStoreInfoLocalFilePath, fileName];

    

    NSDictionary *dic =[ NSDictionary dictionaryWithObjectsAndKeys:

                        self.receipt,                           Request_transactionReceipt,

                        self.date                               DATE                        

                        self.userId                             USERID

                        nil];

    

    [dic writeToFile:savedPath atomically:YES];

}

3.6、APP启动后再次发送持久化存储的购买凭证到后台服务器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    

    //从服务器验证receipt失败之后,在程序再次启动的时候,使用保存的receipt再次到服务器验证

    if (![fileManager fileExistsAtPath:AppStoreInfoLocalFilePath]) {//如果在改路下不存在文件,说明就没有保存验证失败后的购买凭证,也就是说发送凭证成功。

        [fileManager createDirectoryAtPath:AppStoreInfoLocalFilePath//创建目录

               withIntermediateDirectories:YES

                                attributes:nil

                                     error:nil];

    }

    else//存在购买凭证,说明发送凭证失败,再次发起验证

    {

        [self sendFailedIapFiles];

    }

}


//验证receipt失败,App启动后再次验证

- (void)sendFailedIapFiles{

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error = nil;

    

    //搜索该目录下的所有文件和目录

    NSArray *cacheFileNameArray = [fileManager contentsOfDirectoryAtPath:AppStoreInfoLocalFilePath error:&error];

    

    if (error == nil)

    {

        for (NSString *name in cacheFileNameArray)

        {

            if ([name hasSuffix:@".plist"])//如果有plist后缀的文件,说明就是存储的购买凭证

            {

                NSString *filePath = [NSString stringWithFormat:@"%@/%@", AppStoreInfoLocalFilePath, name];

                [self sendAppStoreRequestBuyPlist:filePath];

                

            }

        }

    }

    else

    {

        DebugLog(@"AppStoreInfoLocalFilePath error:%@", [error domain]);

    }

}


-(void)sendAppStoreRequestBuyPlist:(NSString *)plistPath

{

    NSString *path = [NSString stringWithFormat:@"%@%@", AppStoreInfoLocalFilePath, plistPath];

    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

    

    //这里的参数请根据自己公司后台服务器接口定制,但是必须发送的是持久化保存购买凭证

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:

              [dic objectForKey:USERID],                           USERID,                    

              [dic objectForKey:DATE],                             DATE,                                                                                                         [dic objectForKey:Request_transactionReceipt],      Request_transactionReceipt,

                                                                       nil];

                            

                                                                       

        AFHTTPSessionManager manager]GET:@"后台服务器地址"  parameters:params  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        if(凭证有效){

         [self removeReceipt]

        }else{//凭证无效

          你要做的事

        }

        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

                

    }

    

 }


//验证成功就从plist中移除凭证

-(void)removeReceipt{

    [AppUtils removeIapFailedPath:AppStoreInfoLocalFilePath];

}


//AppUtils类方法,验证成功,移除存储的receipt

+ (void)removeIapFailedPath:(NSString *)plistPath{

    NSString *path = [NSString stringWithFormat:@"%@/%@", AppStoreInfoLocalFilePath, plistPath];

    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ([fileManager fileExistsAtPath:AppStoreInfoLocalFilePath])

    {

        [fileManager removeItemAtPath:AppStoreInfoLocalFilePath error:nil];

    }

    

    if ([fileManager fileExistsAtPath:path])

    {

        [fileManager removeItemAtPath:path error:nil];

    }

}


至此,整个流程结束,有任何疑问欢迎大家留言


参考:

  1. http://openfibers.github.io/blog/2015/02/28/in-app-purchase-walk-through/
  2. http://www.himigame.com/iphone-cocos2d/550.html
  3. http://blog.devtang.com/2012/12/09/in-app-purchase-check-list/
  4. http://yarin.blog.51cto.com/1130898/549141
  5. 更多技术文章,欢迎大家访问我的技术博客:http://blog.ximu.site
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值