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获取虚拟产品信息。

设置成功后如下所示:

这里写图片描述


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];
    }
}

//AppUtils 类的方法,每次调用该方法都生成一个新的UUID
+ (NSString *)getUUIDString
{
    CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef strRef = CFUUIDCreateString(kCFAllocatorDefault , uuidRef);
    NSString *uuidString = [(__bridge NSString*)strRef stringByReplacingOccurrencesOfString:@"-" withString:@""];
    CFRelease(strRef);
    CFRelease(uuidRef);
    return uuidString;
}

//持久化存储用户购买凭证(这里最好还要存储当前日期,用户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
{
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];

    //这里的参数请根据自己公司后台服务器接口定制,但是必须发送的是持久化保存购买凭证
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
              [dic objectForKey:USERID],                           USERID,                    
              [dic objectForKey:DATE],                             DATE,  
              [dic objectForKey:Receipt],                            Receipt,                                                                             
              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)sendAppStoreRequestSucceededWithData
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:AppStoreInfoLocalFilePath])
    {
        [fileManager removeItemAtPath:AppStoreInfoLocalFilePath error:nil];
    }
}

4、测试

创建应用的沙盒测试账号

这里写图片描述

点击+号,根据具体信息填写。

至此,整个流程结束。


注: 完成购买的操作中,如果有服务器,需要向服务器验证购买结果,如果没有,就直接完成。

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
调起支付后,输入注册的沙盒账号即可点击购买。

注:这里有几个注意事项

一,测试支付的ipa必须使用[App-Store]证书

二,越狱机器无法测试IAP

三,用SandBox账号测试支付的时候,必须把在系统[设置]里面把[Itunes Store 与 App Store]登录的非                               SandBox账号注销掉,否则向苹果服务器请求不到订单信息

四,Sandbox账号不要在正式支付环境登陆支付,登陆过的正式支付环境的SandBox账号会失效

五,所有在itunes上配置的商品都必须可购买,不能有某些商品根据商户自己的服务器的数据在某个时期出现免费的情况

六,商品列表不能按照某些特定条件进行排序(比如说下载量)

七,非消耗型商品必须的有恢复商品功能

八,非消耗类型的商品不要和商户自己的服务器关联

参考:

https://www.jianshu.com/p/033086546126

https://www.jianshu.com/p/e0ea5b8916f5

http://openfibers.github.io/blog/2015/02/28/in-app-purchase-walk-through/

http://www.himigame.com/iphone-cocos2d/550.html

http://blog.devtang.com/2012/12/09/in-app-purchase-check-list/

http://yarin.blog.51cto.com/1130898/549141

更多技术文章,欢迎大家访问我的技术博客:https://blog.csdn.net/qcx321

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Unity IAPiOS上的支付demo实现步骤: 1. 首先,你需要在Unity中启用IAP服务。在Unity编辑器中,选择“Window”菜单,然后选择“Unity IAP”选项。在弹出的窗口中,选择“Enable IAP”选项。这将启用Unity IAP,并将其添加到Unity项目中。 2. 在Unity IAP控制台中创建产品。在Unity IAP控制台中,选择“Products”选项卡。然后,创建一个新的产品,例如“TestProduct”。 3. 在代码中实现IAP支付功能。在Unity中,你可以使用IAPManager类在代码中实现IAP支付功能。首先,你需要在代码中初始化IAPManager。以下是一个示例: ```csharp using UnityEngine; using UnityEngine.Purchasing; public class IAPManager : MonoBehaviour, IStoreListener { private IStoreController m_Controller; void Start() { var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct("TestProduct", ProductType.Consumable); UnityPurchasing.Initialize(this, builder); } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { m_Controller = controller; } public void OnInitializeFailed(InitializationFailureReason error) { Debug.Log("IAP initialization failed: " + error); } public void OnPurchaseFailed(Product item, PurchaseFailureReason error) { Debug.Log("IAP purchase failed: " + error); } public void OnPurchaseComplete(Product item, PurchaseEventArgs args) { Debug.Log("IAP purchase complete: " + item.definition.id); } public void Purchase(string productId) { if (m_Controller != null) { var product = m_Controller.products.WithID(productId); if (product != null && product.availableToPurchase) { m_Controller.InitiatePurchase(product); } else { Debug.Log("IAP product not available: " + productId); } } } } ``` 在上面的代码中,我们使用ConfigurationBuilder类配置产品,并使用UnityPurchasing类初始化IAPManager。然后,我们在OnInitialized方法中获取IAP控制器。在Purchase方法中,我们检查产品可用性,并调用InitiatePurchase方法启动购买流程。 4. 在iOS项目中配置IAP。要在iOS上启用IAP,你需要在Apple开发者中心创建一个新的IAP项目,并将其添加到Xcode项目中。以下是一些必要步骤: - 在Apple开发者中心中创建一个新的IAP项目。 - 将IAP项目添加到Xcode项目中。 - 在Xcode中设置应用程序的IAP权限。 - 在Xcode中添加IAP产品的标识符。 - 在Xcode中设置IAP产品的价格和描述。 5. 在Unity中测试IAP支付。在Unity中,你可以使用IAPManager中的Purchase方法测试IAP支付功能。在调用Purchase方法之前,请确保你已经在Unity IAP控制台中创建了一个有效的产品,并在iOS项目中正确配置了IAP。 以上就是Unity IAPiOS上的支付demo实现步骤。注意,在实际发布应用程序之前,请确保你已经按照苹果的要求正确实现了IAP支付功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值