美文网首页
iOS内购小总结(代码块)

iOS内购小总结(代码块)

作者: DanDing | 来源:发表于2016-11-11 16:57 被阅读282次

    一:内购逻辑流程图


    pay1.png pay2.png

    二:代码部分

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"购买页面";
          self.navigationItem.rightBarButtonItem = [self rightBarButtonWithName:NSLocalizedString(@"07-restore", nil) target:self action:@selector(restonreProduct)];
        [self BuildUI];
        [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    }
    
    #pragma mark -- 购买按钮事件
    -(void)buycommodity {
    
            if ([SKPaymentQueue canMakePayments]) {
                [self RequestProductData];
                //        NSLog(@"允许程序内付费购买");
            }
            else
            {
                //        NSLog(@"不允许程序内付费购买");
                [self MarkToViewString:NSLocalizedString(@"09-noOpenApplications", nil)];
            }
    }
    
    -(void)RequestProductData
    {
    //    NSLog(@"---------请求对应的产品信息------------");
        NSArray *product = nil;
        product=[[NSArray alloc] initWithObjects:ProductID,nil];
        NSSet *nsset = [NSSet setWithArray:product];
        SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
        request.delegate=self;
        [request start];
    }
    
    //<SKProductsRequestDelegate> 请求协议
    //收到的产品信息
    - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    //    NSLog(@"-----------收到产品反馈信息--------------");
        NSArray *myProduct = response.products;
        if([myProduct count] == 0){
            [SVProgressHUD dismiss];
    //        NSLog(@"--------------没有商品------------------");
            return;
        }
        SKProduct *P = nil;
        for(SKProduct *product in myProduct){
    //        NSLog(@"product info");
    //        NSLog(@"SKProduct 描述信息%@", [product description]);
    //        NSLog(@"产品标题 %@" , product.localizedTitle);
    //        NSLog(@"产品描述信息: %@" , product.localizedDescription);
    //        NSLog(@"价格: %@" , product.price);
    //        NSLog(@"Product id: %@" , product.productIdentifier);
            if ([product.productIdentifier isEqualToString:ProductID]) {
                P = product;
            }
        }
        SKPayment *payment = nil;
        payment = [SKPayment paymentWithProduct:P];
    //    NSLog(@"---------发送购买请求------------");
        [[SKPaymentQueue defaultQueue] addPayment:payment];
    }
    
    //监听购买结果
    - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{
        
        for(SKPaymentTransaction *tran in transaction){
            
            switch (tran.transactionState) {
                case SKPaymentTransactionStatePurchased:{
    //         NSLog(@"交易完成");
                  [self checkTransaction:tran];//去验证
                }
                    break;
                case SKPaymentTransactionStatePurchasing:
                  //  NSLog(@"商品添加进列表");
                    break;
                case SKPaymentTransactionStateRestored:{
                  //  NSLog(@"已经购买过商品");
                    [self restoreTransaction:tran];
                }
                    break;
                case SKPaymentTransactionStateFailed:{
                 //  NSLog(@"交易失败");
                    [[SKPaymentQueue defaultQueue] finishTransaction:tran];
                    [SVProgressHUD showErrorWithStatus:@"购买失败"];
                }
                    break;
                default:
                    break;
            }
        }
    }
    

    验证前检查看看是否有未完成订单

    #pragma mark --检查本次支付信息,并与本地缓存信息对比,查看是否是未完成订单
    - (void)checkTransaction:(SKPaymentTransaction *)transaction{
    
        NSDictionary *saveDic = [[NSDictionary alloc]init];
        saveDic = [DDKeychain keyChainLoad];
        if (saveDic != nil) {
           //取自支付队列未完成的商品ID去缓存中拿对应的信息.有就去验证
            [self connetServerWithTransactionandPayInfo:transaction];
            return;
        }else{
            //全新的支付
            NSDictionary *newPayInfoDic = @{@"product_id":ProductID,
                                            @"apple_orderid":transaction.transactionIdentifier};
            /** 已Keychain将数据存起来 */
            [self setOrderData:newPayInfoDic key:ProductID];
            /** 服务器验证 */
            [self connetServerWithTransactionandPayInfo:transaction];
        }
    }
    
    #pragma mark -- server验证
    - (void)connetServerWithTransactionandPayInfo:(SKPaymentTransaction *)transaction{
        //从沙盒中获取交易凭证并且拼接成请求体数据
        NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL];
        NSData *receiptData=[NSData dataWithContentsOfURL:receiptUrl];
        NSString *receiptString=[receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];//交易凭证转化为base64字符串
    //    /** 商品id 订单号 苹果的订单号 传给 后台(看你们后台要求)*/
    
        /*
         1.调用服务器接口验证成功一定要删除keyChain存储的数据
         2. [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
    
         */
    }
    

    非消耗商品苹果需要提供恢复购买按钮,在已经购买过商品调用恢复购买处理

    #pragma mark -- 恢复购买
    - (void)restonreProduct{
    
        [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
    }
    - (void) restoreTransaction: (SKPaymentTransaction *)transaction
    {
        NSLog(@" 交易恢复处理");
        [self checkTransaction:transaction];//去验证
    
    }
    

    内购基本完成,我项目审核已通过!

    相关文章

      网友评论

          本文标题:iOS内购小总结(代码块)

          本文链接:https://www.haomeiwen.com/subject/qejxpttx.html