IAP-iOS内购流程

2019-01-09  本文已影响0人  疯疯的小龙

目录

  1. 请求商品信息
  2. 添加/移除监听
  3. 购买
  4. 恢复购买

请求商品信息

  1. 发起请求
    SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
    //保持对请求的强引用
    self.productsRequest = productsRequest;
    //SKProductsRequestDelegate
    productsRequest.delegate = self;
    [productsRequest start];
  1. SKProductsRequestDelegate请求商品信息回调
//收到商品反馈消息
- (void)productsRequest:(SKProductsRequest *)request
     didReceiveResponse:(SKProductsResponse *)response {
    self.products = response.products;
    for (NSString *invalidIdentifier in response.invalidProductIdentifiers) {
    // Handle any invalid product identifiers.
    
    }
}

//请求商品结束
- (void)requestDidFinish:(SKRequest *)request {

}

//请求商品信息失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {

}

添加/移除监听

    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
@protocol SKPaymentTransactionObserver <NSObject>
@required
// Sent when the transaction array has changed (additions or state changes).  Client should check state of transactions and finish as appropriate.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions NS_AVAILABLE(10_7, 3_0);

@optional
// Sent when transactions are removed from the queue (via finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions NS_AVAILABLE(10_7, 3_0);

// Sent when an error is encountered while adding transactions from the user's purchase history back to the queue.
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error NS_AVAILABLE(10_7, 3_0);

// Sent when all transactions from the user's purchase history have successfully been added back to the queue.
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue NS_AVAILABLE(10_7, 3_0);

// Sent when the download state has changed.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray<SKDownload *> *)downloads NS_AVAILABLE(10_8, 6_0);

// Sent when a user initiates an IAP buy from the App Store
- (BOOL)paymentQueue:(SKPaymentQueue *)queue shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)product NS_SWIFT_NAME(paymentQueue(_:shouldAddStorePayment:for:)) NS_AVAILABLE_IOS(11_0);

@end

  1. 实现监听方法
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchased:
            {
                MTLOG(@"交易完成");
                  //校验交易是否合法 本地校验or自己的服务端校验
                  //1、记录订单信息,用于校验失败时下次启动app重新发起校验
                  //2、获取本地凭证  *必需
                  NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
                  NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
                  NSString *receiptStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

                  //若获取不到本地凭证,则调取刷新凭证的方法刷新凭证
                  SKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init];
                  request.delegate = self;
                  [request start];

                  //获取product_id *必需
                  NSString *product_id = transaction.payment.productIdentifier;

                  //获取transaction_id *必需
                  NSString * transaction_id = transaction.transactionIdentifier;

                  //3、根据本地凭证本地校验/服务端校验
                  //4、校验成功,更新数据库,刷新UI,移除记录的订单信息,结束订单
                  //(若不结束订单,苹果会一直调用 - (void)paymentQueue:updatedTransactions:直到结束该订单)
                  [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                  //5、校验失败,不移除记录的订单信息,不调用结束订单方法:[[SKPaymentQueue defaultQueue] finishTransaction:transaction]
                  //启动app时检查记录的订单信息数组,若非空说明有订单未成功校验,此时需重新调用校验方法
            }
                break;
            case SKPaymentTransactionStatePurchasing:
            {
                MTLOG(@"商品添加进列表");
            }
                break;
            case SKPaymentTransactionStateRestored:
            {
                MTLOG(@"恢复购买");
                //1、记录已购买过的商品,用于解锁商品。在恢复成功的回调里解锁商品
                //恢复成功回调:- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue;

            }
                break;
            case SKPaymentTransactionStateFailed:
            {
                MTLOG(@"交易失败");
            }
                break;
            default:
                break;
        }
    }
}

- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {

    MTLOG(@"移除购买队列");
}

- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
    
    MTLOG(@"恢复内购失败");
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    
    MTLOG(@"恢复内购完成");
    //1、恢复成功,解锁商品
}

  1. 获取本地凭证
    NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
    NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
    NSString *receiptStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
    SKReceiptRefreshRequest *request = [[SKReceiptRefreshRequest alloc] init];
    request.delegate = self;
    [request start];
@protocol SKRequestDelegate <NSObject>

@optional
- (void)requestDidFinish:(SKRequest *)request NS_AVAILABLE(10_7, 3_0);
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error NS_AVAILABLE(10_7, 3_0);

@end

image.png
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

购买

  1. 判断是否能够购买
    BOOL canMakePayments = [SKPaymentQueue canMakePayments];
  1. 发起购买请求
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    payment.applicationUsername = applicationUsername; //可以唯一标识用户账号即可,是用于apple检测非法活动
    [[SKPaymentQueue defaultQueue] addPayment:payment];
  1. 购买结果回调方法(详见添加/移除监听 — 1. 实现监听方法)
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions;

恢复购买

  1. 发起恢复购买请求
    //恢复已购项目(只有非消耗性、免费(或限时免费)、自动订阅(有效期内)、免费订阅 的项目才能被恢复)
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  1. 恢复内购回调(详见添加/移除监听 — 1. 实现监听方法)
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions;
上一篇下一篇

猜你喜欢

热点阅读